diff --git a/src/append_to_packet.c b/src/append_to_packet.c index 42826e4..4946983 100644 --- a/src/append_to_packet.c +++ b/src/append_to_packet.c @@ -2,12 +2,13 @@ #include #include #include +#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); } } @@ -20,7 +21,7 @@ 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); @@ -28,7 +29,7 @@ void swiftnet_client_append_to_packet(const void* const data, const uint32_t dat 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); diff --git a/src/generic_functions.c b/src/generic_functions.c index 0940204..23bdef1 100644 --- a/src/generic_functions.c +++ b/src/generic_functions.c @@ -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); @@ -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; } @@ -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; } diff --git a/src/initialize_client_socket.c b/src/initialize_client_socket.c index 6422f37..99bf673 100644 --- a/src/initialize_client_socket.c +++ b/src/initialize_client_socket.c @@ -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); } diff --git a/src/initialize_server_socket.c b/src/initialize_server_socket.c index 52e8847..ff943e0 100644 --- a/src/initialize_server_socket.c +++ b/src/initialize_server_socket.c @@ -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 @@ -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); } diff --git a/src/initialize_swift_net.c b/src/initialize_swift_net.c index 8788b86..5c320b2 100644 --- a/src/initialize_swift_net.c +++ b/src/initialize_swift_net.c @@ -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); } @@ -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); } @@ -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); } @@ -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); } diff --git a/src/internal/internal.h b/src/internal/internal.h index 638e9c4..581f696 100644 --- a/src/internal/internal.h +++ b/src/internal/internal.h @@ -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] = { diff --git a/src/process_packets.c b/src/process_packets.c index ed8648b..3e59f3b 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -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)); diff --git a/src/send_packet.c b/src/send_packet.c index 460ecbd..ae099d2 100644 --- a/src/send_packet.c +++ b/src/send_packet.c @@ -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; }