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 <string>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <netinet/ip.h>
|
||||
#include <netinet/ip6.h>
|
||||
@@ -27,7 +28,7 @@ extern "C" {
|
||||
#include "process.h"
|
||||
#include "refresh.h"
|
||||
|
||||
extern Process * unknownudp;
|
||||
extern Process * unknownudp;
|
||||
|
||||
unsigned refreshdelay = 1;
|
||||
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;
|
||||
|
||||
/* get info from userdata, then call getPacket */
|
||||
Packet * packet;
|
||||
Packet * packet;
|
||||
switch (args->sa_family)
|
||||
{
|
||||
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;
|
||||
|
||||
/* TODO get info from userdata, then call getPacket */
|
||||
Packet * packet;
|
||||
Packet * packet;
|
||||
switch (args->sa_family)
|
||||
{
|
||||
case (AF_INET):
|
||||
@@ -200,24 +201,19 @@ void quit_cb (int i)
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void forceExit(const char *msg)
|
||||
{
|
||||
forceExit (msg, NULL);
|
||||
}
|
||||
|
||||
void forceExit(const char *msg, const char* msg2)
|
||||
void forceExit(const char *msg, ...)
|
||||
{
|
||||
if ((!tracemode)&&(!DEBUG)){
|
||||
exit_ui();
|
||||
}
|
||||
std::cerr << msg;
|
||||
if (msg2 != NULL)
|
||||
{
|
||||
std::cerr << msg2;
|
||||
}
|
||||
|
||||
va_list argp;
|
||||
va_start(argp, msg);
|
||||
vfprintf(stderr, msg, argp);
|
||||
va_end(argp);
|
||||
std::cerr << std::endl;
|
||||
|
||||
exit(0);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static void versiondisplay(void)
|
||||
@@ -255,7 +251,7 @@ public:
|
||||
|
||||
class handle {
|
||||
public:
|
||||
handle (dp_handle * m_handle, char * m_devicename = NULL,
|
||||
handle (dp_handle * m_handle, char * m_devicename = NULL,
|
||||
handle * m_next = NULL) {
|
||||
content = m_handle; next = m_next; devicename = m_devicename;
|
||||
}
|
||||
@@ -317,7 +313,7 @@ int main (int argc, char** argv)
|
||||
}
|
||||
|
||||
if (devices == NULL)
|
||||
{
|
||||
{
|
||||
devices = new device (strdup("eth0"));
|
||||
}
|
||||
|
||||
@@ -339,7 +335,7 @@ int main (int argc, char** argv)
|
||||
//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)
|
||||
{
|
||||
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);
|
||||
|
||||
/* 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
|
||||
* also.
|
||||
* also.
|
||||
*/
|
||||
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");
|
||||
|
||||
// Main loop:
|
||||
//
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
bool packets_read = false;
|
||||
@@ -400,7 +396,7 @@ int main (int argc, char** argv)
|
||||
current_handle = current_handle->next;
|
||||
}
|
||||
|
||||
if ((!DEBUG)&&(!tracemode))
|
||||
if ((!DEBUG)&&(!tracemode))
|
||||
{
|
||||
// handle user input
|
||||
ui_tick();
|
||||
@@ -408,7 +404,7 @@ int main (int argc, char** argv)
|
||||
|
||||
if (needrefresh)
|
||||
{
|
||||
do_refresh();
|
||||
do_refresh();
|
||||
needrefresh = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,8 +47,7 @@
|
||||
|
||||
#define PROGNAME_WIDTH 27
|
||||
|
||||
void forceExit(const char *msg);
|
||||
void forceExit(const char *msg, const char* msg2);
|
||||
void forceExit(const char *msg, ...);
|
||||
|
||||
class local_addr {
|
||||
public:
|
||||
|
||||
68
packet.cpp
68
packet.cpp
@@ -16,7 +16,7 @@
|
||||
local_addr * local_addrs = NULL;
|
||||
|
||||
/* moves the pointer right until a non-space is seen */
|
||||
char * stripspaces (char * input)
|
||||
char * stripspaces (char * input)
|
||||
{
|
||||
char * retval = input;
|
||||
while (*retval == ' ')
|
||||
@@ -43,7 +43,7 @@ void getLocal (const char *device, bool tracemode)
|
||||
}
|
||||
strcpy(iFreq.ifr_name, device);
|
||||
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;
|
||||
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 */
|
||||
FILE * ifinfo = fopen ("/proc/net/if_inet6", "r");
|
||||
char buffer [500];
|
||||
if (ifinfo)
|
||||
if (ifinfo)
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -67,7 +67,7 @@ void getLocal (const char *device, bool tracemode)
|
||||
if (!ROBUST)
|
||||
assert (n_results = 2);
|
||||
|
||||
if (strcmp (stripspaces(ifname), device) == 0)
|
||||
if (strcmp (stripspaces(ifname), device) == 0)
|
||||
{
|
||||
local_addrs = new local_addr (address, local_addrs);
|
||||
}
|
||||
@@ -103,32 +103,32 @@ struct ppp_header {
|
||||
/* TCP header */
|
||||
// TODO take from elsewhere.
|
||||
struct tcp_hdr {
|
||||
u_short th_sport; /* source port */
|
||||
u_short th_dport; /* destination port */
|
||||
tcp_seq th_seq; /* sequence number */
|
||||
tcp_seq th_ack; /* acknowledgement number */
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
u_int th_x2:4, /* (unused) */
|
||||
th_off:4; /* data offset */
|
||||
#endif
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_int th_off:4, /* data offset */
|
||||
th_x2:4; /* (unused) */
|
||||
#endif
|
||||
u_char th_flags;
|
||||
#define TH_FIN 0x01
|
||||
#define TH_SYN 0x02
|
||||
#define TH_RST 0x04
|
||||
#define TH_PUSH 0x08
|
||||
#define TH_ACK 0x10
|
||||
#define TH_URG 0x20
|
||||
#define TH_ECE 0x40
|
||||
#define TH_CWR 0x80
|
||||
#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_sum; /* checksum */
|
||||
u_short th_urp; /* urgent pointer */
|
||||
};
|
||||
u_short th_sport; /* source port */
|
||||
u_short th_dport; /* destination port */
|
||||
tcp_seq th_seq; /* sequence number */
|
||||
tcp_seq th_ack; /* acknowledgement number */
|
||||
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||
u_int th_x2:4, /* (unused) */
|
||||
th_off:4; /* data offset */
|
||||
#endif
|
||||
#if BYTE_ORDER == BIG_ENDIAN
|
||||
u_int th_off:4, /* data offset */
|
||||
th_x2:4; /* (unused) */
|
||||
#endif
|
||||
u_char th_flags;
|
||||
#define TH_FIN 0x01
|
||||
#define TH_SYN 0x02
|
||||
#define TH_RST 0x04
|
||||
#define TH_PUSH 0x08
|
||||
#define TH_ACK 0x10
|
||||
#define TH_URG 0x20
|
||||
#define TH_ECE 0x40
|
||||
#define TH_CWR 0x80
|
||||
#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_sum; /* checksum */
|
||||
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)
|
||||
{
|
||||
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 */
|
||||
Packet::Packet (const Packet &old_packet) {
|
||||
sip = old_packet.sip; sport = old_packet.sport;
|
||||
sip6 = old_packet.sip6;
|
||||
dip6 = old_packet.dip6;
|
||||
sip6 = old_packet.sip6;
|
||||
dip6 = old_packet.dip6;
|
||||
dip = old_packet.dip; dport = old_packet.dport;
|
||||
len = old_packet.len; time = old_packet.time;
|
||||
sa_family = old_packet.sa_family;
|
||||
@@ -256,10 +256,10 @@ char * Packet::gethashstring ()
|
||||
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. */
|
||||
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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user