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:";
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 NULL /* last entry must be NULL */
52 config_enumeration_type sort_enumeration[] = {
53 { "2s", OPTION_SORT_DIV1 },
54 { "10s", OPTION_SORT_DIV2 },
55 { "40s", OPTION_SORT_DIV3 },
56 { "source", OPTION_SORT_SRC },
57 { "destination", OPTION_SORT_SRC },
61 config_enumeration_type linedisplay_enumeration[] = {
62 { "two-line", OPTION_LINEDISPLAY_TWO_LINE },
63 { "one-line-both", OPTION_LINEDISPLAY_ONE_LINE_BOTH },
64 { "one-line-sent", OPTION_LINEDISPLAY_ONE_LINE_SENT },
65 { "one-line-received", OPTION_LINEDISPLAY_ONE_LINE_RECV },
69 config_enumeration_type showports_enumeration[] = {
70 { "off", OPTION_PORTS_OFF },
71 { "source-only", OPTION_PORTS_SRC },
72 { "destination-only", OPTION_PORTS_DEST },
73 { "on", OPTION_PORTS_ON },
77 static int is_bad_interface_name(char *i) {
79 for (p = bad_interface_names; *p; ++p)
80 if (strncmp(i, *p, strlen(*p)) == 0)
85 /* This finds the first interface which is up and is not the loopback
86 * interface or one of the interface types listed in bad_interface_names. */
87 static char *get_first_interface(void) {
88 struct if_nameindex * nameindex;
91 /* Use if_nameindex(3) instead? */
93 nameindex = if_nameindex();
94 if(nameindex == NULL) {
98 while(nameindex[j].if_index != 0) {
99 if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) {
100 i = xstrdup(nameindex[j].if_name);
105 if_freenameindex(nameindex);
109 void options_set_defaults() {
111 /* Should go through the list of interfaces, and find the first one which
112 * is up and is not lo or dummy*. */
113 options.interface = get_first_interface();
114 if (!options.interface)
115 options.interface = "eth0";
117 options.filtercode = NULL;
118 options.netfilter = 0;
119 inet_aton("10.0.1.0", &options.netfilternet);
120 inet_aton("255.255.255.0", &options.netfiltermask);
121 options.netfilter6 = 0;
122 inet_pton(AF_INET6, "fe80::", &options.netfilter6net); /* Link-local */
123 inet_pton(AF_INET6, "ffff::", &options.netfilter6mask);
124 options.link_local = 0;
125 options.dnsresolution = 1;
126 options.portresolution = 1;
127 #ifdef NEED_PROMISCUOUS_FOR_OUTGOING
128 options.promiscuous = 1;
129 options.promiscuous_but_choosy = 1;
131 options.promiscuous = 0;
132 options.promiscuous_but_choosy = 0;
134 options.showbars = 1;
135 options.showports = OPTION_PORTS_OFF;
136 options.aggregate_src = 0;
137 options.aggregate_dest = 0;
139 options.showhelp = 0;
140 options.bandwidth_in_bytes = 0;
141 options.sort = OPTION_SORT_DIV2;
142 options.screenfilter = NULL;
143 options.freezeorder = 0;
144 options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE;
145 options.screen_offset = 0;
146 options.show_totals = 0;
147 options.max_bandwidth = 0; /* auto */
148 options.log_scale = 0;
149 options.bar_interval = 1;
151 /* Figure out the name for the config file */
154 int i = strlen(s) + 9 + 1;
155 options.config_file = xmalloc(i);
156 snprintf(options.config_file,i,"%s/.iftoprc",s);
159 options.config_file = xstrdup("iftoprc");
161 options.config_file_specified = 0;
165 static void die(char *msg) {
166 fprintf(stderr, msg);
170 static void set_max_bandwidth(char* arg) {
174 units = arg + strspn(arg, "0123456789");
175 if(strlen(units) > 1) {
176 die("Invalid units\n");
178 if(strlen(units) == 1) {
179 if(*units == 'k' || *units == 'K') {
182 else if(*units == 'm' || *units == 'M') {
185 else if(*units == 'g' || *units == 'G') {
186 mult = 1024 * 1024 * 1024;
190 if(sscanf(arg, "%lld", &value) != 1) {
191 die("Error reading max bandwidth\n");
193 options.max_bandwidth = value * mult;
196 static void set_net_filter(char* arg) {
199 mask = strchr(arg, '/');
201 die("Could not parse net/mask\n");
205 if (inet_aton(arg, &options.netfilternet) == 0)
206 die("Invalid network address\n");
207 /* Accept a netmask like /24 or /255.255.255.0. */
208 if (mask[strspn(mask, "0123456789")] == '\0') {
209 /* Whole string is numeric */
213 die("Invalid netmask");
217 /* This needs to be special cased, although I don't fully
218 * understand why -pdw
220 options.netfiltermask.s_addr = htonl(0xffffffffl);
223 u_int32_t mm = 0xffffffffl;
225 options.netfiltermask.s_addr = htonl(~mm);
229 else if (inet_aton(mask, &options.netfiltermask) == 0) {
230 die("Invalid netmask\n");
232 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
234 options.netfilter = 1;
239 * Print usage information. */
240 static void usage(FILE *fp) {
242 "iftop: display bandwidth usage on an interface by host\n"
244 "Synopsis: iftop -h | [-npblBP] [-i interface] [-f filter code]\n"
245 " [-F net/mask] [-G net6/mask6]\n"
247 " -h display this message\n"
248 " -n don't do hostname lookups\n"
249 " -N don't convert port numbers to services\n"
250 " -p run in promiscuous mode (show traffic between other\n"
251 " hosts on the same network segment)\n"
252 " -b don't display a bar graph of traffic\n"
253 " -B Display bandwidth in bytes\n"
254 " -i interface listen on named interface\n"
255 " -f filter code use filter code to select packets to count\n"
256 " (default: none, but only IP packets are counted)\n"
257 " -F net/mask show traffic flows in/out of IPv4 network\n"
258 " -G net6/mask6 show traffic flows in/out of IPv6 network\n"
259 " -l display and count link-local IPv6 traffic (default: off)\n"
260 " -P show ports as well as hosts\n"
261 " -m limit sets the upper limit for the bandwidth scale\n"
262 " -c config file specifies an alternative configuration file\n"
264 "iftop, version " IFTOP_VERSION "\n"
265 "copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
269 void options_read_args(int argc, char **argv) {
273 while ((opt = getopt(argc, argv, optstr)) != -1) {
280 config_set_string("dns-resolution","false");
284 config_set_string("port-resolution","false");
288 config_set_string("interface", optarg);
292 config_set_string("filter-code", optarg);
296 config_set_string("link-local", "true");
300 config_set_string("promiscuous", "true");
304 config_set_string("port-display", "on");
308 config_set_string("net-filter", optarg);
312 config_set_string("net-filter6", optarg);
316 config_set_string("max-bandwidth", optarg);
320 config_set_string("show-bars", "false");
324 config_set_string("use-bytes", "true");
328 xfree(options.config_file);
329 options.config_file = xstrdup(optarg);
330 options.config_file_specified = 1;
334 fprintf(stderr, "iftop: unknown option -%c\n", optopt);
339 fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
346 if (optind != argc) {
347 fprintf(stderr, "iftop: found arguments following options\n");
348 fprintf(stderr, "*** some options have changed names since v0.9 ***\n");
354 /* options_config_get_string:
355 * Gets a value from the config, sets *value to a copy of the value, if
356 * found. Leaves the option unchanged otherwise. */
357 int options_config_get_string(const char *name, char** value) {
359 s = config_get_string(name);
367 int options_config_get_bool(const char *name, int* value) {
368 if(config_get_string(name)) {
369 *value = config_get_bool(name);
375 int options_config_get_int(const char *name, int* value) {
376 if(config_get_string(name)) {
377 config_get_int(name, value);
383 int options_config_get_enum(char *name, config_enumeration_type* enumeration, int *result) {
385 if(config_get_string(name)) {
386 if(config_get_enum(name, enumeration, &i)) {
394 int options_config_get_promiscuous() {
395 if(config_get_string("promiscuous")) {
396 options.promiscuous = config_get_bool("promiscuous");
397 if(options.promiscuous) {
398 /* User has explicitly requested promiscuous mode, so don't be
400 options.promiscuous_but_choosy = 0;
407 int options_config_get_bw_rate(char *directive, long long* result) {
412 s = config_get_string(directive);
414 units = s + strspn(s, "0123456789");
415 if(strlen(units) > 1) {
416 fprintf(stderr, "Invalid units in value: %s\n", s);
419 if(strlen(units) == 1) {
420 if(*units == 'k' || *units == 'K') {
423 else if(*units == 'm' || *units == 'M') {
426 else if(*units == 'g' || *units == 'G') {
427 mult = 1024 * 1024 * 1024;
429 else if(*units == 'b' || *units == 'B') {
430 /* bits => mult = 1 */
433 fprintf(stderr, "Invalid units in value: %s\n", s);
438 if(sscanf(s, "%lld", &value) != 1) {
439 fprintf(stderr, "Error reading rate: %s\n", s);
441 options.max_bandwidth = value * mult;
448 * Read the net filter option.
450 int options_config_get_net_filter() {
452 s = config_get_string("net-filter");
456 options.netfilter = 0;
458 mask = strchr(s, '/');
460 fprintf(stderr, "Could not parse net/mask: %s\n", s);
465 if (inet_aton(s, &options.netfilternet) == 0) {
466 fprintf(stderr, "Invalid network address: %s\n", s);
469 /* Accept a netmask like /24 or /255.255.255.0. */
470 if (mask[strspn(mask, "0123456789")] == '\0') {
471 /* Whole string is numeric */
475 fprintf(stderr, "Invalid netmask length: %s\n", mask);
479 /* This needs to be special cased, although I don't fully
480 * understand why -pdw
482 options.netfiltermask.s_addr = htonl(0xffffffffl);
485 u_int32_t mm = 0xffffffffl;
487 options.netfiltermask.s_addr = htonl(~mm);
490 options.netfilter = 1;
493 if (inet_aton(mask, &options.netfiltermask) != 0)
494 options.netfilter = 1;
496 fprintf(stderr, "Invalid netmask: %s\n", s);
500 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
507 * Read the net filter IPv6 option.
509 int options_config_get_net_filter6() {
513 s = config_get_string("net-filter6");
517 options.netfilter6 = 0;
519 mask = strchr(s, '/');
521 fprintf(stderr, "Could not parse IPv6 net/prefix: %s\n", s);
526 if (inet_pton(AF_INET6, s, &options.netfilter6net) == 0) {
527 fprintf(stderr, "Invalid IPv6 network address: %s\n", s);
530 /* Accept prefix lengths and address expressions. */
531 if (mask[strspn(mask, "0123456789")] == '\0') {
532 /* Whole string is numeric */
536 if (n > 128 || n < 1) {
537 fprintf(stderr, "Invalid IPv6 prefix length: %s\n", mask);
541 const uint32_t mm = 0xffffffff;
547 for (j=0; j < bl; ++j)
548 options.netfilter6mask.s6_addr32[j] = htonl(mm);
550 options.netfilter6mask.s6_addr32[bl] = htonl(part);
551 options.netfilter6 = 1;
555 if (inet_pton(AF_INET6, mask, &options.netfilter6mask) != 0)
556 options.netfilter6 = 1;
558 fprintf(stderr, "Invalid IPv6 netmask: %s\n", s);
562 /* Prepare any comparison by masking the provided filtered net. */
563 for (j=0; j < 4; ++j)
564 options.netfilter6net.s6_addr32[j] &= options.netfilter6mask.s6_addr32[j];
571 void options_make() {
572 options_config_get_string("interface", &options.interface);
573 options_config_get_bool("dns-resolution", &options.dnsresolution);
574 options_config_get_bool("port-resolution", &options.portresolution);
575 options_config_get_string("filter-code", &options.filtercode);
576 options_config_get_bool("show-bars", &options.showbars);
577 options_config_get_promiscuous();
578 options_config_get_bool("hide-source", &options.aggregate_src);
579 options_config_get_bool("hide-destination", &options.aggregate_dest);
580 options_config_get_bool("use-bytes", &options.bandwidth_in_bytes);
581 options_config_get_enum("sort", sort_enumeration, (int*)&options.sort);
582 options_config_get_enum("line-display", linedisplay_enumeration, (int*)&options.linedisplay);
583 options_config_get_bool("show-totals", &options.show_totals);
584 options_config_get_bool("log-scale", &options.log_scale);
585 options_config_get_bw_rate("max-bandwidth", &options.max_bandwidth);
586 options_config_get_enum("port-display", showports_enumeration, (int*)&options.showports);
587 options_config_get_string("screen-filter", &options.screenfilter);
588 options_config_get_bool("link-local", &options.link_local);
589 options_config_get_net_filter();
590 options_config_get_net_filter6();