@@ -71,7 +71,7 @@ static inline const uint32_t return_lost_chunk_indexes(const uint8_t* const chun
7171 return offset ;
7272}
7373
74- static inline void packet_completed (uint16_t packet_id , struct SwiftNetHashMap * const packets_completed_history , struct SwiftNetMemoryAllocator * const packets_completed_history_memory_allocator ) {
74+ static inline void packet_completed (const uint16_t packet_id , const uint16_t source_port , struct SwiftNetHashMap * const packets_completed_history , struct SwiftNetMemoryAllocator * const packets_completed_history_memory_allocator ) {
7575 struct SwiftNetPacketCompleted * const new_packet_completed = allocator_allocate (packets_completed_history_memory_allocator );
7676 new_packet_completed -> packet_id = packet_id ;
7777 new_packet_completed -> marked_cleanup = false;
@@ -81,23 +81,39 @@ static inline void packet_completed(uint16_t packet_id, struct SwiftNetHashMap*
8181
8282 LOCK_ATOMIC_DATA_TYPE (& packets_completed_history -> atomic_lock );
8383
84- hashmap_insert (heap_key_data_location , sizeof (uint16_t ), new_packet_completed , packets_completed_history );
84+ struct PacketCompletedKey * const key = allocator_allocate (& packet_completed_key_allocator );
85+ * key = (struct PacketCompletedKey ){
86+ .source_port = source_port ,
87+ .packet_id = packet_id
88+ };
89+
90+ hashmap_insert (key , sizeof (struct PacketCompletedKey ), new_packet_completed , packets_completed_history );
8591
8692 UNLOCK_ATOMIC_DATA_TYPE (& packets_completed_history -> atomic_lock );
8793
8894 return ;
8995}
9096
91- static inline bool check_packet_already_completed (uint16_t packet_id , struct SwiftNetHashMap * const packets_completed_history ) {
92- const struct SwiftNetPacketCompleted * const item = hashmap_get (& packet_id , sizeof (packet_id ), packets_completed_history );
97+ static inline bool check_packet_already_completed (const uint16_t packet_id , const uint16_t source_port , struct SwiftNetHashMap * const packets_completed_history ) {
98+ const struct PacketCompletedKey key = {
99+ .packet_id = packet_id ,
100+ .source_port = source_port
101+ };
102+
103+ const struct SwiftNetPacketCompleted * const item = hashmap_get (& key , sizeof (key ), packets_completed_history );
93104
94105 return item != NULL ;
95106}
96107
97- static inline struct SwiftNetPendingMessage * const get_pending_message (struct SwiftNetHashMap * const pending_messages , const enum ConnectionType connection_type , uint16_t packet_id ) {
108+ static inline struct SwiftNetPendingMessage * const get_pending_message (struct SwiftNetHashMap * const pending_messages , const enum ConnectionType connection_type , const uint16_t packet_id , const uint16_t source_port ) {
98109 LOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
99110
100- struct SwiftNetPendingMessage * const pending_message = hashmap_get (& packet_id , sizeof (uint16_t ), pending_messages );
111+ const struct PendingMessagesKey key = {
112+ .source_port = source_port ,
113+ .packet_id = packet_id
114+ };
115+
116+ struct SwiftNetPendingMessage * const pending_message = hashmap_get (& key , sizeof (key ), pending_messages );
101117
102118 UNLOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
103119
@@ -130,7 +146,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co
130146
131147#ifdef SWIFT_NET_REQUESTS
132148
133- static inline void handle_request_response (uint16_t packet_id , struct SwiftNetPendingMessage * const pending_message , void * const packet_data , struct SwiftNetHashMap * const pending_messages , struct SwiftNetMemoryAllocator * const pending_message_memory_allocator , const enum ConnectionType connection_type , const bool loopback ) {
149+ static inline void handle_request_response (uint16_t packet_id , struct SwiftNetPendingMessage * const pending_message , void * const packet_data , struct SwiftNetHashMap * const pending_messages , struct SwiftNetMemoryAllocator * const pending_message_memory_allocator , const enum ConnectionType connection_type , const bool loopback , const uint16_t source_port ) {
134150 bool is_valid_response = false;
135151
136152 LOCK_ATOMIC_DATA_TYPE (& requests_sent .atomic_lock );
@@ -153,7 +169,12 @@ static inline void handle_request_response(uint16_t packet_id, struct SwiftNetPe
153169 if (pending_message != NULL ) {
154170 LOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
155171
156- hashmap_remove (& packet_id , sizeof (uint16_t ), pending_messages );
172+ struct PendingMessagesKey key = {
173+ .source_port = source_port ,
174+ .packet_id = packet_id
175+ };
176+
177+ hashmap_remove (& key , sizeof (key ), pending_messages );
157178
158179 UNLOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
159180 }
@@ -196,7 +217,7 @@ static inline void chunk_received(uint8_t* const chunks_received, const uint32_t
196217 chunks_received [byte ] |= 1 << bit ;
197218}
198219
199- static inline struct SwiftNetPendingMessage * const create_new_pending_message (struct SwiftNetHashMap * 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 ) {
220+ static inline struct SwiftNetPendingMessage * const create_new_pending_message (struct SwiftNetHashMap * 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 , const uint16_t source_port ) {
200221 struct SwiftNetPendingMessage * const new_pending_message = allocator_allocate (pending_messages_memory_allocator );
201222
202223 uint8_t * const allocated_memory = malloc (packet_info -> packet_length );
@@ -212,13 +233,17 @@ static inline struct SwiftNetPendingMessage* const create_new_pending_message(st
212233 new_pending_message -> chunks_received = calloc (chunks_received_byte_size , 1 );
213234
214235 new_pending_message -> packet_id = packet_id ;
236+ new_pending_message -> source_port = source_port ;
215237
216238 LOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
217239
218- uint16_t * const packet_id_mem = allocator_allocate (& uint16_memory_allocator );
219- * packet_id_mem = packet_id ;
240+ struct PendingMessagesKey * const key = allocator_allocate (& pending_message_key_allocator );
241+ * key = (struct PendingMessagesKey ){
242+ .source_port = source_port ,
243+ .packet_id = packet_id
244+ };
220245
221- hashmap_insert (packet_id_mem , sizeof (uint16_t ), new_pending_message , pending_messages );
246+ hashmap_insert (key , sizeof (struct PendingMessagesKey ), new_pending_message , pending_messages );
222247
223248 UNLOCK_ATOMIC_DATA_TYPE (& pending_messages -> atomic_lock );
224249
@@ -391,9 +416,9 @@ static inline void swiftnet_process_packets(
391416 {
392417 const uint32_t mtu = MIN (packet_info .maximum_transmission_unit , maximum_transmission_unit );
393418
394- struct SwiftNetPendingMessage * const pending_message = get_pending_message (pending_messages , connection_type , ip_header .ip_id );
419+ struct SwiftNetPendingMessage * const pending_message = get_pending_message (pending_messages , connection_type , ip_header .ip_id , packet_info . port_info . source_port );
395420 if (pending_message == NULL ) {
396- const bool packet_already_completed = check_packet_already_completed (ip_header .ip_id , packets_completed_history );
421+ const bool packet_already_completed = check_packet_already_completed (ip_header .ip_id , packet_info . port_info . source_port , packets_completed_history );
397422 if (likely (packet_already_completed == true)) {
398423 const struct ip send_packet_ip_header = construct_ip_header (ip_header .ip_src , PACKET_HEADER_SIZE , ip_header .ip_id );
399424
@@ -506,7 +531,7 @@ static inline void swiftnet_process_packets(
506531 break ;
507532 }
508533
509- if (check_packet_already_completed (ip_header .ip_id , packets_completed_history )) {
534+ if (check_packet_already_completed (ip_header .ip_id , packet_info . port_info . source_port , packets_completed_history )) {
510535 allocator_free (& packet_buffer_memory_allocator , packet_buffer );
511536 goto next_packet ;
512537 }
@@ -523,12 +548,12 @@ static inline void swiftnet_process_packets(
523548 const uint32_t mtu = MIN (packet_info .maximum_transmission_unit , maximum_transmission_unit );
524549 const uint32_t chunk_data_size = mtu - PACKET_HEADER_SIZE ;
525550
526- struct SwiftNetPendingMessage * const pending_message = get_pending_message (pending_messages , connection_type , ip_header .ip_id );
551+ struct SwiftNetPendingMessage * const pending_message = get_pending_message (pending_messages , connection_type , ip_header .ip_id , packet_info . port_info . source_port );
527552
528553 if (pending_message == NULL ) {
529554 if (packet_info .packet_length > chunk_data_size ) {
530555 // Split packet into chunks
531- struct SwiftNetPendingMessage * const new_pending_message = create_new_pending_message (pending_messages , pending_messages_memory_allocator , & packet_info , connection_type , ip_header .ip_id );
556+ struct SwiftNetPendingMessage * const new_pending_message = create_new_pending_message (pending_messages , pending_messages_memory_allocator , & packet_info , connection_type , ip_header .ip_id , packet_info . port_info . source_port );
532557
533558 new_pending_message -> chunks_received_number ++ ;
534559
@@ -540,7 +565,7 @@ static inline void swiftnet_process_packets(
540565
541566 goto next_packet ;
542567 } else {
543- packet_completed (ip_header .ip_id , packets_completed_history , packets_completed_history_memory_allocator );
568+ packet_completed (ip_header .ip_id , packet_info . port_info . source_port , packets_completed_history , packets_completed_history_memory_allocator );
544569
545570 if (connection_type == CONNECTION_TYPE_SERVER ) {
546571 struct SwiftNetServerPacketData * const new_packet_data = allocator_allocate (& server_packet_data_memory_allocator ) ;
@@ -559,7 +584,7 @@ static inline void swiftnet_process_packets(
559584
560585 #ifdef SWIFT_NET_REQUESTS
561586 if (packet_info .packet_type == RESPONSE ) {
562- handle_request_response (ip_header .ip_id , NULL , new_packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback );
587+ handle_request_response (ip_header .ip_id , NULL , new_packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback , packet_info . port_info . source_port );
563588 } else {
564589 pass_callback_execution (new_packet_data , packet_callback_queue , NULL , ip_header .ip_id , execute_callback_mtx , execute_callback_cond );
565590 }
@@ -582,7 +607,7 @@ static inline void swiftnet_process_packets(
582607
583608 #ifdef SWIFT_NET_REQUESTS
584609 if (packet_info .packet_type == RESPONSE ) {
585- handle_request_response (ip_header .ip_id , NULL , new_packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback );
610+ handle_request_response (ip_header .ip_id , NULL , new_packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback , packet_info . port_info . source_port );
586611 } else {
587612 pass_callback_execution (new_packet_data , packet_callback_queue , NULL , ip_header .ip_id , execute_callback_mtx , execute_callback_cond );
588613 }
@@ -622,7 +647,7 @@ static inline void swiftnet_process_packets(
622647 }
623648 #endif
624649
625- packet_completed (ip_header .ip_id , packets_completed_history , packets_completed_history_memory_allocator );
650+ packet_completed (ip_header .ip_id , packet_info . port_info . source_port , packets_completed_history , packets_completed_history_memory_allocator );
626651
627652 if (connection_type == CONNECTION_TYPE_SERVER ) {
628653 uint8_t * const ptr = pending_message -> packet_data_start ;
@@ -643,7 +668,7 @@ static inline void swiftnet_process_packets(
643668
644669 #ifdef SWIFT_NET_REQUESTS
645670 if (packet_info .packet_type == RESPONSE ) {
646- handle_request_response (ip_header .ip_id , pending_message , packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback );
671+ handle_request_response (ip_header .ip_id , pending_message , packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback , packet_info . port_info . source_port );
647672 } else {
648673 pass_callback_execution (packet_data , packet_callback_queue , pending_message , ip_header .ip_id , execute_callback_mtx , execute_callback_cond );
649674 }
@@ -668,7 +693,7 @@ static inline void swiftnet_process_packets(
668693
669694 #ifdef SWIFT_NET_REQUESTS
670695 if (packet_info .packet_type == RESPONSE ) {
671- handle_request_response (ip_header .ip_id , pending_message , packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback );
696+ handle_request_response (ip_header .ip_id , pending_message , packet_data , pending_messages , pending_messages_memory_allocator , connection_type , loopback , packet_info . port_info . source_port );
672697 } else {
673698 pass_callback_execution (packet_data , packet_callback_queue , pending_message , ip_header .ip_id , execute_callback_mtx , execute_callback_cond );
674699 }
0 commit comments