diff options
| author | 2025-09-14 14:15:19 +0330 | |
|---|---|---|
| committer | 2025-09-14 14:15:19 +0330 | |
| commit | f47b80f71dd3c6856280d5b0c62661879e191f24 (patch) | |
| tree | e078fd3ac00f1b36755cb7e13c9ab3cd882b5dc5 /src/common.c | |
| parent | moved to 3rd party (diff) | |
add custom port support
Diffstat (limited to 'src/common.c')
| -rw-r--r-- | src/common.c | 91 |
1 files changed, 44 insertions, 47 deletions
diff --git a/src/common.c b/src/common.c index e679cbe..771c3e6 100644 --- a/src/common.c +++ b/src/common.c @@ -79,10 +79,11 @@ typedef uint64_t firehose_reply_new_offset; struct relay_neighbor { bool active; // set to 0 to indicate empty quick connect slot - bool has_ipv4; // whether the neighbor supports ipv4 connection - bool has_ipv6; // whether the neighbor supports ipv6 connection - ipv4 v4_addr; // ipv4 address of the neighbor (if supported) - ipv6 v6_addr; // ipv6 address of the neighbor (if supported) + bool has_ipv4; // whether the neighbor supports ipv4 connection + bool has_ipv6; // whether the neighbor supports ipv6 connection + ipv4 v4_addr; // ipv4 address of the neighbor (if supported) + ipv6 v6_addr; // ipv6 address of the neighbor (if supported) + uint16_t port; // port to connect to }; struct relay_info { @@ -127,11 +128,40 @@ struct data_packet { } contents; }; +static inline uint64_t read_u64_le(const byte *b) { + uint64_t v = 0; + for (int i = 0; i < 8; ++i) v |= (uint64_t) b[ i ] << (8 * i); + return v; +} + +static inline void write_u64_le(byte *b, uint64_t v) { + for (int i = 0; i < 8; ++i) b[ i ] = (v >> (8 * i)) & 0xFF; +} + +static inline uint32_t read_u32_le(const byte *b) { + uint32_t v = 0; + for (int i = 0; i < 4; ++i) v |= (uint32_t) b[ i ] << (8 * i); + return v; +} + +static inline void write_u32_le(byte *b, uint32_t v) { + for (int i = 0; i < 4; ++i) b[ i ] = (v >> (8 * i)) & 0xFF; +} + +static inline uint16_t read_u16_le(const byte *b) { + return (uint16_t) b[ 0 ] | ((uint16_t) b[ 1 ] << 8); +} + +static inline void write_u16_le(byte *b, uint16_t v) { + b[ 0 ] = v & 0xFF; + b[ 1 ] = (v >> 8) & 0xFF; +} + const size_t CONTENTS_SIZE = 1 + 8 + 32 + 32 + 1 + (32 * MAX_RECIPIENTS) + 65536 + 64; const size_t PACKET_SIZE = 4 + 32 + (CONTENTS_SIZE); struct relay_neighbor s2relay_neighbor(size_t len, byte *stream, bool *success) { - if (len != 1 + 1 + 1 + 4 + 16) { + if (len != 1 + 1 + 1 + 4 + 16 + 2) { *success = false; return (struct relay_neighbor) { 0 }; } @@ -143,6 +173,7 @@ struct relay_neighbor s2relay_neighbor(size_t len, byte *stream, bool *success) memcpy(out.v4_addr, stream + 3, 4); memcpy(out.v6_addr, stream + 7, 16); + out.port = read_u16_le(stream + 23); *success = true; return out; @@ -155,10 +186,11 @@ void relay_neighbor2s(struct relay_neighbor data, byte *stream) { memcpy(stream + 3, data.v4_addr, 4); memcpy(stream + 7, data.v6_addr, 16); + write_u16_le(stream + 23, data.port); } struct relay_info s2relay_info(size_t len, byte *stream, bool *success) { - if (len != 1 + 1 + 128 + 2 + 1 + 1 + 1 + 2 + 8 + (4 * (1 + 1 + 1 + 4 + 16))) { + if (len != 1 + 1 + 128 + 2 + 1 + 1 + 1 + 2 + 8 + (4 * (1 + 1 + 1 + 4 + 16 + 2))) { *success = false; return (struct relay_info) { 0 }; } @@ -189,7 +221,7 @@ struct relay_info s2relay_info(size_t len, byte *stream, bool *success) { for (int i = 0; i < 4; i++) { bool ok; - out.statistics.quick_connect[ i ] = s2relay_neighbor(1 + 1 + 1 + 4 + 16, stream + off, &ok); + out.statistics.quick_connect[ i ] = s2relay_neighbor(1 + 1 + 1 + 4 + 16 + 2, stream + off, &ok); if (!ok) { *success = false; return (struct relay_info) { 0 }; @@ -219,18 +251,12 @@ void relay_info2s(struct relay_info data, byte *stream) { stream[ off++ ] = data.statistics.neighbors & 0xFF; stream[ off++ ] = (data.statistics.neighbors >> 8) & 0xFF; - stream[ off++ ] = data.statistics.held_packets & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 8) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 16) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 24) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 32) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 40) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 48) & 0xFF; - stream[ off++ ] = (data.statistics.held_packets >> 56) & 0xFF; + write_u64_le(stream + off, data.statistics.held_packets); + off += 8; for (int i = 0; i < 4; i++) { relay_neighbor2s(data.statistics.quick_connect[ i ], stream + off); - off += 1 + 1 + 1 + 4 + 16; + off += 1 + 1 + 1 + 4 + 16 + 2; } } @@ -394,36 +420,7 @@ uint64_t get_current_time_ms(void) { return (uint64_t) ts.tv_sec * 1000 + (uint64_t) ts.tv_nsec / 1000000; } -#define RELAY_NEIGHBOR_WIRE_LEN (1 + 1 + 1 + 4 + 16) - -static inline uint64_t read_u64_le(const byte *b) { - uint64_t v = 0; - for (int i = 0; i < 8; ++i) v |= (uint64_t) b[ i ] << (8 * i); - return v; -} - -static inline void write_u64_le(byte *b, uint64_t v) { - for (int i = 0; i < 8; ++i) b[ i ] = (v >> (8 * i)) & 0xFF; -} - -static inline uint32_t read_u32_le(const byte *b) { - uint32_t v = 0; - for (int i = 0; i < 4; ++i) v |= (uint32_t) b[ i ] << (8 * i); - return v; -} - -static inline void write_u32_le(byte *b, uint32_t v) { - for (int i = 0; i < 4; ++i) b[ i ] = (v >> (8 * i)) & 0xFF; -} - -static inline uint16_t read_u16_le(const byte *b) { - return (uint16_t) b[ 0 ] | ((uint16_t) b[ 1 ] << 8); -} - -static inline void write_u16_le(byte *b, uint16_t v) { - b[ 0 ] = v & 0xFF; - b[ 1 ] = (v >> 8) & 0xFF; -} +#define RELAY_NEIGHBOR_WIRE_LEN (1 + 1 + 1 + 4 + 16 + 2) struct relay_info run_server_audit(const char *ip, int port) { byte data[] = { audit_server, 0, 0, 0, 0, 0, 0, 0 }; @@ -450,7 +447,7 @@ pthread_mutex_t packet_storage_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t log_io_mutex = PTHREAD_MUTEX_INITIALIZER; enum { _debug = 1, _info = 2, _warn = 3, _error = 4 } log_level = _error; // error only by default -const char* log_level_names[] = { "debug", "info", "warn", "error" }; +const char *log_level_names[] = { "debug", "info", "warn", "error" }; #define log(__level, fmt, ...) \ ({ \ char msg[ 4096 ]; \ |
