diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2022-11-02 00:01:57 +0100 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2022-11-02 00:01:57 +0100 |
commit | 25f4ef74acb73340bd7cba7a7d45ab7191232283 (patch) | |
tree | 0facc8be5feb4d75aaf261cb003e0ddf6a938552 /examples | |
parent | d55e39792970234b0b07582378cc3ec07ab62bf3 (diff) |
Improved examples.
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/c-analysed/c-analysed.c | 78 | ||||
-rw-r--r-- | examples/c-captured/c-captured.c | 40 |
2 files changed, 67 insertions, 51 deletions
diff --git a/examples/c-analysed/c-analysed.c b/examples/c-analysed/c-analysed.c index 0fabd223a..5e6e14fa8 100644 --- a/examples/c-analysed/c-analysed.c +++ b/examples/c-analysed/c-analysed.c @@ -523,13 +523,47 @@ static int parse_options(int argc, char ** argv) return 0; } -int main(int argc, char ** argv) +static int mainloop(void) { - signal(SIGUSR1, sighandler); - signal(SIGINT, sighandler); - signal(SIGTERM, sighandler); - signal(SIGPIPE, sighandler); + enum nDPIsrvd_read_return read_ret = READ_OK; + + while (main_thread_shutdown == 0) + { + read_ret = nDPIsrvd_read(sock); + if (errno == EINTR) + { + continue; + } + if (read_ret == READ_TIMEOUT) + { + printf("No data received during the last %llu second(s).\n", + (long long unsigned int)sock->read_timeout.tv_sec); + continue; + } + if (read_ret != READ_OK) + { + printf("Could not read from socket: %s\n", nDPIsrvd_enum_to_string(read_ret)); + break; + } + + enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(sock); + if (parse_ret != PARSE_NEED_MORE_DATA) + { + printf("Could not parse json string: %s\n", nDPIsrvd_enum_to_string(parse_ret)); + break; + } + } + + if (main_thread_shutdown == 0 && read_ret != READ_OK) + { + return 1; + } + return 0; +} + +int main(int argc, char ** argv) +{ sock = nDPIsrvd_socket_init(0, 0, 0, 0, simple_json_callback, NULL, NULL); if (sock == NULL) { @@ -581,41 +615,11 @@ int main(int argc, char ** argv) return 1; } - enum nDPIsrvd_read_return read_ret = READ_OK; - while (main_thread_shutdown == 0) - { - read_ret = nDPIsrvd_read(sock); - if (errno == EINTR) - { - continue; - } - if (read_ret == READ_TIMEOUT) - { - printf("No data received during the last %llu second(s).\n", - (long long unsigned int)sock->read_timeout.tv_sec); - continue; - } - if (read_ret != READ_OK) - { - break; - } - - enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(sock); - if (parse_ret != PARSE_NEED_MORE_DATA) - { - printf("Could not parse json string: %s\n", nDPIsrvd_enum_to_string(parse_ret)); - break; - } - } - - if (main_thread_shutdown == 0 && read_ret != READ_OK) - { - printf("Parse read %s\n", nDPIsrvd_enum_to_string(read_ret)); - } + int retval = mainloop(); nDPIsrvd_socket_free(&sock); daemonize_shutdown(pidfile); closelog(); - return read_ret; + return retval; } diff --git a/examples/c-captured/c-captured.c b/examples/c-captured/c-captured.c index 229a678eb..5168c8191 100644 --- a/examples/c-captured/c-captured.c +++ b/examples/c-captured/c-captured.c @@ -371,6 +371,7 @@ static enum nDPIsrvd_callback_return captured_json_callback(struct nDPIsrvd_sock struct nDPIsrvd_json_token const * const pkt = TOKEN_GET_SZ(sock, "pkt"); if (pkt == NULL) { + syslog(LOG_DAEMON | LOG_ERR, "%s", "No packet data available."); return CALLBACK_ERROR; } if (flow_user->packets == NULL) @@ -379,6 +380,7 @@ static enum nDPIsrvd_callback_return captured_json_callback(struct nDPIsrvd_sock } if (flow_user->packets == NULL) { + syslog(LOG_DAEMON | LOG_ERR, "%s", "Memory allocation for captured packets failed."); return CALLBACK_ERROR; } @@ -499,6 +501,7 @@ static enum nDPIsrvd_callback_return captured_json_callback(struct nDPIsrvd_sock #endif if (packet_write_pcap_file(flow_user->packets, flow_user->flow_datalink, pcap_filename) != 0) { + syslog(LOG_DAEMON | LOG_ERR, "Could not packet data to pcap file %s", pcap_filename); return CALLBACK_ERROR; } } @@ -773,29 +776,39 @@ static int parse_options(int argc, char ** argv) static int mainloop(void) { - sigset_t sigusr1_block; - - sigemptyset(&sigusr1_block); - sigaddset(&sigusr1_block, SIGUSR1); + enum nDPIsrvd_read_return read_ret = READ_OK; while (main_thread_shutdown == 0) { - sigprocmask(SIG_BLOCK, &sigusr1_block, NULL); - errno = 0; - enum nDPIsrvd_read_return read_ret = nDPIsrvd_read(sock); + read_ret = nDPIsrvd_read(sock); + if (errno == EINTR) + { + continue; + } + if (read_ret == READ_TIMEOUT) + { + syslog(LOG_DAEMON, + "No data received during the last %llu second(s).\n", + (long long unsigned int)sock->read_timeout.tv_sec); + continue; + } if (read_ret != READ_OK) { - syslog(LOG_DAEMON | LOG_ERR, "nDPIsrvd read failed with: %s", nDPIsrvd_enum_to_string(read_ret)); - return 1; + syslog(LOG_DAEMON | LOG_ERR, "Could not read from socket: %s", nDPIsrvd_enum_to_string(read_ret)); + break; } enum nDPIsrvd_parse_return parse_ret = nDPIsrvd_parse_all(sock); if (parse_ret != PARSE_NEED_MORE_DATA) { - syslog(LOG_DAEMON | LOG_ERR, "nDPIsrvd parse failed with: %s", nDPIsrvd_enum_to_string(parse_ret)); - return 1; + syslog(LOG_DAEMON | LOG_ERR, "Could not parse json string: %s", nDPIsrvd_enum_to_string(parse_ret)); + break; } - sigprocmask(SIG_UNBLOCK, &sigusr1_block, NULL); + } + + if (main_thread_shutdown == 0 && read_ret != READ_OK) + { + return 1; } return 0; @@ -819,8 +832,7 @@ int main(int argc, char ** argv) printf("Recv buffer size: %u\n", NETWORK_BUFFER_MAX_SIZE); printf("Connecting to `%s'..\n", serv_optarg); - enum nDPIsrvd_connect_return connect_ret = nDPIsrvd_connect(sock); - if (connect_ret != CONNECT_OK) + if (nDPIsrvd_connect(sock) != CONNECT_OK) { fprintf(stderr, "%s: nDPIsrvd socket connect to %s failed!\n", argv[0], serv_optarg); nDPIsrvd_socket_free(&sock); |