From 2a8883a96e0505670ced394f59fdd17cdac2ddce Mon Sep 17 00:00:00 2001
From: lns <matzeton@googlemail.com>
Date: Fri, 10 Jun 2022 14:29:30 +0200
Subject: CMake: do not add /usr/include/ndpi to include dirs if BUILD_NDPI or
 STATIC_LIBNDPI_INSTALLDIR used.

 * c-collectd: fixed memory leak on failure
 * py-flow-info.py: fancy spinners and stats counting improved

Signed-off-by: lns <matzeton@googlemail.com>
---
 examples/c-collectd/c-collectd.c   | 17 ++++++++---------
 examples/py-flow-info/flow-info.py | 25 +++++++++++++++----------
 2 files changed, 23 insertions(+), 19 deletions(-)

(limited to 'examples')

diff --git a/examples/c-collectd/c-collectd.c b/examples/c-collectd/c-collectd.c
index 3abceec83..b9ea95066 100644
--- a/examples/c-collectd/c-collectd.c
+++ b/examples/c-collectd/c-collectd.c
@@ -686,7 +686,7 @@ int main(int argc, char ** argv)
 
     if (parse_options(argc, argv, sock) != 0)
     {
-        return 1;
+        goto failure;
     }
 
     if (getenv("COLLECTD_HOSTNAME") == NULL && getenv("COLLECTD_INTERVAL") == NULL)
@@ -705,15 +705,13 @@ int main(int argc, char ** argv)
     if (connect_ret != CONNECT_OK)
     {
         LOG(LOG_DAEMON | LOG_ERR, "nDPIsrvd socket connect to %s failed!", serv_optarg);
-        nDPIsrvd_socket_free(&sock);
-        return 1;
+        goto failure;
     }
 
     if (nDPIsrvd_set_nonblock(sock) != 0)
     {
         LOG(LOG_DAEMON | LOG_ERR, "nDPIsrvd set nonblock failed: %s", strerror(errno));
-        nDPIsrvd_socket_free(&sock);
-        return 1;
+        goto failure;
     }
 
     signal(SIGINT, sighandler);
@@ -726,13 +724,13 @@ int main(int argc, char ** argv)
     if (epollfd < 0)
     {
         LOG(LOG_DAEMON | LOG_ERR, "Error creating epoll: %s", strerror(errno));
-        return 1;
+        goto failure;
     }
 
     if (create_collectd_timer() != 0)
     {
         LOG(LOG_DAEMON | LOG_ERR, "Error creating timer: %s", strerror(errno));
-        return 1;
+        goto failure;
     }
 
     {
@@ -740,7 +738,7 @@ int main(int argc, char ** argv)
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, collectd_timerfd, &timer_event) < 0)
         {
             LOG(LOG_DAEMON | LOG_ERR, "Error adding JSON fd to epoll: %s", strerror(errno));
-            return 1;
+            goto failure;
         }
     }
 
@@ -749,13 +747,14 @@ int main(int argc, char ** argv)
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, sock->fd, &socket_event) < 0)
         {
             LOG(LOG_DAEMON | LOG_ERR, "Error adding nDPIsrvd socket fd to epoll: %s", strerror(errno));
-            return 1;
+            goto failure;
         }
     }
 
     LOG(LOG_DAEMON | LOG_NOTICE, "%s", "Initialization succeeded.");
     retval = mainloop(epollfd, sock);
 
+failure:
     nDPIsrvd_socket_free(&sock);
     close(collectd_timerfd);
     close(epollfd);
diff --git a/examples/py-flow-info/flow-info.py b/examples/py-flow-info/flow-info.py
index 7384d687a..9a82b9f8b 100755
--- a/examples/py-flow-info/flow-info.py
+++ b/examples/py-flow-info/flow-info.py
@@ -51,7 +51,12 @@ class Stats:
             self.spinner_state += 1
 
     def getSpinner(self):
-        spinner_states = ['-', '\\', '|', '/']
+        #spinner_states = ['-', '\\', '|', '/']
+        #spinner_states = ['▉', '▊', '▋', '▌', '▍', '▎', '▏', '▎', '▍', '▌', '▋', '▊', '▉']
+        spinner_states = ['←', '↖', '↑', '↗', '→', '↘', '↓', '↙']
+        #spinner_states = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█', '▇', '▆', '▅', '▄', '▃', '▁']
+        #spinner_states = ['▖', '▘', '▝', '▗']
+        #spinner_states = ['┤', '┘', '┴', '└', '├', '┌', '┬', '┐']
         return spinner_states[self.spinner_state % len(spinner_states)]
 
     def getDataFromJson(self, json_dict, current_flow):
@@ -66,16 +71,16 @@ class Stats:
             set_attr_from_dict(current_flow, {}, 'flow_risk', {})
         set_attr_from_dict(current_flow, json_dict, 'midstream', 0)
         set_attr_from_dict(current_flow, json_dict, 'flow_event_name', '')
-        set_attr_if_not_set(current_flow, 'guessed', False)
-        set_attr_if_not_set(current_flow, 'not_detected', False)
+        set_attr_if_not_set(current_flow, 'guessed', 0)
+        set_attr_if_not_set(current_flow, 'not_detected', 0)
 
         if current_flow.flow_event_name == 'detected' or \
            current_flow.flow_event_name == 'detection-update':
-            current_flow.guessed = False
+            current_flow.guessed = 0
         elif current_flow.flow_event_name == 'guessed':
-            current_flow.guessed = True
+            current_flow.guessed = 1
         elif current_flow.flow_event_name == 'not-detected':
-            current_flow.not_detected = True
+            current_flow.not_detected = 1
 
     def update(self, json_dict, current_flow):
         self.updateSpinner()
@@ -90,8 +95,8 @@ class Stats:
         self.expired_avg_l4_payload_len += current_flow.flow_avg_l4_payload_len
         self.risky_flows += 1 if len(current_flow.flow_risk) > 0 else 0
         self.midstream_flows += 1 if current_flow.midstream != 0 else 0
-        self.guessed_flows += 1 if current_flow.guessed is True else 0
-        self.not_detected_flows += 1 if current_flow.not_detected is True else 0
+        self.guessed_flows += 1 if current_flow.guessed != 0 else 0
+        self.not_detected_flows += 1 if current_flow.not_detected != 0 else 0
 
     def getStatsFromFlowMgr(self):
         alias_count = 0
@@ -117,8 +122,8 @@ class Stats:
                     flow_avg_l4_payload_len += current_flow.flow_avg_l4_payload_len
                     risky += 1 if len(current_flow.flow_risk) > 0 else 0
                     midstream += 1 if current_flow.midstream != 0 else 0
-                    guessed += 1 if current_flow.guessed is True else 0
-                    not_detected = 1 if current_flow.not_detected is True else 0
+                    guessed += 1 if current_flow.guessed != 0 else 0
+                    not_detected = 1 if current_flow.not_detected != 0 else 0
 
         return alias_count, source_count, flow_count, \
                flow_tot_l4_payload_len, flow_avg_l4_payload_len, \
-- 
cgit v1.2.3