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
9 changes: 5 additions & 4 deletions src/append_to_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "internal/internal.h"

// These functions append data to a packet buffer and advance the current pointer by the data size.

static inline void validate_args(const void* const data, const uint32_t data_size, const char* const restrict caller) {
static inline void validate_args(const void* const data, const uint32_t data_size) {
if(unlikely(data == NULL || data_size == 0)) {
fprintf(stderr, "Error: Invalid arguments given to: %s.\n", caller);
PRINT_ERROR("Error: Invalid arguments given");
exit(EXIT_FAILURE);
}
}
Expand All @@ -20,15 +21,15 @@ static inline void append_data(uint8_t** const append_pointer, const void* const

void swiftnet_client_append_to_packet(const void* const data, const uint32_t data_size, struct SwiftNetPacketBuffer* const packet) {
#ifdef SWIFT_NET_ERROR
validate_args(data, data_size, __func__);
validate_args(data, data_size);
#endif

append_data(&packet->packet_append_pointer, data, data_size);
}

void swiftnet_server_append_to_packet(const void* const data, const uint32_t data_size, struct SwiftNetPacketBuffer* const packet) {
#ifdef SWIFT_NET_ERROR
validate_args(data, data_size, __func__);
validate_args(data, data_size);
#endif

append_data(&packet->packet_append_pointer, data, data_size);
Expand Down
12 changes: 6 additions & 6 deletions src/generic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@

// Set the handler for incoming packets/messages on the server or client

static inline void swiftnet_validate_new_handler(const void* const new_handler, const char* const restrict caller) {
static inline void swiftnet_validate_new_handler(const void* const new_handler) {
#ifdef SWIFT_NET_ERROR
if(unlikely(new_handler == NULL)) {
fprintf(stderr, "Error: Invalid arguments given to function: %s\n", caller);
PRINT_ERROR("Error: Invalid arguments given");
exit(EXIT_FAILURE);
}
#endif
}

void swiftnet_client_set_message_handler(struct SwiftNetClientConnection* const client, void (* const new_handler)(struct SwiftNetClientPacketData* const, void* const), void* const user_arg) {
swiftnet_validate_new_handler(new_handler, __func__);
swiftnet_validate_new_handler(new_handler);

atomic_store_explicit(&client->packet_handler, new_handler, memory_order_release);
atomic_store_explicit(&client->packet_handler_user_arg, user_arg, memory_order_release);
}

void swiftnet_server_set_message_handler(struct SwiftNetServer* const server, void (* const new_handler)(struct SwiftNetServerPacketData* const, void* const), void* const user_arg) {
swiftnet_validate_new_handler(new_handler, __func__);
swiftnet_validate_new_handler(new_handler);

atomic_store_explicit(&server->packet_handler, new_handler, memory_order_release);
atomic_store_explicit(&server->packet_handler_user_arg, user_arg, memory_order_release);
Expand All @@ -35,7 +35,7 @@ void swiftnet_server_set_message_handler(struct SwiftNetServer* const server, vo
void* swiftnet_client_read_packet(struct SwiftNetClientPacketData* const packet_data, const uint32_t data_size) {
const uint32_t data_already_read = (packet_data->current_pointer - packet_data->data) + data_size;
if (data_already_read > packet_data->metadata.data_length) {
fprintf(stderr, "Error: Tried to read more data than there actually is\n");
PRINT_ERROR("Error: Tried to read more data than there actually is");
return NULL;
}

Expand All @@ -49,7 +49,7 @@ void* swiftnet_client_read_packet(struct SwiftNetClientPacketData* const packet_
void* swiftnet_server_read_packet(struct SwiftNetServerPacketData* const packet_data, const uint32_t data_size) {
const uint32_t data_already_read = (packet_data->current_pointer - packet_data->data) + data_size;
if (data_already_read > packet_data->metadata.data_length) {
fprintf(stderr, "Error: Tried to read more data than there actually is\n");
PRINT_ERROR("Error: Tried to read more data than there actually is");
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/initialize_client_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ struct SwiftNetClientConnection* swiftnet_create_client(const char* const ip_add

new_connection->pcap = swiftnet_pcap_open(loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface);
if (new_connection->pcap == NULL) {
fprintf(stderr, "Failed to open bpf\n");
PRINT_ERROR("Failed to open bpf");
exit(EXIT_FAILURE);
}

Expand Down
4 changes: 2 additions & 2 deletions src/initialize_server_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ struct SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool lo

#ifdef SWIFT_NET_ERROR
if(unlikely(new_server == NULL)) {
fprintf(stderr, "Failed to get an empty server\n");
PRINT_ERROR("Failed to get an empty server");
exit(EXIT_FAILURE);
}
#endif
Expand All @@ -29,7 +29,7 @@ struct SwiftNetServer* swiftnet_create_server(const uint16_t port, const bool lo
// Init pcap device
new_server->pcap = swiftnet_pcap_open(loopback ? LOOPBACK_INTERFACE_NAME : default_network_interface);
if (new_server->pcap == NULL) {
fprintf(stderr, "Failed to open bpf\n");
PRINT_ERROR("Failed to open bpf");
exit(EXIT_FAILURE);
}

Expand Down
10 changes: 5 additions & 5 deletions src/initialize_swift_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct SwiftNetVector listeners;
void swiftnet_initialize() {
const int temp_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (temp_socket < 0) {
perror("socket");
PRINT_ERROR("Failed to create temp socket");
exit(EXIT_FAILURE);
}

Expand All @@ -46,7 +46,7 @@ void swiftnet_initialize() {
inet_pton(AF_INET, "8.8.8.8", &remote.sin_addr);

if (connect(temp_socket, (struct sockaddr *)&remote, sizeof(remote)) < 0) {
fprintf(stderr, "Failed to connect temp socket\n");
PRINT_ERROR("Failed to connect temp socket");
close(temp_socket);
exit(EXIT_FAILURE);
}
Expand All @@ -55,7 +55,7 @@ void swiftnet_initialize() {
socklen_t private_sockaddr_len = sizeof(private_sockaddr);

if(getsockname(temp_socket, &private_sockaddr, &private_sockaddr_len) == -1) {
fprintf(stderr, "Failed to get private ip address\n");
PRINT_ERROR("Failed to get private ip address");
close(temp_socket);
exit(EXIT_FAILURE);
}
Expand All @@ -64,15 +64,15 @@ void swiftnet_initialize() {

const int got_default_interface = get_default_interface_and_mac(default_network_interface, sizeof(default_network_interface), mac_address, temp_socket);
if(unlikely(got_default_interface != 0)) {
PRINT_ERROR("Failed to get the default interface");
close(temp_socket);
fprintf(stderr, "Failed to get the default interface\n");
exit(EXIT_FAILURE);
}

maximum_transmission_unit = get_mtu(default_network_interface, temp_socket);
if(unlikely(maximum_transmission_unit == 0)) {
PRINT_ERROR("Failed to get the maximum transmission unit");
close(temp_socket);
fprintf(stderr, "Failed to get the maximum transmission unit\n");
exit(EXIT_FAILURE);
}

Expand Down
3 changes: 3 additions & 0 deletions src/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@

#define SIZEOF_FIELD(type, field) sizeof(((type *)0)->field)

#define PRINT_ERROR(fmt, ...) \
do { fprintf(stderr, fmt " | function: %s | line: %d\n", ##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)

#define MIN(one, two) (one > two ? two : one)

static const uint16_t crc16_table[256] = {
Expand Down
2 changes: 1 addition & 1 deletion src/process_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ static inline void swiftnet_process_packets(
const uint32_t lost_chunks_num = return_lost_chunk_indexes(pending_message->chunks_received, packet_info.chunk_amount, chunk_data_size, (uint32_t*)lost_chunks_buffer);

if (lost_chunks_num != 0) {
fprintf(stderr, "Packet marked as completed, but %d chunks are missing\n", lost_chunks_num);
PRINT_ERROR("Packet marked as completed, but %d chunks are missing", lost_chunks_num);

for (uint32_t i = 0; i < lost_chunks_num; i++) {
printf("chunk index missing: %d\n", *(lost_chunks_buffer + i));
Expand Down
2 changes: 1 addition & 1 deletion src/send_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ inline void swiftnet_send_packet(

struct SwiftNetPacketSending* const new_packet_sending = allocator_allocate(packets_sending_memory_allocator);
if(unlikely(new_packet_sending == NULL)) {
fprintf(stderr, "Failed to send a packet: exceeded maximum amount of sending packets at the same time\n");
PRINT_ERROR("Failed to send a packet: exceeded maximum amount of sending packets at the same time");
return;
}

Expand Down