Commit a9663192 authored by pdw's avatar pdw

Patch for text output mode by originally by Patrik Bless, updated by Roman Hoog

Antink <rha@open.ch>
parent 59cf3606
......@@ -15,7 +15,7 @@ sbin_PROGRAMS = iftop
iftop_SOURCES = addr_hash.c edline.c hash.c iftop.c ns_hash.c \
options.c resolver.c screenfilter.c serv_hash.c \
sorted_list.c threadprof.c ui.c util.c \
sorted_list.c threadprof.c ui_common.c ui.c tui.c util.c \
addrs_ioctl.c addrs_dlpi.c dlcommon.c \
stringmap.c cfgfile.c vector.c
......@@ -30,7 +30,7 @@ iftop_SOURCES = addr_hash.c edline.c hash.c iftop.c ns_hash.c \
noinst_HEADERS = addr_hash.h ether.h ethertype.h extract.h hash.h iftop.h \
integers.h ip.h llc.h ns_hash.h options.h resolver.h \
screenfilter.h serv_hash.h sll.h sorted_list.h tcp.h \
threadprof.h token.h ui.h dlcommon.h stringmap.h \
threadprof.h token.h ui_common.h ui.h tui.h dlcommon.h stringmap.h \
vector.h cfgfile.h ppp.h
man_MANS = iftop.8
......
......@@ -39,6 +39,9 @@ char * config_directives[] = {
"net-filter6",
"link-local",
"port-display",
"timed-output",
"no-curses",
"num-lines",
NULL
};
......
......@@ -76,6 +76,11 @@ hash_status_enum hash_find(hash_type* hash_table, void* key, void **rec) {
hash_status_enum hash_next_item(hash_type* hash_table, hash_node_type** ppnode) {
int i;
if (hash_table == 0) {
return HASH_STATUS_KEY_NOT_FOUND;
}
if(*ppnode != NULL) {
if((*ppnode)->next != NULL) {
*ppnode = (*ppnode)->next;
......@@ -101,6 +106,11 @@ hash_status_enum hash_next_item(hash_type* hash_table, hash_node_type** ppnode)
void hash_delete_all(hash_type* hash_table) {
int i;
hash_node_type *n, *nn;
if(hash_table == 0) {
return;
}
for(i = 0; i < hash_table->size; i++) {
n = hash_table->table[i];
while(n != NULL) {
......
......@@ -32,7 +32,9 @@
#include "iftop.h"
#include "addr_hash.h"
#include "resolver.h"
#include "ui_common.h"
#include "ui.h"
#include "tui.h"
#include "options.h"
#ifdef DLT_LINUX_SLL
#include "sll.h"
......@@ -65,6 +67,7 @@ extern options_t options;
hash_type* history;
history_type history_totals;
time_t last_timestamp;
time_t first_timestamp;
int history_pos = 0;
int history_len = 1;
pthread_mutex_t tick_mutex;
......@@ -141,13 +144,28 @@ void tick(int print) {
t = time(NULL);
if(t - last_timestamp >= RESOLUTION) {
analyse_data();
if (options.no_curses) {
if (!options.timed_output || options.timed_output && t - first_timestamp >= options.timed_output) {
tui_print();
if (options.timed_output) {
finish(SIGINT);
}
}
}
else {
ui_print();
}
history_rotate();
last_timestamp = t;
}
else {
if (options.no_curses) {
tui_tick(print);
}
else {
ui_tick(print);
}
}
pthread_mutex_unlock(&tick_mutex);
}
......@@ -247,6 +265,8 @@ static void handle_ip_packet(struct ip* iptr, int hw_dir)
memset(&ap, '\0', sizeof(ap));
tick(0);
if( (IP_V(iptr) ==4 && options.netfilter == 0)
|| (IP_V(iptr) == 6 && options.netfilter6 == 0) ) {
/*
......@@ -570,8 +590,6 @@ static void handle_eth_packet(unsigned char* args, const struct pcap_pkthdr* pkt
ether_type = ntohs(eptr->ether_type);
payload = packet + sizeof(struct ether_header);
tick(0);
if(ether_type == ETHERTYPE_8021Q) {
struct vlan_8021q_header* vptr;
vptr = (struct vlan_8021q_header*)payload;
......@@ -784,11 +802,31 @@ int main(int argc, char **argv) {
init_history();
if (options.no_curses) {
tui_init();
}
else {
ui_init();
}
pthread_create(&thread, NULL, (void*)&packet_loop, NULL);
/* Keep the starting time (used for timed termination) */
first_timestamp = time(NULL);
if (options.no_curses) {
if (options.timed_output) {
while(!foad) {
sleep(1);
}
}
else {
tui_loop();
}
}
else {
ui_loop();
}
pthread_cancel(thread);
......
......@@ -30,7 +30,7 @@
options_t options;
char optstr[] = "+i:f:nNF:G:lhpbBPm:c:";
char optstr[] = "+i:f:nNF:G:lhpbBPm:c:s:tL:o:";
/* Global options. */
......@@ -55,7 +55,7 @@ config_enumeration_type sort_enumeration[] = {
{ "10s", OPTION_SORT_DIV2 },
{ "40s", OPTION_SORT_DIV3 },
{ "source", OPTION_SORT_SRC },
{ "destination", OPTION_SORT_SRC },
{ "destination", OPTION_SORT_DEST },
{ NULL, -1 }
};
......@@ -155,6 +155,9 @@ void options_set_defaults() {
options.max_bandwidth = 0; /* auto */
options.log_scale = 0;
options.bar_interval = 1;
options.timed_output = 0;
options.no_curses = 0;
options.num_lines = 10;
/* Figure out the name for the config file */
s = getenv("HOME");
......@@ -268,6 +271,18 @@ static void usage(FILE *fp) {
" -P show ports as well as hosts\n"
" -m limit sets the upper limit for the bandwidth scale\n"
" -c config file specifies an alternative configuration file\n"
" -t use text interface without ncurses\n"
"\n"
" Sorting orders:\n"
" -o 2s Sort by first column (2s traffic average)\n"
" -o 10s Sort by second column (10s traffic average) [default]\n"
" -o 40s Sort by third column (40s traffic average)\n"
" -o source Sort by source address\n"
" -o destination Sort by destination address\n"
"\n"
" The following options are only available in combination with -t\n"
" -s num print one single text output afer num seconds, then quit\n"
" -L num number of lines to print\n"
"\n"
"iftop, version " IFTOP_VERSION "\n"
"copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
......@@ -332,6 +347,22 @@ void options_read_args(int argc, char **argv) {
config_set_string("use-bytes", "true");
break;
case 's':
config_set_string("timed-output", optarg);
break;
case 't':
config_set_string("no-curses", "true");
break;
case 'L':
config_set_string("num-lines", optarg);
break;
case 'o':
config_set_string("sort", optarg);
break;
case 'c':
xfree(options.config_file);
options.config_file = xstrdup(optarg);
......@@ -595,6 +626,9 @@ void options_make() {
options_config_get_enum("port-display", showports_enumeration, (int*)&options.showports);
options_config_get_string("screen-filter", &options.screenfilter);
options_config_get_bool("link-local", &options.link_local);
options_config_get_int("timed-output", &options.timed_output);
options_config_get_bool("no-curses", &options.no_curses);
options_config_get_int("num-lines", &options.num_lines);
options_config_get_net_filter();
options_config_get_net_filter6();
};
......@@ -56,6 +56,9 @@ typedef struct {
int aggregate_dest;
int paused;
int showhelp;
int timed_output;
int no_curses;
int num_lines;
int bandwidth_in_bytes;
option_sort_t sort;
......
This diff is collapsed.
This diff is collapsed.
/*
* ui_common.h
*
*
*/
#ifndef __UI_COMMON_H_ /* include guard */
#define __UI_COMMON_H_
#include <string.h>
#include <stdio.h>
#include "addr_hash.h"
#include "serv_hash.h"
#include "iftop.h"
#include "resolver.h"
#include "sorted_list.h"
#include "options.h"
#define HISTORY_DIVISIONS 3
#define UNIT_DIVISIONS 4
#define HOSTNAME_LENGTH 256
typedef struct host_pair_line_tag {
addr_pair ap;
double long total_recv;
double long total_sent;
double long recv[HISTORY_DIVISIONS];
double long sent[HISTORY_DIVISIONS];
} host_pair_line;
extern options_t options;
sorted_list_type screen_list;
host_pair_line totals;
int peaksent, peakrecv, peaktotal;
extern history_type history_totals;
hash_type* screen_hash;
hash_type* service_hash;
void analyse_data(void);
void screen_list_init(void);
void sprint_host(char * line, int af, struct in6_addr* addr, unsigned int port, unsigned int protocol, int L, int unspecified_as_star);
void readable_size(float, char*, int, int, int);
#endif /* __UI_COMMON_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment