diff --git a/src/handle_packets.c b/src/handle_packets.c index bde3108..9771c65 100644 --- a/src/handle_packets.c +++ b/src/handle_packets.c @@ -54,6 +54,7 @@ static inline struct PacketQueueNode* construct_node(const uint32_t data_read, v node->data = data; node->data_read = data_read; node->sender_address.s_addr = sender_address; + node->next = NULL; return node; @@ -82,6 +83,7 @@ static inline void swiftnet_handle_packets(const uint16_t source_port, pthread_t struct ip *ip_header = (struct ip *)(packet_buffer + sizeof(struct ether_header)); sender_address = ip_header->ip_src.s_addr; + } else { allocator_free(&packet_buffer_memory_allocator, packet_buffer); return; diff --git a/src/process_packets.c b/src/process_packets.c index 3ebca3c..265ac43 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -113,15 +113,14 @@ static inline bool check_packet_already_completed(const uint16_t packet_id, stru return false; } -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) { +static inline struct SwiftNetPendingMessage* const get_pending_message(struct SwiftNetVector* const pending_messages_vector, const enum ConnectionType connection_type, const uint16_t packet_id) { vector_lock(pending_messages_vector); for(uint32_t i = 0; i < pending_messages_vector->size; i++) { struct SwiftNetPendingMessage* const current_pending_message = vector_get((struct SwiftNetVector*)pending_messages_vector, i); - 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)) { + if(current_pending_message->packet_id == packet_id) { vector_unlock((struct SwiftNetVector*)pending_messages_vector); - return current_pending_message; } } @@ -160,7 +159,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co #ifdef SWIFT_NET_REQUESTS -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) { +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) { bool is_valid_response = false; vector_lock(&requests_sent); @@ -173,12 +172,6 @@ static inline void handle_request_response(const uint16_t packet_id, const struc } if (current_request_sent->packet_id == packet_id) { - if (!loopback) { - if (current_request_sent->address.s_addr != sender.s_addr) { - continue; - } - } - atomic_store_explicit(¤t_request_sent->packet_data, packet_data, memory_order_release); vector_remove(&requests_sent, i); @@ -237,7 +230,7 @@ static inline void chunk_received(uint8_t* const chunks_received, const uint32_t chunks_received[byte] |= 1 << bit; } -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) { +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) { struct SwiftNetPendingMessage* const new_pending_message = allocator_allocate(pending_messages_memory_allocator); uint8_t* const allocated_memory = malloc(packet_info->packet_length); @@ -254,10 +247,6 @@ static inline struct SwiftNetPendingMessage* const create_new_pending_message(st new_pending_message->packet_id = packet_id; - if(connection_type == CONNECTION_TYPE_SERVER) { - new_pending_message->sender_address.s_addr = sender_address.s_addr; - } - vector_lock(pending_messages); vector_push((struct SwiftNetVector*)pending_messages, new_pending_message); @@ -399,7 +388,7 @@ static inline void swiftnet_process_packets( switch(packet_info.packet_type) { case REQUEST_INFORMATION: { - const struct ip send_server_info_ip_header = construct_ip_header(node->sender_address, PACKET_HEADER_SIZE, rand()); + const struct ip send_server_info_ip_header = construct_ip_header(ip_header.ip_src, PACKET_HEADER_SIZE, rand()); const struct SwiftNetPacketInfo packet_info_new = construct_packet_info( sizeof(struct SwiftNetServerInformation), @@ -432,11 +421,11 @@ static inline void swiftnet_process_packets( { const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit); - struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_src, ip_header.ip_id); + struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id); if(pending_message == NULL) { const bool packet_already_completed = check_packet_already_completed(ip_header.ip_id, packets_completed_history); if(likely(packet_already_completed == true)) { - const struct ip send_packet_ip_header = construct_ip_header(node->sender_address, PACKET_HEADER_SIZE, ip_header.ip_id); + const struct ip send_packet_ip_header = construct_ip_header(ip_header.ip_src, PACKET_HEADER_SIZE, ip_header.ip_id); struct SwiftNetPacketInfo send_packet_info = construct_packet_info( 0x00, @@ -465,7 +454,7 @@ static inline void swiftnet_process_packets( goto next_packet; } - struct ip send_lost_packets_ip_header = construct_ip_header(node->sender_address, 0, ip_header.ip_id); + struct ip send_lost_packets_ip_header = construct_ip_header(ip_header.ip_src, 0, ip_header.ip_id); struct SwiftNetPacketInfo packet_info_new = construct_packet_info( 0, @@ -553,7 +542,7 @@ static inline void swiftnet_process_packets( } struct SwiftNetClientAddrData sender = { - .sender_address.s_addr = loopback == true ? inet_addr("127.0.0.1") : node->sender_address.s_addr, + .maximum_transmission_unit = packet_info.maximum_transmission_unit, .port = packet_info.port_info.source_port, }; @@ -565,12 +554,12 @@ static inline void swiftnet_process_packets( const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit); const uint32_t chunk_data_size = mtu - PACKET_HEADER_SIZE; - struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, node->sender_address, ip_header.ip_id); + struct SwiftNetPendingMessage* const pending_message = get_pending_message(pending_messages, connection_type, ip_header.ip_id); if(pending_message == NULL) { if(packet_info.packet_length > chunk_data_size) { // Split packet into chunks - 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); + struct SwiftNetPendingMessage* const new_pending_message = create_new_pending_message(pending_messages, pending_messages_memory_allocator, &packet_info, connection_type, ip_header.ip_id); new_pending_message->chunks_received_number++; @@ -601,7 +590,7 @@ static inline void swiftnet_process_packets( #ifdef SWIFT_NET_REQUESTS if (packet_info.packet_type == RESPONSE) { - handle_request_response(ip_header.ip_id, sender.sender_address, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); + handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); } else { pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id); } @@ -624,7 +613,7 @@ static inline void swiftnet_process_packets( #ifdef SWIFT_NET_REQUESTS if (packet_info.packet_type == RESPONSE) { - 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); + handle_request_response(ip_header.ip_id, NULL, new_packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); } else { pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id); } @@ -685,7 +674,7 @@ static inline void swiftnet_process_packets( #ifdef SWIFT_NET_REQUESTS if (packet_info.packet_type == RESPONSE) { - handle_request_response(ip_header.ip_id, sender.sender_address, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); + handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); } else { pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id); } @@ -710,7 +699,7 @@ static inline void swiftnet_process_packets( #ifdef SWIFT_NET_REQUESTS if (packet_info.packet_type == RESPONSE) { - 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); + handle_request_response(ip_header.ip_id, pending_message, packet_data, pending_messages, pending_messages_memory_allocator, connection_type, loopback); } else { pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id); } diff --git a/src/swift_net.h b/src/swift_net.h index 94b6bcf..fe9aaa2 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -71,7 +71,7 @@ struct SwiftNetPortInfo { }; struct SwiftNetClientAddrData { - struct in_addr sender_address; + struct in_addr sender_address; uint32_t maximum_transmission_unit; uint16_t port; uint8_t mac_address[6]; @@ -99,7 +99,6 @@ struct SwiftNetPendingMessage { uint8_t* packet_data_start; struct SwiftNetPacketInfo packet_info; uint16_t packet_id; - struct in_addr sender_address; uint8_t* chunks_received; uint32_t chunks_received_length; uint32_t chunks_received_number; @@ -147,7 +146,7 @@ struct PacketQueueNode { struct PacketQueueNode* next; uint8_t* data; uint32_t data_read; - struct in_addr sender_address; + struct in_addr sender_address; }; struct PacketQueue {