Merge pull request #50 from raboof/warnWhenDown
Warn when no devices are up/running (fixes #45)
This commit is contained in:
68
devices.cpp
68
devices.cpp
@@ -28,7 +28,40 @@
|
|||||||
#include <net/if.h>
|
#include <net/if.h>
|
||||||
#include <ifaddrs.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;
|
struct ifaddrs *ifaddr, *ifa;
|
||||||
|
|
||||||
@@ -44,33 +77,20 @@ device * get_default_devices()
|
|||||||
{
|
{
|
||||||
if (ifa->ifa_addr == NULL)
|
if (ifa->ifa_addr == NULL)
|
||||||
continue;
|
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 ?
|
devices = new device(strdup(ifa->ifa_name),devices);
|
||||||
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);
|
freeifaddrs(ifaddr);
|
||||||
return devices;
|
return devices;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
device * get_default_devices() {
|
||||||
|
return get_devices(0, NULL, false);
|
||||||
|
}
|
||||||
|
|||||||
14
devices.h
14
devices.h
@@ -34,10 +34,16 @@ public:
|
|||||||
device * next;
|
device * next;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/** get all devices that are up, running and not loopback */
|
||||||
* This function can return null, if no good interface is found
|
|
||||||
* The function avoids loopback interface and down/not running interfaces
|
|
||||||
*/
|
|
||||||
device * get_default_devices();
|
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
|
#endif
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ static int nethogsmonitor_init()
|
|||||||
device * devices = get_default_devices();
|
device * devices = get_default_devices();
|
||||||
if ( devices == NULL )
|
if ( devices == NULL )
|
||||||
{
|
{
|
||||||
std::cerr << "Not devices to monitor" << std::endl;
|
std::cerr << "No devices to monitor" << std::endl;
|
||||||
return NETHOGS_STATUS_NO_DEVICE;
|
return NETHOGS_STATUS_NO_DEVICE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
22
main.cpp
22
main.cpp
@@ -33,6 +33,7 @@ static void help(bool iserror)
|
|||||||
//output << " -f : format of packets on interface, default is eth.\n";
|
//output << " -f : format of packets on interface, default is eth.\n";
|
||||||
output << " -p : sniff in promiscious mode (not recommended).\n";
|
output << " -p : sniff in promiscious mode (not recommended).\n";
|
||||||
output << " -s : sort output by sent column.\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 << " device : device(s) to monitor. default is all interfaces up and running excluding loopback\n";
|
||||||
output << std::endl;
|
output << std::endl;
|
||||||
output << "When nethogs is running, press:\n";
|
output << "When nethogs is running, press:\n";
|
||||||
@@ -137,11 +138,11 @@ int main (int argc, char** argv)
|
|||||||
{
|
{
|
||||||
process_init();
|
process_init();
|
||||||
|
|
||||||
device * devices = NULL;
|
|
||||||
int promisc = 0;
|
int promisc = 0;
|
||||||
|
bool all = false;
|
||||||
|
|
||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(argc, argv, "Vhbtpd:v:c:s")) != -1) {
|
while ((opt = getopt(argc, argv, "Vahbtpd:v:c:sa")) != -1) {
|
||||||
switch(opt) {
|
switch(opt) {
|
||||||
case 'V':
|
case 'V':
|
||||||
versiondisplay();
|
versiondisplay();
|
||||||
@@ -171,25 +172,18 @@ int main (int argc, char** argv)
|
|||||||
case 'c':
|
case 'c':
|
||||||
refreshlimit = atoi(optarg);
|
refreshlimit = atoi(optarg);
|
||||||
break;
|
break;
|
||||||
|
case 'a':
|
||||||
|
all = true;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
help(true);
|
help(true);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (optind < argc) {
|
device * devices = get_devices(argc - optind, argv + optind, all);
|
||||||
devices = new device (strdup(argv[optind++]), devices);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (devices == NULL)
|
if (devices == NULL)
|
||||||
{
|
forceExit(false, "No devices to monitor. Use '-a' to allow monitoring loopback interfaces or devices that are not up/running");
|
||||||
devices = get_default_devices();
|
|
||||||
if ( devices == NULL )
|
|
||||||
{
|
|
||||||
std::cerr << "Not devices to monitor" << std::endl;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((!tracemode) && (!DEBUG)){
|
if ((!tracemode) && (!DEBUG)){
|
||||||
init_ui();
|
init_ui();
|
||||||
|
|||||||
@@ -37,12 +37,9 @@
|
|||||||
|
|
||||||
#include "process.h"
|
#include "process.h"
|
||||||
#include "nethogs.h"
|
#include "nethogs.h"
|
||||||
/* #include "inodeproc.cpp" */
|
|
||||||
#include "inode2prog.h"
|
#include "inode2prog.h"
|
||||||
#include "conninode.h"
|
#include "conninode.h"
|
||||||
|
|
||||||
extern local_addr * local_addrs;
|
|
||||||
|
|
||||||
extern timeval curtime;
|
extern timeval curtime;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user