From ff8151fe96522a1b26ff806b5a7ef3e095b48094 Mon Sep 17 00:00:00 2001 From: Arnout Engelen Date: Tue, 12 Jul 2011 21:53:54 +0000 Subject: [PATCH] some 'const' specifications, putting the code to determine the default device in its own file --- Makefile | 4 +++- decpcap.c | 2 +- decpcap.h | 2 +- decpcap_test.cpp | 2 +- devices.cpp | 7 +++++++ devices.h | 13 +++++++++++++ nethogs.cpp | 25 ++++++++----------------- process.cpp | 4 ++-- process.h | 2 +- 9 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 devices.cpp create mode 100644 devices.h diff --git a/Makefile b/Makefile index e72bc5e..2efe5e8 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ all: nethogs decpcap_test CFLAGS=-g -Wall -Wextra #CFLAGS=-O2 -OBJS=packet.o connection.o process.o refresh.o decpcap.o cui.o inode2prog.o conninode.o +OBJS=packet.o connection.o process.o refresh.o decpcap.o cui.o inode2prog.o conninode.o devices.o .PHONY: tgz tgz: clean @@ -53,6 +53,8 @@ inode2prog.o: inode2prog.cpp inode2prog.h nethogs.h $(CXX) $(CFLAGS) -c inode2prog.cpp conninode.o: conninode.cpp nethogs.h conninode.h $(CXX) $(CFLAGS) -c conninode.cpp +#devices.o: devices.cpp devices.h +# $(CXX) $(CFLAGS) -c devices.cpp cui.o: cui.cpp cui.h nethogs.h $(CXX) $(CFLAGS) -c cui.cpp -DVERSION=\"$(VERSION)\" -DSUBVERSION=\"$(SUBVERSION)\" -DMINORVERSION=\"$(MINORVERSION)\" diff --git a/decpcap.c b/decpcap.c index 4194875..72a522d 100644 --- a/decpcap.c +++ b/decpcap.c @@ -55,7 +55,7 @@ struct dp_handle * dp_open_offline(char * fname, char * ebuf) return dp_fillhandle(temp); } -struct dp_handle * dp_open_live(char * device, int snaplen, int promisc, int to_ms, char * ebuf) +struct dp_handle * dp_open_live(const char * device, int snaplen, int promisc, int to_ms, const char * ebuf) { pcap_t * temp = pcap_open_live(device, snaplen, promisc, to_ms, ebuf); diff --git a/decpcap.h b/decpcap.h index 70475f8..b7b3684 100644 --- a/decpcap.h +++ b/decpcap.h @@ -40,7 +40,7 @@ struct dp_handle { /* functions to set up a handle (which is basically just a pcap handle) */ -struct dp_handle * dp_open_live(char * device, int snaplen, int promisc, int to_ms, char * ebuf); +struct dp_handle * dp_open_live(const char * device, int snaplen, int promisc, int to_ms, const char * ebuf); struct dp_handle * dp_open_offline(char * fname, char * ebuf); /* functions to add callbacks */ diff --git a/decpcap_test.cpp b/decpcap_test.cpp index 371021f..8657561 100644 --- a/decpcap_test.cpp +++ b/decpcap_test.cpp @@ -4,7 +4,7 @@ extern "C" { #include "decpcap.h" } -int process_tcp (u_char * userdata, const dp_header * header, const u_char * m_packet) { +int process_tcp (u_char * /* userdata */, const dp_header * /* header */, const u_char * /* m_packet */) { std::cout << "Callback for processing TCP packet called" << std::endl; return 0; } diff --git a/devices.cpp b/devices.cpp new file mode 100644 index 0000000..f8e0ae7 --- /dev/null +++ b/devices.cpp @@ -0,0 +1,7 @@ +#include "devices.h" + +device * determine_default_device() +{ + return new device("eth0"); +} + diff --git a/devices.h b/devices.h new file mode 100644 index 0000000..7971a2c --- /dev/null +++ b/devices.h @@ -0,0 +1,13 @@ +#include // NULL + +class device { +public: + device (const char * m_name, device * m_next = NULL) + { + name = m_name; next = m_next; + } + const char * name; + device * next; +}; + +device * determine_default_device(); diff --git a/nethogs.cpp b/nethogs.cpp index aafb482..0fbd063 100644 --- a/nethogs.cpp +++ b/nethogs.cpp @@ -28,6 +28,7 @@ extern "C" { #include "connection.h" #include "process.h" #include "refresh.h" +#include "devices.h" extern Process * unknownudp; @@ -39,7 +40,7 @@ bool needrefresh = true; //dp_link_type linktype = dp_link_ethernet; const char version[] = " version " VERSION "." SUBVERSION "." MINORVERSION; -char * currentdevice = NULL; +const char * currentdevice = NULL; timeval curtime; @@ -172,7 +173,7 @@ int process_udp (u_char * userdata, const dp_header * header, const u_char * m_p return true; } -int process_ip (u_char * userdata, const dp_header * header, const u_char * m_packet) { +int process_ip (u_char * userdata, const dp_header * /* header */, const u_char * m_packet) { struct dpargs * args = (struct dpargs *) userdata; struct ip * ip = (struct ip *) m_packet; args->sa_family = AF_INET; @@ -183,7 +184,7 @@ int process_ip (u_char * userdata, const dp_header * header, const u_char * m_pa return false; } -int process_ip6 (u_char * userdata, const dp_header * header, const u_char * m_packet) { +int process_ip6 (u_char * userdata, const dp_header * /* header */, const u_char * m_packet) { struct dpargs * args = (struct dpargs *) userdata; const struct ip6_hdr * ip6 = (struct ip6_hdr *) m_packet; args->sa_family = AF_INET6; @@ -194,7 +195,7 @@ int process_ip6 (u_char * userdata, const dp_header * header, const u_char * m_p return false; } -void quit_cb (int i) +void quit_cb (int /* i */) { procclean(); if ((!tracemode) && (!DEBUG)) @@ -240,24 +241,14 @@ static void help(void) } -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 (dp_handle * m_handle, char * m_devicename = NULL, + handle (dp_handle * m_handle, const char * m_devicename = NULL, handle * m_next = NULL) { content = m_handle; next = m_next; devicename = m_devicename; } dp_handle * content; - char * devicename; + const char * devicename; handle * next; }; @@ -313,7 +304,7 @@ int main (int argc, char** argv) if (devices == NULL) { - devices = new device (strdup("eth0")); + devices = determine_default_device(); } if ((!tracemode) && (!DEBUG)){ diff --git a/process.cpp b/process.cpp index cedf7ea..cef744e 100644 --- a/process.cpp +++ b/process.cpp @@ -174,7 +174,7 @@ void check_all_procs () * if the inode is not associated with any PID, return the unknown process * if the process is not yet in the proclist, add it */ -Process * getProcess (unsigned long inode, char * devicename) +Process * getProcess (unsigned long inode, const char * devicename) { struct prg_node * node = findPID(inode); @@ -232,7 +232,7 @@ Process * getProcess (unsigned long inode, char * devicename) * is made. If no process can be found even then, it's added to the * 'unknown' process. */ -Process * getProcess (Connection * connection, char * devicename) +Process * getProcess (Connection * connection, const char * devicename) { unsigned long inode = conninode[connection->refpacket->gethashstring()]; diff --git a/process.h b/process.h index 83a92d0..a313125 100644 --- a/process.h +++ b/process.h @@ -109,7 +109,7 @@ private: Process * val; }; -Process * getProcess (Connection * connection, char * devicename = NULL); +Process * getProcess (Connection * connection, const char * devicename = NULL); void process_init ();