Skip to content

Commit 8785732

Browse files
Merge pull request #73 from deadlightreal/issue-72
Issue 72
2 parents d334640 + d5e12cc commit 8785732

16 files changed

+131
-5
lines changed

build/output/libswift_net.a

12.3 KB
Binary file not shown.

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ set(SOURCE_FILES
1616
process_packets.c
1717
execute_packet_callback.c
1818
create_packet_buffer.c
19+
add_debug_flags.c
1920
destroy_packet_buffer.c
2021
internal/get_mtu.c
2122
internal/get_default_interface.c

src/add_debug_flags.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "swift_net.h"
2+
#include "internal/internal.h"
3+
4+
void swiftnet_add_debug_flags(const uint32_t flags) {
5+
debugger.flags |= flags;
6+
}

src/initialize_client_socket.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ void* request_server_information(void* request_server_information_args_void) {
3232
return NULL;
3333
}
3434

35+
SwiftNetDebug(
36+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
37+
send_debug_message("Requested server information: {\"server_ip_address\": \"%s\"}\n", inet_ntoa(request_server_information_args->server_addr.sin_addr));
38+
}
39+
)
40+
3541
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);
3642

3743
usleep(1000000);
@@ -125,16 +131,33 @@ SwiftNetClientConnection* swiftnet_create_client(const char* const restrict ip_a
125131
while(1) {
126132
const int bytes_received = recvfrom(empty_connection->sockfd, server_information_buffer, sizeof(server_information_buffer), 0x00, NULL, NULL);
127133
if(bytes_received != PACKET_HEADER_SIZE + sizeof(SwiftNetServerInformation)) {
134+
SwiftNetDebug(
135+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
136+
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));
137+
}
138+
)
128139
continue;
129140
}
130141

131-
const SwiftNetPacketInfo* const restrict packetInfo = (SwiftNetPacketInfo *)&server_information_buffer[sizeof(struct ip)];
142+
const struct ip* const restrict ip_header = (struct ip*)&server_information_buffer;
143+
144+
const SwiftNetPacketInfo* const restrict packet_info = (SwiftNetPacketInfo *)&server_information_buffer[sizeof(struct ip)];
132145

133-
if(packetInfo->port_info.destination_port != empty_connection->port_info.source_port || packetInfo->port_info.source_port != empty_connection->port_info.destination_port) {
146+
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) {
147+
SwiftNetDebug(
148+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
149+
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);
150+
}
151+
)
134152
continue;
135153
}
136154

137-
if(packetInfo->packet_type != PACKET_TYPE_REQUEST_INFORMATION) {
155+
if(packet_info->packet_type != PACKET_TYPE_REQUEST_INFORMATION) {
156+
SwiftNetDebug(
157+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
158+
send_debug_message("Invalid packet type: {\"packet_type\": %d}\n", packet_info->packet_type);
159+
}
160+
)
138161
continue;
139162
}
140163

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

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

179+
SwiftNetDebug(
180+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
181+
send_debug_message("Successfully initialized client\n");
182+
}
183+
)
184+
156185
return empty_connection;
157186
}

src/initialize_server_socket.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ SwiftNetServer* swiftnet_create_server(const uint16_t port) {
4747

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

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

71+
SwiftNetDebug(
72+
if (check_debug_flag(DEBUG_INITIALIZATION)) {
73+
send_debug_message("Successfully initialized server\n");
74+
}
75+
)
76+
7277
return empty_server;
7378
}

src/initialize_swift_net.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#include "internal/internal.h"
88
#include <unistd.h>
99

10+
SwiftNetDebug(
11+
SwiftNetDebugger debugger = {.flags = 0};
12+
)
13+
1014
uint32_t maximum_transmission_unit = 0x00;
1115

1216
void swiftnet_initialize() {

src/internal/internal.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#pragma once
22

3+
#include <string.h>
34
#include <netinet/in.h>
45
#include <stdatomic.h>
56
#include <stdlib.h>
7+
#include "../swift_net.h"
68
#include <sys/socket.h>
9+
#include <stdarg.h>
10+
#include <stdio.h>
711

812
#define REQUEST_LOST_PACKETS_RETURN_UPDATED_BIT_ARRAY 0x00
913
#define REQUEST_LOST_PACKETS_RETURN_COMPLETED_PACKET 0x01
@@ -107,6 +111,34 @@ extern void* swiftnet_client_process_packets(void* restrict const void_client);
107111
extern void* execute_packet_callback_client(void* void_client);
108112
extern void* execute_packet_callback_server(void* void_server);
109113

114+
SwiftNetDebug(
115+
extern SwiftNetDebugger debugger;
116+
117+
static inline bool check_debug_flag(SwiftNetDebugFlags flag) {
118+
return (debugger.flags & flag) != 0;
119+
}
120+
121+
static inline void send_debug_message(const char* message, ...) {
122+
va_list args;
123+
va_start(args, message);
124+
125+
char* prefix = "[DEBUG] ";
126+
127+
const uint32_t prefix_length = strlen(prefix);
128+
const uint32_t message_length = strlen(message);
129+
130+
char full_message[prefix_length + message_length + 1];
131+
132+
memcpy(full_message, prefix, prefix_length);
133+
memcpy(full_message + prefix_length, message, message_length);
134+
full_message[prefix_length + message_length] = '\0';
135+
136+
vprintf(full_message, args);
137+
138+
va_end(args);
139+
}
140+
);
141+
110142
typedef enum {
111143
CONNECTION_TYPE_SERVER,
112144
CONNECTION_TYPE_CLIENT

src/process_packets.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "internal/internal.h"
22
#include "swift_net.h"
3+
#include <arpa/inet.h>
34
#include <stdatomic.h>
45
#include <stdint.h>
56
#include <stddef.h>
@@ -269,9 +270,20 @@ static inline void swiftnet_process_packets(
269270
memset(&packet_buffer[sizeof(struct ip) + offsetof(SwiftNetPacketInfo, checksum)], 0x00, SIZEOF_FIELD(SwiftNetPacketInfo, checksum));
270271

271272
if(packet_corrupted(checksum_received, packet_info.chunk_size + sizeof(SwiftNetPacketInfo), &packet_buffer[sizeof(struct ip)]) == true) {
273+
SwiftNetDebug(
274+
if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) {
275+
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);
276+
}
277+
)
272278
goto next_packet;
273279
}
274280

281+
SwiftNetDebug(
282+
if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) {
283+
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);
284+
}
285+
)
286+
275287
switch(packet_info.packet_type) {
276288
case PACKET_TYPE_REQUEST_INFORMATION:
277289
{

src/send_packet.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ static inline void swiftnet_send_packet(
164164
.maximum_transmission_unit = maximum_transmission_unit
165165
};
166166

167+
SwiftNetDebug(
168+
if (check_debug_flag(DEBUG_PACKETS_SENDING)) {
169+
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);
170+
}
171+
)
172+
167173
memcpy(packet->packet_buffer_start, &packet_info, sizeof(SwiftNetPacketInfo));
168174

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

195+
SwiftNetDebug(
196+
if (check_debug_flag(DEBUG_PACKETS_SENDING)) {
197+
send_debug_message("Sent chunk: {\"chunk_index\": %d}\n", i);
198+
}
199+
)
200+
189201
memcpy(&buffer[offsetof(SwiftNetPacketInfo, chunk_index)], &i, SIZEOF_FIELD(SwiftNetPacketInfo, chunk_index));
190202

191203
memset(&buffer[offsetof(SwiftNetPacketInfo, checksum)], 0x00, SIZEOF_FIELD(SwiftNetPacketInfo, checksum));

src/swift_net.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,24 @@
3535
#define SwiftNetErrorCheck(code)
3636
#endif
3737

38+
#ifndef SWIFT_NET_DISABLE_DEBUGGING
39+
#define SwiftNetDebug(code) code
40+
#else
41+
#define SwiftNetDebug(code) code
42+
#endif
43+
3844
extern uint32_t maximum_transmission_unit;
3945

46+
typedef enum {
47+
DEBUG_PACKETS_SENDING = 1u << 0,
48+
DEBUG_PACKETS_RECEIVING = 1u << 1,
49+
DEBUG_INITIALIZATION = 1u << 2
50+
} SwiftNetDebugFlags;
51+
52+
typedef struct {
53+
uint32_t flags;
54+
} SwiftNetDebugger;
55+
4056
typedef struct {
4157
uint16_t destination_port;
4258
uint16_t source_port;
@@ -191,6 +207,9 @@ extern SwiftNetPacketBuffer swiftnet_server_create_packet_buffer(const uint32_t
191207
extern SwiftNetPacketBuffer swiftnet_client_create_packet_buffer(const uint32_t buffer_size);
192208
extern void swiftnet_server_destroy_packet_buffer(SwiftNetPacketBuffer* restrict const packet);
193209
extern void swiftnet_client_destroy_packet_buffer(SwiftNetPacketBuffer* restrict const packet);
194-
195210
extern SwiftNetServer* swiftnet_create_server(const uint16_t port);
196211
extern SwiftNetClientConnection* swiftnet_create_client(const char* const restrict ip_address, const uint16_t port);
212+
213+
SwiftNetDebug(
214+
extern void swiftnet_add_debug_flags(const uint32_t flags);
215+
)

0 commit comments

Comments
 (0)