From 3be534eb2b75f4c3376bf61014c7a06d719cb9e3 Mon Sep 17 00:00:00 2001 From: Marc Date: Sat, 20 Aug 2022 16:10:00 +0100 Subject: Add macOS support (#8) Add macOS build support --- .github/workflows/linux_x86.yml | 64 +++++++++++++++++++++++++++++++++++++++ .github/workflows/macos_x86.yml | 64 +++++++++++++++++++++++++++++++++++++++ .github/workflows/x86_64.yml | 64 --------------------------------------- .gitignore | 8 ++++- CMakeLists.txt | 14 +++++---- README.md | 11 ++++--- src/libwifi/core/core.c | 10 +++++- src/libwifi/core/misc/byteswap.h | 25 ++++++++++++++- src/libwifi/parse/misc/radiotap.c | 8 +++-- test/CMakeLists.txt | 3 ++ utils/CMakeLists.txt | 3 ++ utils/src/test_parsing.c | 2 +- 12 files changed, 195 insertions(+), 81 deletions(-) create mode 100644 .github/workflows/linux_x86.yml create mode 100644 .github/workflows/macos_x86.yml delete mode 100644 .github/workflows/x86_64.yml diff --git a/.github/workflows/linux_x86.yml b/.github/workflows/linux_x86.yml new file mode 100644 index 0000000..f9d536c --- /dev/null +++ b/.github/workflows/linux_x86.yml @@ -0,0 +1,64 @@ +name: libwifi (Linux) (64-Bit) + +on: [push] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build Linux X86_64 + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config $BUILD_TYPE + + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build Linux X86_64 + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config $BUILD_TYPE + + - name: Create Test Environment + run: cmake -E make_directory ${{github.workspace}}/test/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake ${{github.workspace}}/test + + - name: Build Tests + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake --build . + + - name: Test Linux X86_64 + shell: bash + working-directory: ${{github.workspace}}/test/build + run: ctest --output-on-failure diff --git a/.github/workflows/macos_x86.yml b/.github/workflows/macos_x86.yml new file mode 100644 index 0000000..abdb664 --- /dev/null +++ b/.github/workflows/macos_x86.yml @@ -0,0 +1,64 @@ +name: libwifi (MacOS) (64-Bit) + +on: [push] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build MacOS X86_64 + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config $BUILD_TYPE + + test: + runs-on: macos-latest + + steps: + - uses: actions/checkout@v2 + + - name: Create Build Environment + run: cmake -E make_directory ${{github.workspace}}/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE + + - name: Build MacOS X86_64 + shell: bash + working-directory: ${{github.workspace}}/build + run: cmake --build . --config $BUILD_TYPE + + - name: Create Test Environment + run: cmake -E make_directory ${{github.workspace}}/test/build + + - name: Configure CMake + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake ${{github.workspace}}/test + + - name: Build Tests + shell: bash + working-directory: ${{github.workspace}}/test/build + run: cmake --build . + + - name: Test MacOS X86_64 + shell: bash + working-directory: ${{github.workspace}}/test/build + run: ctest --output-on-failure diff --git a/.github/workflows/x86_64.yml b/.github/workflows/x86_64.yml deleted file mode 100644 index cc90e1d..0000000 --- a/.github/workflows/x86_64.yml +++ /dev/null @@ -1,64 +0,0 @@ -name: libwifi (64-Bit) - -on: [push] - -env: - # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) - BUILD_TYPE: Release - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - name: Create Build Environment - run: cmake -E make_directory ${{github.workspace}}/build - - - name: Configure CMake - shell: bash - working-directory: ${{github.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - - - name: Build X86_64 - shell: bash - working-directory: ${{github.workspace}}/build - run: cmake --build . --config $BUILD_TYPE - - test: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - name: Create Build Environment - run: cmake -E make_directory ${{github.workspace}}/build - - - name: Configure CMake - shell: bash - working-directory: ${{github.workspace}}/build - run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE - - - name: Build X86_64 - shell: bash - working-directory: ${{github.workspace}}/build - run: cmake --build . --config $BUILD_TYPE - - - name: Create Test Environment - run: cmake -E make_directory ${{github.workspace}}/test/build - - - name: Configure CMake - shell: bash - working-directory: ${{github.workspace}}/test/build - run: cmake ${{github.workspace}}/test - - - name: Build Tests - shell: bash - working-directory: ${{github.workspace}}/test/build - run: cmake --build . - - - name: Test X86_64 - shell: bash - working-directory: ${{github.workspace}}/test/build - run: ctest --output-on-failure diff --git a/.gitignore b/.gitignore index dbbe0da..7ee9e2d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.a *.o *.so* +*.dylib # Build CMakeLists.txt.user @@ -20,4 +21,9 @@ CMakeUserPresets.json # Output build/ -tests/build/ +test/build/ +test/*_tests +utils/test_* + +# Temporary Patches +*.patch diff --git a/CMakeLists.txt b/CMakeLists.txt index a0d9809..1b5304c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project(wifi DESCRIPTION "802.11 Parsing / Generation library" VERSION 0.1) execute_process(COMMAND git rev-parse --abbrev-ref HEAD OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE GITBRANCH) execute_process(COMMAND git log -1 --pretty=format:%h OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE GITHASH) execute_process(COMMAND date OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE BUILDTIME) -set(LIBWIFI_VERSION "0.0.6") +set(LIBWIFI_VERSION "0.0.7") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu17") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") @@ -13,6 +13,8 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wno-language-extension-token") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes -Wstrict-prototypes") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-strong") +set(CMAKE_MACOSX_RPATH 1) + message(" ") message(" _ _ _ _ __ _ ") message("| |(_)| | (_) / _|(_) ") @@ -48,9 +50,9 @@ set_target_properties(wifi PROPERTIES VERSION ${LIBWIFI_VERSION}) set_target_properties(wifi PROPERTIES SOVERSION 0) set_target_properties(wifi PROPERTIES PUBLIC_HEADER src/libwifi.h) -install(DIRECTORY src/libwifi/core/ DESTINATION /usr/include/libwifi/core FILES_MATCHING PATTERN "*.h") -install(DIRECTORY src/libwifi/parse/ DESTINATION /usr/include/libwifi/parse FILES_MATCHING PATTERN "*.h") -install(DIRECTORY src/libwifi/gen/ DESTINATION /usr/include/libwifi/gen FILES_MATCHING PATTERN "*.h") -install(TARGETS wifi LIBRARY DESTINATION /usr/lib PUBLIC_HEADER DESTINATION /usr/include) +install(DIRECTORY src/libwifi/core/ DESTINATION /usr/local/include/libwifi/core FILES_MATCHING PATTERN "*.h") +install(DIRECTORY src/libwifi/parse/ DESTINATION /usr/local/include/libwifi/parse FILES_MATCHING PATTERN "*.h") +install(DIRECTORY src/libwifi/gen/ DESTINATION /usr/local/include/libwifi/gen FILES_MATCHING PATTERN "*.h") +install(TARGETS wifi LIBRARY DESTINATION /usr/local/lib PUBLIC_HEADER DESTINATION /usr/local/include) -add_custom_target(uninstall COMMAND rm -rf /usr/include/libwifi; rm -rf /usr/include/libwifi.h) +add_custom_target(uninstall COMMAND rm -rf /usr/local/include/libwifi; rm -rf /usr/local/include/libwifi.h; rm -rf /usr/local/lib/libwifi*) diff --git a/README.md b/README.md index bd036d8..542c061 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,15 @@ # libwifi 802.11 Parsing / Generation library -| Build Status | OS | Architecture | -| ---------------------------------------------------------------------------------- | ------| ------------ | -|![X86_64](https://github.com/libwifi/libwifi/actions/workflows/x86_64.yml/badge.svg) | Linux | x86_64 | +| Build Status | OS | +|---------------------------------------------------------------------------------------|-------| +|![linux](https://github.com/libwifi/libwifi/actions/workflows/linux_x86.yml/badge.svg) | Linux | +|![macOS](https://github.com/libwifi/libwifi/actions/workflows/macos_x86.yml/badge.svg) | macOS | ## What is this? -libwifi is a C library with a permissive license for generating and parsing a wide variety of 802.11 wireless frames (see the [Feature Checklist](https://libwifi.so/features)) on Linux with a few lines of straight forward code (see the [Examples section](#examples) below). +libwifi is a C library with a permissive license for generating and parsing a wide variety of 802.11 wireless frames (see the [Feature Checklist](https://libwifi.so/features)) on Linux and macOS with a few lines of straight forward code (see the [Examples section](#examples) below). -It is written with a simple-to-use approach while also exposing features that allow more advanced use, with clean and readable code being a priority. Other goals of the library include cross-architecture support, clean compilation without warnings and strict error checking. +libwifi has been tested across Linux and macOS, on x86, MIPS and ARM, and is written with a simple-to-use approach while also exposing features that allow more advanced use, with clean and readable code being a priority. Other goals of the library include cross-architecture support, clean compilation without warnings and strict error checking. The library is fully documented with code comments in both the headers files and the code files. diff --git a/src/libwifi/core/core.c b/src/libwifi/core/core.c index 0340a82..863e58f 100644 --- a/src/libwifi/core/core.c +++ b/src/libwifi/core/core.c @@ -25,9 +25,17 @@ void libwifi_random_mac(unsigned char buf[6], unsigned char prefix[3]) { memset(buf, 0, 6); if (prefix != NULL) { memcpy(buf, prefix, 3); - getrandom(buf + 3, 3, 0); +#if __APPLE__ + arc4random_buf(buf + 3, 3); +#else + getrandom(buf + 3, 3, 0); +#endif /* __APPLE__ */ } else { +#if __APPLE__ + arc4random_buf(buf, 6); +#else getrandom(buf, 6, 0); +#endif /* __APPLE__ */ } } diff --git a/src/libwifi/core/misc/byteswap.h b/src/libwifi/core/misc/byteswap.h index cab264f..aa91a37 100644 --- a/src/libwifi/core/misc/byteswap.h +++ b/src/libwifi/core/misc/byteswap.h @@ -16,6 +16,28 @@ #ifndef LIBWIFI_CORE_BYTESWAP_H #define LIBWIFI_CORE_BYTESWAP_H +#if __APPLE__ +#include + +#define BYTESWAP16(x) OSSwapInt16(x) +#define BYTESWAP32(x) OSSwapInt32(x) +#define BYTESWAP64(x) OSSwapInt64(x) + +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#else #include #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ @@ -26,6 +48,7 @@ #define BYTESWAP16(x) (__bswap_16(x)) #define BYTESWAP32(x) (__bswap_32(x)) #define BYTESWAP64(x) (__bswap_32(x)) -#endif +#endif /* __BYTE_ORDER__ */ +#endif /* __APPLE__ */ #endif /* LIBWIFI_CORE_BYTESWAP_H */ diff --git a/src/libwifi/parse/misc/radiotap.c b/src/libwifi/parse/misc/radiotap.c index 80ddced..faf6009 100644 --- a/src/libwifi/parse/misc/radiotap.c +++ b/src/libwifi/parse/misc/radiotap.c @@ -17,14 +17,18 @@ #include "../../core/radiotap/radiotap_iter.h" #include -#include #include +#if !(__APPLE__) +#include +#endif + /** * The libwifi radiotap parser uses the usual ieee80211_radiotap_iterator to parse incoming * radiotap headers into a consumable libwifi_radiotap_info struct. */ -int libwifi_parse_radiotap_info(struct libwifi_radiotap_info *info, const unsigned char *frame, size_t frame_len) { +int libwifi_parse_radiotap_info(struct libwifi_radiotap_info *info, const unsigned char *frame, + size_t frame_len) { memset(info, 0, sizeof(struct libwifi_radiotap_info)); if (frame_len < sizeof(struct ieee80211_radiotap_header)) { diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 64f8f36..19729df 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,6 +9,9 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") link_directories("../build/") +link_directories(AFTER "/usr/local/lib") +include_directories(AFTER "/usr/local/include") + add_executable(action_tests src/action_tests.c) target_link_libraries(action_tests wifi) add_executable(assoc_req_tests src/assoc_req_tests.c) diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index bdf7227..f3c9824 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -7,6 +7,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ggdb -O0") +link_directories(AFTER "/usr/local/lib") +include_directories(AFTER "/usr/local/include") + add_executable(test_misc src/helpers.c src/test_misc.c) add_executable(test_generation src/helpers.c src/test_generation.c) add_executable(test_parsing src/helpers.c src/test_parsing.c) diff --git a/utils/src/test_parsing.c b/utils/src/test_parsing.c index 339816e..b9f9dbc 100644 --- a/utils/src/test_parsing.c +++ b/utils/src/test_parsing.c @@ -370,7 +370,7 @@ void parse_data_eapol(struct libwifi_frame frame, unsigned char *args, const str printf("EAPOL: Descriptor: %d\n", data.descriptor); printf("EAPOL: Key Info: Information: 0x%04x\n", data.key_info.information); printf("EAPOL: Key Info: Key Length: %d\n", data.key_info.key_length); - printf("EAPOL: Key Info: Replay Counter: %lu\n", data.key_info.replay_counter); + printf("EAPOL: Key Info: Replay Counter: %llu\n", data.key_info.replay_counter); printf("EAPOL: Key Info: Nonce: "); for (size_t i = 0; i < sizeof(data.key_info.nonce); ++i) printf("%02x ", data.key_info.nonce[i]); printf("\n"); -- cgit 1.4.1