Skip to content

Commit d2fa5ba

Browse files
Refactor: Remove sender_address from SwiftNet send_packet API and update header (#163)
* Refactor: Remove sender_address from SwiftNet send_packet API and update headers * Resolved Issues * Resolved issues * Forgot to init sender address --------- Co-authored-by: Morcules <morcules@protonmail.com>
1 parent 22e0496 commit d2fa5ba

3 files changed

Lines changed: 19 additions & 29 deletions

File tree

src/handle_packets.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ static inline struct PacketQueueNode* construct_node(const uint32_t data_read, v
5454
node->data = data;
5555
node->data_read = data_read;
5656
node->sender_address.s_addr = sender_address;
57+
5758
node->next = NULL;
5859

5960
return node;
@@ -82,6 +83,7 @@ static inline void swiftnet_handle_packets(const uint16_t source_port, pthread_t
8283
struct ip *ip_header = (struct ip *)(packet_buffer + sizeof(struct ether_header));
8384

8485
sender_address = ip_header->ip_src.s_addr;
86+
8587
} else {
8688
allocator_free(&packet_buffer_memory_allocator, packet_buffer);
8789
return;

src/process_packets.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,14 @@ static inline bool check_packet_already_completed(const uint16_t packet_id, stru
113113
return false;
114114
}
115115

116-
static inline struct SwiftNetPendingMessage* const get_pending_message(struct SwiftNetVector* const pending_messages_vector, const enum ConnectionType connection_type, const struct in_addr sender_address, const uint16_t packet_id) {
116+
static inline struct SwiftNetPendingMessage* const get_pending_message(struct SwiftNetVector* const pending_messages_vector, const enum ConnectionType connection_type, const uint16_t packet_id) {
117117
vector_lock(pending_messages_vector);
118118

119119
for(uint32_t i = 0; i < pending_messages_vector->size; i++) {
120120
struct SwiftNetPendingMessage* const current_pending_message = vector_get((struct SwiftNetVector*)pending_messages_vector, i);
121121

122-
if((connection_type == CONNECTION_TYPE_CLIENT && current_pending_message->packet_id == packet_id) || (connection_type == CONNECTION_TYPE_SERVER && current_pending_message->sender_address.s_addr == sender_address.s_addr && current_pending_message->packet_id == packet_id)) {
122+
if(current_pending_message->packet_id == packet_id) {
123123
vector_unlock((struct SwiftNetVector*)pending_messages_vector);
124-
125124
return current_pending_message;
126125
}
127126
}
@@ -160,7 +159,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co
160159

161160
#ifdef SWIFT_NET_REQUESTS
162161

163-
static inline void handle_request_response(const uint16_t packet_id, const struct in_addr sender, struct SwiftNetPendingMessage* const pending_message, void* const packet_data, struct SwiftNetVector* const pending_messages, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, const enum ConnectionType connection_type, const bool loopback) {
162+
static inline void handle_request_response(const uint16_t packet_id, struct SwiftNetPendingMessage* const pending_message, void* const packet_data, struct SwiftNetVector* const pending_messages, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, const enum ConnectionType connection_type, const bool loopback) {
164163
bool is_valid_response = false;
165164

166165
vector_lock(&requests_sent);
@@ -173,12 +172,6 @@ static inline void handle_request_response(const uint16_t packet_id, const struc
173172
}
174173

175174
if (current_request_sent->packet_id == packet_id) {
176-
if (!loopback) {
177-
if (current_request_sent->address.s_addr != sender.s_addr) {
178-
continue;
179-
}
180-
}
181-
182175
atomic_store_explicit(&current_request_sent->packet_data, packet_data, memory_order_release);
183176

184177
vector_remove(&requests_sent, i);
@@ -237,7 +230,7 @@ static inline void chunk_received(uint8_t* const chunks_received, const uint32_t
237230
chunks_received[byte] |= 1 << bit;
238231
}
239232

240-
static inline struct SwiftNetPendingMessage* const create_new_pending_message(struct SwiftNetVector* const pending_messages, struct SwiftNetMemoryAllocator* const pending_messages_memory_allocator, const struct SwiftNetPacketInfo* const packet_info, const enum ConnectionType connection_type, const struct in_addr sender_address, const uint16_t packet_id) {
233+
static inline struct SwiftNetPendingMessage* const create_new_pending_message(struct SwiftNetVector* const pending_messages, struct SwiftNetMemoryAllocator* const pending_messages_memory_allocator, const struct SwiftNetPacketInfo* const packet_info, const enum ConnectionType connection_type, const uint16_t packet_id) {
241234
struct SwiftNetPendingMessage* const new_pending_message = allocator_allocate(pending_messages_memory_allocator);
242235

243236
uint8_t* const allocated_memory = malloc(packet_info->packet_length);
@@ -254,10 +247,6 @@ static inline struct SwiftNetPendingMessage* const create_new_pending_message(st
254247

255248
new_pending_message->packet_id = packet_id;
256249

257-
if(connection_type == CONNECTION_TYPE_SERVER) {
258-
new_pending_message->sender_address.s_addr = sender_address.s_addr;
259-
}
260-
261250
vector_lock(pending_messages);
262251

263252
vector_push((struct SwiftNetVector*)pending_messages, new_pending_message);
@@ -399,7 +388,7 @@ static inline void swiftnet_process_packets(
399388
switch(packet_info.packet_type) {
400389
case REQUEST_INFORMATION:
401390
{
402-
const struct ip send_server_info_ip_header = construct_ip_header(node->sender_address, PACKET_HEADER_SIZE, rand());
391+
const struct ip send_server_info_ip_header = construct_ip_header(ip_header.ip_src, PACKET_HEADER_SIZE, rand());
403392

404393
const struct SwiftNetPacketInfo packet_info_new = construct_packet_info(
405394
sizeof(struct SwiftNetServerInformation),
@@ -432,11 +421,11 @@ static inline void swiftnet_process_packets(
432421
{
433422
const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit);
434423

435-
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_src, ip_header.ip_id);
424+
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id);
436425
if(pending_message == NULL) {
437426
const bool packet_already_completed = check_packet_already_completed(ip_header.ip_id, packets_completed_history);
438427
if(likely(packet_already_completed == true)) {
439-
const struct ip send_packet_ip_header = construct_ip_header(node->sender_address, PACKET_HEADER_SIZE, ip_header.ip_id);
428+
const struct ip send_packet_ip_header = construct_ip_header(ip_header.ip_src, PACKET_HEADER_SIZE, ip_header.ip_id);
440429

441430
struct SwiftNetPacketInfo send_packet_info = construct_packet_info(
442431
0x00,
@@ -465,7 +454,7 @@ static inline void swiftnet_process_packets(
465454
goto next_packet;
466455
}
467456

468-
struct ip send_lost_packets_ip_header = construct_ip_header(node->sender_address, 0, ip_header.ip_id);
457+
struct ip send_lost_packets_ip_header = construct_ip_header(ip_header.ip_src, 0, ip_header.ip_id);
469458

470459
struct SwiftNetPacketInfo packet_info_new = construct_packet_info(
471460
0,
@@ -553,7 +542,7 @@ static inline void swiftnet_process_packets(
553542
}
554543

555544
struct SwiftNetClientAddrData sender = {
556-
.sender_address.s_addr = loopback == true ? inet_addr("127.0.0.1") : node->sender_address.s_addr,
545+
557546
.maximum_transmission_unit = packet_info.maximum_transmission_unit,
558547
.port = packet_info.port_info.source_port,
559548
};
@@ -565,12 +554,12 @@ static inline void swiftnet_process_packets(
565554
const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit);
566555
const uint32_t chunk_data_size = mtu - PACKET_HEADER_SIZE;
567556

568-
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, node->sender_address, ip_header.ip_id);
557+
struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id);
569558

570559
if(pending_message == NULL) {
571560
if(packet_info.packet_length > chunk_data_size) {
572561
// Split packet into chunks
573-
struct SwiftNetPendingMessage* const new_pending_message = create_new_pending_message(pending_messages, pending_messages_memory_allocator, &packet_info, connection_type, node->sender_address, ip_header.ip_id);
562+
struct SwiftNetPendingMessage* const new_pending_message = create_new_pending_message(pending_messages, pending_messages_memory_allocator, &packet_info, connection_type, ip_header.ip_id);
574563

575564
new_pending_message->chunks_received_number++;
576565

@@ -601,7 +590,7 @@ static inline void swiftnet_process_packets(
601590

602591
#ifdef SWIFT_NET_REQUESTS
603592
if (packet_info.packet_type == RESPONSE) {
604-
handle_request_response(ip_header.ip_id, sender.sender_address, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
593+
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
605594
} else {
606595
pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id);
607596
}
@@ -624,7 +613,7 @@ static inline void swiftnet_process_packets(
624613

625614
#ifdef SWIFT_NET_REQUESTS
626615
if (packet_info.packet_type == RESPONSE) {
627-
handle_request_response(ip_header.ip_id, ((struct SwiftNetClientConnection*)connection)->server_addr, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
616+
handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
628617
} else {
629618
pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id);
630619
}
@@ -685,7 +674,7 @@ static inline void swiftnet_process_packets(
685674

686675
#ifdef SWIFT_NET_REQUESTS
687676
if (packet_info.packet_type == RESPONSE) {
688-
handle_request_response(ip_header.ip_id, sender.sender_address, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
677+
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
689678
} else {
690679
pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id);
691680
}
@@ -710,7 +699,7 @@ static inline void swiftnet_process_packets(
710699

711700
#ifdef SWIFT_NET_REQUESTS
712701
if (packet_info.packet_type == RESPONSE) {
713-
handle_request_response(ip_header.ip_id, ((struct SwiftNetClientConnection*)connection)->server_addr, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
702+
handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback);
714703
} else {
715704
pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id);
716705
}

src/swift_net.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct SwiftNetPortInfo {
7171
};
7272

7373
struct SwiftNetClientAddrData {
74-
struct in_addr sender_address;
74+
struct in_addr sender_address;
7575
uint32_t maximum_transmission_unit;
7676
uint16_t port;
7777
uint8_t mac_address[6];
@@ -99,7 +99,6 @@ struct SwiftNetPendingMessage {
9999
uint8_t* packet_data_start;
100100
struct SwiftNetPacketInfo packet_info;
101101
uint16_t packet_id;
102-
struct in_addr sender_address;
103102
uint8_t* chunks_received;
104103
uint32_t chunks_received_length;
105104
uint32_t chunks_received_number;
@@ -147,7 +146,7 @@ struct PacketQueueNode {
147146
struct PacketQueueNode* next;
148147
uint8_t* data;
149148
uint32_t data_read;
150-
struct in_addr sender_address;
149+
struct in_addr sender_address;
151150
};
152151

153152
struct PacketQueue {

0 commit comments

Comments
 (0)