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/run_all_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ jobs:
working-directory: tests
- name: run_tests
run: |
sudo ./run_all_tests
sudo ./output/run_tests
working-directory: tests
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ endif()

add_library(swiftnet STATIC ${SOURCE_FILES})
target_compile_options(swiftnet PRIVATE -O0)
target_link_options(swiftnet PRIVATE -O0)
target_link_options(swiftnet PRIVATE -lpcap -O0)

set_target_properties(swiftnet PROPERTIES
OUTPUT_NAME "swiftnet"
Expand Down
57 changes: 57 additions & 0 deletions src/cleanup_connection.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "internal/internal.h"
#include "swift_net.h"
#include <stdatomic.h>
#include <stdint.h>
#include <unistd.h>

void swiftnet_client_cleanup(SwiftNetClientConnection* const client) {
Expand All @@ -12,6 +13,34 @@ void swiftnet_client_cleanup(SwiftNetClientConnection* const client) {
vector_destroy(&client->pending_messages);
vector_destroy(&client->packets_completed);

const char* interface_name = client->loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface;

vector_lock(&listeners);

for (uint16_t i = 0; i < listeners.size; i++) {
Listener* const current_listener = vector_get(&listeners, i);
if (strcmp(interface_name, current_listener->interface_name) == 0) {
vector_lock(&current_listener->client_connections);

for (uint16_t inx = 0; inx < current_listener->client_connections.size; inx++) {
SwiftNetClientConnection* const current_client_conn = vector_get(&current_listener->client_connections, i);
if (current_client_conn != client) {
continue;
}

vector_remove(&current_listener->client_connections, inx);

break;
}

vector_unlock(&current_listener->client_connections);

break;
}
}

vector_unlock(&listeners);

atomic_store_explicit(&client->closing, true, memory_order_release);

pthread_join(client->process_packets_thread, NULL);
Expand All @@ -31,6 +60,34 @@ void swiftnet_server_cleanup(SwiftNetServer* const server) {
vector_destroy(&server->pending_messages);
vector_destroy(&server->packets_completed);

const char* interface_name = server->loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface;

vector_lock(&listeners);

for (uint16_t i = 0; i < listeners.size; i++) {
Listener* const current_listener = vector_get(&listeners, i);
if (strcmp(interface_name, current_listener->interface_name) == 0) {
vector_lock(&current_listener->servers);

for (uint16_t inx = 0; inx < current_listener->servers.size; inx++) {
SwiftNetServer* const current_server = vector_get(&current_listener->servers, i);
if (current_server != server) {
continue;
}

vector_remove(&current_listener->servers, inx);

break;
}

vector_unlock(&current_listener->servers);

break;
}
}

vector_unlock(&listeners);

atomic_store_explicit(&server->closing, true, memory_order_release);

pthread_join(server->process_packets_thread, NULL);
Expand Down
19 changes: 0 additions & 19 deletions src/execute_packet_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,6 @@ void execute_packet_callback(PacketCallbackQueue* const queue, void (* const _At

(*packet_handler_loaded)(node->packet_data);

if(node->pending_message != NULL) {
free(node->pending_message->chunks_received);
allocator_free(pending_message_memory_allocator, node->pending_message);

if (connection_type == 0) {
free(((const SwiftNetClientPacketData* const)(node->packet_data))->data);
} else {
free(((const SwiftNetServerPacketData* const)(node->packet_data))->data);
}
} else {
if (connection_type == 0) {
allocator_free(&packet_buffer_memory_allocator, ((SwiftNetClientPacketData*)(node->packet_data))->data - PACKET_HEADER_SIZE);
allocator_free(&client_packet_data_memory_allocator, node->packet_data);
} else {
allocator_free(&packet_buffer_memory_allocator, ((SwiftNetServerPacketData*)(node->packet_data))->data - PACKET_HEADER_SIZE);
allocator_free(&server_packet_data_memory_allocator, node->packet_data);
}
}

allocator_free(&packet_callback_queue_node_memory_allocator, (void*)node);
}
}
Expand Down
26 changes: 22 additions & 4 deletions src/generic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,28 @@ void* swiftnet_server_read_packet(SwiftNetServerPacketData* const packet_data, c
return ptr;
}

void swiftnet_client_destroy_packet_data(SwiftNetClientPacketData* const packet_data) {
allocator_free(&client_packet_data_memory_allocator, packet_data);
void swiftnet_client_destroy_packet_data(SwiftNetClientPacketData* const packet_data, SwiftNetClientConnection* const client_conn) {
if(packet_data->internal_pending_message != NULL) {
free(packet_data->internal_pending_message->chunks_received);

allocator_free(&client_conn->pending_messages_memory_allocator, packet_data->internal_pending_message);

free(packet_data->data);
} else {
allocator_free(&packet_buffer_memory_allocator, packet_data->data - PACKET_HEADER_SIZE - client_conn->prepend_size);
allocator_free(&client_packet_data_memory_allocator, packet_data);
}
}

void swiftnet_server_destroy_packet_data(SwiftNetServerPacketData* const packet_data) {
allocator_free(&server_packet_data_memory_allocator, packet_data);
void swiftnet_server_destroy_packet_data(SwiftNetServerPacketData* const packet_data, SwiftNetServer* const server) {
if(packet_data->internal_pending_message != NULL) {
free(packet_data->internal_pending_message->chunks_received);

allocator_free(&server->pending_messages_memory_allocator, packet_data->internal_pending_message);

free(packet_data->data);
} else {
allocator_free(&packet_buffer_memory_allocator, packet_data->data - PACKET_HEADER_SIZE - server->prepend_size);
allocator_free(&server_packet_data_memory_allocator, packet_data);
}
}
2 changes: 1 addition & 1 deletion src/initialize_server_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool loopback)
new_server->server_port = port;
new_server->loopback = loopback;

// Init bpf device
// Init pcap device
new_server->pcap = swiftnet_pcap_open(loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface);
if (new_server->pcap == NULL) {
fprintf(stderr, "Failed to open bpf\n");
Expand Down
2 changes: 1 addition & 1 deletion src/make_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void swiftnet_server_make_response(SwiftNetServer* const server, SwiftNetServerP
.destination_port = packet_data->metadata.port_info.source_port
};

swiftnet_send_packet(server, packet_data->metadata.sender.maximum_transmission_unit, packet_data->metadata.port_info, buffer, packet_length, &packet_data->metadata.sender.sender_address, &server->packets_sending, &server->packets_sending_memory_allocator, server->pcap, server->eth_header, server->loopback, server->addr_type, server->prepend_size, NULL, true, packet_data->metadata.packet_id);
swiftnet_send_packet(server, packet_data->metadata.sender.maximum_transmission_unit, port_info, buffer, packet_length, &packet_data->metadata.sender.sender_address, &server->packets_sending, &server->packets_sending_memory_allocator, server->pcap, server->eth_header, server->loopback, server->addr_type, server->prepend_size, NULL, true, packet_data->metadata.packet_id);
}

#endif
15 changes: 8 additions & 7 deletions src/process_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,11 @@ static inline void handle_request_response(const uint16_t packet_id, const struc

if (is_valid_response == true) {
if (pending_message != NULL) {
free(pending_message->chunks_received);

allocator_free(pending_message_memory_allocator, pending_message);

vector_lock(pending_messages);

for (uint32_t i = 0; i < pending_messages->size; i++) {
const SwiftNetPendingMessage* const pending_message = vector_get(pending_messages, i);
if ((connection_type == CONNECTION_TYPE_CLIENT && pending_message->packet_id == packet_id) || (connection_type == CONNECTION_TYPE_SERVER && pending_message->sender_address.s_addr == sender.s_addr && pending_message->packet_id == packet_id)) {
const SwiftNetPendingMessage* const current_pending_message = vector_get(pending_messages, i);
if (current_pending_message == pending_message) {
vector_remove(pending_messages, i);
}
}
Expand Down Expand Up @@ -503,8 +499,9 @@ static inline void swiftnet_process_packets(
const uint16_t lost_chunk_indexes = return_lost_chunk_indexes(pending_message->chunks_received, pending_message->packet_info.chunk_amount, mtu - PACKET_HEADER_SIZE, (uint32_t*)(buffer + header_size));

const uint16_t packet_length = sizeof(struct ip) + sizeof(SwiftNetPacketInfo) + (lost_chunk_indexes * sizeof(uint32_t));
const uint16_t packet_length_net_order = htons(packet_length);

memcpy(buffer + prepend_size + offsetof(struct ip, ip_len), &packet_length, SIZEOF_FIELD(struct ip, ip_len));
memcpy(buffer + prepend_size + offsetof(struct ip, ip_len), &packet_length_net_order, SIZEOF_FIELD(struct ip, ip_len));

HANDLE_CHECKSUM(buffer, packet_length + prepend_size, prepend_size);

Expand Down Expand Up @@ -596,6 +593,7 @@ static inline void swiftnet_process_packets(
SwiftNetServerPacketData* const new_packet_data = allocator_allocate(&server_packet_data_memory_allocator) ;
new_packet_data->data = packet_data;
new_packet_data->current_pointer = packet_data;
new_packet_data->internal_pending_message = NULL;
new_packet_data->metadata = (SwiftNetPacketServerMetadata){
.port_info = packet_info.port_info,
.sender = sender,
Expand All @@ -619,6 +617,7 @@ static inline void swiftnet_process_packets(
SwiftNetClientPacketData* const new_packet_data = allocator_allocate(&client_packet_data_memory_allocator) ;
new_packet_data->data = packet_data;
new_packet_data->current_pointer = packet_data;
new_packet_data->internal_pending_message = NULL;
new_packet_data->metadata = (SwiftNetPacketClientMetadata){
.port_info = packet_info.port_info,
.data_length = packet_info.packet_length,
Expand Down Expand Up @@ -672,6 +671,7 @@ static inline void swiftnet_process_packets(
SwiftNetServerPacketData* const packet_data = allocator_allocate(&server_packet_data_memory_allocator);
packet_data->data = ptr;
packet_data->current_pointer = ptr;
packet_data->internal_pending_message = pending_message;
packet_data->metadata = (SwiftNetPacketServerMetadata){
.port_info = packet_info.port_info,
.sender = sender,
Expand All @@ -697,6 +697,7 @@ static inline void swiftnet_process_packets(
SwiftNetClientPacketData* const packet_data = allocator_allocate(&client_packet_data_memory_allocator) ;
packet_data->data = ptr;
packet_data->current_pointer = ptr;
packet_data->internal_pending_message = pending_message;
packet_data->metadata = (SwiftNetPacketClientMetadata){
.port_info = packet_info.port_info,
.data_length = packet_info.packet_length,
Expand Down
3 changes: 2 additions & 1 deletion src/send_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,10 @@ inline void swiftnet_send_packet(
if(current_offset + (mtu - PACKET_HEADER_SIZE) >= packet_info.packet_length) {
// Last chunk
const uint16_t bytes_to_send = (uint16_t)packet_length - current_offset + PACKET_HEADER_SIZE + prepend_size;
const uint16_t bytes_to_send_net_order = htons(bytes_to_send - prepend_size);

memcpy(&buffer[PACKET_HEADER_SIZE + prepend_size], packet->packet_data_start + current_offset, bytes_to_send - prepend_size - PACKET_HEADER_SIZE);
memcpy(&buffer[prepend_size + offsetof(struct ip, ip_len)], &bytes_to_send, SIZEOF_FIELD(struct ip, ip_len));
memcpy(&buffer[prepend_size + offsetof(struct ip, ip_len)], &bytes_to_send_net_order, SIZEOF_FIELD(struct ip, ip_len));

HANDLE_CHECKSUM(buffer, bytes_to_send, prepend_size)

Expand Down
44 changes: 23 additions & 21 deletions src/swift_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ typedef struct {
#endif
} SwiftNetPacketClientMetadata;

typedef struct {
uint32_t packet_length;
SwiftNetPortInfo port_info;
uint8_t packet_type;
uint32_t chunk_amount;
uint32_t chunk_index;
uint32_t maximum_transmission_unit;
} SwiftNetPacketInfo;

typedef struct {
uint8_t* packet_data_start;
SwiftNetPacketInfo packet_info;
uint16_t packet_id;
struct in_addr sender_address;
uint8_t* chunks_received;
uint32_t chunks_received_length;
uint32_t chunks_received_number;
} SwiftNetPendingMessage;

typedef struct {
uint32_t data_length;
SwiftNetPortInfo port_info;
Expand All @@ -88,15 +107,6 @@ typedef struct {
#endif
} SwiftNetPacketServerMetadata;

typedef struct {
uint32_t packet_length;
SwiftNetPortInfo port_info;
uint8_t packet_type;
uint32_t chunk_amount;
uint32_t chunk_index;
uint32_t maximum_transmission_unit;
} SwiftNetPacketInfo;

typedef struct {
uint32_t maximum_transmission_unit;
} SwiftNetServerInformation;
Expand Down Expand Up @@ -126,16 +136,6 @@ typedef struct {
uint8_t* packet_append_pointer; // Current position to append new data
} SwiftNetPacketBuffer;

typedef struct {
uint8_t* packet_data_start;
SwiftNetPacketInfo packet_info;
uint16_t packet_id;
struct in_addr sender_address;
uint8_t* chunks_received;
uint32_t chunks_received_length;
uint32_t chunks_received_number;
} SwiftNetPendingMessage;

typedef struct PacketQueueNode PacketQueueNode;

struct PacketQueueNode {
Expand Down Expand Up @@ -165,12 +165,14 @@ typedef struct {
uint8_t* data;
uint8_t* current_pointer;
SwiftNetPacketServerMetadata metadata;
SwiftNetPendingMessage* internal_pending_message; // Do not use!!
} SwiftNetServerPacketData;

typedef struct {
uint8_t* data;
uint8_t* current_pointer;
SwiftNetPacketClientMetadata metadata;
SwiftNetPendingMessage* internal_pending_message; // Do not use!!
} SwiftNetClientPacketData;

typedef struct {
Expand Down Expand Up @@ -278,8 +280,8 @@ extern SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool lo
extern SwiftNetClientConnection* swiftnet_create_client(const char* const ip_address, const uint16_t port, const uint32_t timeout_ms);
extern void* swiftnet_client_read_packet(SwiftNetClientPacketData* const packet_data, const uint32_t data_size);
extern void* swiftnet_server_read_packet(SwiftNetServerPacketData* const packet_data, const uint32_t data_size);
extern void swiftnet_client_destroy_packet_data(SwiftNetClientPacketData* const packet_data);
extern void swiftnet_server_destroy_packet_data(SwiftNetServerPacketData* const packet_data);
extern void swiftnet_client_destroy_packet_data(SwiftNetClientPacketData* const packet_data, SwiftNetClientConnection* const client_conn);
extern void swiftnet_server_destroy_packet_data(SwiftNetServerPacketData* const packet_data, SwiftNetServer* const server);

extern void swiftnet_cleanup();

Expand Down
14 changes: 4 additions & 10 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0")

file(GLOB SRC_FILES "${CMAKE_SOURCE_DIR}/src/*.c")

foreach(src_file IN LISTS SRC_FILES)
get_filename_component(exec_name ${src_file} NAME_WE)
add_executable(${exec_name} ${src_file})
target_compile_options(${exec_name} PRIVATE)
target_link_libraries(${exec_name} swiftnet pcap)
endforeach()
add_executable(run_tests ${SRC_FILES})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
target_link_libraries(run_tests swiftnet pcap)
target_link_options(run_tests PRIVATE -g -O0)

add_executable(run_all_tests run_all_tests.c)
target_link_libraries(run_all_tests swiftnet pcap)
target_link_options(run_all_tests PRIVATE -g -O0)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
Loading