Merge branch 'master' of github.com:raboof/nethogs
This commit is contained in:
89
devices.cpp
89
devices.cpp
@@ -15,7 +15,8 @@
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
||||
*USA.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -28,68 +29,62 @@
|
||||
#include <net/if.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
bool selected(int devc, char** devicenames, char* devicename) {
|
||||
if (devc == 0)
|
||||
return true;
|
||||
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;
|
||||
for (int i = 0; i < devc; i++)
|
||||
if (strcmp(devicenames[i], devicename) == 0)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
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;
|
||||
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) {
|
||||
return !(ifa_flags & IFF_LOOPBACK) &&
|
||||
(ifa_flags & IFF_UP) &&
|
||||
(ifa_flags & IFF_RUNNING);
|
||||
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
|
||||
* 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;
|
||||
device *get_devices(int devc, char **devicenames, bool all) {
|
||||
struct ifaddrs *ifaddr, *ifa;
|
||||
|
||||
if (getifaddrs(&ifaddr) == -1)
|
||||
{
|
||||
std::cerr << "Fail to get interface addresses" << std::endl;
|
||||
// perror("getifaddrs");
|
||||
return NULL;
|
||||
}
|
||||
if (getifaddrs(&ifaddr) == -1) {
|
||||
std::cerr << "Failed to get interface addresses" << std::endl;
|
||||
// perror("getifaddrs");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
device* devices = NULL;
|
||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
|
||||
{
|
||||
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;
|
||||
device *devices = NULL;
|
||||
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
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;
|
||||
|
||||
devices = new device(strdup(ifa->ifa_name),devices);
|
||||
}
|
||||
devices = new device(strdup(ifa->ifa_name), devices);
|
||||
}
|
||||
|
||||
freeifaddrs(ifaddr);
|
||||
return devices;
|
||||
freeifaddrs(ifaddr);
|
||||
return devices;
|
||||
}
|
||||
|
||||
device * get_default_devices() {
|
||||
return get_devices(0, NULL, false);
|
||||
}
|
||||
device *get_default_devices() { return get_devices(0, NULL, false); }
|
||||
|
||||
Reference in New Issue
Block a user