Browse Source

split PACKET's transform field to give way for seperate compression field

pull/359/head
Logan007 4 years ago
parent
commit
9c10116d51
  1. 13
      include/n2n_define.h
  2. 4
      include/n2n_wire.h
  3. 2
      src/edge.c
  4. 22
      src/edge_utils.c
  5. 6
      src/wire.c

13
include/n2n_define.h

@ -58,18 +58,13 @@
/* Compression is disabled by default for outgoing packets if no cli /* Compression is disabled by default for outgoing packets if no cli
* option is given. All edges are built with decompression support so * option is given. All edges are built with decompression support so
* they are able to understand each other (this applies to lzo only). */ * 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_INVALID 0
#define N2N_COMPRESSION_ID_LZO 1 /* set if '-z1' or '-z' cli option is present, see setOption(...) in edge.c */ #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 #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) */ #define ZSTD_COMPRESSION_LEVEL 7 /* 1 (faster) ... 22 (more compression) */
#endif #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) */ /* (un)purgeable community indicator (supernode) */
#define COMMUNITY_UNPURGEABLE 0 #define COMMUNITY_UNPURGEABLE 0

4
include/n2n_wire.h

@ -140,8 +140,8 @@ typedef struct n2n_PACKET
n2n_mac_t srcMac; n2n_mac_t srcMac;
n2n_mac_t dstMac; n2n_mac_t dstMac;
n2n_sock_t sock; n2n_sock_t sock;
uint16_t transform; uint8_t transform;
uint16_t compression; uint8_t compression;
} n2n_PACKET_t; } n2n_PACKET_t;
/* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */ /* Linked with n2n_register_super in n2n_pc_t. Only from edge to supernode. */

2
src/edge.c

@ -389,7 +389,7 @@ static int setOption(int optkey, char *optargument, n2n_tuntap_priv_config_t *ec
if (optargument) { if (optargument) {
compression = atoi(optargument); compression = atoi(optargument);
} else } else
compression = N2N_COMPRESSION_ID_LZO; // default, if '-z' only compression = 1; // default, if '-z' only, equals -z1
setPayloadCompression(conf, compression); setPayloadCompression(conf, compression);
break; break;

22
src/edge_utils.c

@ -1020,17 +1020,10 @@ static int handle_PACKET(n2n_edge_t * eee,
uint8_t decodebuf[N2N_PKT_BUF_SIZE]; uint8_t decodebuf[N2N_PKT_BUF_SIZE];
size_t eth_size; size_t eth_size;
n2n_transform_t rx_transop_id; n2n_transform_t rx_transop_id;
uint8_t rx_compression_id;
rx_transop_id = (n2n_transform_t)pkt->transform; rx_transop_id = (n2n_transform_t)pkt->transform;
/* optional compression is encoded in uppermost bit of transform field. rx_compression_id = pkt->compression;
* 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;
if(rx_transop_id == eee->conf.transop_id) { if(rx_transop_id == eee->conf.transop_id) {
uint8_t is_multicast; uint8_t is_multicast;
@ -1071,7 +1064,7 @@ static int handle_PACKET(n2n_edge_t * eee,
return (-1); // cannot handle it 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", traceEvent (TRACE_DEBUG, "payload decompression [%s]: deflated %u bytes to %u bytes",
compression_str(rx_compression_id), eth_size, (int)deflated_len); compression_str(rx_compression_id), eth_size, (int)deflated_len);
memcpy(eth_payload ,deflation_buffer, deflated_len ); memcpy(eth_payload ,deflation_buffer, deflated_len );
@ -1484,7 +1477,7 @@ void edge_send_packet2net(n2n_edge_t * eee,
break; break;
} }
if(pkt.compression) { if(pkt.compression != N2N_COMPRESSION_ID_NONE) {
traceEvent (TRACE_DEBUG, "payload compression [%s]: compressed %u bytes to %u bytes\n", traceEvent (TRACE_DEBUG, "payload compression [%s]: compressed %u bytes to %u bytes\n",
compression_str(pkt.compression), len, compression_len); compression_str(pkt.compression), len, compression_len);
@ -1493,12 +1486,6 @@ void edge_send_packet2net(n2n_edge_t * eee,
free (compression_buffer); 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; idx=0;
encode_PACKET(pktbuf, &idx, &cmn, &pkt); 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); edge_init_conf_defaults(&conf);
conf.encrypt_key = encrypt_key; conf.encrypt_key = encrypt_key;
conf.transop_id = N2N_TRANSFORM_ID_TWOFISH; 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); snprintf((char*)conf.community_name, sizeof(conf.community_name), "%s", community_name);
edge_conf_add_supernode(&conf, supernode_ip_address_port); edge_conf_add_supernode(&conf, supernode_ip_address_port);

6
src/wire.c

@ -453,7 +453,8 @@ int encode_PACKET( uint8_t * base,
{ {
retval += encode_sock( base, idx, &(pkt->sock) ); 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; return retval;
} }
@ -475,7 +476,8 @@ int decode_PACKET( n2n_PACKET_t * pkt,
retval += decode_sock( &(pkt->sock), base, rem, idx ); 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; return retval;
} }

Loading…
Cancel
Save