Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
35 changes: 0 additions & 35 deletions .github/workflows/run_all_tests_self_hosted.yml

This file was deleted.

1 change: 1 addition & 0 deletions src/internal/datatype_hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,6 @@ void hashmap_destroy(struct SwiftNetHashMap* const hashmap) {
}

free(hashmap->items);
free(hashmap->item_occupation);
}

19 changes: 9 additions & 10 deletions src/internal/networking.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) { \
Expand Down
13 changes: 6 additions & 7 deletions src/internal/pcap/pcap_open.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#include "../internal.h"
#include <pcap/pcap.h>

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;

Expand All @@ -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;
}

1 change: 1 addition & 0 deletions src/swift_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
127 changes: 29 additions & 98 deletions tests/integration_tests_pcap/src/run_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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(&current_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;
Expand Down
10 changes: 5 additions & 5 deletions tests/integration_tests_pcap/src/run_tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +29,7 @@ union Args {

struct Test {
int (*function)(const union Args*);
const union Args args;
union Args args;
const char* test_name;
};

Expand Down
Loading