From 5375587282ff43a6e80285355ec7bc6a18320d0a Mon Sep 17 00:00:00 2001 From: Andreas Gocht-Zech Date: Sun, 17 Sep 2023 22:35:57 +0200 Subject: [PATCH] move C-style-list to std::list --- src/libnethogs.cpp | 18 ++++++------------ src/main.cpp | 8 ++++---- src/nethogs.cpp | 5 +---- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/src/libnethogs.cpp b/src/libnethogs.cpp index 1aad548..689bfc6 100644 --- a/src/libnethogs.cpp +++ b/src/libnethogs.cpp @@ -10,6 +10,7 @@ extern "C" { #include #include #include +#include ////////////////////////////// extern ProcList *processes; @@ -33,7 +34,7 @@ static fd_set pc_loop_fd_set; static std::vector pc_loop_fd_list; static bool pc_loop_use_select = true; -static handle *handles = NULL; +static std::list handles; static std::pair create_self_pipe() { int pfd[2]; @@ -117,7 +118,7 @@ static int nethogsmonitor_init(int devc, char **devicenames, bool all, if (dp_setnonblock(newhandle, 1, errbuf) == -1) { fprintf(stderr, "Error putting libpcap in nonblocking mode\n"); } - handles = new handle(newhandle, current_dev->name, handles); + handles.push_front(handle(newhandle, current_dev->name)); if (pc_loop_use_select) { // some devices may not support pcap_get_selectable_fd @@ -264,15 +265,10 @@ static void nethogsmonitor_handle_update(NethogsMonitorCallback cb) { static void nethogsmonitor_clean_up() { // clean up - handle *current_handle = handles; - handle *rem; - while (current_handle != NULL) { + for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++){ pcap_close(current_handle->content->pcap_handle); - rem = current_handle; - current_handle = current_handle->next; - free(rem); } - handles = NULL; + handles.clear(); // close file descriptors for (std::vector::const_iterator it = pc_loop_fd_list.begin(); @@ -307,8 +303,7 @@ int nethogsmonitor_loop_devices(NethogsMonitorCallback cb, char *filter, while (monitor_run_flag) { bool packets_read = false; - handle *current_handle = handles; - while (current_handle != NULL) { + for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++) { userdata->device = current_handle->devicename; userdata->sa_family = AF_UNSPEC; int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, @@ -320,7 +315,6 @@ int nethogsmonitor_loop_devices(NethogsMonitorCallback cb, char *filter, } else { gettimeofday(&curtime, NULL); } - current_handle = current_handle->next; } time_t const now = ::time(NULL); diff --git a/src/main.cpp b/src/main.cpp index b5dfdec..de95c5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #ifdef __linux__ #include @@ -236,7 +237,7 @@ int main(int argc, char **argv) { int nb_devices = 0; int nb_failed_devices = 0; - handle *handles = NULL; + std::list handles; device *current_dev = devices; while (current_dev != NULL) { ++nb_devices; @@ -262,7 +263,7 @@ int main(int argc, char **argv) { if (dp_setnonblock(newhandle, 1, errbuf) == -1) { fprintf(stderr, "Error putting libpcap in nonblocking mode\n"); } - handles = new handle(newhandle, current_dev->name, handles); + handles.push_front(handle(newhandle, current_dev->name)); if (pc_loop_use_select) { // some devices may not support pcap_get_selectable_fd @@ -308,8 +309,7 @@ int main(int argc, char **argv) { while (1) { bool packets_read = false; - for (handle *current_handle = handles; current_handle != NULL; - current_handle = current_handle->next) { + for (auto current_handle = handles.begin(); current_handle != handles.end(); current_handle ++) { userdata->device = current_handle->devicename; userdata->sa_family = AF_UNSPEC; int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, diff --git a/src/nethogs.cpp b/src/nethogs.cpp index a1c446c..6e5d487 100644 --- a/src/nethogs.cpp +++ b/src/nethogs.cpp @@ -237,13 +237,10 @@ int process_ip6(u_char *userdata, const dp_header * /* header */, class handle { public: - handle(dp_handle *m_handle, const char *m_devicename = NULL, - handle *m_next = NULL) { + handle(dp_handle *m_handle, const char *m_devicename = NULL) { content = m_handle; - next = m_next; devicename = m_devicename; } dp_handle *content; const char *devicename; - handle *next; };