Allow compression to fail, and in that case send uncompressed packets

That allows to cancel compression early, if it seems to expand the
packet. Suggested by David Woodhouse.
This commit is contained in:
Nikos Mavrogiannopoulos
2015-01-15 17:43:45 +01:00
parent 7f997cc3fc
commit 67f621976b
2 changed files with 5 additions and 15 deletions

View File

@@ -299,9 +299,9 @@ int lz4_decompress(void *dst, int dstlen, const void *src, int srclen)
static
int lz4_compress(void *dst, int dstlen, const void *src, int srclen)
{
if (dstlen < LZ4_compressBound(srclen))
return -1;
return LZ4_compress(src, dst, srclen);
/* we intentionally restrict output to srclen so that
* compression fails early for packets that expand. */
return LZ4_compress_limitedOutput(src, dst, srclen, srclen);
}
#endif

View File

@@ -1235,12 +1235,7 @@ static int tun_mainloop(struct worker_st *ws, struct timespec *tnow)
if (ws->udp_state == UP_ACTIVE && ws->dtls_selected_comp != NULL && l > MIN_COMPRESSED_SIZE) {
/* otherwise don't compress */
ret = ws->dtls_selected_comp->compress(ws->decomp+8, sizeof(ws->decomp)-8, ws->buffer, l);
if (ret <= 0) {
oclog(ws, LOG_ERR, "error in %s compression %d\n", ws->dtls_selected_comp->name, ret);
return -1;
}
if (ret < l) {
if (ret > 0 && ret < l) {
dtls_to_send.data = ws->decomp;
dtls_to_send.size = ret;
dtls_type = AC_PKT_COMPRESSED;
@@ -1256,12 +1251,7 @@ static int tun_mainloop(struct worker_st *ws, struct timespec *tnow)
} else if (ws->cstp_selected_comp != NULL && l > MIN_COMPRESSED_SIZE) {
/* otherwise don't compress */
ret = ws->cstp_selected_comp->compress(ws->decomp+8, sizeof(ws->decomp)-8, ws->buffer, l);
if (ret <= 0) {
oclog(ws, LOG_ERR, "error in %s compression %d\n", ws->dtls_selected_comp->name, ret);
return -1;
}
if (ret < l) {
if (ret > 0 && ret < l) {
cstp_to_send.data = ws->decomp;
cstp_to_send.size = ret;
cstp_type = AC_PKT_COMPRESSED;