diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5d90ba5..04bd941 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -39,7 +39,7 @@ jobs: - name: install_dependencies_macos if: runner.os == 'macOS' - run: brew install cmake clang + run: brew install cmake - name: build run: | diff --git a/.github/workflows/run_all_tests_self_hosted.yml b/.github/workflows/run_all_tests_self_hosted.yml deleted file mode 100644 index 8c0d1e5..0000000 --- a/.github/workflows/run_all_tests_self_hosted.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Run all tests inside repo/tests - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - -jobs: - build: - strategy: - matrix: - os: [macOS] - arch: [ARM64] - - runs-on: - - self-hosted - - ${{ matrix.os }} - - ${{ matrix.arch }} - - steps: - - uses: actions/checkout@v4 - - name: build - run: | - cp profiles/pcap_profile.cmake profile.cmake - ./build.sh - working-directory: build - - name: build_integration_tests - run: | - ./build_tests.sh - working-directory: tests/integration_tests_pcap/build - - name: run_integration_tests - run: | - sudo ./run_tests - working-directory: tests/integration_tests_pcap/output diff --git a/src/internal/datatype_hashmap.c b/src/internal/datatype_hashmap.c index 4db1161..5ebfa43 100644 --- a/src/internal/datatype_hashmap.c +++ b/src/internal/datatype_hashmap.c @@ -302,5 +302,6 @@ void hashmap_destroy(struct SwiftNetHashMap* const hashmap) { } free(hashmap->items); + free(hashmap->item_occupation); } diff --git a/src/internal/networking.h b/src/internal/networking.h index c6c0c1a..3b92620 100644 --- a/src/internal/networking.h +++ b/src/internal/networking.h @@ -39,12 +39,13 @@ #define FREE_PACKET_CONSTRUCTION(buffer_name, network_data) - extern pcap_t* swiftnet_pcap_open(const char* const restrict interface); + extern void swiftnet_pcap_open(const char* const restrict interface, struct SwiftNetNetworkData* const network_data); extern int swiftnet_pcap_send(pcap_t *pcap, const uint8_t *data, const uint32_t len); inline static struct SwiftNetNetworkData swiftnet_initialize_networking(const char* const restrict interface) { - pcap_t* pcap = swiftnet_pcap_open(interface); - if (unlikely(pcap == NULL)) { + struct SwiftNetNetworkData network_data; + swiftnet_pcap_open(interface, &network_data); + if (unlikely(network_data.pcap == NULL)) { PRINT_ERROR("Failed to open pcap"); struct SwiftNetNetworkData net_data_null; @@ -53,13 +54,10 @@ return net_data_null; } - const uint8_t addr_type = (uint8_t)pcap_datalink(pcap); + const uint8_t addr_type = (uint8_t)pcap_datalink(network_data.pcap); - struct SwiftNetNetworkData network_data = { - .pcap = pcap, - .addr_type = addr_type, - .prepend_size = PACKET_PREPEND_SIZE(addr_type) - }; + network_data.addr_type = addr_type; + network_data.prepend_size = PACKET_PREPEND_SIZE(addr_type); return network_data; } @@ -71,7 +69,8 @@ pcap_loop((network_data)->pcap, 0, pcap_packet_handle, (void*)listener); #define SWIFTNET_CLOSE_CONNECTION(network_data) \ - pcap_close((network_data)->pcap); + pcap_close((network_data)->pcap); \ + pcap_freecode(&(network_data)->fp); #define SWIFTNET_SEND_PACKET(network_data, buffer, len, packet_metadata) \ while(swiftnet_pcap_send((network_data)->pcap, buffer, len + GET_PREPEND_SIZE(network_data) + PACKET_HEADER_SIZE + (packet_metadata ? sizeof(struct SwiftNetPacketMetadata) : 0)) == -2) { \ diff --git a/src/internal/pcap/pcap_open.c b/src/internal/pcap/pcap_open.c index 5d71ee9..56737bc 100644 --- a/src/internal/pcap/pcap_open.c +++ b/src/internal/pcap/pcap_open.c @@ -1,10 +1,9 @@ #include "../internal.h" #include -pcap_t* swiftnet_pcap_open(const char* const restrict interface) { +void swiftnet_pcap_open(const char* const restrict interface, struct SwiftNetNetworkData* const network_data) { char errbuf[PCAP_ERRBUF_SIZE]; pcap_t* p; - struct bpf_program fp; char filter_exp[] = "ip proto 253"; bpf_u_int32 net; @@ -21,19 +20,19 @@ pcap_t* swiftnet_pcap_open(const char* const restrict interface) { if(!p) { PRINT_ERROR("pcap_open_live failed: %s", errbuf); - return NULL; + network_data->pcap = NULL; + return; } - if(pcap_compile(p, &fp, filter_exp, 0, net) == -1) { + if(pcap_compile(p, &network_data->fp, filter_exp, 0, net) == -1) { PRINT_ERROR("Couldn't parse filter %s: %s", filter_exp, pcap_geterr(p)); exit(EXIT_FAILURE); } - if(pcap_setfilter(p, &fp) != 0) { + if(pcap_setfilter(p, &network_data->fp) != 0) { PRINT_ERROR("Unable to set filter: %s", pcap_geterr(p)); } - - return p; + network_data->pcap = p; } diff --git a/src/swift_net.h b/src/swift_net.h index 4f18934..bdd12f4 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -45,6 +45,7 @@ struct SwiftNetNetworkData { #endif #ifdef SWIFT_NET_BACKEND_PCAP pcap_t* pcap; + struct bpf_program fp; uint16_t addr_type; uint8_t prepend_size; #endif diff --git a/tests/integration_tests_pcap/src/run_tests.c b/tests/integration_tests_pcap/src/run_tests.c index 872f549..3917b82 100644 --- a/tests/integration_tests_pcap/src/run_tests.c +++ b/tests/integration_tests_pcap/src/run_tests.c @@ -18,7 +18,7 @@ char private_ip_address_testing[INET_ADDRSTRLEN]; int main(void) { get_private_ip_from_socket(); - const struct Test tests[] = { + struct Test tests[] = { // Loopback tests { .function = test_sending_packet, @@ -116,103 +116,6 @@ int main(void) { }}, .test_name = "Test server making large response" }, - // local default interface test - { - .function = test_sending_packet, - .args = {.test_sending_packet_args = { - .client_data_len = 50, - .server_data_len = 50, - .ip_address = private_ip_address_testing, - .loopback = false - }}, - .test_name = "Test sending small packets" - }, - { - .function = test_sending_packet, - .args = {.test_sending_packet_args = { - .client_data_len = 0, - .server_data_len = 10000, - .ip_address = private_ip_address_testing, - .loopback = false - }}, - .test_name = "Test client sending large packet" - }, - { - .function = test_sending_packet, - .args = {.test_sending_packet_args = { - .client_data_len = 10000, - .server_data_len = 10, - .ip_address = private_ip_address_testing, - .loopback = false - }}, - .test_name = "Test server sending large packet" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Server, - .request_data_len = 100, - .response_data_len = 100 - }}, - .test_name = "Test client making small request" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Client, - .request_data_len = 100, - .response_data_len = 100 - }}, - .test_name = "Test server making small request" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Client, - .request_data_len = 10000, - .response_data_len = 100 - }}, - .test_name = "Test server making large request" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Server, - .request_data_len = 10000, - .response_data_len = 100 - }}, - .test_name = "Test client making large request" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Client, - .request_data_len = 100, - .response_data_len = 10000 - }}, - .test_name = "Test client making large response" - }, - { - .function = test_making_request, - .args = {.test_making_request_args = { - .ip_address = private_ip_address_testing, - .loopback = false, - .receiver = Server, - .request_data_len = 100, - .response_data_len = 10000 - }}, - .test_name = "Test server making large response" - }, }; swiftnet_initialize(); @@ -236,6 +139,34 @@ int main(void) { continue; } + + // Test default network interface only on macos, because it works without any setup + #ifdef __APPLE__ + for (uint16_t i = 0; i < sizeof(tests) / sizeof(struct Test); i++) { + struct Test* current_test = &tests[i]; + if (current_test->function == test_making_request) { + current_test->args.test_making_request_args.loopback = false; + current_test->args.test_making_request_args.ip_address = private_ip_address_testing; + } else if (current_test->function == test_sending_packet) { + current_test->args.test_sending_packet_args.loopback = false; + current_test->args.test_sending_packet_args.ip_address = private_ip_address_testing; + } + + int result = current_test->function(¤t_test->args); + if (result != 0) { + PRINT_ERROR("Failed test: %s", current_test->test_name); + + swiftnet_cleanup(); + + return -1; + } + + PRINT_SUCCESS("Successfully completed test: %s", current_test->test_name); + + continue; + } + #endif + swiftnet_cleanup(); return 0; diff --git a/tests/integration_tests_pcap/src/run_tests.h b/tests/integration_tests_pcap/src/run_tests.h index 1698d8f..cbcf5a7 100644 --- a/tests/integration_tests_pcap/src/run_tests.h +++ b/tests/integration_tests_pcap/src/run_tests.h @@ -8,15 +8,15 @@ #include "../../shared.h" struct TestSendingPacketArgs { - const char* ip_address; - const bool loopback; + char* ip_address; + bool loopback; const uint32_t client_data_len; const uint32_t server_data_len; }; struct TestMakingRequestArgs { - const char* ip_address; - const bool loopback; + char* ip_address; + bool loopback; const enum ConnectionType receiver; const uint32_t request_data_len; const uint32_t response_data_len; @@ -29,7 +29,7 @@ union Args { struct Test { int (*function)(const union Args*); - const union Args args; + union Args args; const char* test_name; };