diff --git a/src/execute_packet_callback.c b/src/execute_packet_callback.c index f5ea3b1..bbe216b 100644 --- a/src/execute_packet_callback.c +++ b/src/execute_packet_callback.c @@ -8,9 +8,9 @@ #include static inline void lock_packet_queue(struct PacketCallbackQueue* const packet_queue) { - uint32_t owner_none = PACKET_CALLBACK_QUEUE_OWNER_NONE; - while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, PACKET_CALLBACK_QUEUE_OWNER_EXECUTE_PACKET_CALLBACK, memory_order_acquire, memory_order_relaxed)) { - owner_none = PACKET_CALLBACK_QUEUE_OWNER_NONE; + enum PacketQueueOwner owner_none = NONE; + while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, SOME, memory_order_acquire, memory_order_relaxed)) { + owner_none = NONE; } } @@ -18,7 +18,7 @@ static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struc lock_packet_queue(packet_queue); if(packet_queue->first_node == NULL) { - atomic_store_explicit(&packet_queue->owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); return NULL; } @@ -28,14 +28,14 @@ static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struc packet_queue->first_node = NULL; packet_queue->last_node = NULL; - atomic_store_explicit(&packet_queue->owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); return node_to_process; } packet_queue->first_node = node_to_process->next; - atomic_store_explicit(&packet_queue->owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); return node_to_process; } diff --git a/src/handle_packets.c b/src/handle_packets.c index a4ff8a3..bde3108 100644 --- a/src/handle_packets.c +++ b/src/handle_packets.c @@ -11,14 +11,14 @@ #include static inline void lock_packet_queue(struct PacketQueue* const packet_queue) { - uint32_t owner_none = PACKET_QUEUE_OWNER_NONE; - while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, PACKET_QUEUE_OWNER_HANDLE_PACKETS, memory_order_acquire, memory_order_relaxed)) { - owner_none = PACKET_QUEUE_OWNER_NONE; + enum PacketQueueOwner owner_none = NONE; + while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, SOME, memory_order_acquire, memory_order_relaxed)) { + owner_none = NONE; } } static inline void unlock_packet_queue(struct PacketQueue* const packet_queue) { - atomic_store_explicit(&packet_queue->owner, PACKET_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); } static inline void insert_queue_node(struct PacketQueueNode* const new_node, struct PacketQueue* const packet_queue, const enum ConnectionType contype) { @@ -106,7 +106,7 @@ static void handle_client_init(struct SwiftNetClientConnection* user, const stru if(bytes_received != PACKET_HEADER_SIZE + sizeof(struct SwiftNetServerInformation) + client_connection->prepend_size) { #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { send_debug_message("Invalid packet received from server. Expected server information: {\"bytes_received\": %u, \"expected_bytes\": %u}\n", bytes_received, PACKET_HEADER_SIZE + sizeof(struct SwiftNetServerInformation)); } #endif @@ -125,7 +125,7 @@ static void handle_client_init(struct SwiftNetClientConnection* user, const stru if(packet_info->port_info.destination_port != client_connection->port_info.source_port || packet_info->port_info.source_port != client_connection->port_info.destination_port) { #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { 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, inet_ntoa(ip_header->ip_src)); } #endif @@ -133,9 +133,9 @@ static void handle_client_init(struct SwiftNetClientConnection* user, const stru return; } - if(packet_info->packet_type != PACKET_TYPE_REQUEST_INFORMATION) { + if(packet_info->packet_type != REQUEST_INFORMATION) { #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { send_debug_message("Invalid packet type: {\"packet_type\": %d}\n", packet_info->packet_type); } #endif diff --git a/src/initialize_client_socket.c b/src/initialize_client_socket.c index ef45109..908ac89 100644 --- a/src/initialize_client_socket.c +++ b/src/initialize_client_socket.c @@ -51,7 +51,7 @@ void* request_server_information(void* const request_server_information_args_voi } #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { send_debug_message("Requested server information: {\"server_ip_address\": \"%s\"}\n", inet_ntoa(request_server_information_args->server_addr)); } #endif @@ -95,13 +95,13 @@ static inline struct SwiftNetClientConnection* const construct_client_connection .last_node = NULL }; - atomic_store_explicit(&new_connection->packet_queue.owner, PACKET_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&new_connection->packet_queue.owner, NONE, memory_order_release); atomic_store_explicit(&new_connection->closing, false, memory_order_release); atomic_store_explicit(&new_connection->initialized, false, memory_order_release); atomic_store_explicit(&new_connection->packet_handler_user_arg, NULL, memory_order_release); memset(&new_connection->packet_callback_queue, 0x00, sizeof(struct PacketCallbackQueue)); - atomic_store_explicit(&new_connection->packet_callback_queue.owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&new_connection->packet_callback_queue.owner, NONE, memory_order_release); return new_connection; } @@ -135,7 +135,7 @@ struct SwiftNetClientConnection* swiftnet_create_client(const char* const ip_add // Request the server information, and proccess it const struct SwiftNetPacketInfo request_server_information_packet_info = construct_packet_info( 0x00, - PACKET_TYPE_REQUEST_INFORMATION, + REQUEST_INFORMATION, 1, 0, new_connection->port_info @@ -180,7 +180,7 @@ struct SwiftNetClientConnection* swiftnet_create_client(const char* const ip_add pthread_create(&new_connection->execute_callback_thread, NULL, execute_packet_callback_client, new_connection); #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { send_debug_message("Successfully initialized client\n"); } #endif diff --git a/src/initialize_server_socket.c b/src/initialize_server_socket.c index 089686c..5b571d3 100644 --- a/src/initialize_server_socket.c +++ b/src/initialize_server_socket.c @@ -36,8 +36,8 @@ static inline struct SwiftNetServer* const construct_server(const bool loopback, memset(&new_server->packet_callback_queue, 0x00, sizeof(struct PacketCallbackQueue)); - atomic_store_explicit(&new_server->packet_queue.owner, PACKET_QUEUE_OWNER_NONE, memory_order_release); - atomic_store_explicit(&new_server->packet_callback_queue.owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&new_server->packet_queue.owner, NONE, memory_order_release); + atomic_store_explicit(&new_server->packet_callback_queue.owner, NONE, memory_order_release); atomic_store_explicit(&new_server->packet_handler, NULL, memory_order_release); atomic_store_explicit(&new_server->packet_handler_user_arg, NULL, memory_order_release); atomic_store_explicit(&new_server->closing, false, memory_order_release); @@ -69,7 +69,7 @@ struct SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool lo pthread_create(&new_server->execute_callback_thread, NULL, execute_packet_callback_server, new_server); #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_INITIALIZATION)) { + if (check_debug_flag(INITIALIZATION)) { send_debug_message("Successfully initialized server\n"); } #endif diff --git a/src/internal/internal.h b/src/internal/internal.h index 1a5dead..37d4154 100644 --- a/src/internal/internal.h +++ b/src/internal/internal.h @@ -19,8 +19,10 @@ #define LOOPBACK_INTERFACE_NAME "lo0\0" #endif -#define REQUEST_LOST_PACKETS_RETURN_UPDATED_BIT_ARRAY 0x00 -#define REQUEST_LOST_PACKETS_RETURN_COMPLETED_PACKET 0x01 +typedef enum { + REQUEST_LOST_PACKETS_RETURN_UPDATED_BIT_ARRAY = 0x00, + REQUEST_LOST_PACKETS_RETURN_COMPLETED_PACKET = 0x01 +} RequestLostPacketsReturnType; #define PACKET_PREPEND_SIZE(addr_type) ((addr_type == DLT_NULL) ? sizeof(uint32_t) : addr_type == DLT_EN10MB ? sizeof(struct ether_header) : 0) #define PACKET_HEADER_SIZE (sizeof(struct ip) + sizeof(struct SwiftNetPacketInfo)) @@ -41,15 +43,7 @@ uint16_t checksum = htons(crc16(buffer, size)); \ memcpy(buffer + prepend_size + offsetof(struct ip, ip_sum), &checksum, sizeof(checksum)); -#define PACKET_QUEUE_OWNER_NONE 0x00 -#define PACKET_QUEUE_OWNER_HANDLE_PACKETS 0x01 -#define PACKET_QUEUE_OWNER_PROCESS_PACKETS 0x02 - -#define PACKET_CALLBACK_QUEUE_OWNER_NONE 0x00 -#define PACKET_CALLBACK_QUEUE_OWNER_PROCESS_PACKETS 0x01 -#define PACKET_CALLBACK_QUEUE_OWNER_EXECUTE_PACKET_CALLBACK 0x02 - -#define PROTOCOL_NUMBER 253 +#define PROT_NUMBER 253 #define SIZEOF_FIELD(type, field) sizeof(((type *)0)->field) @@ -98,13 +92,22 @@ static inline uint16_t crc16(const uint8_t *data, size_t length) { for (size_t i = 0; i < length; i++) { uint8_t byte = data[i]; - crc = (crc >> 8) ^ crc16_table[(crc ^ byte) & 0xFF]; } return crc ^ 0xFFFF; } +enum StackCreatingState { + STACK_CREATING_LOCKED = 0, + STACK_CREATING_UNLOCKED = 1 +}; + +enum AllocatorStackState { + ALLOCATOR_STACK_FREE = 0, + ALLOCATOR_STACK_OCCUPIED = 1 +}; + struct Listener { pcap_t* pcap; char interface_name[IFNAMSIZ]; @@ -256,7 +259,7 @@ static struct ip construct_ip_header(struct in_addr destination_addr, const uint .ip_v = 4, // Version (ipv4) .ip_hl = 5, // Header length .ip_tos = 0, // Type of service - .ip_p = PROTOCOL_NUMBER, // Protocol + .ip_p = PROT_NUMBER, // Protocol .ip_len = htons(packet_size), // Chunk size .ip_id = htons(packet_id), // Packet id .ip_off = htons(0), // Not used diff --git a/src/process_packets.c b/src/process_packets.c index 6da9630..00aee7d 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -136,9 +136,9 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co return; } - uint32_t owner_none = PACKET_CALLBACK_QUEUE_OWNER_NONE; - while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, PACKET_CALLBACK_QUEUE_OWNER_PROCESS_PACKETS, memory_order_acquire, memory_order_relaxed)) { - owner_none = PACKET_CALLBACK_QUEUE_OWNER_NONE; + enum PacketQueueOwner owner_none = NONE; + while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, SOME, memory_order_acquire, memory_order_relaxed)) { + owner_none = NONE; } if(packet_queue->last_node == NULL) { @@ -153,7 +153,7 @@ static inline void insert_callback_queue_node(struct PacketCallbackQueueNode* co packet_queue->first_node = new_node; } - atomic_store_explicit(&packet_queue->owner, PACKET_CALLBACK_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); return; } @@ -286,13 +286,13 @@ static inline struct SwiftNetPacketSending* const get_packet_sending(struct Swif } struct PacketQueueNode* const wait_for_next_packet(struct PacketQueue* const packet_queue) { - uint32_t owner_none = PACKET_QUEUE_OWNER_NONE; - while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, PACKET_QUEUE_OWNER_PROCESS_PACKETS, memory_order_acquire, memory_order_relaxed)) { - owner_none = PACKET_QUEUE_OWNER_NONE; + enum PacketQueueOwner owner_none = NONE; + while(!atomic_compare_exchange_strong_explicit(&packet_queue->owner, &owner_none, SOME, memory_order_acquire, memory_order_relaxed)) { + owner_none = NONE; } if(packet_queue->first_node == NULL) { - atomic_store(&packet_queue->owner, PACKET_QUEUE_OWNER_NONE); + atomic_store(&packet_queue->owner, NONE); return NULL; } @@ -302,14 +302,14 @@ struct PacketQueueNode* const wait_for_next_packet(struct PacketQueue* const pac packet_queue->first_node = NULL; packet_queue->last_node = NULL; - atomic_store(&packet_queue->owner, PACKET_QUEUE_OWNER_NONE); + atomic_store(&packet_queue->owner, NONE); return node_to_process; } packet_queue->first_node = node_to_process->next; - atomic_store_explicit(&packet_queue->owner, PACKET_QUEUE_OWNER_NONE, memory_order_release); + atomic_store_explicit(&packet_queue->owner, NONE, memory_order_release); return node_to_process; } @@ -379,7 +379,7 @@ static inline void swiftnet_process_packets( if(memcmp(&ip_header.ip_src, &ip_header.ip_dst, sizeof(struct in_addr)) != 0 && is_private_ip(ip_header.ip_src) == false && is_private_ip(ip_header.ip_dst)) { if(ip_header.ip_sum != 0 && packet_corrupted(checksum_received, node->data_read, packet_buffer) == true) { #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) { + if (check_debug_flag(PACKETS_RECEIVING)) { send_debug_message("Received corrupted packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d, \"received_checsum\": %d, \"real_checksum\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, ip_header.ip_id, checksum_received, crc16(packet_buffer, node->data_read)); } #endif @@ -391,19 +391,19 @@ static inline void swiftnet_process_packets( } #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_PACKETS_RECEIVING)) { + if (check_debug_flag(PACKETS_RECEIVING)) { send_debug_message("Received packet: {\"source_ip_address\": \"%s\", \"source_port\": %d, \"packet_id\": %d, \"packet_type\": %d, \"packet_length\": %d, \"chunk_index\": %d, \"connection_type\": %d}\n", inet_ntoa(ip_header.ip_src), packet_info.port_info.source_port, ip_header.ip_id, packet_info.packet_type, packet_info.packet_length, packet_info.chunk_index, connection_type); } #endif switch(packet_info.packet_type) { - case PACKET_TYPE_REQUEST_INFORMATION: + case REQUEST_INFORMATION: { const struct ip send_server_info_ip_header = construct_ip_header(node->sender_address, PACKET_HEADER_SIZE, rand()); const struct SwiftNetPacketInfo packet_info_new = construct_packet_info( sizeof(struct SwiftNetServerInformation), - PACKET_TYPE_REQUEST_INFORMATION, + REQUEST_INFORMATION, 1, 0, (struct SwiftNetPortInfo){ @@ -428,7 +428,7 @@ static inline void swiftnet_process_packets( goto next_packet; } - case PACKET_TYPE_SEND_LOST_PACKETS_REQUEST: + case SEND_LOST_PACKETS_REQUEST: { const uint32_t mtu = MIN(packet_info.maximum_transmission_unit, maximum_transmission_unit); @@ -440,7 +440,7 @@ static inline void swiftnet_process_packets( struct SwiftNetPacketInfo send_packet_info = construct_packet_info( 0x00, - PACKET_TYPE_SUCCESSFULLY_RECEIVED_PACKET, + SUCCESSFULLY_RECEIVED_PACKET, 1, 0, (struct SwiftNetPortInfo){ @@ -469,7 +469,7 @@ static inline void swiftnet_process_packets( struct SwiftNetPacketInfo packet_info_new = construct_packet_info( 0, - PACKET_TYPE_SEND_LOST_PACKETS_RESPONSE, + SEND_LOST_PACKETS_RESPONSE, 1, 0, (struct SwiftNetPortInfo){ @@ -497,7 +497,7 @@ static inline void swiftnet_process_packets( goto next_packet; } - case PACKET_TYPE_SEND_LOST_PACKETS_RESPONSE: + case SEND_LOST_PACKETS_RESPONSE: { struct SwiftNetPacketSending* const target_packet_sending = get_packet_sending(packets_sending, ip_header.ip_id); @@ -527,7 +527,7 @@ static inline void swiftnet_process_packets( goto next_packet; } - case PACKET_TYPE_SUCCESSFULLY_RECEIVED_PACKET: + case SUCCESSFULLY_RECEIVED_PACKET: { struct SwiftNetPacketSending* const target_packet_sending = get_packet_sending(packets_sending, ip_header.ip_id); @@ -595,12 +595,12 @@ static inline void swiftnet_process_packets( .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id #ifdef SWIFT_NET_REQUESTS - , .expecting_response = packet_info.packet_type == PACKET_TYPE_REQUEST + , .expecting_response = packet_info.packet_type == REQUEST #endif }; #ifdef SWIFT_NET_REQUESTS - if (packet_info.packet_type == PACKET_TYPE_RESPONSE) { + 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); } else { pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id); @@ -618,12 +618,12 @@ static inline void swiftnet_process_packets( .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id #ifdef SWIFT_NET_REQUESTS - , .expecting_response = packet_info.packet_type == PACKET_TYPE_REQUEST + , .expecting_response = packet_info.packet_type == REQUEST #endif }; #ifdef SWIFT_NET_REQUESTS - if (packet_info.packet_type == PACKET_TYPE_RESPONSE) { + 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); } else { pass_callback_execution(new_packet_data, packet_callback_queue, NULL, ip_header.ip_id); @@ -679,12 +679,12 @@ static inline void swiftnet_process_packets( .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id #ifdef SWIFT_NET_REQUESTS - , .expecting_response = packet_info.packet_type == PACKET_TYPE_REQUEST + , .expecting_response = packet_info.packet_type == REQUEST #endif }; #ifdef SWIFT_NET_REQUESTS - if (packet_info.packet_type == PACKET_TYPE_RESPONSE) { + 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); } else { pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id); @@ -704,12 +704,12 @@ static inline void swiftnet_process_packets( .data_length = packet_info.packet_length, .packet_id = ip_header.ip_id #ifdef SWIFT_NET_REQUESTS - , .expecting_response = packet_info.packet_type == PACKET_TYPE_REQUEST + , .expecting_response = packet_info.packet_type == REQUEST #endif }; #ifdef SWIFT_NET_REQUESTS - if (packet_info.packet_type == PACKET_TYPE_RESPONSE) { + 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); } else { pass_callback_execution(packet_data, packet_callback_queue, pending_message, ip_header.ip_id); diff --git a/src/send_packet.c b/src/send_packet.c index 5eb2e29..10b9ca7 100644 --- a/src/send_packet.c +++ b/src/send_packet.c @@ -28,7 +28,7 @@ static inline void unlock_packet_sending(struct SwiftNetPacketSending* const pac static inline uint8_t request_lost_packets_bitarray(const uint8_t* const raw_data, const uint32_t data_size, const struct sockaddr* const destination, pcap_t* const pcap, struct SwiftNetPacketSending* const packet_sending) { while(1) { - if(check_debug_flag(DEBUG_LOST_PACKETS)) { + if(check_debug_flag(LOST_PACKETS)) { send_debug_message("Requested list of lost packets: {\"packet_id\": %d}\n", packet_sending->packet_id); } @@ -82,7 +82,7 @@ static inline void handle_lost_packets( struct SwiftNetPacketInfo request_lost_packets_bit_array = construct_packet_info( 0x00, - PACKET_TYPE_SEND_LOST_PACKETS_REQUEST, + SEND_LOST_PACKETS_REQUEST, 1, 0, port_info @@ -100,7 +100,7 @@ static inline void handle_lost_packets( #ifdef SWIFT_NET_REQUESTS packet_type, #else - PACKET_TYPE_MESSAGE, + MESSAGE, #endif chunk_amount, 0, @@ -144,7 +144,7 @@ static inline void handle_lost_packets( for(uint32_t i = 0; i < packet_sending->lost_chunks_size; i++) { const uint32_t lost_chunk_index = packet_sending->lost_chunks[i]; - if (check_debug_flag(DEBUG_LOST_PACKETS) == true) { + if (check_debug_flag(LOST_PACKETS) == true) { send_debug_message("Packet lost: {\"packet_id\": %d, \"chunk index\": %d}\n", packet_sending->packet_id, lost_chunk_index); } @@ -202,7 +202,7 @@ inline void swiftnet_send_packet( const uint32_t mtu = MIN(target_maximum_transmission_unit, maximum_transmission_unit); #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_PACKETS_SENDING)) { + if (check_debug_flag(PACKETS_SENDING)) { send_debug_message("Sending packet: {\"destination_ip_address\": \"%s\", \"destination_port\": %d, \"packet_length\": %d}\n", inet_ntoa(*target_addr), port_info.destination_port, packet_length); } #endif @@ -229,7 +229,7 @@ inline void swiftnet_send_packet( #endif #ifdef SWIFT_NET_REQUESTS - const uint8_t packet_type = response ? PACKET_TYPE_RESPONSE : request_sent == NULL ? PACKET_TYPE_MESSAGE : PACKET_TYPE_REQUEST; + const uint8_t packet_type = response ? RESPONSE : request_sent == NULL ? MESSAGE : REQUEST; #endif const uint32_t chunk_amount = (packet_length + (mtu - PACKET_HEADER_SIZE) - 1) / (mtu - PACKET_HEADER_SIZE); @@ -240,7 +240,7 @@ inline void swiftnet_send_packet( #ifdef SWIFT_NET_REQUESTS packet_type, #else - PACKET_TYPE_MESSAGE, + MESSAGE, #endif chunk_amount, 0, @@ -273,7 +273,7 @@ inline void swiftnet_send_packet( const uint32_t current_offset = i * (mtu - PACKET_HEADER_SIZE); #ifdef SWIFT_NET_DEBUG - if (check_debug_flag(DEBUG_PACKETS_SENDING)) { + if (check_debug_flag(PACKETS_SENDING)) { send_debug_message("Sent chunk: {\"chunk_index\": %d}\n", i); } #endif @@ -319,7 +319,7 @@ inline void swiftnet_send_packet( #ifdef SWIFT_NET_REQUESTS packet_type, #else - PACKET_TYPE_MESSAGE, + MESSAGE, #endif 1, 0, diff --git a/src/swift_net.h b/src/swift_net.h index 8853491..576a42d 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -28,15 +28,22 @@ #define SWIFT_NET_DEBUG #endif -#define PACKET_TYPE_MESSAGE 0x01 -#define PACKET_TYPE_REQUEST_INFORMATION 0x02 -#define PACKET_TYPE_SEND_LOST_PACKETS_REQUEST 0x03 -#define PACKET_TYPE_SEND_LOST_PACKETS_RESPONSE 0x04 -#define PACKET_TYPE_SUCCESSFULLY_RECEIVED_PACKET 0x05 -#define PACKET_TYPE_REQUEST 0x06 +enum PacketQueueOwner { + NONE = 0x00, + SOME = 0xFF +}; + +enum PacketType { + MESSAGE = 0x01, + REQUEST_INFORMATION = 0x02, + SEND_LOST_PACKETS_REQUEST = 0x03, + SEND_LOST_PACKETS_RESPONSE = 0x04, + SUCCESSFULLY_RECEIVED_PACKET = 0x05, + REQUEST = 0x06, #ifdef SWIFT_NET_REQUESTS -#define PACKET_TYPE_RESPONSE 0x07 + RESPONSE = 0x07, #endif +}; #define PACKET_INFO_ID_NONE 0xFFFF @@ -47,10 +54,10 @@ extern uint32_t maximum_transmission_unit; #ifdef SWIFT_NET_DEBUG enum SwiftNetDebugFlags { - DEBUG_PACKETS_SENDING = 1u << 0, - DEBUG_PACKETS_RECEIVING = 1u << 1, - DEBUG_INITIALIZATION = 1u << 2, - DEBUG_LOST_PACKETS = 1u << 3 + PACKETS_SENDING = 1u << 0, + PACKETS_RECEIVING = 1u << 1, + INITIALIZATION = 1u << 2, + LOST_PACKETS = 1u << 3 }; struct SwiftNetDebugger { @@ -144,7 +151,7 @@ struct PacketQueueNode { }; struct PacketQueue { - _Atomic uint32_t owner; + _Atomic enum PacketQueueOwner owner; struct PacketQueueNode* first_node; struct PacketQueueNode* last_node; }; @@ -171,7 +178,7 @@ struct SwiftNetClientPacketData { }; struct PacketCallbackQueue { - _Atomic uint32_t owner; + _Atomic enum PacketQueueOwner owner; struct PacketCallbackQueueNode* first_node; struct PacketCallbackQueueNode* last_node; }; diff --git a/tests/src/run_tests.c b/tests/src/run_tests.c index 485f807..9b4cc7f 100644 --- a/tests/src/run_tests.c +++ b/tests/src/run_tests.c @@ -259,7 +259,7 @@ int main() { swiftnet_initialize(); - swiftnet_add_debug_flags(DEBUG_INITIALIZATION | DEBUG_LOST_PACKETS | DEBUG_PACKETS_RECEIVING | DEBUG_PACKETS_SENDING); + swiftnet_add_debug_flags(INITIALIZATION | LOST_PACKETS | PACKETS_RECEIVING | PACKETS_SENDING); for (uint16_t i = 0; i < sizeof(tests) / sizeof(struct Test); i++) { const struct Test* current_test = &tests[i];