|
1 | 1 | #include "swift_net.h" |
2 | 2 | #include <arpa/inet.h> |
3 | 3 | #include <stdatomic.h> |
| 4 | +#include <stdint.h> |
4 | 5 | #include <stdio.h> |
5 | 6 | #include <unistd.h> |
6 | 7 | #include <string.h> |
@@ -36,59 +37,143 @@ static inline void insert_queue_node(PacketQueueNode* const new_node, volatile P |
36 | 37 | return; |
37 | 38 | } |
38 | 39 |
|
39 | | -static inline void swiftnet_handle_packets(const int sockfd, const uint16_t source_port, pthread_t* const process_packets_thread, void* connection, const ConnectionType connection_type, PacketQueue* const packet_queue, const _Atomic bool* closing) { |
40 | | - while(1) { |
41 | | - if (atomic_load_explicit(closing, memory_order_acquire) == true) { |
42 | | - break; |
43 | | - } |
| 40 | +static inline void swiftnet_handle_packets(const uint16_t source_port, pthread_t* const process_packets_thread, void* connection, const ConnectionType connection_type, PacketQueue* const packet_queue, const _Atomic bool* closing, const bool loopback, const uint16_t addr_type, const struct pcap_pkthdr* hdr, const uint8_t* packet) { |
| 41 | + PacketQueueNode *node = allocator_allocate(&packet_queue_node_memory_allocator); |
| 42 | + if (unlikely(node == NULL)) { |
| 43 | + return; |
| 44 | + } |
44 | 45 |
|
45 | | - PacketQueueNode* const node = allocator_allocate(&packet_queue_node_memory_allocator); |
46 | | - if(unlikely(node == NULL)) { |
47 | | - continue; |
48 | | - } |
| 46 | + uint8_t *packet_buffer = allocator_allocate(&packet_buffer_memory_allocator); |
| 47 | + if (unlikely(packet_buffer == NULL)) { |
| 48 | + allocator_free(&packet_queue_node_memory_allocator, node); |
| 49 | + return; |
| 50 | + } |
49 | 51 |
|
50 | | - node->server_address_length = sizeof(node->sender_address); |
| 52 | + uint32_t len = hdr->caplen; |
| 53 | + memcpy(packet_buffer, packet, len); |
51 | 54 |
|
52 | | - uint8_t* const packet_buffer = allocator_allocate(&packet_buffer_memory_allocator); |
53 | | - if(unlikely(packet_buffer == NULL)) { |
54 | | - allocator_free(&packet_queue_node_memory_allocator, node); |
55 | | - continue; |
56 | | - } |
| 55 | + if (len == 0) { |
| 56 | + allocator_free(&packet_queue_node_memory_allocator, node); |
| 57 | + allocator_free(&packet_buffer_memory_allocator, packet_buffer); |
| 58 | + return; |
| 59 | + } |
57 | 60 |
|
58 | | - const int received_sucessfully = recvfrom(sockfd, packet_buffer, maximum_transmission_unit, 0, (struct sockaddr *)&node->sender_address, &node->server_address_length); |
59 | | - |
60 | | - if(received_sucessfully < 0) { |
| 61 | + if (addr_type == DLT_EN10MB) { |
| 62 | + struct ether_header *eth = (struct ether_header *)packet_buffer; |
| 63 | + |
| 64 | + if (ntohs(eth->ether_type) == ETHERTYPE_IP) { |
| 65 | + struct ip *ip_header = (struct ip *)(packet_buffer + sizeof(struct ether_header)); |
| 66 | + |
| 67 | + node->sender_address = ip_header->ip_src; |
| 68 | + } else { |
61 | 69 | allocator_free(&packet_queue_node_memory_allocator, node); |
62 | 70 | allocator_free(&packet_buffer_memory_allocator, packet_buffer); |
63 | | - continue; |
| 71 | + return; |
64 | 72 | } |
| 73 | + } |
| 74 | + |
| 75 | + node->data_read = len; |
| 76 | + node->data = packet_buffer; |
| 77 | + node->next = NULL; |
| 78 | + |
| 79 | + node->server_address_length = sizeof(node->sender_address); |
| 80 | + |
| 81 | + atomic_thread_fence(memory_order_release); |
| 82 | + |
| 83 | + insert_queue_node(node, packet_queue, connection_type); |
| 84 | +} |
| 85 | + |
| 86 | +static void handle_client_init(SwiftNetClientConnection* user, const struct pcap_pkthdr* hdr, const uint8_t* buffer) { |
| 87 | + SwiftNetClientConnection* const client_connection = (SwiftNetClientConnection*)user; |
65 | 88 |
|
66 | | - struct in_addr sender_addr; |
67 | | - memcpy(&sender_addr, &packet_buffer[offsetof(struct ip, ip_src)], sizeof(struct in_addr)); |
| 89 | + if (atomic_load_explicit(&client_connection->closing, memory_order_acquire) == true) { |
| 90 | + return; |
| 91 | + } |
68 | 92 |
|
69 | | - node->data_read = received_sucessfully; |
70 | | - node->data = packet_buffer; |
71 | | - node->sender_address.sin_addr = sender_addr; |
72 | | - node->next = NULL; |
| 93 | + const uint32_t bytes_received = hdr->caplen; |
73 | 94 |
|
74 | | - atomic_thread_fence(memory_order_release); |
| 95 | + if(bytes_received != PACKET_HEADER_SIZE + sizeof(SwiftNetServerInformation) + client_connection->prepend_size) { |
| 96 | + #ifdef SWIFT_NET_DEBUG |
| 97 | + if (check_debug_flag(DEBUG_INITIALIZATION)) { |
| 98 | + send_debug_message("Invalid packet received from server. Expected server information: {\"bytes_received\": %u, \"expected_bytes\": %u}\n", bytes_received, PACKET_HEADER_SIZE + sizeof(SwiftNetServerInformation)); |
| 99 | + } |
| 100 | + #endif |
75 | 101 |
|
76 | | - insert_queue_node(node, packet_queue, connection_type); |
| 102 | + return; |
77 | 103 | } |
| 104 | + |
| 105 | + struct ip* ip_header = (struct ip*)(buffer + client_connection->prepend_size); |
| 106 | + SwiftNetPacketInfo* packet_info = (SwiftNetPacketInfo*)(buffer + client_connection->prepend_size + sizeof(struct ip)); |
| 107 | + SwiftNetServerInformation* server_information = (SwiftNetServerInformation*)(buffer + client_connection->prepend_size + sizeof(struct ip) + sizeof(SwiftNetPacketInfo)); |
| 108 | + |
| 109 | + 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) { |
| 110 | + #ifdef SWIFT_NET_DEBUG |
| 111 | + if (check_debug_flag(DEBUG_INITIALIZATION)) { |
| 112 | + 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)); |
| 113 | + } |
| 114 | + #endif |
| 115 | + |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if(packet_info->packet_type != PACKET_TYPE_REQUEST_INFORMATION) { |
| 120 | + #ifdef SWIFT_NET_DEBUG |
| 121 | + if (check_debug_flag(DEBUG_INITIALIZATION)) { |
| 122 | + send_debug_message("Invalid packet type: {\"packet_type\": %d}\n", packet_info->packet_type); |
| 123 | + } |
| 124 | + #endif |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + client_connection->maximum_transmission_unit = server_information->maximum_transmission_unit; |
| 129 | + |
| 130 | + atomic_store_explicit(&client_connection->initialized, true, memory_order_release); |
78 | 131 | } |
79 | 132 |
|
80 | | -void* swiftnet_client_handle_packets(void* const client_void) { |
81 | | - SwiftNetClientConnection* const client = (SwiftNetClientConnection*)client_void; |
| 133 | +static void pcap_packet_handle(uint8_t* user, const struct pcap_pkthdr* hdr, const uint8_t* packet) { |
| 134 | + Listener* const listener = (Listener*)user; |
82 | 135 |
|
83 | | - swiftnet_handle_packets(client->sockfd, client->port_info.source_port, &client->process_packets_thread, client, CONNECTION_TYPE_CLIENT, &client->packet_queue, &client->closing); |
| 136 | + SwiftNetPortInfo* const port_info = (SwiftNetPortInfo*)(packet + PACKET_PREPEND_SIZE(listener->addr_type) + sizeof(struct ip) + offsetof(SwiftNetPacketInfo, port_info)); |
84 | 137 |
|
85 | | - return NULL; |
| 138 | + vector_lock(&listener->servers); |
| 139 | + |
| 140 | + for (uint16_t i = 0; i < listener->servers.size; i++) { |
| 141 | + SwiftNetServer* const server = vector_get(&listener->servers, i); |
| 142 | + if (server->server_port == port_info->destination_port) { |
| 143 | + vector_unlock(&listener->servers); |
| 144 | + |
| 145 | + swiftnet_handle_packets(server->server_port, &server->process_packets_thread, server, CONNECTION_TYPE_SERVER, &server->packet_queue, &server->closing, server->loopback, server->addr_type, hdr, packet); |
| 146 | + |
| 147 | + return; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + vector_unlock(&listener->servers); |
| 152 | + |
| 153 | + vector_lock(&listener->client_connections); |
| 154 | + |
| 155 | + for (uint16_t i = 0; i < listener->client_connections.size; i++) { |
| 156 | + SwiftNetClientConnection* const client_connection = vector_get(&listener->client_connections, i); |
| 157 | + if (client_connection->port_info.source_port == port_info->destination_port) { |
| 158 | + vector_unlock(&listener->client_connections); |
| 159 | + |
| 160 | + if (client_connection->initialized == false) { |
| 161 | + handle_client_init(client_connection, hdr, packet); |
| 162 | + } else { |
| 163 | + swiftnet_handle_packets(client_connection->port_info.source_port, &client_connection->process_packets_thread, client_connection, CONNECTION_TYPE_CLIENT, &client_connection->packet_queue, &client_connection->closing, client_connection->loopback, client_connection->addr_type, hdr, packet); |
| 164 | + } |
| 165 | + |
| 166 | + return; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + vector_unlock(&listener->client_connections); |
86 | 171 | } |
87 | 172 |
|
88 | | -void* swiftnet_server_handle_packets(void* const server_void) { |
89 | | - SwiftNetServer* const server = (SwiftNetServer*)server_void; |
| 173 | +void* interface_start_listening(void* listener_void) { |
| 174 | + Listener* listener = listener_void; |
90 | 175 |
|
91 | | - swiftnet_handle_packets(server->sockfd, server->server_port, &server->process_packets_thread, server, CONNECTION_TYPE_SERVER, &server->packet_queue, &server->closing); |
| 176 | + pcap_loop(listener->pcap, 0, pcap_packet_handle, listener_void); |
92 | 177 |
|
93 | 178 | return NULL; |
94 | 179 | } |
0 commit comments