diff --git a/libknet/Makefile.am b/libknet/Makefile.am index 18cbb82f..747af0bc 100644 --- a/libknet/Makefile.am +++ b/libknet/Makefile.am @@ -1,122 +1,124 @@ # # Copyright (C) 2010-2017 Red Hat, Inc. All rights reserved. # # Authors: Fabio M. Di Nitto # Federico Simoncelli # # This software licensed under GPL-2.0+, LGPL-2.0+ # MAINTAINERCLEANFILES = Makefile.in include $(top_srcdir)/build-aux/check.mk SYMFILE = libknet_exported_syms EXTRA_DIST = $(SYMFILE) SUBDIRS = . tests man libversion = 1:0:0 # override global LIBS that pulls in lots of craft we don't need here LIBS = sources = \ common.c \ compat.c \ compress.c \ compress_zlib.c \ compress_lz4.c \ compress_lzo2.c \ compress_lzma.c \ compress_bzip2.c \ crypto.c \ crypto_nss.c \ crypto_openssl.c \ handle.c \ host.c \ links.c \ logging.c \ netutils.c \ threads_common.c \ threads_dsthandler.c \ threads_heartbeat.c \ threads_pmtud.c \ threads_rx.c \ threads_tx.c \ transport_udp.c \ transport_sctp.c \ transport_loopback.c \ transport_common.c include_HEADERS = libknet.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libknet.pc noinst_HEADERS = \ common.h \ compat.h \ compress.h \ - compress_zlib.h \ compress_lz4.h \ compress_lzo2.h \ compress_lzma.h \ compress_bzip2.h \ + compress_zlib.h \ + compress_zlib_remap.h \ crypto.h \ crypto_nss.h \ crypto_openssl.h \ host.h \ internals.h \ links.h \ logging.h \ netutils.h \ onwire.h \ + remap.h \ threads_common.h \ threads_dsthandler.h \ threads_heartbeat.h \ threads_pmtud.h \ threads_rx.h \ threads_tx.h \ transports.h lib_LTLIBRARIES = libknet.la libknet_la_SOURCES = $(sources) libknet_la_CFLAGS = $(nss_CFLAGS) $(openssl_CFLAGS) \ $(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS) $(bzip2_CFLAGS) EXTRA_libknet_la_DEPENDENCIES = $(SYMFILE) libknet_la_LDFLAGS = -Wl,--version-script=$(srcdir)/$(SYMFILE) \ --export-dynamic \ -version-number $(libversion) libknet_la_LIBADD = $(dl_LIBS) $(pthread_LIBS) $(rt_LIBS) $(m_LIBS) noinst_PROGRAMS = crypto_canary compress_canary crypto_canary_SOURCES = crypto_canary.c crypto_canary_CFLAGS = $(nss_CFLAGS) $(openssl_CFLAGS) crypto_canary_LDADD = $(nss_LIBS) $(openssl_LIBS) compress_canary_SOURCES = compress_canary.c compress_canary_CFLAGS = $(zlib_CFLAGS) $(liblz4_CFLAGS) $(lzo2_CFLAGS) $(liblzma_CFLAGS) $(bzip2_CFLAGS) compress_canary_LDADD = $(zlib_LIBS) $(liblz4_LIBS) $(lzo2_LIBS) $(liblzma_LIBS) $(bzip2_LIBS) dist_man_MANS = man update-man-pages: doxyfile.stamp doxyfile.stamp: Doxyfile if MANPAGEUPDATES $(DOXYGEN) Doxyfile $(DOXY2MAN) -o $(abs_srcdir)/man -s 3 --short-pkg @PACKAGE_NAME@ --pkg "Kronosnet Programmer's Manual" $(builddir)/xml/libknet_8h.xml else @echo this system does not have doxy2man or doxygen installed. Unable to update man pages automatically. endif touch doxyfile.stamp clean-local: rm -rf doxyfile.stamp xml diff --git a/libknet/compress_canary.c b/libknet/compress_canary.c index 9cdf6d53..a9088c61 100644 --- a/libknet/compress_canary.c +++ b/libknet/compress_canary.c @@ -1,32 +1,36 @@ /* Transform the binary into dependencies like: * dpkg-shlibdeps -pcompress -dSuggests -xlibc6 -elibknet/compress_canary */ #include "config.h" +#define CANARY + char BZ2_bzBuffToBuffCompress(void); char LZ4_compress_HC(void); char lzma_easy_buffer_encode(void); char lzo1x_1_compress(void); -char compress2(void); +#include "compress_zlib_remap.h" + +#define CANARY_CALL int main (void) { return #ifdef BUILDCOMPBZIP2 BZ2_bzBuffToBuffCompress() + #endif #ifdef BUILDCOMPLZ4 LZ4_compress_HC() + #endif #ifdef BUILDCOMPLZMA lzma_easy_buffer_encode() + #endif #ifdef BUILDCOMPLZO2 lzo1x_1_compress() + #endif #ifdef BUILDCOMPZLIB - compress2() + +#include "compress_zlib_remap.h" #endif 0; } diff --git a/libknet/compress_zlib.c b/libknet/compress_zlib.c index 8623db8a..f384b4d6 100644 --- a/libknet/compress_zlib.c +++ b/libknet/compress_zlib.c @@ -1,189 +1,182 @@ /* * Copyright (C) 2017 Red Hat, Inc. All rights reserved. * * Author: Fabio M. Di Nitto * * This software licensed under GPL-2.0+, LGPL-2.0+ */ #include "config.h" #include #include #include #include #ifdef BUILDCOMPZLIB #include #include "internals.h" #include "compress_zlib.h" #include "logging.h" #include "common.h" #ifdef KNET_LINUX #define LIBZ_1 "libz.so.1" #endif #ifdef KNET_BSD #define LIBZ_1 "libz.so" #endif /* * global vars for dlopen */ static void *zlib_lib; -/* - * symbols remapping - */ -int (*_int_uncompress)(Bytef *dest, uLongf *destLen, - const Bytef *source, uLong sourceLen); -int (*_int_compress2)(Bytef *dest, uLongf *destLen, - const Bytef *source, uLong sourceLen, - int level); +#include "compress_zlib_remap.h" static int zlib_remap_symbols(knet_handle_t knet_h) { if (!(_int_uncompress = remap_symbol (knet_h, KNET_SUB_ZLIBCOMP, zlib_lib, "uncompress"))) goto fail; if (!(_int_compress2 = remap_symbol (knet_h, KNET_SUB_ZLIBCOMP, zlib_lib, "compress2"))) goto fail; return 0; fail: _int_uncompress = NULL; _int_compress2 = NULL; errno = EINVAL; return -1; } int zlib_load_lib( knet_handle_t knet_h) { int err = 0, savederrno = 0; if (!zlib_lib) { zlib_lib = open_lib(knet_h, LIBZ_1, 0); if (!zlib_lib) { savederrno = EAGAIN; err = -1; goto out; } } if (zlib_remap_symbols(knet_h) < 0) { savederrno = errno; err = -1; } out: errno = savederrno; return err; } int zlib_val_level( knet_handle_t knet_h, int compress_level) { if (compress_level < 0) { log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support negative compression level %d", compress_level); return -1; } if (compress_level > 9) { log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib does not support compression level higher than 9"); return -1; } if (compress_level == 0) { log_warn(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress level 0 does NOT perform any compression"); } return 0; } int zlib_compress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int zerr = 0, err = 0; int savederrno = 0; uLongf destLen = *buf_out_len; zerr = (*_int_compress2)(buf_out, &destLen, buf_in, buf_in_len, knet_h->compress_level); *buf_out_len = destLen; switch(zerr) { case Z_OK: err = 0; savederrno = 0; break; case Z_MEM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress mem error"); err = -1; savederrno = ENOMEM; break; case Z_BUF_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress buf error"); err = -1; savederrno = ENOBUFS; break; case Z_STREAM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib compress stream error"); err = -1; savederrno = EINVAL; break; default: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib unknown compress error: %d", zerr); break; } errno = savederrno; return err; } int zlib_decompress( knet_handle_t knet_h, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len) { int zerr = 0, err = 0; int savederrno = 0; uLongf destLen = *buf_out_len; zerr = (*_int_uncompress)(buf_out, &destLen, buf_in, buf_in_len); *buf_out_len = destLen; switch(zerr) { case Z_OK: err = 0; savederrno = 0; break; case Z_MEM_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress mem error"); err = -1; savederrno = ENOMEM; break; case Z_BUF_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress buf error"); err = -1; savederrno = ENOBUFS; break; case Z_DATA_ERROR: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib decompress data error"); err = -1; savederrno = EINVAL; break; default: log_err(knet_h, KNET_SUB_ZLIBCOMP, "zlib unknown decompress error: %d", zerr); break; } errno = savederrno; return err; } #endif diff --git a/libknet/compress_zlib_remap.h b/libknet/compress_zlib_remap.h new file mode 100644 index 00000000..7ff16e67 --- /dev/null +++ b/libknet/compress_zlib_remap.h @@ -0,0 +1,9 @@ +#include "remap.h" + +REMAP_PROTO(int,uncompress, + (Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)) +REMAP_PROTO(int,compress2, + (Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)) diff --git a/libknet/remap.h b/libknet/remap.h new file mode 100644 index 00000000..03ccf792 --- /dev/null +++ b/libknet/remap.h @@ -0,0 +1,16 @@ +/* Repeated inclusions define this differently */ +#undef REMAP_PROTO + +#ifdef CANARY + +/* Canary uses fake prototype to make "calls" uniform */ +#ifdef CANARY_CALL +#define REMAP_PROTO(ret,name,args) name() + +#else +#define REMAP_PROTO(ret,name,args) char name(void); +#endif /* CANARY_CALL */ + +#else +#define REMAP_PROTO(ret,name,args) ret (*_int_ ## name)args; + +#endif /* CANARY */