Commit 1388f3ad by chris

""

1 parent 3004ea06
......@@ -2,7 +2,7 @@
#CC = gcc
CFLAGS += -g -Wall -I/usr/local/include
CFLAGS += -g -Wall -I/usr/include/pcap
LDFLAGS += -g
LDLIBS += -L/usr/local/lib -lpcap -lpthread -lcurses
......@@ -13,6 +13,7 @@ SRCS = iftop.c \
ns_hash.c \
resolver.c \
ui.c \
util.c \
sorted_list.c
OBJS = $(SRCS:.c=.o)
......@@ -43,4 +44,139 @@ nodepend:
# DO NOT DELETE
iftop.o: /usr/include/stdio.h /usr/include/features.h
iftop.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
iftop.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
iftop.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
iftop.o: /usr/include/bits/types.h /usr/include/libio.h
iftop.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
iftop.o: /usr/include/stdlib.h /usr/include/sys/types.h /usr/include/time.h
iftop.o: /usr/include/endian.h /usr/include/bits/endian.h
iftop.o: /usr/include/sys/select.h /usr/include/bits/select.h
iftop.o: /usr/include/bits/sigset.h /usr/include/sys/sysmacros.h
iftop.o: /usr/include/alloca.h /usr/include/net/ethernet.h
iftop.o: /usr/include/linux/if_ether.h /usr/include/netinet/ip.h
iftop.o: /usr/include/netinet/in.h /usr/include/limits.h
iftop.o: /usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h
iftop.o: /usr/include/linux/limits.h /usr/include/bits/posix2_lim.h
iftop.o: /usr/include/stdint.h /usr/include/bits/wordsize.h
iftop.o: /usr/include/bits/socket.h /usr/include/bits/sockaddr.h
iftop.o: /usr/include/asm/socket.h /usr/include/asm/sockios.h
iftop.o: /usr/include/bits/in.h /usr/include/bits/byteswap.h
iftop.o: /usr/include/pthread.h /usr/include/sched.h
iftop.o: /usr/include/bits/sched.h /usr/include/signal.h
iftop.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sigthread.h
iftop.o: /usr/include/curses.h /usr/include/ncurses/unctrl.h
iftop.o: /usr/include/ncurses/curses.h /usr/include/string.h iftop.h
iftop.o: addr_hash.h /usr/include/sys/socket.h /usr/include/arpa/inet.h
iftop.o: hash.h resolver.h ui.h
addr_hash.o: /usr/include/stdio.h /usr/include/features.h
addr_hash.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
addr_hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
addr_hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
addr_hash.o: /usr/include/bits/types.h /usr/include/libio.h
addr_hash.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
addr_hash.o: /usr/include/stdlib.h /usr/include/sys/types.h
addr_hash.o: /usr/include/time.h /usr/include/endian.h
addr_hash.o: /usr/include/bits/endian.h /usr/include/sys/select.h
addr_hash.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
addr_hash.o: /usr/include/sys/sysmacros.h /usr/include/alloca.h addr_hash.h
addr_hash.o: /usr/include/sys/socket.h /usr/include/bits/socket.h
addr_hash.o: /usr/include/limits.h /usr/include/bits/posix1_lim.h
addr_hash.o: /usr/include/bits/local_lim.h /usr/include/linux/limits.h
addr_hash.o: /usr/include/bits/posix2_lim.h /usr/include/bits/sockaddr.h
addr_hash.o: /usr/include/asm/socket.h /usr/include/asm/sockios.h
addr_hash.o: /usr/include/netinet/in.h /usr/include/stdint.h
addr_hash.o: /usr/include/bits/wordsize.h /usr/include/bits/in.h
addr_hash.o: /usr/include/bits/byteswap.h /usr/include/arpa/inet.h hash.h
hash.o: /usr/include/stdio.h /usr/include/features.h /usr/include/sys/cdefs.h
hash.o: /usr/include/gnu/stubs.h
hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
hash.o: /usr/include/bits/types.h /usr/include/libio.h
hash.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
hash.o: /usr/include/stdlib.h /usr/include/sys/types.h /usr/include/time.h
hash.o: /usr/include/endian.h /usr/include/bits/endian.h
hash.o: /usr/include/sys/select.h /usr/include/bits/select.h
hash.o: /usr/include/bits/sigset.h /usr/include/sys/sysmacros.h
hash.o: /usr/include/alloca.h hash.h
ns_hash.o: /usr/include/stdio.h /usr/include/features.h
ns_hash.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
ns_hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
ns_hash.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
ns_hash.o: /usr/include/bits/types.h /usr/include/libio.h
ns_hash.o: /usr/include/_G_config.h /usr/include/bits/stdio_lim.h
ns_hash.o: /usr/include/stdlib.h /usr/include/sys/types.h /usr/include/time.h
ns_hash.o: /usr/include/endian.h /usr/include/bits/endian.h
ns_hash.o: /usr/include/sys/select.h /usr/include/bits/select.h
ns_hash.o: /usr/include/bits/sigset.h /usr/include/sys/sysmacros.h
ns_hash.o: /usr/include/alloca.h /usr/include/sys/socket.h
ns_hash.o: /usr/include/bits/socket.h /usr/include/limits.h
ns_hash.o: /usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h
ns_hash.o: /usr/include/linux/limits.h /usr/include/bits/posix2_lim.h
ns_hash.o: /usr/include/bits/sockaddr.h /usr/include/asm/socket.h
ns_hash.o: /usr/include/asm/sockios.h /usr/include/netinet/in.h
ns_hash.o: /usr/include/stdint.h /usr/include/bits/wordsize.h
ns_hash.o: /usr/include/bits/in.h /usr/include/bits/byteswap.h
ns_hash.o: /usr/include/arpa/inet.h ns_hash.h hash.h
resolver.o: /usr/include/netinet/in.h /usr/include/features.h
resolver.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
resolver.o: /usr/include/limits.h /usr/include/bits/posix1_lim.h
resolver.o: /usr/include/bits/local_lim.h /usr/include/linux/limits.h
resolver.o: /usr/include/bits/posix2_lim.h /usr/include/stdint.h
resolver.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
resolver.o: /usr/include/bits/wordsize.h /usr/include/sys/types.h
resolver.o: /usr/include/bits/types.h /usr/include/time.h
resolver.o: /usr/include/endian.h /usr/include/bits/endian.h
resolver.o: /usr/include/sys/select.h /usr/include/bits/select.h
resolver.o: /usr/include/bits/sigset.h /usr/include/sys/sysmacros.h
resolver.o: /usr/include/bits/socket.h /usr/include/bits/sockaddr.h
resolver.o: /usr/include/asm/socket.h /usr/include/asm/sockios.h
resolver.o: /usr/include/bits/in.h /usr/include/bits/byteswap.h
resolver.o: /usr/include/sys/socket.h /usr/include/arpa/inet.h
resolver.o: /usr/include/pthread.h /usr/include/sched.h
resolver.o: /usr/include/bits/sched.h /usr/include/signal.h
resolver.o: /usr/include/bits/pthreadtypes.h /usr/include/bits/sigthread.h
resolver.o: /usr/include/stdio.h
resolver.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
resolver.o: /usr/include/libio.h /usr/include/_G_config.h
resolver.o: /usr/include/bits/stdio_lim.h /usr/include/stdlib.h
resolver.o: /usr/include/alloca.h /usr/include/netdb.h
resolver.o: /usr/include/rpc/netdb.h /usr/include/errno.h
resolver.o: /usr/include/bits/errno.h /usr/include/linux/errno.h
resolver.o: /usr/include/asm/errno.h /usr/include/string.h ns_hash.h hash.h
ui.o: /usr/include/curses.h /usr/include/stdio.h /usr/include/features.h
ui.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
ui.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
ui.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
ui.o: /usr/include/bits/types.h /usr/include/libio.h /usr/include/_G_config.h
ui.o: /usr/include/bits/stdio_lim.h /usr/include/ncurses/unctrl.h
ui.o: /usr/include/ncurses/curses.h /usr/include/string.h
ui.o: /usr/include/pthread.h /usr/include/sched.h /usr/include/time.h
ui.o: /usr/include/bits/sched.h /usr/include/signal.h
ui.o: /usr/include/bits/sigset.h /usr/include/bits/pthreadtypes.h
ui.o: /usr/include/bits/sigthread.h /usr/include/stdlib.h
ui.o: /usr/include/sys/types.h /usr/include/endian.h
ui.o: /usr/include/bits/endian.h /usr/include/sys/select.h
ui.o: /usr/include/bits/select.h /usr/include/sys/sysmacros.h
ui.o: /usr/include/alloca.h addr_hash.h /usr/include/sys/socket.h
ui.o: /usr/include/bits/socket.h /usr/include/limits.h
ui.o: /usr/include/bits/posix1_lim.h /usr/include/bits/local_lim.h
ui.o: /usr/include/linux/limits.h /usr/include/bits/posix2_lim.h
ui.o: /usr/include/bits/sockaddr.h /usr/include/asm/socket.h
ui.o: /usr/include/asm/sockios.h /usr/include/netinet/in.h
ui.o: /usr/include/stdint.h /usr/include/bits/wordsize.h
ui.o: /usr/include/bits/in.h /usr/include/bits/byteswap.h
ui.o: /usr/include/arpa/inet.h hash.h iftop.h resolver.h sorted_list.h
sorted_list.o: /usr/include/stdlib.h /usr/include/features.h
sorted_list.o: /usr/include/sys/cdefs.h /usr/include/gnu/stubs.h
sorted_list.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stddef.h
sorted_list.o: /usr/include/sys/types.h /usr/include/bits/types.h
sorted_list.o: /usr/include/time.h /usr/include/endian.h
sorted_list.o: /usr/include/bits/endian.h /usr/include/sys/select.h
sorted_list.o: /usr/include/bits/select.h /usr/include/bits/sigset.h
sorted_list.o: /usr/include/sys/sysmacros.h /usr/include/alloca.h
sorted_list.o: /usr/include/stdio.h
sorted_list.o: /usr/lib/gcc-lib/i386-redhat-linux/egcs-2.91.66/include/stdarg.h
sorted_list.o: /usr/include/libio.h /usr/include/_G_config.h
sorted_list.o: /usr/include/bits/stdio_lim.h sorted_list.h
......@@ -4,6 +4,7 @@
#include <stdlib.h>
#include "addr_hash.h"
#include "hash.h"
#include "iftop.h"
#define hash_table_size 256
......@@ -36,11 +37,8 @@ int hash(void* key) {
}
void* copy_key(void* orig) {
addr_pair* copy = malloc(sizeof(addr_pair));
if(copy == NULL) {
printf("Out of memory\n");
exit(1);
}
addr_pair* copy;
copy = xmalloc(sizeof *copy);
*copy = *(addr_pair*)orig;
return copy;
}
......@@ -54,10 +52,7 @@ void delete_key(void* key) {
*/
hash_type* addr_hash_create() {
hash_type* hash_table;
if ((hash_table = calloc(hash_table_size, sizeof(hash_type*))) == 0) {
fprintf (stderr, "out of memory (hashTable)\n");
exit(1);
}
hash_table = xcalloc(hash_table_size, sizeof *hash_table);
hash_table->size = hash_table_size;
hash_table->compare = &compare;
hash_table->hash = &hash;
......
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "hash.h"
#include "iftop.h"
hash_status_enum hash_insert(hash_type* hash_table, void* key, void* rec) {
hash_node_type *p, *p0;
......@@ -15,8 +16,7 @@ hash_status_enum hash_insert(hash_type* hash_table, void* key, void* rec) {
/* insert node at beginning of list */
bucket = hash_table->hash(key);
if ((p = malloc(sizeof(hash_node_type))) == 0)
return HASH_STATUS_MEM_EXHAUSTED;
p = xmalloc(sizeof *p);
p0 = hash_table->table[bucket];
hash_table->table[bucket] = p;
p->next = p0;
......@@ -98,10 +98,7 @@ hash_status_enum hash_next_item(hash_type* hash_table, hash_node_type** ppnode)
* Allocate and return a hash
*/
hash_status_enum hash_initialise(hash_type* hash_table) {
if ((hash_table->table = calloc(hash_table->size, sizeof(hash_node_type *))) == 0) {
fprintf (stderr, "out of memory (hash_table)\n");
return HASH_STATUS_MEM_EXHAUSTED;
}
hash_table->table = xcalloc(hash_table->size, sizeof *hash_table->table);
return HASH_STATUS_OK;
}
......
......@@ -38,10 +38,7 @@ void init_history() {
history_type* history_create() {
history_type* h;
if ((h = calloc(1, sizeof(history_type))) == 0) {
fprintf (stderr, "out of memory (history_type)\n");
exit(1);
}
h = xcalloc(1, sizeof *h);
return h;
}
......@@ -135,6 +132,7 @@ void packet_loop(void* ptr) {
char errbuf[PCAP_ERRBUF_SIZE];
char* device;
pcap_t* pd;
struct bpf_program F;
resolver_initialise();
......@@ -142,25 +140,36 @@ void packet_loop(void* ptr) {
printf("Device: %s\n",device);
pd = pcap_open_live(device,CAPTURE_LENGTH,1,1000,errbuf);
if(pd == NULL) {
printf("pcap_open_live(): %s\n",errbuf);
fprintf(stderr, "pcap_open_live(): %s\n",errbuf);
exit(1);
}
if (pcap_compile(pd, &F, "ip", 1, 0) == -1) {
fprintf(stderr, "pcap_compile: %s\n", pcap_geterr(pd));
exit(1);
}
if (pcap_setfilter(pd, &F) == -1) {
fprintf(stderr, "pcap_setfilter: %s\n", pcap_geterr(pd));
exit(1);
}
printf("Begin loop\n");
init_history();
pcap_loop(pd,0,(pcap_handler)handle_packet,NULL);
printf("end loop\n");
}
sig_atomic_t foad;
static void finish(int sig)
{
ui_finish();
exit(0);
foad = sig;
}
int main(int argc, char **argv) {
pthread_t thread;
struct sigaction sa = {0};
sa.sa_handler = finish;
(void) signal(SIGINT, finish); /* arrange interrupts to terminate */
sigaction(SIGINT, &sa, NULL);
pthread_mutex_init(&tick_mutex, NULL);
......@@ -168,5 +177,9 @@ int main(int argc, char **argv) {
ui_loop();
pthread_cancel(thread);
ui_finish();
return 0;
}
......@@ -18,4 +18,10 @@ typedef struct {
void tick();
void *xmalloc(size_t n);
void *xcalloc(size_t n, size_t m);
void *xrealloc(void *w, size_t n);
char *xstrdup(const char *s);
void xfree(void *v);
#endif /* __IFTOP_H_ */
......@@ -7,6 +7,7 @@
#include <arpa/inet.h>
#include "ns_hash.h"
#include "hash.h"
#include "iftop.h"
#define hash_table_size 256
......@@ -31,11 +32,8 @@ int ns_hash_hash(void* key) {
}
void* ns_hash_copy_key(void* orig) {
struct in_addr* copy = malloc(sizeof(struct in_addr));
if(copy == NULL) {
printf("Out of memory\n");
exit(1);
}
struct in_addr* copy;
copy = xmalloc(sizeof *copy);
*copy = *(struct in_addr*)orig;
return copy;
}
......@@ -49,10 +47,7 @@ void ns_hash_delete_key(void* key) {
*/
hash_type* ns_hash_create() {
hash_type* hash_table;
if ((hash_table = calloc(hash_table_size, sizeof(hash_type*))) == 0) {
fprintf (stderr, "out of memory (hashTable)\n");
exit(1);
}
hash_table = xcalloc(hash_table_size, sizeof *hash_table);
hash_table->size = hash_table_size;
hash_table->compare = &ns_hash_compare;
hash_table->hash = &ns_hash_hash;
......
......@@ -14,6 +14,7 @@
#include <string.h>
#include "ns_hash.h"
#include "iftop.h"
#define RESOLVE_QUEUE_LENGTH 20
......@@ -57,7 +58,7 @@ void resolver_worker(void* ptr) {
hstbuflen = 1024;
/* Allocate buffer, remember to free it to avoid memory leakage. */
tmphstbuf = malloc (hstbuflen);
tmphstbuf = xmalloc (hstbuflen);
while ((res = gethostbyaddr_r (&addr, sizeof(addr), AF_INET,
&hostbuf, tmphstbuf, hstbuflen,
......@@ -84,7 +85,7 @@ void resolver_worker(void* ptr) {
//printf("[ Resolved: %s ]\n", hp->h_name);
if(hash_find(ns_hash, &addr, (void**)&hostname) == HASH_STATUS_OK) {
hash_delete(ns_hash, &addr);
free(hostname);
xfree(hostname);
}
else {
//printf("[ Warning: Could not find hash entry for key: %s ]\n", inet_ntoa(addr));
......@@ -93,7 +94,7 @@ void resolver_worker(void* ptr) {
hash_insert(ns_hash, &addr, (void*)hostname);
}
free(tmphstbuf);
xfree(tmphstbuf);
}
pthread_mutex_unlock(&resolver_queue_access_mutex);
}
......
......@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <stdio.h>
#include "sorted_list.h"
#include "iftop.h"
void sorted_list_insert(sorted_list_type* list, void* item) {
......@@ -17,11 +18,7 @@ void sorted_list_insert(sorted_list_type* list, void* item) {
p = p->next;
}
node = (sorted_list_node*)malloc(sizeof(sorted_list_node));
if(node == NULL) {
fprintf(stderr,"Out of memory\n");
exit(1);
}
node = xmalloc(sizeof *node);
node->next = p->next;
node->data = item;
......
......@@ -6,7 +6,9 @@
#include <curses.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include "addr_hash.h"
#include "iftop.h"
#include "resolver.h"
......@@ -30,6 +32,8 @@ extern hash_type* history;
extern int history_pos;
extern int history_len;
void ui_finish();
int screen_line_compare(void* a, void* b) {
host_pair_line* aa = (host_pair_line*)a;
host_pair_line* bb = (host_pair_line*)b;
......@@ -66,7 +70,7 @@ void ui_print() {
host_pair_line* screen_line;
int i;
screen_line = (host_pair_line*)calloc(1,sizeof(host_pair_line));
screen_line = xcalloc(1, sizeof *screen_line);
screen_line->ap = (addr_pair*)n->key;
for(i = 0; i < HISTORY_LENGTH; i++) {
......@@ -125,7 +129,7 @@ void ui_print() {
void ui_loop() {
pthread_mutex_t tick_wait_mutex;
pthread_cond_t tick_wait_cond;
extern sig_atomic_t foad;
(void) initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
......@@ -133,11 +137,11 @@ void ui_loop() {
(void) cbreak(); /* take input chars one at a time, no wait for \n */
(void) noecho(); /* don't echo input */
pthread_mutex_init(&tick_wait_mutex, NULL);
pthread_cond_init(&tick_wait_cond, NULL);
while(1) {
while(foad == 0) {
struct timespec t;
int i;
t.tv_sec = time(NULL) + 1;
t.tv_nsec = 0;
......@@ -145,7 +149,6 @@ void ui_loop() {
//fprintf(stderr,"timeout tick\n");
tick();
}
}
void ui_finish() {
......
/*
* util.c:
* Various utility functions.
*
* Copyright (c) 2002 Chris Lightfoot. All rights reserved.
* Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
*
*/
static const char rcsid[] = "$Id$";
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "iftop.h"
/* xmalloc:
* Malloc, and abort if malloc fails. */
void *xmalloc(size_t n) {
void *v;
v = malloc(n);
if (!v) abort();
return v;
}
/* xcalloc:
* As above. */
void *xcalloc(size_t n, size_t m) {
void *v;
v = calloc(n, m);
if (!v) abort();
return v;
}
/* xrealloc:
* As above. */
void *xrealloc(void *w, size_t n) {
void *v;
v = realloc(w, n);
if (n != 0 && !v) abort();
return v;
}
/* xstrdup:
* As above. */
char *xstrdup(const char *s) {
char *t;
t = strdup(s);
if (!t) abort();
return t;
}
/* xfree:
* Free, ignoring a passed NULL value. */
void xfree(void *v) {
if (v) free(v);
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!