Cleanup: detecting the link type is done automatically per handle

This commit is contained in:
Arnout Engelen
2016-03-20 19:20:51 +01:00
parent 82924a97e1
commit cc675ce776
2 changed files with 14 additions and 29 deletions

View File

@@ -9,7 +9,7 @@ static time_t last_refresh_time = 0;
//selectable file descriptors for the main loop //selectable file descriptors for the main loop
static fd_set pc_loop_fd_set; 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 void versiondisplay(void) static void versiondisplay(void)
@@ -20,7 +20,7 @@ static void versiondisplay(void)
static void help(bool iserror) static void help(bool iserror)
{ {
std::ostream & output = (iserror ? std::cerr : std::cout); std::ostream & output = (iserror ? std::cerr : std::cout);
//output << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))] [device [device [device ...]]]\n"; //output << "usage: nethogs [-V] [-b] [-d seconds] [-t] [-p] [-f (eth|ppp))] [device [device [device ...]]]\n";
output << "usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [device [device [device ...]]]\n"; output << "usage: nethogs [-V] [-h] [-b] [-d seconds] [-v mode] [-c count] [-t] [-p] [-s] [device [device [device ...]]]\n";
output << " -V : prints version.\n"; output << " -V : prints version.\n";
@@ -76,7 +76,7 @@ void forceExit(bool success, const char *msg, ...)
std::pair<int, int> create_self_pipe() std::pair<int, int> create_self_pipe()
{ {
int pfd[2]; int pfd[2];
if (pipe(pfd) == -1) if (pipe(pfd) == -1)
return std::make_pair(-1, -1); return std::make_pair(-1, -1);
if (fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK) == -1) if (fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK) == -1)
@@ -115,7 +115,7 @@ bool wait_for_next_trigger()
// If select() not possible, pause to prevent 100% // If select() not possible, pause to prevent 100%
usleep(1000); usleep(1000);
} }
return true; return true;
} }
@@ -127,10 +127,10 @@ void clean_up()
{ {
close(*it); close(*it);
} }
procclean(); procclean();
if ((!tracemode) && (!DEBUG)) if ((!tracemode) && (!DEBUG))
exit_ui(); exit_ui();
} }
int main (int argc, char** argv) int main (int argc, char** argv)
@@ -138,7 +138,6 @@ int main (int argc, char** argv)
process_init(); process_init();
device * devices = NULL; device * devices = NULL;
//dp_link_type linktype = dp_link_ethernet;
int promisc = 0; int promisc = 0;
int opt; int opt;
@@ -172,16 +171,6 @@ int main (int argc, char** argv)
case 'c': case 'c':
refreshlimit = atoi(optarg); refreshlimit = atoi(optarg);
break; break;
/*
case 'f':
argv++;
if (strcmp (optarg, "ppp") == 0)
linktype = dp_link_ppp;
else if (strcmp (optarg, "eth") == 0)
linktype = dp_link_ethernet;
}
break;
*/
default: default:
help(true); help(true);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@@ -205,10 +194,10 @@ int main (int argc, char** argv)
if ((!tracemode) && (!DEBUG)){ if ((!tracemode) && (!DEBUG)){
init_ui(); init_ui();
} }
if (NEEDROOT && (geteuid() != 0)) if (NEEDROOT && (geteuid() != 0))
forceExit(false, "You need to be root to run NetHogs!"); forceExit(false, "You need to be root to run NetHogs!");
//use the Self-Pipe trick to interrupt the select() in the main loop //use the Self-Pipe trick to interrupt the select() in the main loop
self_pipe = create_self_pipe(); self_pipe = create_self_pipe();
if( self_pipe.first == -1 || self_pipe.second == -1 ) if( self_pipe.first == -1 || self_pipe.second == -1 )
@@ -226,7 +215,7 @@ int main (int argc, char** argv)
handle * handles = NULL; handle * handles = NULL;
device * current_dev = devices; device * current_dev = devices;
while (current_dev != NULL) { while (current_dev != NULL) {
if( !getLocal(current_dev->name, tracemode) ) if( !getLocal(current_dev->name, tracemode) )
{ {
forceExit(false, "getifaddrs failed while establishing local IP."); forceExit(false, "getifaddrs failed while establishing local IP.");
@@ -251,7 +240,7 @@ int main (int argc, char** argv)
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 = new handle (newhandle, current_dev->name, handles);
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
@@ -266,7 +255,7 @@ int main (int argc, char** argv)
pc_loop_fd_list.clear(); pc_loop_fd_list.clear();
fprintf(stderr, "failed to get selectable_fd for %s\n", current_dev->name); fprintf(stderr, "failed to get selectable_fd for %s\n", current_dev->name);
} }
} }
} }
else else
{ {
@@ -317,7 +306,7 @@ int main (int argc, char** argv)
} }
do_refresh(); do_refresh();
} }
//if not packets, do a select() until next packet //if not packets, do a select() until next packet
if (!packets_read) if (!packets_read)
{ {
@@ -328,8 +317,7 @@ int main (int argc, char** argv)
} }
} }
} }
//clean up //clean up
clean_up(); clean_up();
} }

View File

@@ -1,4 +1,4 @@
/* /*
* nethogs.cpp * nethogs.cpp
* *
* Copyright (c) 2004-2006,2008,2011 Arnout Engelen * Copyright (c) 2004-2006,2008,2011 Arnout Engelen
@@ -60,8 +60,6 @@ bool bughuntmode = false;
bool sortRecv = true; bool sortRecv = true;
// viewMode: kb/s or total // viewMode: kb/s or total
int viewMode = VIEWMODE_KBPS; int viewMode = VIEWMODE_KBPS;
//packet_type packettype = packet_ethernet;
//dp_link_type linktype = dp_link_ethernet;
const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION; const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION;
timeval curtime; timeval curtime;
@@ -235,4 +233,3 @@ public:
const char * devicename; const char * devicename;
handle * next; handle * next;
}; };