X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_packet.c;h=417c74ece48d162a35eacfcf10a81b1d4cc6a07e;hb=e050753d1fc4d4465032919746c2a4db78f19932;hp=b8997e51c0e537a22b9f8709b9e3a7f67fbe28fb;hpb=0f18410ebaf1c503e5d0ca9624b24df930561d7d;p=tinc diff --git a/src/net_packet.c b/src/net_packet.c index b8997e51..417c74ec 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -25,6 +25,8 @@ #ifdef HAVE_ZLIB #define ZLIB_CONST #include +#include + #endif #ifdef HAVE_LZO @@ -35,10 +37,6 @@ #include LZ4_H #endif -#ifdef HAVE_LZ4_BUILTIN -#include "lib/lz4/lz4.h" -#endif - #include "address_cache.h" #include "cipher.h" #include "conf.h" @@ -75,9 +73,7 @@ static char lzo_wrkmem[LZO1X_999_MEM_COMPRESS > LZO1X_1_MEM_COMPRESS ? LZO1X_999 #ifdef HAVE_LZ4_BUILTIN static LZ4_stream_t lz4_stream; #else -#ifdef HAVE_LZ4_STATE static void *lz4_state = NULL; -#endif /* HAVE_LZ4_STATE */ #endif /* HAVE_LZ4_BUILTIN */ static void send_udppacket(node_t *, vpn_packet_t *); @@ -222,57 +218,61 @@ static void udp_probe_h(node_t *n, vpn_packet_t *packet, length_t len) { } } -static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) { - switch(level) { #ifdef HAVE_LZ4 - - case 12: +static length_t compress_packet_lz4(uint8_t *dest, const uint8_t *source, length_t len) { #ifdef HAVE_LZ4_BUILTIN - return LZ4_compress_fast_extState(&lz4_stream, (char *)source, (char *) dest, len, MAXSIZE, 0); - + return LZ4_compress_fast_extState(&lz4_stream, (const char *) source, (char *) dest, len, MAXSIZE, 0); #else -#ifdef HAVE_LZ4_STATE - - /* @FIXME: Put this in a better place, and free() it too. */ - if(lz4_state == NULL) { - lz4_state = malloc(LZ4_sizeofState()); - } - if(lz4_state == NULL) { - logger(DEBUG_ALWAYS, LOG_ERR, "Failed to allocate lz4_state, error: %i", errno); - return 0; - } - - return LZ4_compress_fast_extState(lz4_state, source, dest, len, MAXSIZE, 0); + /* @FIXME: Put this in a better place, and free() it too. */ + if(lz4_state == NULL) { + lz4_state = malloc(LZ4_sizeofState()); + } -#else - return LZ4_compress_shim(source, dest, len, MAXSIZE); + if(lz4_state == NULL) { + logger(DEBUG_ALWAYS, LOG_ERR, "Failed to allocate lz4_state, error: %i", errno); + return 0; + } -#endif /* HAVE_LZ4_STATE */ + return LZ4_compress_fast_extState(lz4_state, (const char *) source, (char *) dest, len, MAXSIZE, 0); #endif /* HAVE_LZ4_BUILTIN */ -#endif /* HAVE_LZ4 */ +} +#endif /* HAVE_LZ4 */ + #ifdef HAVE_LZO +static length_t compress_packet_lzo(uint8_t *dest, const uint8_t *source, length_t len, int level) { + assert(level == 10 || level == 11); - case 11: { - lzo_uint lzolen = MAXSIZE; + lzo_uint lzolen = MAXSIZE; + int result; - if(lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem) == LZO_E_OK) { - return lzolen; - } else { - return 0; - } + if(level == 11) { + result = lzo1x_999_compress(source, len, dest, &lzolen, lzo_wrkmem); + } else { // level == 10 + result = lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem); } - case 10: { - lzo_uint lzolen = MAXSIZE; - - if(lzo1x_1_compress(source, len, dest, &lzolen, lzo_wrkmem) == LZO_E_OK) { - return lzolen; - } else { - return 0; - } + if(result == LZO_E_OK) { + return lzolen; + } else { + return 0; } +} +#endif +static length_t compress_packet(uint8_t *dest, const uint8_t *source, length_t len, int level) { + switch(level) { +#ifdef HAVE_LZ4 + + case 12: + return compress_packet_lz4(dest, source, len); +#endif + +#ifdef HAVE_LZO + + case 11: + case 10: + return compress_packet_lzo(dest, source, len, level); #endif #ifdef HAVE_ZLIB