Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/execute_packet_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
#include <pthread.h>

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;
}
}

static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struct PacketCallbackQueue* const packet_queue) {
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;
}

Expand All @@ -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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/handle_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
#include <stddef.h>

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) {
Expand Down Expand Up @@ -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
Expand All @@ -125,17 +125,17 @@ 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

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
Expand Down
10 changes: 5 additions & 5 deletions src/initialize_client_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/initialize_server_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
29 changes: 16 additions & 13 deletions src/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)

Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down
Loading