diff options
-rw-r--r-- | .travis.yml | 6 | ||||
-rw-r--r-- | Makefile | 29 | ||||
-rw-r--r-- | dependencies/nDPIsrvd.h | 34 | ||||
-rwxr-xr-x | dependencies/update_uthash.sh | 6 | ||||
-rw-r--r-- | examples/c-captured/c-captured.c | 55 |
5 files changed, 94 insertions, 36 deletions
diff --git a/.travis.yml b/.travis.yml index b635d7983..2be9adb4b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,13 +1,13 @@ language: c before_install: - sudo apt-get -qq update -- sudo apt-get install -y build-essential make binutils gcc autoconf automake libtool pkg-config git libpcap-dev libgcrypt-dev libgpg-error-dev libjson-c-dev liblua5.1.0-dev netcat-openbsd +- sudo apt-get install -y build-essential make binutils gcc autoconf automake libtool pkg-config git libpcap-dev libgcrypt-dev libgpg-error-dev libjson-c-dev netcat-openbsd script: - git clone https://github.com/ntop/nDPI.git - cd nDPI && ./autogen.sh --prefix=$(realpath ./_install) --with-sanitizer --with-only-libndpi && make install -j4 && cd .. -- make NDPI_WITH_GCRYPT=yes CUSTOM_LIBNDPI=./nDPI/_install/lib/libndpi.a ENABLE_LUA=yes ENABLE_DEBUG=yes ENABLE_SANITIZER=yes LUA_LIBNAME=lua5.1 all examples +- make NDPI_WITH_GCRYPT=yes CUSTOM_LIBNDPI=./nDPI/_install/lib/libndpi.a ENABLE_DEBUG=yes ENABLE_SANITIZER=yes all examples # RUNTIME TESTS (disabled, needs further testing..) #- ./tests/do.sh ./nDPI # EoF RUNTIME TESTS - make clean -- PKG_CONFIG_PATH="$(realpath ./nDPI/_install/lib/pkgconfig)" make PKG_CONFIG_BIN=pkg-config ENABLE_LUA=yes ENABLE_DEBUG=yes ENABLE_SANITIZER=yes LUA_LIBNAME=lua5.1 all examples +- PKG_CONFIG_PATH="$(realpath ./nDPI/_install/lib/pkgconfig)" make PKG_CONFIG_BIN=pkg-config ENABLE_DEBUG=yes ENABLE_SANITIZER=yes all examples @@ -10,12 +10,6 @@ ifneq ($(PKG_CONFIG_BIN),) PC_CFLAGS=$(shell $(PKG_CONFIG_BIN) --cflags libndpi) PC_LDFLAGS=$(shell $(PKG_CONFIG_BIN) --libs libndpi) -ifeq ($(ENABLE_LUA),yes) -LUA_LIBNAME=lua -LUA_CFLAGS=$(shell $(PKG_CONFIG_BIN) --cflags $(LUA_LIBNAME)) -LUA_LIBS=$(shell $(PKG_CONFIG_BIN) --libs $(LUA_LIBNAME)) -endif - ifneq ($(PKG_CONFIG_PATH),) PROJECT_CFLAGS += -Wl,-rpath='$(shell dirname $(PKG_CONFIG_PATH))' endif @@ -75,15 +69,7 @@ nDPIsrvd: nDPIsrvd.c utils.c $(CC) $(PROJECT_CFLAGS) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(STATIC_NDPI_LIB) $(LIBS) examples/c-captured/c-captured: examples/c-captured/c-captured.c -ifneq ($(PKG_CONFIG_BIN),) -ifeq ($(ENABLE_LUA),yes) - $(CC) $(PROJECT_CFLAGS) $(CFLAGS) $(JSMN_CFLAGS) $(LUA_CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS) $(LUA_LIBS) -else - @echo '*** Not building examples/c-captured/c-captured as it requires ENABLE_LUA=yes ***' -endif -else - @echo '*** Not building examples/c-captured/c-captured as it requires PKG_CONFIG_BIN to be set to your target pkg-config executable ***' -endif + $(CC) $(PROJECT_CFLAGS) $(CFLAGS) $(JSMN_CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS) examples/c-json-stdout/c-json-stdout: examples/c-json-stdout/c-json-stdout.c $(CC) $(PROJECT_CFLAGS) $(CFLAGS) $(JSMN_CFLAGS) $@.c -o $@ $(LDFLAGS) $(LIBS) @@ -130,11 +116,6 @@ else @echo 'NDPI_WITH_PCRE = no' endif endif # PKG_CONFIG_BIN -ifeq ($(ENABLE_LUA),yes) - @echo 'ENABLE_LUA = yes' -else - @echo 'ENABLE_LUA = no' -endif ifeq ($(ENABLE_DEBUG),yes) @echo 'ENABLE_DEBUG = yes' else @@ -155,4 +136,10 @@ endif mocksrvd: nc -k -l -U /tmp/ndpid-collector.sock -.PHONY: all examples install clean help mocksrvd +run-nDPIsrvd: nDPIsrvd + ./nDPIsrvd -l + +run-nDPId: nDPId + sudo ./nDPId -l -a run-test -u $(shell id -u -n) + +.PHONY: all examples install clean help mocksrvd run diff --git a/dependencies/nDPIsrvd.h b/dependencies/nDPIsrvd.h index dcadc61ca..d21952883 100644 --- a/dependencies/nDPIsrvd.h +++ b/dependencies/nDPIsrvd.h @@ -3,9 +3,11 @@ #include <arpa/inet.h> #include <errno.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> +#include <sys/un.h> #include <unistd.h> #include "config.h" @@ -103,9 +105,17 @@ static inline struct nDPIsrvd_socket * nDPIsrvd_init(void) sock->fd = -1; sock->socket_family = -1; } + return sock; } +static inline void nDPIsrvd_free(struct nDPIsrvd_socket ** const sock) +{ + free(*sock); + + *sock = NULL; +} + static inline enum nDPIsrvd_connect_return nDPIsrvd_connect_ip(struct nDPIsrvd_socket * const sock, char const * dst_ip, unsigned short dst_port) @@ -137,8 +147,25 @@ static inline enum nDPIsrvd_connect_return nDPIsrvd_connect_ip(struct nDPIsrvd_s static inline enum nDPIsrvd_connect_return nDPIsrvd_connect_unix(struct nDPIsrvd_socket * const sock, char const * const path) { - (void)sock; - (void)path; + struct sockaddr_un remote_addr = {}; + + sock->socket_family = remote_addr.sun_family = AF_UNIX; + sock->fd = socket(sock->socket_family, SOCK_STREAM, 0); + + if (sock->fd < 0) + { + return CONNECT_ERROR_SOCKET; + } + + if (snprintf(remote_addr.sun_path, sizeof(remote_addr.sun_path), "%s", path) <= 0) + { + return CONNECT_ERROR_SOCKET; + } + + if (connect(sock->fd, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) != 0) + { + return CONNECT_ERROR; + } return CONNECT_OK; } @@ -257,7 +284,8 @@ static inline enum nDPIsrvd_parse_return nDPIsrvd_parse(struct nDPIsrvd_socket * break; } - if (sock->buffer.raw[sock->buffer.json_string_length - 1] != '}') + if (sock->buffer.raw[sock->buffer.json_string_length - 2] != '}' || + sock->buffer.raw[sock->buffer.json_string_length - 1] != '\n') { return PARSE_INVALID_CLOSING_CHAR; } diff --git a/dependencies/update_uthash.sh b/dependencies/update_uthash.sh new file mode 100755 index 000000000..cbf1e5cf7 --- /dev/null +++ b/dependencies/update_uthash.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +MYDIR="$(dirname ${0})" +cd "${MYDIR}/.." + +git subtree pull --squash --prefix=dependencies/uthash https://github.com/troydhanson/uthash.git master diff --git a/examples/c-captured/c-captured.c b/examples/c-captured/c-captured.c index 4ddb9e1e7..5c7715eff 100644 --- a/examples/c-captured/c-captured.c +++ b/examples/c-captured/c-captured.c @@ -1,5 +1,6 @@ #include <arpa/inet.h> #include <errno.h> +#include <signal.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -12,10 +13,13 @@ //#define VERBOSE 1 -static char serv_listen_addr[INET_ADDRSTRLEN] = DISTRIBUTOR_HOST; -static uint16_t serv_listen_port = DISTRIBUTOR_PORT; +struct nDPIsrvd_socket * sock = NULL; +static int main_thread_shutdown = 0; +static char const serv_listen_path[] = DISTRIBUTOR_UNIX_SOCKET; +static char const serv_listen_addr[INET_ADDRSTRLEN] = DISTRIBUTOR_HOST; +static uint16_t const serv_listen_port = DISTRIBUTOR_PORT; -static enum nDPIsrvd_callback_return nDPIsrvd_json_callback(struct nDPIsrvd_socket * const sock, void * user_data) +enum nDPIsrvd_callback_return nDPIsrvd_json_callback(struct nDPIsrvd_socket * const sock, void * user_data) { (void)user_data; @@ -45,6 +49,10 @@ static enum nDPIsrvd_callback_return nDPIsrvd_json_callback(struct nDPIsrvd_sock { printf("Not detected flow.\n"); } + else if (value_equals(sock, "detected") == 1) + { + printf("Detected flow.\n"); + } } #ifdef VERBOSE printf("[%.*s : %.*s] ", @@ -63,27 +71,54 @@ static enum nDPIsrvd_callback_return nDPIsrvd_json_callback(struct nDPIsrvd_sock return CALLBACK_OK; } -int main(int argc, char ** argv) +static void sighandler(int signum) { - struct nDPIsrvd_socket * sock = nDPIsrvd_init(); + (void)signum; - (void)argc; + if (main_thread_shutdown == 0) + { + main_thread_shutdown = 1; + } +} +int main(int argc, char ** argv) +{ + sock = nDPIsrvd_init(); if (sock == NULL) { fprintf(stderr, "%s: nDPIsrvd socket memory allocation failed!\n", argv[0]); return 1; } - printf("Connecting to %s:%u\n", serv_listen_addr, serv_listen_port); - enum nDPIsrvd_connect_return connect_ret = nDPIsrvd_connect_ip(sock, serv_listen_addr, serv_listen_port); + signal(SIGINT, sighandler); + signal(SIGTERM, sighandler); + signal(SIGPIPE, sighandler); + + enum nDPIsrvd_connect_return connect_ret; + + if (argc == 2) + { + printf("Connecting to UNIX socket: %s\n", argv[1]); + connect_ret = nDPIsrvd_connect_unix(sock, argv[1]); + } else if (argc == 1) { + if (access(serv_listen_path, R_OK) == 0) + { + printf("Connecting to %s\n", serv_listen_path); + connect_ret = nDPIsrvd_connect_unix(sock, serv_listen_path); + } else { + printf("Connecting to %s:%u\n", serv_listen_addr, serv_listen_port); + connect_ret = nDPIsrvd_connect_ip(sock, serv_listen_addr, serv_listen_port); + } + } + if (connect_ret != CONNECT_OK) { fprintf(stderr, "%s: nDPIsrvd socket connect failed!\n", argv[0]); + nDPIsrvd_free(&sock); return 1; } - while (1) + while (main_thread_shutdown == 0) { errno = 0; enum nDPIsrvd_read_return read_ret = nDPIsrvd_read(sock); @@ -100,5 +135,7 @@ int main(int argc, char ** argv) } } + nDPIsrvd_free(&sock); + return 0; } |