From ad5533bee2f690f76179347a6d9f6902fc58acc5 Mon Sep 17 00:00:00 2001 From: Pavel Odintsov Date: Sun, 23 Aug 2020 14:29:12 +0100 Subject: [PATCH] Extracted endianless conversion functions to separate file --- src/fast_endianless.hpp | 44 +++++++++++++++++++++++++++++++++++++++++ src/fast_library.cpp | 29 --------------------------- src/fast_library.h | 11 ++--------- 3 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 src/fast_endianless.hpp diff --git a/src/fast_endianless.hpp b/src/fast_endianless.hpp new file mode 100644 index 00000000..1935b4d7 --- /dev/null +++ b/src/fast_endianless.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include + +// Linux standard functions for endian conversions are ugly because there are no checks about arguments length +// And you could accidentally use ntohs (suitable only for 16 bit) for 32 or 64 bit value and nobody will warning you +// With this wrapper functions it's pretty complicated to use them for incorrect length type! :) + +// Type safe versions of ntohl, ntohs with type control +inline uint16_t fast_ntoh(uint16_t value) { + return ntohs(value); +} + +inline uint32_t fast_ntoh(uint32_t value) { + return ntohl(value); +} + +inline int32_t fast_ntoh(int32_t value) { + return ntohl(value); +} + +// network (big endian) byte order to host byte order +inline uint64_t fast_ntoh(uint64_t value) { + return be64toh(value); +} + +// Type safe version of htonl, htons +inline uint16_t fast_hton(uint16_t value) { + return htons(value); +} + +inline uint32_t fast_hton(uint32_t value) { + return htonl(value); +} + +inline int32_t fast_hton(int32_t value) { + return htonl(value); +} + +inline uint64_t fast_hton(uint64_t value) { + // host to big endian (network byte order) + return htobe64(value); +} + diff --git a/src/fast_library.cpp b/src/fast_library.cpp index fbd20c74..3affd7f2 100644 --- a/src/fast_library.cpp +++ b/src/fast_library.cpp @@ -37,35 +37,6 @@ int convert_string_to_integer(std::string line) { return atoi(line.c_str()); } -// Type safe versions of ntohl, ntohs with type control -uint16_t fast_ntoh(uint16_t value) { - return ntohs(value); -} - -uint32_t fast_ntoh(uint32_t value) { - return ntohl(value); -} - -// network (big endian) byte order to host byte order -uint64_t fast_ntoh(uint64_t value) { - return be64toh(value); -} - -// Type safe version of htonl, htons -uint16_t fast_hton(uint16_t value) { - return htons(value); -} - -uint32_t fast_hton(uint32_t value) { - return htonl(value); -} - -uint64_t fast_hton(uint64_t value) { - // host to big endian (network byte order) - return htobe64(value); -} - - uint32_t convert_ip_as_string_to_uint(std::string ip) { struct in_addr ip_addr; inet_aton(ip.c_str(), &ip_addr); diff --git a/src/fast_library.h b/src/fast_library.h index 7114db58..6df04d14 100644 --- a/src/fast_library.h +++ b/src/fast_library.h @@ -28,6 +28,8 @@ #include #endif +#include "fast_endianless.hpp" + #define TCP_FIN_FLAG_SHIFT 1 #define TCP_SYN_FLAG_SHIFT 2 #define TCP_RST_FLAG_SHIFT 3 @@ -81,15 +83,6 @@ void copy_networks_from_string_form_to_binary(std::vector networks_ std::vector& our_networks); int convert_string_to_integer(std::string line); -// Byte order type safe converters -uint16_t fast_ntoh(uint16_t value); -uint32_t fast_ntoh(uint32_t value); -uint64_t fast_ntoh(uint64_t value); - -uint16_t fast_hton(uint16_t value); -uint32_t fast_hton(uint32_t value); -uint64_t fast_hton(uint64_t value); - bool print_pid_to_file(pid_t pid, std::string pid_path); bool read_pid_from_file(pid_t& pid, std::string pid_path);