From 9c10116d519dc3779beda38e9f2fdc3465926635 Mon Sep 17 00:00:00 2001 From: Logan007 Date: Sat, 15 Aug 2020 22:48:57 +0545 Subject: [PATCH 1/2] split PACKET's transform field to give way for seperate compression field --- include/n2n_define.h | 13 ++++--------- include/n2n_wire.h | 4 ++-- src/edge.c | 2 +- src/edge_utils.c | 22 +++++----------------- src/wire.c | 6 ++++-- 5 files changed, 16 insertions(+), 31 deletions(-) diff --git a/include/n2n_define.h b/include/n2n_define.h index 9f3b07f..ca8e997 100644 --- a/include/n2n_define.h +++ b/include/n2n_define.h @@ -58,18 +58,13 @@ /* Compression is disabled by default for outgoing packets if no cli * option is given. All edges are built with decompression support so * they are able to understand each other (this applies to lzo only). */ -#define N2N_COMPRESSION_ID_NONE 0 /* default, see edge_init_conf_defaults(...) in edge_utils.c */ -#define N2N_COMPRESSION_ID_LZO 1 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */ +#define N2N_COMPRESSION_ID_INVALID 0 +#define N2N_COMPRESSION_ID_NONE 1 /* default, see edge_init_conf_defaults(...) in edge_utils.c */ +#define N2N_COMPRESSION_ID_LZO 2 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */ #ifdef N2N_HAVE_ZSTD -#define N2N_COMPRESSION_ID_ZSTD 2 /* set if '-z2' cli option is present, available only if compiled with zstd lib */ +#define N2N_COMPRESSION_ID_ZSTD 3 /* set if '-z2' cli option is present, available only if compiled with zstd lib */ #define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */ #endif -// with the next major packet structure update, make '0' = invalid, and '1' = no compression -// '2' = LZO, '3' = ZSTD, ... REVISIT then (also: change all occurences in source). - -#define N2N_COMPRESSION_ID_BITLEN 3 /* number of bits used for encoding compression id in the uppermost - bits of transform_id; will be obsolete as soon as compression gets - its own field in the packet. REVISIT then. */ /* (un)purgeable community indicator (supernode) */ #define COMMUNITY_UNPURGEABLE 0 diff --git a/include/n2n_wire.h b/include/n2n_wire.h index e5c1ebd..0ebc714 100644 --- a/include/n2n_wire.h +++ b/include/n2n_wire.h @@ -140,8 +140,8 @@ typedef struct n2n_PACKET n2n_mac_t srcMac; n2n_mac_t dstMac; n2n_sock_t sock; - uint16_t transform; - uint16_t compression; + uint8_t transform; + uint8_t compression; } n2n_PACKET_t; /* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */ diff --git a/src/edge.c b/src/edge.c index d874c43..7e52687 100644 --- a/src/edge.c +++ b/src/edge.c @@ -389,7 +389,7 @@ static int setOption(int optkey, char *optargument, n2n_tuntap_priv_config_t *ec if (optargument) { compression = atoi(optargument); } else - compression = N2N_COMPRESSION_ID_LZO; // default, if '-z' only + compression = 1; // default, if '-z' only, equals -z1 setPayloadCompression(conf, compression); break; diff --git a/src/edge_utils.c b/src/edge_utils.c index 377afce..0990909 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -1020,17 +1020,10 @@ static int handle_PACKET(n2n_edge_t * eee, uint8_t decodebuf[N2N_PKT_BUF_SIZE]; size_t eth_size; n2n_transform_t rx_transop_id; + uint8_t rx_compression_id; rx_transop_id = (n2n_transform_t)pkt->transform; - /* optional compression is encoded in uppermost bit of transform field. - * this is an intermediate solution to maintain compatibility until some - * upcoming major release (3.0?) brings up changes in packet structure anyway - * in the course of which a dedicated compression field could be spent. - * REVISIT then. */ - uint16_t rx_compression_id; - - rx_compression_id = (uint16_t)rx_transop_id >> (8*sizeof((uint16_t)rx_transop_id)-N2N_COMPRESSION_ID_BITLEN); - rx_transop_id &= (1 << (8*sizeof((uint16_t)rx_transop_id)-N2N_COMPRESSION_ID_BITLEN)) -1; + rx_compression_id = pkt->compression; if(rx_transop_id == eee->conf.transop_id) { uint8_t is_multicast; @@ -1071,7 +1064,7 @@ static int handle_PACKET(n2n_edge_t * eee, return (-1); // cannot handle it } - if(rx_compression_id) { + if(rx_compression_id != N2N_COMPRESSION_ID_NONE) { traceEvent (TRACE_DEBUG, "payload decompression [%s]: deflated %u bytes to %u bytes", compression_str(rx_compression_id), eth_size, (int)deflated_len); memcpy(eth_payload ,deflation_buffer, deflated_len ); @@ -1484,7 +1477,7 @@ void edge_send_packet2net(n2n_edge_t * eee, break; } - if(pkt.compression) { + if(pkt.compression != N2N_COMPRESSION_ID_NONE) { traceEvent (TRACE_DEBUG, "payload compression [%s]: compressed %u bytes to %u bytes\n", compression_str(pkt.compression), len, compression_len); @@ -1493,12 +1486,6 @@ void edge_send_packet2net(n2n_edge_t * eee, free (compression_buffer); } } - /* optional compression is encoded in uppermost bits of transform field. - * this is an intermediate solution to maintain compatibility until some - * upcoming major release (3.0?) brings up changes in packet structure anyway - * in the course of which a dedicated compression field could be spent. - * REVISIT then. */ - pkt.transform = pkt.transform | (pkt.compression << (8*sizeof(pkt.transform)-N2N_COMPRESSION_ID_BITLEN)); idx=0; encode_PACKET(pktbuf, &idx, &cmn, &pkt); @@ -2618,6 +2605,7 @@ int quick_edge_init(char *device_name, char *community_name, edge_init_conf_defaults(&conf); conf.encrypt_key = encrypt_key; conf.transop_id = N2N_TRANSFORM_ID_TWOFISH; + conf.compression = N2N_COMPRESSION_ID_NONE; snprintf((char*)conf.community_name, sizeof(conf.community_name), "%s", community_name); edge_conf_add_supernode(&conf, supernode_ip_address_port); diff --git a/src/wire.c b/src/wire.c index a23d47b..66bb771 100644 --- a/src/wire.c +++ b/src/wire.c @@ -453,7 +453,8 @@ int encode_PACKET( uint8_t * base, { retval += encode_sock( base, idx, &(pkt->sock) ); } - retval += encode_uint16( base, idx, pkt->transform ); + retval += encode_uint8( base, idx, pkt->compression ); + retval += encode_uint8( base, idx, pkt->transform ); return retval; } @@ -475,7 +476,8 @@ int decode_PACKET( n2n_PACKET_t * pkt, retval += decode_sock( &(pkt->sock), base, rem, idx ); } - retval += decode_uint16( &(pkt->transform), base, rem, idx ); + retval += decode_uint8( &(pkt->compression), base, rem, idx ); + retval += decode_uint8( &(pkt->transform), base, rem, idx ); return retval; } From 1d8b9d83b0325587f4ab96323e4e35de4c294f2a Mon Sep 17 00:00:00 2001 From: Logan007 Date: Sat, 15 Aug 2020 22:58:15 +0545 Subject: [PATCH 2/2] split PACKET's transform field to give way for separate compression field --- doc/Crypto.md | 2 +- doc/HACKING | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/Crypto.md b/doc/Crypto.md index 547a965..44acb5f 100644 --- a/doc/Crypto.md +++ b/doc/Crypto.md @@ -116,7 +116,7 @@ In case of a PACKET-type, it is succeeded by the fields depicted below: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36 ! Destination IPv4 Address ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -40 ! Transform ID ! Payload ... ! +40 ! Compress'n ID ! Transform ID ! Payload ... ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + 44 ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+... diff --git a/doc/HACKING b/doc/HACKING index 8b04ed1..b11d472 100644 --- a/doc/HACKING +++ b/doc/HACKING @@ -234,7 +234,7 @@ Version 2 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 36 ! Destination IPv4 Address ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -40 ! Transform ID ! +40 ! Compress'n ID ! Transform ID ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 44 ! Payload +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ @@ -256,7 +256,7 @@ follows: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 48 : ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ -52 ! Transform ID ! +52 ! Compress'n ID ! Transform ID ! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 56 ! Encapsulated ethernet payload +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+