make 'forceexit' more fprintf-like, better error message when ioctl fails.
This commit is contained in:
44
nethogs.cpp
44
nethogs.cpp
@@ -10,6 +10,7 @@
|
|||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
#include <netinet/ip.h>
|
#include <netinet/ip.h>
|
||||||
#include <netinet/ip6.h>
|
#include <netinet/ip6.h>
|
||||||
@@ -27,7 +28,7 @@ extern "C" {
|
|||||||
#include "process.h"
|
#include "process.h"
|
||||||
#include "refresh.h"
|
#include "refresh.h"
|
||||||
|
|
||||||
extern Process * unknownudp;
|
extern Process * unknownudp;
|
||||||
|
|
||||||
unsigned refreshdelay = 1;
|
unsigned refreshdelay = 1;
|
||||||
bool tracemode = false;
|
bool tracemode = false;
|
||||||
@@ -91,7 +92,7 @@ int process_tcp (u_char * userdata, const dp_header * header, const u_char * m_p
|
|||||||
curtime = header->ts;
|
curtime = header->ts;
|
||||||
|
|
||||||
/* get info from userdata, then call getPacket */
|
/* get info from userdata, then call getPacket */
|
||||||
Packet * packet;
|
Packet * packet;
|
||||||
switch (args->sa_family)
|
switch (args->sa_family)
|
||||||
{
|
{
|
||||||
case (AF_INET):
|
case (AF_INET):
|
||||||
@@ -133,7 +134,7 @@ int process_udp (u_char * userdata, const dp_header * header, const u_char * m_p
|
|||||||
curtime = header->ts;
|
curtime = header->ts;
|
||||||
|
|
||||||
/* TODO get info from userdata, then call getPacket */
|
/* TODO get info from userdata, then call getPacket */
|
||||||
Packet * packet;
|
Packet * packet;
|
||||||
switch (args->sa_family)
|
switch (args->sa_family)
|
||||||
{
|
{
|
||||||
case (AF_INET):
|
case (AF_INET):
|
||||||
@@ -200,24 +201,19 @@ void quit_cb (int i)
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void forceExit(const char *msg)
|
void forceExit(const char *msg, ...)
|
||||||
{
|
|
||||||
forceExit (msg, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void forceExit(const char *msg, const char* msg2)
|
|
||||||
{
|
{
|
||||||
if ((!tracemode)&&(!DEBUG)){
|
if ((!tracemode)&&(!DEBUG)){
|
||||||
exit_ui();
|
exit_ui();
|
||||||
}
|
}
|
||||||
std::cerr << msg;
|
|
||||||
if (msg2 != NULL)
|
va_list argp;
|
||||||
{
|
va_start(argp, msg);
|
||||||
std::cerr << msg2;
|
vfprintf(stderr, msg, argp);
|
||||||
}
|
va_end(argp);
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void versiondisplay(void)
|
static void versiondisplay(void)
|
||||||
@@ -255,7 +251,7 @@ public:
|
|||||||
|
|
||||||
class handle {
|
class handle {
|
||||||
public:
|
public:
|
||||||
handle (dp_handle * m_handle, char * m_devicename = NULL,
|
handle (dp_handle * m_handle, char * m_devicename = NULL,
|
||||||
handle * m_next = NULL) {
|
handle * m_next = NULL) {
|
||||||
content = m_handle; next = m_next; devicename = m_devicename;
|
content = m_handle; next = m_next; devicename = m_devicename;
|
||||||
}
|
}
|
||||||
@@ -317,7 +313,7 @@ int main (int argc, char** argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (devices == NULL)
|
if (devices == NULL)
|
||||||
{
|
{
|
||||||
devices = new device (strdup("eth0"));
|
devices = new device (strdup("eth0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,7 +335,7 @@ int main (int argc, char** argv)
|
|||||||
//caption->append(" ");
|
//caption->append(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
dp_handle * newhandle = dp_open_live(current_dev->name, BUFSIZ, promisc, 100, errbuf);
|
dp_handle * newhandle = dp_open_live(current_dev->name, BUFSIZ, promisc, 100, errbuf);
|
||||||
if (newhandle != NULL)
|
if (newhandle != NULL)
|
||||||
{
|
{
|
||||||
dp_addcb (newhandle, dp_packet_ip, process_ip);
|
dp_addcb (newhandle, dp_packet_ip, process_ip);
|
||||||
@@ -348,10 +344,10 @@ int main (int argc, char** argv)
|
|||||||
dp_addcb (newhandle, dp_packet_udp, process_udp);
|
dp_addcb (newhandle, dp_packet_udp, process_udp);
|
||||||
|
|
||||||
/* The following code solves sf.net bug 1019381, but is only available
|
/* The following code solves sf.net bug 1019381, but is only available
|
||||||
* in newer versions (from 0.8 it seems) of libpcap
|
* in newer versions (from 0.8 it seems) of libpcap
|
||||||
*
|
*
|
||||||
* update: version 0.7.2, which is in debian stable now, should be ok
|
* update: version 0.7.2, which is in debian stable now, should be ok
|
||||||
* also.
|
* also.
|
||||||
*/
|
*/
|
||||||
if (dp_setnonblock (newhandle, 1, errbuf) == -1)
|
if (dp_setnonblock (newhandle, 1, errbuf) == -1)
|
||||||
{
|
{
|
||||||
@@ -374,9 +370,9 @@ int main (int argc, char** argv)
|
|||||||
fprintf(stderr, "Waiting for first packet to arrive (see sourceforge.net bug 1019381)\n");
|
fprintf(stderr, "Waiting for first packet to arrive (see sourceforge.net bug 1019381)\n");
|
||||||
|
|
||||||
// Main loop:
|
// Main loop:
|
||||||
//
|
//
|
||||||
// Walks though the 'handles' list, which contains handles opened in non-blocking mode.
|
// Walks though the 'handles' list, which contains handles opened in non-blocking mode.
|
||||||
// This causes the CPU utilisation to go up to 100%. This is tricky:
|
// This causes the CPU utilisation to go up to 100%. This is tricky:
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
bool packets_read = false;
|
bool packets_read = false;
|
||||||
@@ -400,7 +396,7 @@ int main (int argc, char** argv)
|
|||||||
current_handle = current_handle->next;
|
current_handle = current_handle->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((!DEBUG)&&(!tracemode))
|
if ((!DEBUG)&&(!tracemode))
|
||||||
{
|
{
|
||||||
// handle user input
|
// handle user input
|
||||||
ui_tick();
|
ui_tick();
|
||||||
@@ -408,7 +404,7 @@ int main (int argc, char** argv)
|
|||||||
|
|
||||||
if (needrefresh)
|
if (needrefresh)
|
||||||
{
|
{
|
||||||
do_refresh();
|
do_refresh();
|
||||||
needrefresh = false;
|
needrefresh = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,8 +47,7 @@
|
|||||||
|
|
||||||
#define PROGNAME_WIDTH 27
|
#define PROGNAME_WIDTH 27
|
||||||
|
|
||||||
void forceExit(const char *msg);
|
void forceExit(const char *msg, ...);
|
||||||
void forceExit(const char *msg, const char* msg2);
|
|
||||||
|
|
||||||
class local_addr {
|
class local_addr {
|
||||||
public:
|
public:
|
||||||
|
|||||||
68
packet.cpp
68
packet.cpp
@@ -16,7 +16,7 @@
|
|||||||
local_addr * local_addrs = NULL;
|
local_addr * local_addrs = NULL;
|
||||||
|
|
||||||
/* moves the pointer right until a non-space is seen */
|
/* moves the pointer right until a non-space is seen */
|
||||||
char * stripspaces (char * input)
|
char * stripspaces (char * input)
|
||||||
{
|
{
|
||||||
char * retval = input;
|
char * retval = input;
|
||||||
while (*retval == ' ')
|
while (*retval == ' ')
|
||||||
@@ -43,7 +43,7 @@ void getLocal (const char *device, bool tracemode)
|
|||||||
}
|
}
|
||||||
strcpy(iFreq.ifr_name, device);
|
strcpy(iFreq.ifr_name, device);
|
||||||
if(ioctl(sock, SIOCGIFADDR, &iFreq)<0){
|
if(ioctl(sock, SIOCGIFADDR, &iFreq)<0){
|
||||||
forceExit("ioctl failed while establishing local IP for selected device ", device);
|
forceExit("ioctl failed while establishing local IP for selected device %s. You may specify the device on the command line.", device);
|
||||||
}
|
}
|
||||||
saddr=(struct sockaddr_in*)&iFreq.ifr_addr;
|
saddr=(struct sockaddr_in*)&iFreq.ifr_addr;
|
||||||
local_addrs = new local_addr (saddr->sin_addr.s_addr, local_addrs);
|
local_addrs = new local_addr (saddr->sin_addr.s_addr, local_addrs);
|
||||||
@@ -55,7 +55,7 @@ void getLocal (const char *device, bool tracemode)
|
|||||||
/* also get local IPv6 addresses */
|
/* also get local IPv6 addresses */
|
||||||
FILE * ifinfo = fopen ("/proc/net/if_inet6", "r");
|
FILE * ifinfo = fopen ("/proc/net/if_inet6", "r");
|
||||||
char buffer [500];
|
char buffer [500];
|
||||||
if (ifinfo)
|
if (ifinfo)
|
||||||
{
|
{
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -67,7 +67,7 @@ void getLocal (const char *device, bool tracemode)
|
|||||||
if (!ROBUST)
|
if (!ROBUST)
|
||||||
assert (n_results = 2);
|
assert (n_results = 2);
|
||||||
|
|
||||||
if (strcmp (stripspaces(ifname), device) == 0)
|
if (strcmp (stripspaces(ifname), device) == 0)
|
||||||
{
|
{
|
||||||
local_addrs = new local_addr (address, local_addrs);
|
local_addrs = new local_addr (address, local_addrs);
|
||||||
}
|
}
|
||||||
@@ -103,32 +103,32 @@ struct ppp_header {
|
|||||||
/* TCP header */
|
/* TCP header */
|
||||||
// TODO take from elsewhere.
|
// TODO take from elsewhere.
|
||||||
struct tcp_hdr {
|
struct tcp_hdr {
|
||||||
u_short th_sport; /* source port */
|
u_short th_sport; /* source port */
|
||||||
u_short th_dport; /* destination port */
|
u_short th_dport; /* destination port */
|
||||||
tcp_seq th_seq; /* sequence number */
|
tcp_seq th_seq; /* sequence number */
|
||||||
tcp_seq th_ack; /* acknowledgement number */
|
tcp_seq th_ack; /* acknowledgement number */
|
||||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||||
u_int th_x2:4, /* (unused) */
|
u_int th_x2:4, /* (unused) */
|
||||||
th_off:4; /* data offset */
|
th_off:4; /* data offset */
|
||||||
#endif
|
#endif
|
||||||
#if BYTE_ORDER == BIG_ENDIAN
|
#if BYTE_ORDER == BIG_ENDIAN
|
||||||
u_int th_off:4, /* data offset */
|
u_int th_off:4, /* data offset */
|
||||||
th_x2:4; /* (unused) */
|
th_x2:4; /* (unused) */
|
||||||
#endif
|
#endif
|
||||||
u_char th_flags;
|
u_char th_flags;
|
||||||
#define TH_FIN 0x01
|
#define TH_FIN 0x01
|
||||||
#define TH_SYN 0x02
|
#define TH_SYN 0x02
|
||||||
#define TH_RST 0x04
|
#define TH_RST 0x04
|
||||||
#define TH_PUSH 0x08
|
#define TH_PUSH 0x08
|
||||||
#define TH_ACK 0x10
|
#define TH_ACK 0x10
|
||||||
#define TH_URG 0x20
|
#define TH_URG 0x20
|
||||||
#define TH_ECE 0x40
|
#define TH_ECE 0x40
|
||||||
#define TH_CWR 0x80
|
#define TH_CWR 0x80
|
||||||
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
|
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
|
||||||
u_short th_win; /* window */
|
u_short th_win; /* window */
|
||||||
u_short th_sum; /* checksum */
|
u_short th_sum; /* checksum */
|
||||||
u_short th_urp; /* urgent pointer */
|
u_short th_urp; /* urgent pointer */
|
||||||
};
|
};
|
||||||
Packet::Packet (in_addr m_sip, unsigned short m_sport, in_addr m_dip, unsigned short m_dport, u_int32_t m_len, timeval m_time, direction m_dir)
|
Packet::Packet (in_addr m_sip, unsigned short m_sport, in_addr m_dip, unsigned short m_dport, u_int32_t m_len, timeval m_time, direction m_dir)
|
||||||
{
|
{
|
||||||
sip = m_sip; sport = m_sport;
|
sip = m_sip; sport = m_sport;
|
||||||
@@ -158,8 +158,8 @@ Packet * Packet::newInverted () {
|
|||||||
/* constructs returns a new Packet() structure with the same contents as this one */
|
/* constructs returns a new Packet() structure with the same contents as this one */
|
||||||
Packet::Packet (const Packet &old_packet) {
|
Packet::Packet (const Packet &old_packet) {
|
||||||
sip = old_packet.sip; sport = old_packet.sport;
|
sip = old_packet.sip; sport = old_packet.sport;
|
||||||
sip6 = old_packet.sip6;
|
sip6 = old_packet.sip6;
|
||||||
dip6 = old_packet.dip6;
|
dip6 = old_packet.dip6;
|
||||||
dip = old_packet.dip; dport = old_packet.dport;
|
dip = old_packet.dip; dport = old_packet.dport;
|
||||||
len = old_packet.len; time = old_packet.time;
|
len = old_packet.len; time = old_packet.time;
|
||||||
sa_family = old_packet.sa_family;
|
sa_family = old_packet.sa_family;
|
||||||
@@ -256,10 +256,10 @@ char * Packet::gethashstring ()
|
|||||||
return hashstring;
|
return hashstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 2 packets match if they have the same
|
/* 2 packets match if they have the same
|
||||||
* source and destination ports and IP's. */
|
* source and destination ports and IP's. */
|
||||||
bool Packet::match (Packet * other)
|
bool Packet::match (Packet * other)
|
||||||
{
|
{
|
||||||
return (sport == other->sport) && (dport == other->dport)
|
return (sport == other->sport) && (dport == other->dport)
|
||||||
&& (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip));
|
&& (sameinaddr(sip, other->sip)) && (sameinaddr(dip, other->dip));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user