Skip to content

Issue 72 #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2025
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
Binary file modified build/output/libswift_net.a
Binary file not shown.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(SOURCE_FILES
process_packets.c
execute_packet_callback.c
create_packet_buffer.c
add_debug_flags.c
destroy_packet_buffer.c
internal/get_mtu.c
internal/get_default_interface.c
Expand Down
6 changes: 6 additions & 0 deletions src/add_debug_flags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "swift_net.h"
#include "internal/internal.h"

void swiftnet_add_debug_flags(const uint32_t flags) {
debugger.flags |= flags;
}
35 changes: 32 additions & 3 deletions src/initialize_client_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ void* request_server_information(void* request_server_information_args_void) {
return NULL;
}

SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Requested server information: {\"server_ip_address\": \"%s\"}\n", inet_ntoa(request_server_information_args->server_addr.sin_addr));
}
)

sendto(request_server_information_args->sockfd, request_server_information_args->data, request_server_information_args->size, 0, (struct sockaddr *)&request_server_information_args->server_addr, request_server_information_args->server_addr_len);

usleep(1000000);
Expand Down Expand Up @@ -125,16 +131,33 @@ SwiftNetClientConnection* swiftnet_create_client(const char* const restrict ip_a
while(1) {
const int bytes_received = recvfrom(empty_connection->sockfd, server_information_buffer, sizeof(server_information_buffer), 0x00, NULL, NULL);
if(bytes_received != PACKET_HEADER_SIZE + sizeof(SwiftNetServerInformation)) {
SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Invalid packet received from server. Expected server information: {\"bytes_received\": %d, \"expected_bytes\": %d}\n", bytes_received, PACKET_HEADER_SIZE + sizeof(SwiftNetServerInformation));
}
)
continue;
}

const SwiftNetPacketInfo* const restrict packetInfo = (SwiftNetPacketInfo *)&server_information_buffer[sizeof(struct ip)];
const struct ip* const restrict ip_header = (struct ip*)&server_information_buffer;

const SwiftNetPacketInfo* const restrict packet_info = (SwiftNetPacketInfo *)&server_information_buffer[sizeof(struct ip)];

if(packetInfo->port_info.destination_port != empty_connection->port_info.source_port || packetInfo->port_info.source_port != empty_connection->port_info.destination_port) {
if(packet_info->port_info.destination_port != empty_connection->port_info.source_port || packet_info->port_info.source_port != empty_connection->port_info.destination_port) {
SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Port info does not match: {\"destination_port\": %d, \"source_port\": %d, \"source_ip_address\": \"%s\"}\n", packet_info->port_info.destination_port, packet_info->port_info.source_port, ip_header->ip_src.s_addr);
}
)
continue;
}

if(packetInfo->packet_type != PACKET_TYPE_REQUEST_INFORMATION) {
if(packet_info->packet_type != PACKET_TYPE_REQUEST_INFORMATION) {
SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Invalid packet type: {\"packet_type\": %d}\n", packet_info->packet_type);
}
)
continue;
}

Expand All @@ -153,5 +176,11 @@ SwiftNetClientConnection* swiftnet_create_client(const char* const restrict ip_a

pthread_create(&empty_connection->handle_packets_thread, NULL, swiftnet_client_handle_packets, empty_connection);

SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Successfully initialized client\n");
}
)

return empty_connection;
}
7 changes: 6 additions & 1 deletion src/initialize_server_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ SwiftNetServer* swiftnet_create_server(const uint16_t port) {

const uint8_t opt = 1;
setsockopt(empty_server->sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));


empty_server->packet_queue = (PacketQueue){
.first_node = NULL,
Expand All @@ -69,5 +68,11 @@ SwiftNetServer* swiftnet_create_server(const uint16_t port) {
// Create a new thread that will handle all packets received
pthread_create(&empty_server->handle_packets_thread, NULL, swiftnet_server_handle_packets, empty_server);

SwiftNetDebug(
if (check_debug_flag(DEBUG_INITIALIZATION)) {
send_debug_message("Successfully initialized server\n");
}
)

return empty_server;
}
4 changes: 4 additions & 0 deletions src/initialize_swift_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "internal/internal.h"
#include <unistd.h>

SwiftNetDebug(
SwiftNetDebugger debugger = {.flags = 0};
)

uint32_t maximum_transmission_unit = 0x00;

void swiftnet_initialize() {
Expand Down
32 changes: 32 additions & 0 deletions src/internal/internal.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#pragma once

#include <string.h>
#include <netinet/in.h>
#include <stdatomic.h>
#include <stdlib.h>
#include "../swift_net.h"
#include <sys/socket.h>
#include <stdarg.h>
#include <stdio.h>

#define REQUEST_LOST_PACKETS_RETURN_UPDATED_BIT_ARRAY 0x00
#define REQUEST_LOST_PACKETS_RETURN_COMPLETED_PACKET 0x01
Expand Down Expand Up @@ -107,6 +111,34 @@ extern void* swiftnet_client_process_packets(void* restrict const void_client);
extern void* execute_packet_callback_client(void* void_client);
extern void* execute_packet_callback_server(void* void_server);

SwiftNetDebug(
extern SwiftNetDebugger debugger;

static inline bool check_debug_flag(SwiftNetDebugFlags flag) {
return (debugger.flags & flag) != 0;
}

static inline void send_debug_message(const char* message, ...) {
va_list args;
va_start(args, message);

char* prefix = "[DEBUG] ";

const uint32_t prefix_length = strlen(prefix);
const uint32_t message_length = strlen(message);

char full_message[prefix_length + message_length + 1];

memcpy(full_message, prefix, prefix_length);
memcpy(full_message + prefix_length, message, message_length);
full_message[prefix_length + message_length] = '\0';

vprintf(full_message, args);

va_end(args);
}
);

typedef enum {
CONNECTION_TYPE_SERVER,
CONNECTION_TYPE_CLIENT
Expand Down
12 changes: 12 additions & 0 deletions src/process_packets.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "internal/internal.h"
#include "swift_net.h"
#include <arpa/inet.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stddef.h>
Expand Down Expand Up @@ -269,9 +270,20 @@ static inline void swiftnet_process_packets(
memset(&packet_buffer[sizeof(struct ip) + offsetof(SwiftNetPacketInfo, checksum)], 0x00, SIZEOF_FIELD(SwiftNetPacketInfo, checksum));

if(packet_corrupted(checksum_received, packet_info.chunk_size + sizeof(SwiftNetPacketInfo), &packet_buffer[sizeof(struct ip)]) == true) {
SwiftNetDebug(
if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) {
send_debug_message("Received corrupted packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, packet_info.packet_id);
}
)
goto next_packet;
}

SwiftNetDebug(
if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) {
send_debug_message("Received packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d, \"packet_type\": %d, \"packet_length\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, packet_info.packet_id, packet_info.packet_type, packet_info.packet_length);
}
)

switch(packet_info.packet_type) {
case PACKET_TYPE_REQUEST_INFORMATION:
{
Expand Down
12 changes: 12 additions & 0 deletions src/send_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ static inline void swiftnet_send_packet(
.maximum_transmission_unit = maximum_transmission_unit
};

SwiftNetDebug(
if (check_debug_flag(DEBUG_PACKETS_SENDING)) {
send_debug_message("Sending packet: {\"destination_ip_address\": \"%s\", \"destination_port\": %d, \"packet_length\": %d}\n", inet_ntoa(target_addr->sin_addr), port_info.destination_port, packet_length);
}
)

memcpy(packet->packet_buffer_start, &packet_info, sizeof(SwiftNetPacketInfo));

if(packet_length > mtu) {
Expand All @@ -186,6 +192,12 @@ static inline void swiftnet_send_packet(
for(uint32_t i = 0; ; i++) {
const uint32_t current_offset = i * (mtu - PACKET_HEADER_SIZE);

SwiftNetDebug(
if (check_debug_flag(DEBUG_PACKETS_SENDING)) {
send_debug_message("Sent chunk: {\"chunk_index\": %d}\n", i);
}
)

memcpy(&buffer[offsetof(SwiftNetPacketInfo, chunk_index)], &i, SIZEOF_FIELD(SwiftNetPacketInfo, chunk_index));

memset(&buffer[offsetof(SwiftNetPacketInfo, checksum)], 0x00, SIZEOF_FIELD(SwiftNetPacketInfo, checksum));
Expand Down
21 changes: 20 additions & 1 deletion src/swift_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,24 @@
#define SwiftNetErrorCheck(code)
#endif

#ifndef SWIFT_NET_DISABLE_DEBUGGING
#define SwiftNetDebug(code) code
#else
#define SwiftNetDebug(code) code
#endif

extern uint32_t maximum_transmission_unit;

typedef enum {
DEBUG_PACKETS_SENDING = 1u << 0,
DEBUG_PACKETS_RECEIVING = 1u << 1,
DEBUG_INITIALIZATION = 1u << 2
} SwiftNetDebugFlags;

typedef struct {
uint32_t flags;
} SwiftNetDebugger;

typedef struct {
uint16_t destination_port;
uint16_t source_port;
Expand Down Expand Up @@ -191,6 +207,9 @@ extern SwiftNetPacketBuffer swiftnet_server_create_packet_buffer(const uint32_t
extern SwiftNetPacketBuffer swiftnet_client_create_packet_buffer(const uint32_t buffer_size);
extern void swiftnet_server_destroy_packet_buffer(SwiftNetPacketBuffer* restrict const packet);
extern void swiftnet_client_destroy_packet_buffer(SwiftNetPacketBuffer* restrict const packet);

extern SwiftNetServer* swiftnet_create_server(const uint16_t port);
extern SwiftNetClientConnection* swiftnet_create_client(const char* const restrict ip_address, const uint16_t port);

SwiftNetDebug(
extern void swiftnet_add_debug_flags(const uint32_t flags);
)
Binary file modified tests/output/test_basic_client_server_communication
Binary file not shown.
Binary file modified tests/output/test_client_sending_large_packet_to_server
Binary file not shown.
Binary file modified tests/output/test_server_sending_large_packet_to_client
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/src/test_basic_client_server_communication.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ void server_message_handler(uint8_t* data, SwiftNetPacketServerMetadata* restric
}

int main() {
swiftnet_add_debug_flags(DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING | DEBUG_INITIALIZATION);

swiftnet_initialize();

server = swiftnet_create_server(8080);
Expand Down
2 changes: 2 additions & 0 deletions tests/src/test_client_sending_large_packet_to_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void server_message_handler(uint8_t* data, SwiftNetPacketServerMetadata* restric
}

int main() {
swiftnet_add_debug_flags(DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING | DEBUG_INITIALIZATION);

random_generated_data = malloc(DATA_TO_SEND);
if (random_generated_data == NULL) {
fprintf(stderr, "failed to allocate memory\n");
Expand Down
2 changes: 2 additions & 0 deletions tests/src/test_server_sending_large_packet_to_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ void server_message_handler(uint8_t* data, SwiftNetPacketServerMetadata* restric
}

int main() {
swiftnet_add_debug_flags(DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING | DEBUG_INITIALIZATION);

random_generated_data = malloc(DATA_TO_SEND);
if (random_generated_data == NULL) {
fprintf(stderr, "failed to allocate memory\n");
Expand Down
Loading