added support for monitoring multiple interfaces at once
This commit is contained in:
10
Changelog
10
Changelog
@@ -1,8 +1,16 @@
|
|||||||
Changelog
|
Changelog
|
||||||
|
|
||||||
2/07/04 (Arnout)
|
06/07/04 (Arnout)
|
||||||
|
-added support for monitoring multiple interfaces
|
||||||
|
at once
|
||||||
|
|
||||||
|
-------------------------------------------------
|
||||||
|
|
||||||
|
02/07/04 (Arnout)
|
||||||
-enabled 'q' for quitting
|
-enabled 'q' for quitting
|
||||||
|
|
||||||
|
-------------------------------------------------
|
||||||
|
|
||||||
29/06/04 (Fabian) <-> 0.5.1
|
29/06/04 (Fabian) <-> 0.5.1
|
||||||
-Adding forceExit when device is ifdown
|
-Adding forceExit when device is ifdown
|
||||||
(handle was null => segfault)
|
(handle was null => segfault)
|
||||||
|
|||||||
4
Makefile
4
Makefile
@@ -2,8 +2,8 @@ VERSION := 0
|
|||||||
SUBVERSION := 6
|
SUBVERSION := 6
|
||||||
MINORVERSION := pre
|
MINORVERSION := pre
|
||||||
|
|
||||||
bin := /usr/local/bin
|
bin := $(DESTDIR)/usr/bin
|
||||||
man8 := /usr/local/man/man8/
|
man8 := $(DESTDIR)/usr/share/man/man8/
|
||||||
|
|
||||||
all: nethogs
|
all: nethogs
|
||||||
|
|
||||||
|
|||||||
83
nethogs.cpp
83
nethogs.cpp
@@ -1,6 +1,4 @@
|
|||||||
/* nethogs.cpp
|
/* nethogs.cpp */
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "nethogs.h"
|
#include "nethogs.h"
|
||||||
|
|
||||||
@@ -27,12 +25,20 @@ extern "C" {
|
|||||||
bool needrefresh = true;
|
bool needrefresh = true;
|
||||||
unsigned refreshdelay = 1;
|
unsigned refreshdelay = 1;
|
||||||
|
|
||||||
|
char * currentdevice = NULL;
|
||||||
|
|
||||||
const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION;
|
const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION;
|
||||||
|
|
||||||
timeval curtime;
|
timeval curtime;
|
||||||
std::string * caption;
|
std::string * caption;
|
||||||
|
|
||||||
|
bool local_addr::contains (const in_addr_t & n_addr) {
|
||||||
|
if (n_addr == addr)
|
||||||
|
return true;
|
||||||
|
if (next == NULL)
|
||||||
|
return false;
|
||||||
|
return next->contains(n_addr);
|
||||||
|
}
|
||||||
|
|
||||||
void process (u_char * args, const struct pcap_pkthdr * header, const u_char * m_packet)
|
void process (u_char * args, const struct pcap_pkthdr * header, const u_char * m_packet)
|
||||||
{
|
{
|
||||||
@@ -49,8 +55,7 @@ void process (u_char * args, const struct pcap_pkthdr * header, const u_char * m
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
connection = new Connection (packet);
|
connection = new Connection (packet);
|
||||||
Process * process = getProcess(connection);
|
Process * process = getProcess(connection, currentdevice);
|
||||||
//process->addConnection (connection);
|
|
||||||
|
|
||||||
if (needrefresh)
|
if (needrefresh)
|
||||||
{
|
{
|
||||||
@@ -91,9 +96,29 @@ static void help(void)
|
|||||||
std::cerr << " device : device to monitor. default is eth0\n";
|
std::cerr << " device : device to monitor. default is eth0\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class device {
|
||||||
|
public:
|
||||||
|
device (char * m_name, device * m_next = NULL)
|
||||||
|
{
|
||||||
|
name = m_name; next = m_next;
|
||||||
|
}
|
||||||
|
char * name;
|
||||||
|
device * next;
|
||||||
|
};
|
||||||
|
|
||||||
|
class handle {
|
||||||
|
public:
|
||||||
|
handle (pcap_t * m_handle, char * m_devicename = NULL, handle * m_next = NULL) {
|
||||||
|
content = m_handle; next = m_next; devicename = m_devicename;
|
||||||
|
}
|
||||||
|
pcap_t * content;
|
||||||
|
char * devicename;
|
||||||
|
handle * next;
|
||||||
|
};
|
||||||
|
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
char* dev = strdup("eth0");
|
device * devices = NULL;
|
||||||
|
|
||||||
for (argv++; *argv; argv++)
|
for (argv++; *argv; argv++)
|
||||||
{
|
{
|
||||||
@@ -118,9 +143,12 @@ int main (int argc, char** argv)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
dev = strdup(*argv);
|
devices = new device (strdup(*argv), devices);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (devices == NULL)
|
||||||
|
devices = new device (strdup("eth0"));
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
#else
|
#else
|
||||||
WINDOW * screen = initscr();
|
WINDOW * screen = initscr();
|
||||||
@@ -129,34 +157,57 @@ int main (int argc, char** argv)
|
|||||||
cbreak();
|
cbreak();
|
||||||
nodelay(screen, TRUE);
|
nodelay(screen, TRUE);
|
||||||
#endif
|
#endif
|
||||||
getLocal(dev);
|
|
||||||
|
|
||||||
caption = new std::string ("NetHogs");
|
caption = new std::string ("NetHogs");
|
||||||
caption->append(version);
|
caption->append(version);
|
||||||
caption->append(", running at ");
|
caption->append(", running at ");
|
||||||
caption->append(dev);
|
|
||||||
|
|
||||||
if (NEEDROOT && (getuid() != 0))
|
if (NEEDROOT && (getuid() != 0))
|
||||||
forceExit("You need to be root to run NetHogs !");
|
forceExit("You need to be root to run NetHogs !");
|
||||||
|
|
||||||
char errbuf[PCAP_ERRBUF_SIZE];
|
char errbuf[PCAP_ERRBUF_SIZE];
|
||||||
|
|
||||||
pcap_t * handle;
|
handle * handles = NULL;
|
||||||
handle = pcap_open_live(dev, BUFSIZ, 0, 1000, errbuf);
|
device * current_dev = devices;
|
||||||
|
while (current_dev != NULL) {
|
||||||
|
getLocal(current_dev->name);
|
||||||
|
caption->append(current_dev->name);
|
||||||
|
caption->append(" ");
|
||||||
|
|
||||||
if (!handle)
|
pcap_t * newhandle = pcap_open_live(current_dev->name, BUFSIZ, 0, 100, errbuf);
|
||||||
forceExit("Device is not active");
|
if (newhandle != NULL)
|
||||||
|
{
|
||||||
|
handles = new handle (newhandle, current_dev->name, handles);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_dev = current_dev->next;
|
||||||
|
}
|
||||||
|
|
||||||
signal (SIGALRM, &alarm_cb);
|
signal (SIGALRM, &alarm_cb);
|
||||||
signal (SIGINT, &quit_cb);
|
signal (SIGINT, &quit_cb);
|
||||||
alarm (refreshdelay);
|
alarm (refreshdelay);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
pcap_dispatch (handle, -1, process, NULL);
|
handle * current_handle = handles;
|
||||||
|
while (current_handle != NULL)
|
||||||
|
{
|
||||||
|
currentdevice = current_handle->devicename;
|
||||||
|
pcap_dispatch (current_handle->content, -1, process, NULL);
|
||||||
|
current_handle = current_handle->next;
|
||||||
|
}
|
||||||
|
if (!DEBUG) {
|
||||||
switch (getch()) {
|
switch (getch()) {
|
||||||
case 'q':
|
case 'q':
|
||||||
|
/* quit */
|
||||||
quit_cb(0);
|
quit_cb(0);
|
||||||
break;
|
break;
|
||||||
|
case 's':
|
||||||
|
/* sort on 'sent' */
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
/* sort on 'received' */
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (needrefresh)
|
if (needrefresh)
|
||||||
{
|
{
|
||||||
|
|||||||
15
nethogs.h
15
nethogs.h
@@ -1,6 +1,7 @@
|
|||||||
#ifndef __NETHOGS_H
|
#ifndef __NETHOGS_H
|
||||||
#define __NETHOGS_H
|
#define __NETHOGS_H
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#define _BSD_SOURCE 1
|
#define _BSD_SOURCE 1
|
||||||
|
|
||||||
@@ -21,8 +22,20 @@
|
|||||||
#define DEBUG 0
|
#define DEBUG 0
|
||||||
|
|
||||||
|
|
||||||
#define PROGNAME_WIDTH 30
|
#define PROGNAME_WIDTH 27
|
||||||
|
|
||||||
void forceExit(const char *msg);
|
void forceExit(const char *msg);
|
||||||
|
|
||||||
|
class local_addr {
|
||||||
|
public:
|
||||||
|
local_addr (in_addr_t m_addr, local_addr * m_next = NULL)
|
||||||
|
{
|
||||||
|
addr = m_addr;
|
||||||
|
next = m_next;
|
||||||
|
}
|
||||||
|
bool contains (const in_addr_t & n_addr);
|
||||||
|
in_addr_t addr;
|
||||||
|
local_addr * next;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
21
packet.cpp
21
packet.cpp
@@ -9,13 +9,14 @@
|
|||||||
#include "nethogs.h"
|
#include "nethogs.h"
|
||||||
// #include "inet6.c"
|
// #include "inet6.c"
|
||||||
|
|
||||||
struct in_addr * local_addr = NULL;
|
local_addr * local_addrs = NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* getLocal
|
* getLocal
|
||||||
* device: This should be device explicit (e.g. eth0:1)
|
* device: This should be device explicit (e.g. eth0:1)
|
||||||
*
|
*
|
||||||
* ioctl device for address
|
* uses ioctl to get address of this device, and adds it to the
|
||||||
|
* local_addrs-list.
|
||||||
*/
|
*/
|
||||||
void getLocal (const char *device)
|
void getLocal (const char *device)
|
||||||
{
|
{
|
||||||
@@ -23,14 +24,6 @@ void getLocal (const char *device)
|
|||||||
struct ifreq iFreq;
|
struct ifreq iFreq;
|
||||||
struct sockaddr_in *saddr;
|
struct sockaddr_in *saddr;
|
||||||
|
|
||||||
if (local_addr != NULL)
|
|
||||||
{
|
|
||||||
std::cerr << "getLocal only needs to be called once in connection.cpp" << std::endl;
|
|
||||||
free (local_addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
local_addr = (struct in_addr *) malloc (sizeof(struct in_addr));
|
|
||||||
|
|
||||||
if((sock=socket(AF_INET, SOCK_PACKET, htons(0x0806)))<0){
|
if((sock=socket(AF_INET, SOCK_PACKET, htons(0x0806)))<0){
|
||||||
forceExit("creating socket failed while establishing local IP - are you root?");
|
forceExit("creating socket failed while establishing local IP - are you root?");
|
||||||
}
|
}
|
||||||
@@ -39,7 +32,9 @@ void getLocal (const char *device)
|
|||||||
forceExit("ioctl failed while establishing local IP");
|
forceExit("ioctl failed while establishing local IP");
|
||||||
}
|
}
|
||||||
saddr=(struct sockaddr_in*)&iFreq.ifr_addr;
|
saddr=(struct sockaddr_in*)&iFreq.ifr_addr;
|
||||||
(*local_addr)=saddr->sin_addr;
|
local_addrs = new local_addr (saddr->sin_addr.s_addr, local_addrs);
|
||||||
|
//malloc
|
||||||
|
//(*local_addr)=saddr->sin_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef u_int32_t tcp_seq;
|
typedef u_int32_t tcp_seq;
|
||||||
@@ -155,9 +150,9 @@ bool Packet::isOlderThan (timeval t) {
|
|||||||
bool Packet::Outgoing () {
|
bool Packet::Outgoing () {
|
||||||
/* must be initialised with getLocal("eth0:1");) */
|
/* must be initialised with getLocal("eth0:1");) */
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
assert (local_addr != NULL);
|
assert (local_addrs != NULL);
|
||||||
|
|
||||||
return (sip.s_addr == local_addr->s_addr);
|
return (local_addrs->contains(sip.s_addr));
|
||||||
}
|
}
|
||||||
|
|
||||||
char * Packet::gethashstring ()
|
char * Packet::gethashstring ()
|
||||||
|
|||||||
33
process.cpp
33
process.cpp
@@ -224,7 +224,7 @@ private:
|
|||||||
Process * val;
|
Process * val;
|
||||||
};
|
};
|
||||||
|
|
||||||
Process * unknownproc = new Process (0, "unknown");
|
Process * unknownproc = new Process (0, "", "unknown");
|
||||||
ProcList * processes = new ProcList (unknownproc, NULL);
|
ProcList * processes = new ProcList (unknownproc, NULL);
|
||||||
|
|
||||||
float tokbps (bpf_u_int32 bytes)
|
float tokbps (bpf_u_int32 bytes)
|
||||||
@@ -250,11 +250,12 @@ char * uid2username (int uid)
|
|||||||
class Line
|
class Line
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Line (const char * name, double n_sent_kbps, double n_recv_kbps, int pid, int uid)
|
Line (const char * name, double n_sent_kbps, double n_recv_kbps, int pid, int uid, const char * n_devicename)
|
||||||
{
|
{
|
||||||
m_name = name;
|
m_name = name;
|
||||||
sent_kbps = n_sent_kbps;
|
sent_kbps = n_sent_kbps;
|
||||||
recv_kbps = n_recv_kbps;
|
recv_kbps = n_recv_kbps;
|
||||||
|
devicename = n_devicename;
|
||||||
m_pid = pid;
|
m_pid = pid;
|
||||||
m_uid = uid;
|
m_uid = uid;
|
||||||
}
|
}
|
||||||
@@ -262,16 +263,17 @@ public:
|
|||||||
void show (int row)
|
void show (int row)
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
std::cout << m_name << "\t" << m_sent_kbps << "\t" << recv_kbps << std::endl;
|
std::cout << m_name << "\t" << sent_kbps << "\t" << recv_kbps << std::endl;
|
||||||
#else
|
#else
|
||||||
mvprintw (3+row, 0, "%d", m_pid);
|
mvprintw (3+row, 0, "%d", m_pid);
|
||||||
char * username = uid2username(m_uid);
|
char * username = uid2username(m_uid);
|
||||||
mvprintw (3+row, 6, "%s", username);
|
mvprintw (3+row, 6, "%s", username);
|
||||||
free (username);
|
free (username);
|
||||||
mvprintw (3+row, 6 + 9, "%s", m_name);
|
mvprintw (3+row, 6 + 9, "%s", m_name);
|
||||||
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2, "%10.3f", sent_kbps);
|
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2, "%s", devicename);
|
||||||
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 9 + 3, "%10.3f", recv_kbps);
|
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6, "%10.3f", sent_kbps);
|
||||||
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 9 + 3 + 11, "KB/sec", recv_kbps);
|
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3, "%10.3f", recv_kbps);
|
||||||
|
mvprintw (3+row, 6 + 9 + PROGNAME_WIDTH + 2 + 6 + 9 + 3 + 11, "KB/sec", recv_kbps);
|
||||||
// TODO fix
|
// TODO fix
|
||||||
//if(m_kbps-upload_kbps>upload_kbps)
|
//if(m_kbps-upload_kbps>upload_kbps)
|
||||||
// mvprintw (3+row, 6 + 20 + PROGNAME_WIDTH + 2, "<<<<");
|
// mvprintw (3+row, 6 + 20 + PROGNAME_WIDTH + 2, "<<<<");
|
||||||
@@ -284,6 +286,7 @@ public:
|
|||||||
double recv_kbps;
|
double recv_kbps;
|
||||||
private:
|
private:
|
||||||
const char * m_name;
|
const char * m_name;
|
||||||
|
const char * devicename;
|
||||||
int m_pid;
|
int m_pid;
|
||||||
int m_uid;
|
int m_uid;
|
||||||
};
|
};
|
||||||
@@ -322,7 +325,7 @@ void do_refresh()
|
|||||||
clear();
|
clear();
|
||||||
mvprintw (0, 0, "%s", caption->c_str());
|
mvprintw (0, 0, "%s", caption->c_str());
|
||||||
attron(A_REVERSE);
|
attron(A_REVERSE);
|
||||||
mvprintw (2, 0, " PID USER PROGRAM SENT RECEIVED ");
|
mvprintw (2, 0, " PID USER PROGRAM DEV SENT RECEIVED ");
|
||||||
attroff(A_REVERSE);
|
attroff(A_REVERSE);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -372,7 +375,7 @@ void do_refresh()
|
|||||||
sum_conn+=sum;
|
sum_conn+=sum;
|
||||||
curconn = curconn->getNext();
|
curconn = curconn->getNext();
|
||||||
}
|
}
|
||||||
lines[n] = new Line (curproc->getVal()->name, tokbps(sum_conn), tokbps(sum_connLocal), curproc->getVal()->pid, curproc->getVal()->uid);
|
lines[n] = new Line (curproc->getVal()->name, tokbps(sum_conn), tokbps(sum_connLocal), curproc->getVal()->pid, curproc->getVal()->uid, curproc->getVal()->devicename);
|
||||||
lastproc = curproc;
|
lastproc = curproc;
|
||||||
curproc = curproc->next;
|
curproc = curproc->next;
|
||||||
n++;
|
n++;
|
||||||
@@ -390,7 +393,7 @@ void do_refresh()
|
|||||||
|
|
||||||
/* returns the process from proclist with matching pid
|
/* returns the process from proclist with matching pid
|
||||||
* if none, creates it */
|
* if none, creates it */
|
||||||
Process * getProcess (unsigned long inode)
|
Process * getProcess (unsigned long inode, char * devicename)
|
||||||
{
|
{
|
||||||
struct prg_node * node = prg_cache_get(inode);
|
struct prg_node * node = prg_cache_get(inode);
|
||||||
|
|
||||||
@@ -411,7 +414,7 @@ Process * getProcess (unsigned long inode)
|
|||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
Process * newproc = new Process (inode);
|
Process * newproc = new Process (inode, strdup(devicename));
|
||||||
newproc->name = strdup(node->name);
|
newproc->name = strdup(node->name);
|
||||||
newproc->pid = node->pid;
|
newproc->pid = node->pid;
|
||||||
|
|
||||||
@@ -425,13 +428,17 @@ Process * getProcess (unsigned long inode)
|
|||||||
return newproc;
|
return newproc;
|
||||||
}
|
}
|
||||||
|
|
||||||
Process * getProcess (Connection * connection)
|
Process * getProcess (Connection * connection, char * devicename)
|
||||||
{
|
{
|
||||||
ProcList * curproc = processes;
|
ProcList * curproc = processes;
|
||||||
|
|
||||||
// see if we already know the inode for this connection
|
// see if we already know the inode for this connection
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
std::cout << "Connection reference packet found at " << connection->refpacket << std::endl;
|
{
|
||||||
|
std::cout << "Connection reference packet found at ";
|
||||||
|
std::cout << connection->refpacket << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long * inode = (unsigned long *) conninode->get(connection->refpacket->gethashstring());
|
unsigned long * inode = (unsigned long *) conninode->get(connection->refpacket->gethashstring());
|
||||||
|
|
||||||
if (inode == NULL)
|
if (inode == NULL)
|
||||||
@@ -451,7 +458,7 @@ Process * getProcess (Connection * connection)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Process * proc = getProcess(*inode);
|
Process * proc = getProcess(*inode, devicename);
|
||||||
proc->incoming = new ConnList (connection, proc->incoming);
|
proc->incoming = new ConnList (connection, proc->incoming);
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,10 +30,11 @@ private:
|
|||||||
class Process
|
class Process
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Process (unsigned long m_inode, char* m_name = NULL)
|
Process (unsigned long m_inode, char * m_devicename, char * m_name = NULL)
|
||||||
{
|
{
|
||||||
inode = m_inode;
|
inode = m_inode;
|
||||||
name = m_name;
|
name = m_name;
|
||||||
|
devicename = m_devicename;
|
||||||
incoming = NULL;
|
incoming = NULL;
|
||||||
outgoing = NULL;
|
outgoing = NULL;
|
||||||
}
|
}
|
||||||
@@ -56,6 +57,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const char * name;
|
const char * name;
|
||||||
|
const char * devicename;
|
||||||
int pid;
|
int pid;
|
||||||
int uid;
|
int uid;
|
||||||
|
|
||||||
@@ -64,7 +66,7 @@ public:
|
|||||||
ConnList * outgoing;
|
ConnList * outgoing;
|
||||||
};
|
};
|
||||||
|
|
||||||
Process * getProcess (Connection * connection);
|
Process * getProcess (Connection * connection, char * devicename = NULL);
|
||||||
void do_refresh ();
|
void do_refresh ();
|
||||||
|
|
||||||
void procclean ();
|
void procclean ();
|
||||||
|
|||||||
Reference in New Issue
Block a user