Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 90 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ jobs:
os: ubuntu-24.04-arm
- name: macos-arm64
os: macos-latest
- name: windows-arm64
os: windows-11-arm
- name: windows-x86
os: windows-latest


name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
Expand All @@ -26,11 +31,6 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: show_arch
run: |
uname -m
cc -dumpmachine

- name: install_dependencies_linux
if: runner.os == 'Linux'
run: |
Expand All @@ -41,16 +41,101 @@ jobs:
if: runner.os == 'macOS'
run: brew install cmake

- name: install_dependencies_windows
if: runner.os == 'Windows'
shell: pwsh
run: |
$ErrorActionPreference = "Stop"

if("${{ runner.arch }}" -eq "ARM64") {
$LlvmUrl = "https://github.com/mstorsjo/llvm-mingw/releases/download/20261020/llvm-mingw-20261020-ucrt-aarch64.zip"
} else {
$LlvmUrl = "https://github.com/mstorsjo/llvm-mingw/releases/download/20261020/llvm-mingw-20261020-ucrt-x86_64.zip"
}

choco install cmake make -y --no-progress

if ($LASTEXITCODE -ne 0) {
throw "Chocolatey installation failed: $LASTEXITCODE"
}

$sdk = "$env:RUNNER_TEMP\npcap-sdk.zip"
$dest = "$env:RUNNER_TEMP\npcap-sdk"

Invoke-WebRequest `
-Uri "https://npcap.com/dist/npcap-sdk-1.17.zip" `
-OutFile $sdk

Expand-Archive -Path $sdk -DestinationPath $dest -Force

$header = Get-ChildItem $dest -Filter pcap.h -Recurse | Select-Object -First 1

if (!$header) {
throw "Npcap SDK headers not found"
}

$sdkRoot = $header.Directory.Parent.FullName

if (!(Test-Path "$sdkRoot\Lib\ARM64\wpcap.lib")) {
throw "ARM64 wpcap.lib missing"
}
if (!(Test-Path "$sdkRoot\Lib\ARM64\Packet.lib")) {
throw "ARM64 Packet.lib missing"
}

if (!(Test-Path "$sdkRoot\Lib\x64\wpcap.lib")) {
throw "ARM64 wpcap.lib missing"
}
if (!(Test-Path "$sdkRoot\Lib\x64\Packet.lib")) {
throw "ARM64 Packet.lib missing"
}

"NPCAP_SDK=$sdkRoot" >> $env:GITHUB_ENV

$llvmDownloadLoc = "$env:RUNNER_TEMP\llvm-mingw.zip"
$llvmDest = "$env:RUNNER_TEMP\llvm-mingw"

Invoke-WebRequest `
-Uri $LlvmUrl `
-OutFile $llvmDownloadLoc

Expand-Archive -Path $llvmDownloadLoc -DestinationPath $llvmDest -Force

$clang = Get-ChildItem $llvmDest -Filter clang.exe -Recurse |
Select-Object -First 1

if (!$clang) {
throw "LLVM clang.exe not found"
}

$llvmBin = $clang.Directory.FullName

"$llvmBin" >> $env:GITHUB_PATH

& "$llvmBin\clang.exe" --version

if ($LASTEXITCODE -ne 0) {
throw "LLVM installation verification failed"
}

- name: build_integration_tests_pcap
if: runner.os == 'Linux' || runner.os == 'macOS'
run: |
./build_pcap_tests.sh
working-directory: tests/integration_tests/build

- name: run_integration_tests_pcap
if: runner.os == 'Linux' || runner.os == 'macOS'
run: |
sudo ./run_tests
working-directory: tests/integration_tests/output

- name: build_integration_tests_pcap_win
if: runner.os == 'Windows'
run: |
./build_pcap_tests.bat
working-directory: tests/integration_tests/build

- name: enable_hugepages
if: runner.os == 'Linux'
run: sudo sysctl -w vm.nr_hugepages=128
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Makefile
*.cache
!/build/profile_template.cmake
!/build/profiles/*
.vscode

**/*.dll
**/*.a
**/*.trace

**/diff.git
Expand Down
55 changes: 48 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

## Supported Platforms
- **Apple Silicon (macOS arm64)**
- **Linux arm64** (Release build works, but testing is disabled due to TSAN issues)
- **Windows 11 (aarch64)**
- **Windows 11 (x86-64)**
- **Linux aarch64** (Release build works, but testing is disabled due to TSAN issues)
- **Linux x86-64**

## Features
Expand Down Expand Up @@ -81,23 +83,62 @@ vcpkg install morcules-swiftnet
```

## From Source
1. Clone the repository to your local machine:

## Windows

1. Install NPcap (only supported backend on windows right now)
2. Install NPcap SDK
3. Put the NPcap SDK location into ENV variable "NPCAP_SDK"
4. Clone the repository to your local machine:
```bash
git clone https://github.com/SwiftNetProject/SwiftNet
```
5. Navigate to the build directory inside the SwiftNet directory:
```bash
cd SwiftNet/build
```
6. Copy profile:
```bash
cp profiles/x_profile.cmake profile.cmake
```
7. Compile:
Run it with admin privileges
```bash
sudo ./build.bat
```
8. To use SwiftNet in your project:
- Include `swift_net.h` in your main source file (e.g., `main.c`).
- Link against the static library `libswiftnet.a` using your compiler.

## Linux/MacOS

1. Install a backend (either libpcap-dev or libdpdk-dev (Only use DPDK with enough experience and in production)
macOS
```bash
brew install libpcap-dev | brew install libdpdk-dev
```
linux
```bash
apt install libpcap-dev | apt install libdpdk-dev
```

2. Clone the repository to your local machine:
```bash
git clone https://github.com/morcules/SwiftNet
git clone https://github.com/SwiftNetProject/SwiftNet
```
2. Navigate to the build directory inside the SwiftNet directory:
3. Navigate to the build directory inside the SwiftNet directory:
```bash
cd SwiftNet/build
```
3. Copy profile:
4. Copy profile:
```bash
cp profiles/x_profile.cmake profile.cmake
```
3. Compile:
5. Compile:
```bash
./build.sh
```
4. To use SwiftNet in your project:
6. To use SwiftNet in your project:
- Include `swift_net.h` in your main source file (e.g., `main.c`).
- Link against the static library `libswiftnet.a` using your compiler.

Expand Down
16 changes: 16 additions & 0 deletions build/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@echo off
setlocal

cmake -S ..\src -B . -G "MinGW Makefiles" ^
-DCMAKE_C_COMPILER=clang ^
-DCMAKE_CXX_COMPILER=clang++ ^
-DCMAKE_VERBOSE_MAKEFILE=OFF ^
-DCMAKE_MAKE_PROGRAM=make

if errorlevel 1 exit /b %errorlevel%

cmake --build . --parallel 4 --clean-first

if errorlevel 1 exit /b %errorlevel%

endlocal
103 changes: 96 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ elseif(BACKEND STREQUAL "dpdk")
endif()


if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
add_compile_options(-march=armv8-a)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|AMD64|i.86")
add_compile_options(-march=x86-64 -msse3 -mssse3)
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
OUTPUT_VARIABLE TARGET_TRIPLE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

message(STATUS "Compiler target: ${TARGET_TRIPLE}")

if(TARGET_TRIPLE MATCHES "^(aarch64|arm64)")
add_compile_options(-march=armv8-a)
set(SN_ARCH aarch64)
elseif(TARGET_TRIPLE MATCHES "^(x86_64|amd64|i[3-6]86)")
add_compile_options(-march=x86-64 -msse3 -mssse3)
set(SN_ARCH x86)
endif()

add_library(swiftnet STATIC ${SOURCE_FILES})
Expand Down Expand Up @@ -147,10 +158,74 @@ if(BACKEND STREQUAL "pcap")
message(WARNING "libpcap not found!!!")
endif()
else()
target_link_options(swiftnet PRIVATE -lpcap)
target_link_options(swiftnet_shared PRIVATE -lpcap)
if(WIN32)
find_path(NPCAP_INCLUDE_DIR
NAMES pcap.h
PATH_SUFFIXES Include include
PATHS
"C:/Npcap"
"$ENV{NPCAP_SDK}"
)

if(SN_ARCH MATCHES aarch64)
find_library(NPCAP_WPCAP_LIBRARY
NAMES wpcap
PATHS
"C:/Npcap/Lib/ARM64"
"$ENV{NPCAP_SDK}/Lib/ARM64"
)

find_library(NPCAP_PACKET_LIBRARY
NAMES Packet
PATHS
"C:/Npcap/Lib/ARM64"
"$ENV{NPCAP_SDK}/Lib/ARM64"
)
elseif(SN_ARCH MATCHES x86)
find_library(NPCAP_WPCAP_LIBRARY
NAMES wpcap
PATHS
"C:/Npcap/Lib/x64"
"$ENV{NPCAP_SDK}/Lib/x64"
)

find_library(NPCAP_PACKET_LIBRARY
NAMES Packet
PATHS
"C:/Npcap/Lib/x64"
"$ENV{NPCAP_SDK}/Lib/x64"
)
endif()

if(NOT NPCAP_INCLUDE_DIR OR
NOT NPCAP_WPCAP_LIBRARY OR
NOT NPCAP_PACKET_LIBRARY)
message(FATAL_ERROR "Npcap SDK not found")
endif()

target_include_directories(swiftnet PRIVATE
"${NPCAP_INCLUDE_DIR}"
)

target_link_libraries(swiftnet PRIVATE
"${NPCAP_WPCAP_LIBRARY}"
"${NPCAP_PACKET_LIBRARY}"
)

target_include_directories(swiftnet_shared PRIVATE
"${NPCAP_INCLUDE_DIR}"
)

target_link_libraries(swiftnet_shared PRIVATE
"${NPCAP_WPCAP_LIBRARY}"
"${NPCAP_PACKET_LIBRARY}"
)
else()
target_link_options(swiftnet PRIVATE -lpcap)
target_link_options(swiftnet_shared PRIVATE -lpcap)

message("using linking option: -lpcap")
message("using linking option: -lpcap")
endif()
endif()
elseif(BACKEND STREQUAL "dpdk")
find_package(PkgConfig REQUIRED)
Expand All @@ -172,6 +247,20 @@ elseif(BACKEND STREQUAL "dpdk")
message(STATUS "DPDK backend selected")
endif()

if(WIN32)
target_link_libraries(swiftnet PRIVATE
ws2_32
iphlpapi
pthread
)

target_link_libraries(swiftnet_shared PRIVATE
ws2_32
iphlpapi
pthread
)
endif()

set_target_properties(swiftnet PROPERTIES
OUTPUT_NAME "swiftnet"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/output"
Expand Down
2 changes: 0 additions & 2 deletions src/cleanup_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include "internal/networking.h"
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Expand Down
4 changes: 4 additions & 0 deletions src/cleanup_swiftnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,9 @@ void swiftnet_cleanup(void) {
#ifdef SWIFT_NET_INTERNAL_TESTING
printf("Bytes leaked: %d\nItems leaked: %d\n", bytes_leaked, items_leaked);
#endif

#ifdef _WIN32
WSACleanup();
#endif
}

Loading
Loading