move C-style-list to std::list

This commit is contained in:
Andreas Gocht-Zech
2023-09-17 22:35:57 +02:00
parent 059390b145
commit 5375587282
3 changed files with 11 additions and 20 deletions

View File

@@ -10,6 +10,7 @@ extern "C" {
#include <map> #include <map>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <list>
////////////////////////////// //////////////////////////////
extern ProcList *processes; extern ProcList *processes;
@@ -33,7 +34,7 @@ static fd_set pc_loop_fd_set;
static std::vector<int> pc_loop_fd_list; static std::vector<int> pc_loop_fd_list;
static bool pc_loop_use_select = true; static bool pc_loop_use_select = true;
static handle *handles = NULL; static std::list<handle> handles;
static std::pair<int, int> create_self_pipe() { static std::pair<int, int> create_self_pipe() {
int pfd[2]; int pfd[2];
@@ -117,7 +118,7 @@ static int nethogsmonitor_init(int devc, char **devicenames, bool all,
if (dp_setnonblock(newhandle, 1, errbuf) == -1) { if (dp_setnonblock(newhandle, 1, errbuf) == -1) {
fprintf(stderr, "Error putting libpcap in nonblocking mode\n"); 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) { if (pc_loop_use_select) {
// some devices may not support pcap_get_selectable_fd // 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() { static void nethogsmonitor_clean_up() {
// clean up // clean up
handle *current_handle = handles; for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++){
handle *rem;
while (current_handle != NULL) {
pcap_close(current_handle->content->pcap_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 // close file descriptors
for (std::vector<int>::const_iterator it = pc_loop_fd_list.begin(); for (std::vector<int>::const_iterator it = pc_loop_fd_list.begin();
@@ -307,8 +303,7 @@ int nethogsmonitor_loop_devices(NethogsMonitorCallback cb, char *filter,
while (monitor_run_flag) { while (monitor_run_flag) {
bool packets_read = false; bool packets_read = false;
handle *current_handle = handles; for(auto current_handle = handles.begin(); current_handle != handles.end(); current_handle++) {
while (current_handle != NULL) {
userdata->device = current_handle->devicename; userdata->device = current_handle->devicename;
userdata->sa_family = AF_UNSPEC; userdata->sa_family = AF_UNSPEC;
int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata,
@@ -320,7 +315,6 @@ int nethogsmonitor_loop_devices(NethogsMonitorCallback cb, char *filter,
} else { } else {
gettimeofday(&curtime, NULL); gettimeofday(&curtime, NULL);
} }
current_handle = current_handle->next;
} }
time_t const now = ::time(NULL); time_t const now = ::time(NULL);

View File

@@ -2,6 +2,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <set> #include <set>
#include <vector> #include <vector>
#include <list>
#ifdef __linux__ #ifdef __linux__
#include <linux/capability.h> #include <linux/capability.h>
@@ -236,7 +237,7 @@ int main(int argc, char **argv) {
int nb_devices = 0; int nb_devices = 0;
int nb_failed_devices = 0; int nb_failed_devices = 0;
handle *handles = NULL; std::list<handle> handles;
device *current_dev = devices; device *current_dev = devices;
while (current_dev != NULL) { while (current_dev != NULL) {
++nb_devices; ++nb_devices;
@@ -262,7 +263,7 @@ int main(int argc, char **argv) {
if (dp_setnonblock(newhandle, 1, errbuf) == -1) { if (dp_setnonblock(newhandle, 1, errbuf) == -1) {
fprintf(stderr, "Error putting libpcap in nonblocking mode\n"); 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) { if (pc_loop_use_select) {
// some devices may not support pcap_get_selectable_fd // some devices may not support pcap_get_selectable_fd
@@ -308,8 +309,7 @@ int main(int argc, char **argv) {
while (1) { while (1) {
bool packets_read = false; bool packets_read = false;
for (handle *current_handle = handles; current_handle != NULL; for (auto current_handle = handles.begin(); current_handle != handles.end(); current_handle ++) {
current_handle = current_handle->next) {
userdata->device = current_handle->devicename; userdata->device = current_handle->devicename;
userdata->sa_family = AF_UNSPEC; userdata->sa_family = AF_UNSPEC;
int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata, int retval = dp_dispatch(current_handle->content, -1, (u_char *)userdata,

View File

@@ -237,13 +237,10 @@ int process_ip6(u_char *userdata, const dp_header * /* header */,
class handle { class handle {
public: public:
handle(dp_handle *m_handle, const char *m_devicename = NULL, handle(dp_handle *m_handle, const char *m_devicename = NULL) {
handle *m_next = NULL) {
content = m_handle; content = m_handle;
next = m_next;
devicename = m_devicename; devicename = m_devicename;
} }
dp_handle *content; dp_handle *content;
const char *devicename; const char *devicename;
handle *next;
}; };