Skip to content

Commit 0351199

Browse files
committed
added some basic debugging
1 parent 6fac543 commit 0351199

11 files changed

+45
-2
lines changed

build/output/libswift_net.a

6.88 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/internal/internal.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <_string.h>
34
#include <netinet/in.h>
45
#include <stdatomic.h>
56
#include <stdlib.h>
@@ -113,15 +114,26 @@ extern void* execute_packet_callback_server(void* void_server);
113114
SwiftNetDebug(
114115
extern SwiftNetDebugger debugger;
115116

116-
static inline bool check_debug_flag(uint32_t flag) {
117+
static inline bool check_debug_flag(SwiftNetDebugFlags flag) {
117118
return (debugger.flags & flag) != 0;
118119
}
119120

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

124-
vprintf(message, args);
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);
125137

126138
va_end(args);
127139
}

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));
976 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.

tests/src/test_basic_client_server_communication.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ void server_message_handler(uint8_t* data, SwiftNetPacketServerMetadata* restric
3030
}
3131

3232
int main() {
33+
swiftnet_add_debug_flags(DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING);
34+
3335
swiftnet_initialize();
3436

3537
server = swiftnet_create_server(8080);

tests/src/test_client_sending_large_packet_to_server.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void server_message_handler(uint8_t* data, SwiftNetPacketServerMetadata* restric
3131
}
3232

3333
int main() {
34+
swiftnet_add_debug_flags(DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING);
35+
3436
random_generated_data = malloc(DATA_TO_SEND);
3537
if (random_generated_data == NULL) {
3638
fprintf(stderr, "failed to allocate memory\n");

0 commit comments

Comments
 (0)