Warn when no devices are up/running (fixes #45)

This commit is contained in:
Arnout Engelen
2016-03-21 00:09:27 +01:00
parent f2cf510d8e
commit cb6ad14afc
5 changed files with 119 additions and 102 deletions

View File

@@ -28,7 +28,40 @@
#include <net/if.h>
#include <ifaddrs.h>
device * get_default_devices()
bool selected(int devc, char** devicenames, char* devicename) {
if (devc == 0)
return true;
for (int i = 0; i < devc; i++)
if (strcmp(devicenames[i], devicename) == 0)
return true;
return false;
}
bool already_seen(device* devices, char* devicename) {
for (class device* current_device = devices;
current_device != NULL;
current_device = current_device->next) {
if (strcmp(current_device->name, devicename) == 0)
return true;
}
return false;
}
// The interface is up, not a loopback and running?
bool up_running(int ifa_flags) {
std::cout << "up: " << (ifa_flags & IFF_UP) << std::endl;
return !(ifa_flags & IFF_LOOPBACK) &&
(ifa_flags & IFF_UP) &&
(ifa_flags & IFF_RUNNING);
}
/**
* This function can return null, if no good interface is found
* When 'all' is set to 'false', the function avoids loopback interface and down/not running interfaces
*/
device * get_devices(int devc, char** devicenames, bool all)
{
struct ifaddrs *ifaddr, *ifa;
@@ -44,33 +77,20 @@ device * get_default_devices()
{
if (ifa->ifa_addr == NULL)
continue;
if (!selected(devc, devicenames, ifa->ifa_name))
continue;
if (already_seen(devices, ifa->ifa_name))
continue;
if (!all && !up_running(ifa->ifa_flags))
continue;
// The interface is up, not a loopback and running ?
if ( !(ifa->ifa_flags & IFF_LOOPBACK) &&
(ifa->ifa_flags & IFF_UP) &&
(ifa->ifa_flags & IFF_RUNNING) )
{
// Check if the interface is already known by going through all the devices
bool found = false;
device* pIter = devices;
while(pIter != NULL)
{
if ( strcmp(ifa->ifa_name,pIter->name) == 0 )
{
found = true;
}
pIter = pIter->next;
}
// We found a new interface, let's add it
if ( found == false )
{
devices = new device(strdup(ifa->ifa_name),devices);
}
}
}
freeifaddrs(ifaddr);
return devices;
}
device * get_default_devices() {
return get_devices(0, NULL, false);
}

View File

@@ -34,10 +34,16 @@ public:
device * next;
};
/**
* This function can return null, if no good interface is found
* The function avoids loopback interface and down/not running interfaces
*/
/** get all devices that are up, running and not loopback */
device * get_default_devices();
/**
* Get all specified devices.
* If no devices are specified, get all devices.
*
* when 'all' is set, also return loopback interfaces and interfaces
* that are down or not running
*/
device * get_devices(int devc, char** devv, bool all);
#endif

View File

@@ -88,7 +88,7 @@ static int nethogsmonitor_init()
device * devices = get_default_devices();
if ( devices == NULL )
{
std::cerr << "Not devices to monitor" << std::endl;
std::cerr << "No devices to monitor" << std::endl;
return NETHOGS_STATUS_NO_DEVICE;
}

View File

@@ -33,6 +33,7 @@ static void help(bool iserror)
//output << " -f : format of packets on interface, default is eth.\n";
output << " -p : sniff in promiscious mode (not recommended).\n";
output << " -s : sort output by sent column.\n";
output << " -a : monitor all devices, even loopback/stopped ones.\n";
output << " device : device(s) to monitor. default is all interfaces up and running excluding loopback\n";
output << std::endl;
output << "When nethogs is running, press:\n";
@@ -137,11 +138,11 @@ int main (int argc, char** argv)
{
process_init();
device * devices = NULL;
int promisc = 0;
bool all = false;
int opt;
while ((opt = getopt(argc, argv, "Vhbtpd:v:c:s")) != -1) {
while ((opt = getopt(argc, argv, "Vahbtpd:v:c:sa")) != -1) {
switch(opt) {
case 'V':
versiondisplay();
@@ -171,25 +172,18 @@ int main (int argc, char** argv)
case 'c':
refreshlimit = atoi(optarg);
break;
case 'a':
all = true;
break;
default:
help(true);
exit(EXIT_FAILURE);
}
}
while (optind < argc) {
devices = new device (strdup(argv[optind++]), devices);
}
device * devices = get_devices(argc - optind, argv + optind, all);
if (devices == NULL)
{
devices = get_default_devices();
if ( devices == NULL )
{
std::cerr << "Not devices to monitor" << std::endl;
return 0;
}
}
forceExit(false, "No devices to monitor. Use '-a' to allow monitoring loopback interfaces or devices that are not up/running");
if ((!tracemode) && (!DEBUG)){
init_ui();

View File

@@ -37,12 +37,9 @@
#include "process.h"
#include "nethogs.h"
/* #include "inodeproc.cpp" */
#include "inode2prog.h"
#include "conninode.h"
extern local_addr * local_addrs;
extern timeval curtime;
/*