diff options
-rw-r--r-- | Dockerfile | 6 | ||||
-rwxr-xr-x | examples/py-machine-learning/keras-autoencoder.py | 14 | ||||
-rw-r--r-- | nDPId-test.c | 138 | ||||
-rw-r--r-- | nDPId.c | 5 |
4 files changed, 81 insertions, 82 deletions
diff --git a/Dockerfile b/Dockerfile index bce898961..651cbee08 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,10 @@ -FROM ubuntu:22.04 as builder +FROM ubuntu:22.04 AS builder WORKDIR /root RUN env DEBIAN_FRONTEND=noninteractive apt-get -y update && apt-get install -y --no-install-recommends autoconf automake build-essential ca-certificates wget unzip git make cmake pkg-config libpcap-dev autoconf libtool +RUN env DEBIAN_FRONTEND=noninteractive apt-get clean +RUN env DEBIAN_FRONTEND=noninteractive apt-get autoclean RUN git clone https://github.com/utoni/nDPId.git RUN cd nDPId && mkdir -p build && cd build && cmake .. -DBUILD_NDPI=ON && make @@ -14,6 +16,8 @@ COPY --from=builder /root/nDPId/build/nDPId /usr/sbin/nDPId COPY --from=builder /root/nDPId/build/nDPIsrvd /usr/bin/nDPIsrvd RUN env DEBIAN_FRONTEND=noninteractive apt-get -y update && apt-get install -y --no-install-recommends libpcap-dev +RUN env DEBIAN_FRONTEND=noninteractive apt-get clean +RUN env DEBIAN_FRONTEND=noninteractive apt-get autoclean USER nobody RUN /usr/bin/nDPIsrvd -h || { RC=$?; test ${RC} -eq 1; } diff --git a/examples/py-machine-learning/keras-autoencoder.py b/examples/py-machine-learning/keras-autoencoder.py index 6e80b38a5..086cf505c 100755 --- a/examples/py-machine-learning/keras-autoencoder.py +++ b/examples/py-machine-learning/keras-autoencoder.py @@ -89,7 +89,7 @@ def get_autoencoder(load_from_file=None): decoder_submodel = autoencoder.layers[2] return encoder_submodel, decoder_submodel, autoencoder -def onJsonLineRecvd(json_dict, instance, current_flow, global_user_data): +def on_json_line(json_dict, instance, current_flow, global_user_data): if 'packet_event_name' not in json_dict: return True @@ -138,13 +138,13 @@ def onJsonLineRecvd(json_dict, instance, current_flow, global_user_data): return True -def nDPIsrvd_worker(address, shared_shutdown_event, shared_training_event, shared_packet_list): +def ndpisrvd_worker(address, shared_shutdown_event, shared_training_event, shared_packet_list): nsock = nDPIsrvdSocket() try: nsock.connect(address) print_dots = 1 - nsock.loop(onJsonLineRecvd, None, (shared_shutdown_event, shared_training_event, shared_packet_list, print_dots)) + nsock.loop(on_json_line, None, (shared_shutdown_event, shared_training_event, shared_packet_list, print_dots)) except nDPIsrvd.SocketConnectionBroken as err: sys.stderr.write('\nnDPIsrvd-Worker Socket Error: {}\n'.format(err)) except KeyboardInterrupt: @@ -158,11 +158,11 @@ def nDPIsrvd_worker(address, shared_shutdown_event, shared_training_event, share def keras_worker(load_model, save_model, shared_shutdown_event, shared_training_event, shared_packet_queue, shared_plot_queue): shared_training_event.set() try: - encoder, decoder, autoencoder = get_autoencoder(load_model) + encoder, _, autoencoder = get_autoencoder(load_model) except Exception as err: sys.stderr.write('Could not load Keras model from file: {}\n'.format(str(err))) sys.stderr.flush() - encoder, decoder, autoencoder = get_autoencoder() + encoder, _, autoencoder = get_autoencoder() autoencoder.summary() tensorboard = TensorBoard(log_dir=TB_LOGPATH, histogram_freq=1) early_stopping = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=ES_PATIENCE, restore_best_weights=True, start_from_epoch=0, verbose=0, mode='auto') @@ -213,7 +213,7 @@ def keras_worker(load_model, save_model, shared_shutdown_event, shared_training_ try: shared_shutdown_event.set() - except: + except Exception: pass def plot_animate(i, shared_plot_queue, ax, xs, ys): @@ -345,7 +345,7 @@ if __name__ == '__main__': shared_packet_queue = mgr.JoinableQueue() shared_plot_queue = mgr.JoinableQueue() - nDPIsrvd_job = mp.Process(target=nDPIsrvd_worker, args=( + nDPIsrvd_job = mp.Process(target=ndpisrvd_worker, args=( address, shared_shutdown_event, shared_training_event, diff --git a/nDPId-test.c b/nDPId-test.c index 0f3bd8539..3d12a6013 100644 --- a/nDPId-test.c +++ b/nDPId-test.c @@ -168,13 +168,13 @@ static unsigned long long int nDPIsrvd_free_bytes = 0; do \ { \ ((struct thread_return_value *)thread_arg)->val = (errno != 0 ? errno : 1); \ - } while (0); + } while (0) #define THREAD_ERROR_GOTO(thread_arg) \ do \ { \ THREAD_ERROR(thread_arg); \ goto error; \ - } while (0); + } while (0) static void nDPIsrvd_memprof_log(char const * const format, ...) { @@ -384,82 +384,78 @@ static void * nDPIsrvd_mainloop_thread(void * const arg) { struct remote_desc * remote = (struct remote_desc *)nio_get_ptr(&io, i); - if (remote == mock_json_desc || remote == mock_test_desc || - remote == mock_buff_desc || remote == mock_null_desc || - remote == mock_arpa_desc) + if (remote != mock_json_desc && remote != mock_test_desc && remote != mock_buff_desc && + remote != mock_null_desc && remote != mock_arpa_desc) { - if (nio_has_error(&io, i) == NIO_SUCCESS) + logger(1, "nDPIsrvd epoll returned unexpected event data: %p", remote); + THREAD_ERROR_GOTO(arg); + } + + if (nio_has_error(&io, i) == NIO_SUCCESS) + { + char const * remote_desc_name; + if (remote == mock_json_desc) { - char const * remote_desc_name; - if (remote == mock_json_desc) - { - remote_desc_name = "Mock JSON"; - do - { - if (mock_test_desc->fd >= 0) - drain_write_buffers_blocking(mock_test_desc); - if (mock_buff_desc->fd >= 0) - drain_write_buffers_blocking(mock_buff_desc); - if (mock_null_desc->fd >= 0) - drain_write_buffers_blocking(mock_null_desc); - if (mock_arpa_desc->fd >= 0) - drain_write_buffers_blocking(mock_arpa_desc); - } while (handle_data_event(&io, i) == 0); - } - else if (remote == mock_test_desc) - { - remote_desc_name = "Mock Test"; - } - else if (remote == mock_buff_desc) + remote_desc_name = "Mock JSON"; + do { - remote_desc_name = "Mock Buffer"; - } - else if (remote == mock_null_desc) - { - remote_desc_name = "Mock NULL"; - } - else if (remote == mock_arpa_desc) - { - remote_desc_name = "Mock ARPA"; - } - else - { - remote_desc_name = "UNKNOWN"; - } - nDPIsrvd_distributor_disconnects++; - logger(1, - "nDPIsrvd distributor '%s' connection closed (%d/%d)", - remote_desc_name, - nDPIsrvd_distributor_disconnects, - nDPIsrvd_distributor_expected_disconnects); - free_remote(&io, remote); + if (mock_test_desc->fd >= 0) + drain_write_buffers_blocking(mock_test_desc); + if (mock_buff_desc->fd >= 0) + drain_write_buffers_blocking(mock_buff_desc); + if (mock_null_desc->fd >= 0) + drain_write_buffers_blocking(mock_null_desc); + if (mock_arpa_desc->fd >= 0) + drain_write_buffers_blocking(mock_arpa_desc); + } while (handle_data_event(&io, i) == 0); + } + else if (remote == mock_test_desc) + { + remote_desc_name = "Mock Test"; + } + else if (remote == mock_buff_desc) + { + remote_desc_name = "Mock Buffer"; + } + else if (remote == mock_null_desc) + { + remote_desc_name = "Mock NULL"; + } + else if (remote == mock_arpa_desc) + { + remote_desc_name = "Mock ARPA"; } else { - if (handle_data_event(&io, i) != 0) - { - if (mock_arpa_desc == remote) - { - // arpa mock does not care about shutdown events - free_remote(&io, mock_arpa_desc); - nDPIsrvd_distributor_disconnects++; - logger(1, - "nDPIsrvd distributor '%s' connection closed (%d/%d)", - "Mock ARPA", - nDPIsrvd_distributor_disconnects, - nDPIsrvd_distributor_expected_disconnects); - continue; - } - logger(1, "%s", "nDPIsrvd data event handler failed"); - THREAD_ERROR_GOTO(arg); - } + remote_desc_name = "UNKNOWN"; } + nDPIsrvd_distributor_disconnects++; + logger(1, + "nDPIsrvd distributor '%s' connection closed (%d/%d)", + remote_desc_name, + nDPIsrvd_distributor_disconnects, + nDPIsrvd_distributor_expected_disconnects); + free_remote(&io, remote); } else { - logger(1, - "nDPIsrvd epoll returned unexpected event data: %p", remote); - THREAD_ERROR_GOTO(arg); + if (handle_data_event(&io, i) != 0) + { + if (mock_arpa_desc == remote) + { + // arpa mock does not care about shutdown events + free_remote(&io, mock_arpa_desc); + nDPIsrvd_distributor_disconnects++; + logger(1, + "nDPIsrvd distributor '%s' connection closed (%d/%d)", + "Mock ARPA", + nDPIsrvd_distributor_disconnects, + nDPIsrvd_distributor_expected_disconnects); + continue; + } + logger(1, "%s", "nDPIsrvd data event handler failed"); + THREAD_ERROR_GOTO(arg); + } } } } @@ -1186,8 +1182,7 @@ static void * distributor_client_mainloop_thread(void * const arg) #endif else { - logger(1, - "Distributor epoll returned unexpected event data: %p", nio_get_ptr(&io, i)); + logger(1, "Distributor epoll returned unexpected event data: %p", nio_get_ptr(&io, i)); THREAD_ERROR_GOTO(trv); } } @@ -1621,8 +1616,7 @@ static int nio_selftest() goto error; } - if (nio_del_fd(&io, pipefds[0]) != NIO_SUCCESS - || nio_del_fd(&io, pipefds[1]) != NIO_SUCCESS) + if (nio_del_fd(&io, pipefds[0]) != NIO_SUCCESS || nio_del_fd(&io, pipefds[1]) != NIO_SUCCESS) { logger(1, "%s", "Event delete failed"); goto error; @@ -155,7 +155,7 @@ enum nDPId_flow_state enum nDPId_flow_direction { FD_SRC2DST = 0, - FD_DST2SRC = 1, + FD_DST2SRC, FD_COUNT }; @@ -1066,7 +1066,8 @@ static void get_ip4_address_and_netmask(struct ifaddrs const * const ifaddr) static int get_ip_netmask_from_pcap_dev(char const * const pcap_dev) { int retval = 0, found_dev = 0; - int ip4_interface_avail = 0, ip6_interface_avail = 0; + int ip4_interface_avail = 0; + int ip6_interface_avail = 0; struct ifaddrs * ifaddrs = NULL; struct ifaddrs * ifa; |