16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
27 #if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON)
28 # define inet_aton(a, b) inet_pton(AF_INET, (a), (b))
33 char optstr[] = "+i:f:nNF:G:lhpbBPm:c:s:tL:o:";
37 /* Selecting an interface on which to listen: */
39 /* This is a list of interface name prefixes which are `bad' in the sense
40 * that they don't refer to interfaces of external type on which we are
41 * likely to want to listen. We also compare candidate interfaces to lo. */
42 static char *bad_interface_names[] = {
45 "stf", /* pseudo-device 6to4 tunnel interface */
46 "gif", /* psuedo-device generic tunnel interface */
49 "wmaster", /* wmaster0 is an internal-use interface for mac80211, a Linux WiFi API. */
50 NULL /* last entry must be NULL */
53 config_enumeration_type sort_enumeration[] = {
54 { "2s", OPTION_SORT_DIV1 },
55 { "10s", OPTION_SORT_DIV2 },
56 { "40s", OPTION_SORT_DIV3 },
57 { "source", OPTION_SORT_SRC },
58 { "destination", OPTION_SORT_DEST },
62 config_enumeration_type linedisplay_enumeration[] = {
63 { "two-line", OPTION_LINEDISPLAY_TWO_LINE },
64 { "one-line-both", OPTION_LINEDISPLAY_ONE_LINE_BOTH },
65 { "one-line-sent", OPTION_LINEDISPLAY_ONE_LINE_SENT },
66 { "one-line-received", OPTION_LINEDISPLAY_ONE_LINE_RECV },
70 config_enumeration_type showports_enumeration[] = {
71 { "off", OPTION_PORTS_OFF },
72 { "source-only", OPTION_PORTS_SRC },
73 { "destination-only", OPTION_PORTS_DEST },
74 { "on", OPTION_PORTS_ON },
78 static int is_bad_interface_name(char *i) {
80 for (p = bad_interface_names; *p; ++p)
81 if (strncmp(i, *p, strlen(*p)) == 0)
86 /* This finds the first interface which is up and is not the loopback
87 * interface or one of the interface types listed in bad_interface_names. */
88 static char *get_first_interface(void) {
89 struct if_nameindex * nameindex;
94 /* Use if_nameindex(3) instead? */
96 nameindex = if_nameindex();
97 if(nameindex == NULL) {
101 s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
103 while(nameindex[j].if_index != 0) {
104 if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) {
105 strncpy(ifr.ifr_name, nameindex[j].if_name, sizeof(ifr.ifr_name));
106 if ((s == -1) || (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) || (ifr.ifr_flags & IFF_UP)) {
107 i = xstrdup(nameindex[j].if_name);
113 if_freenameindex(nameindex);
117 void options_set_defaults() {
119 /* Should go through the list of interfaces, and find the first one which
120 * is up and is not lo or dummy*. */
121 options.interface = get_first_interface();
122 if (!options.interface)
123 options.interface = "eth0";
125 options.filtercode = NULL;
126 options.netfilter = 0;
127 inet_aton("10.0.1.0", &options.netfilternet);
128 inet_aton("255.255.255.0", &options.netfiltermask);
129 options.netfilter6 = 0;
130 inet_pton(AF_INET6, "fe80::", &options.netfilter6net); /* Link-local */
131 inet_pton(AF_INET6, "ffff::", &options.netfilter6mask);
132 options.link_local = 0;
133 options.dnsresolution = 1;
134 options.portresolution = 1;
135 #ifdef NEED_PROMISCUOUS_FOR_OUTGOING
136 options.promiscuous = 1;
137 options.promiscuous_but_choosy = 1;
139 options.promiscuous = 0;
140 options.promiscuous_but_choosy = 0;
142 options.showbars = 1;
143 options.showports = OPTION_PORTS_OFF;
144 options.aggregate_src = 0;
145 options.aggregate_dest = 0;
147 options.showhelp = 0;
148 options.bandwidth_in_bytes = 0;
149 options.sort = OPTION_SORT_DIV2;
150 options.screenfilter = NULL;
151 options.freezeorder = 0;
152 options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE;
153 options.screen_offset = 0;
154 options.show_totals = 0;
155 options.max_bandwidth = 0; /* auto */
156 options.log_scale = 0;
157 options.bar_interval = 1;
158 options.timed_output = 0;
159 options.no_curses = 0;
160 options.num_lines = 10;
162 /* Figure out the name for the config file */
165 int i = strlen(s) + 9 + 1;
166 options.config_file = xmalloc(i);
167 snprintf(options.config_file,i,"%s/.iftoprc",s);
170 options.config_file = xstrdup("iftoprc");
172 options.config_file_specified = 0;
177 * Print usage information. */
178 static void usage(FILE *fp) {
180 "iftop: display bandwidth usage on an interface by host\n"
182 "Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]\n"
183 " [-F net/mask] [-G net6/mask6]\n"
185 " -h display this message\n"
186 " -n don't do hostname lookups\n"
187 " -N don't convert port numbers to services\n"
188 " -p run in promiscuous mode (show traffic between other\n"
189 " hosts on the same network segment)\n"
190 " -b don't display a bar graph of traffic\n"
191 " -B Display bandwidth in bytes\n"
192 " -i interface listen on named interface\n"
193 " -f filter code use filter code to select packets to count\n"
194 " (default: none, but only IP packets are counted)\n"
195 " -F net/mask show traffic flows in/out of IPv4 network\n"
196 " -G net6/mask6 show traffic flows in/out of IPv6 network\n"
197 " -l display and count link-local IPv6 traffic (default: off)\n"
198 " -P show ports as well as hosts\n"
199 " -m limit sets the upper limit for the bandwidth scale\n"
200 " -c config file specifies an alternative configuration file\n"
201 " -t use text interface without ncurses\n"
204 " -o 2s Sort by first column (2s traffic average)\n"
205 " -o 10s Sort by second column (10s traffic average) [default]\n"
206 " -o 40s Sort by third column (40s traffic average)\n"
207 " -o source Sort by source address\n"
208 " -o destination Sort by destination address\n"
210 " The following options are only available in combination with -t\n"
211 " -s num print one single text output afer num seconds, then quit\n"
212 " -L num number of lines to print\n"
214 "iftop, version " PACKAGE_VERSION "\n"
215 "copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
219 void options_read_args(int argc, char **argv) {
223 while ((opt = getopt(argc, argv, optstr)) != -1) {
230 config_set_string("dns-resolution","false");
234 config_set_string("port-resolution","false");
238 config_set_string("interface", optarg);
242 config_set_string("filter-code", optarg);
246 config_set_string("link-local", "true");
250 config_set_string("promiscuous", "true");
254 config_set_string("port-display", "on");
258 config_set_string("net-filter", optarg);
262 config_set_string("net-filter6", optarg);
266 config_set_string("max-bandwidth", optarg);
270 config_set_string("show-bars", "false");
274 config_set_string("use-bytes", "true");
278 config_set_string("timed-output", optarg);
282 config_set_string("no-curses", "true");
286 config_set_string("num-lines", optarg);
290 config_set_string("sort", optarg);
294 xfree(options.config_file);
295 options.config_file = xstrdup(optarg);
296 options.config_file_specified = 1;
300 fprintf(stderr, "iftop: unknown option -%c\n", optopt);
305 fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
312 if (optind != argc) {
313 fprintf(stderr, "iftop: found arguments following options\n");
314 fprintf(stderr, "*** some options have changed names since v0.9 ***\n");
320 /* options_config_get_string:
321 * Gets a value from the config, sets *value to a copy of the value, if
322 * found. Leaves the option unchanged otherwise. */
323 int options_config_get_string(const char *name, char** value) {
325 s = config_get_string(name);
333 int options_config_get_bool(const char *name, int* value) {
334 if(config_get_string(name)) {
335 *value = config_get_bool(name);
341 int options_config_get_int(const char *name, int* value) {
342 if(config_get_string(name)) {
343 config_get_int(name, value);
349 int options_config_get_enum(char *name, config_enumeration_type* enumeration, int *result) {
351 if(config_get_string(name)) {
352 if(config_get_enum(name, enumeration, &i)) {
360 int options_config_get_promiscuous() {
361 if(config_get_string("promiscuous")) {
362 options.promiscuous = config_get_bool("promiscuous");
363 if(options.promiscuous) {
364 /* User has explicitly requested promiscuous mode, so don't be
366 options.promiscuous_but_choosy = 0;
373 int options_config_get_bw_rate(char *directive, long long* result) {
378 s = config_get_string(directive);
380 units = s + strspn(s, "0123456789");
381 if(strlen(units) > 1) {
382 fprintf(stderr, "Invalid units in value: %s\n", s);
385 if(strlen(units) == 1) {
386 if(*units == 'k' || *units == 'K') {
389 else if(*units == 'm' || *units == 'M') {
392 else if(*units == 'g' || *units == 'G') {
393 mult = 1024 * 1024 * 1024;
395 else if(*units == 'b' || *units == 'B') {
396 /* bits => mult = 1 */
399 fprintf(stderr, "Invalid units in value: %s\n", s);
404 if(sscanf(s, "%lld", &value) != 1) {
405 fprintf(stderr, "Error reading rate: %s\n", s);
407 options.max_bandwidth = value * mult;
414 * Read the net filter option.
416 int options_config_get_net_filter() {
418 s = config_get_string("net-filter");
422 options.netfilter = 0;
424 mask = strchr(s, '/');
426 fprintf(stderr, "Could not parse net/mask: %s\n", s);
431 if (inet_aton(s, &options.netfilternet) == 0) {
432 fprintf(stderr, "Invalid network address: %s\n", s);
435 /* Accept a netmask like /24 or /255.255.255.0. */
436 if (mask[strspn(mask, "0123456789")] == '\0') {
437 /* Whole string is numeric */
441 fprintf(stderr, "Invalid netmask length: %s\n", mask);
445 /* This needs to be special cased, although I don't fully
446 * understand why -pdw
448 options.netfiltermask.s_addr = htonl(0xffffffffl);
451 u_int32_t mm = 0xffffffffl;
453 options.netfiltermask.s_addr = htonl(~mm);
456 options.netfilter = 1;
459 if (inet_aton(mask, &options.netfiltermask) != 0)
460 options.netfilter = 1;
462 fprintf(stderr, "Invalid netmask: %s\n", s);
466 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
473 * Read the net filter IPv6 option.
475 int options_config_get_net_filter6() {
479 s = config_get_string("net-filter6");
483 options.netfilter6 = 0;
485 mask = strchr(s, '/');
487 fprintf(stderr, "Could not parse IPv6 net/prefix: %s\n", s);
492 if (inet_pton(AF_INET6, s, &options.netfilter6net) == 0) {
493 fprintf(stderr, "Invalid IPv6 network address: %s\n", s);
496 /* Accept prefix lengths and address expressions. */
497 if (mask[strspn(mask, "0123456789")] == '\0') {
498 /* Whole string is numeric */
502 if (n > 128 || n < 1) {
503 fprintf(stderr, "Invalid IPv6 prefix length: %s\n", mask);
507 const uint8_t mm = 0xff;
513 for (j=0; j < bl; ++j)
514 options.netfilter6mask.s6_addr[j] = mm;
517 options.netfilter6mask.s6_addr[bl] = part;
518 options.netfilter6 = 1;
522 if (inet_pton(AF_INET6, mask, &options.netfilter6mask) != 0)
523 options.netfilter6 = 1;
525 fprintf(stderr, "Invalid IPv6 netmask: %s\n", s);
529 /* Prepare any comparison by masking the provided filtered net. */
530 for (j=0; j < 16; ++j)
531 options.netfilter6net.s6_addr[j] &= options.netfilter6mask.s6_addr[j];
538 void options_make() {
539 options_config_get_string("interface", &options.interface);
540 options_config_get_bool("dns-resolution", &options.dnsresolution);
541 options_config_get_bool("port-resolution", &options.portresolution);
542 options_config_get_string("filter-code", &options.filtercode);
543 options_config_get_bool("show-bars", &options.showbars);
544 options_config_get_promiscuous();
545 options_config_get_bool("hide-source", &options.aggregate_src);
546 options_config_get_bool("hide-destination", &options.aggregate_dest);
547 options_config_get_bool("use-bytes", &options.bandwidth_in_bytes);
548 options_config_get_enum("sort", sort_enumeration, (int*)&options.sort);
549 options_config_get_enum("line-display", linedisplay_enumeration, (int*)&options.linedisplay);
550 options_config_get_bool("show-totals", &options.show_totals);
551 options_config_get_bool("log-scale", &options.log_scale);
552 options_config_get_bw_rate("max-bandwidth", &options.max_bandwidth);
553 options_config_get_enum("port-display", showports_enumeration, (int*)&options.showports);
554 options_config_get_string("screen-filter", &options.screenfilter);
555 options_config_get_bool("link-local", &options.link_local);
556 options_config_get_int("timed-output", &options.timed_output);
557 options_config_get_bool("no-curses", &options.no_curses);
558 options_config_get_int("num-lines", &options.num_lines);
559 options_config_get_net_filter();
560 options_config_get_net_filter6();