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 "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_SRC },
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;
159 /* Figure out the name for the config file */
162 int i = strlen(s) + 9 + 1;
163 options.config_file = xmalloc(i);
164 snprintf(options.config_file,i,"%s/.iftoprc",s);
167 options.config_file = xstrdup("iftoprc");
169 options.config_file_specified = 0;
173 static void die(char *msg) {
174 fprintf(stderr, "%s", msg);
178 static void set_max_bandwidth(char* arg) {
182 units = arg + strspn(arg, "0123456789");
183 if(strlen(units) > 1) {
184 die("Invalid units\n");
186 if(strlen(units) == 1) {
187 if(*units == 'k' || *units == 'K') {
190 else if(*units == 'm' || *units == 'M') {
193 else if(*units == 'g' || *units == 'G') {
194 mult = 1024 * 1024 * 1024;
198 if(sscanf(arg, "%lld", &value) != 1) {
199 die("Error reading max bandwidth\n");
201 options.max_bandwidth = value * mult;
204 static void set_net_filter(char* arg) {
207 mask = strchr(arg, '/');
209 die("Could not parse net/mask\n");
213 if (inet_aton(arg, &options.netfilternet) == 0)
214 die("Invalid network address\n");
215 /* Accept a netmask like /24 or /255.255.255.0. */
216 if (mask[strspn(mask, "0123456789")] == '\0') {
217 /* Whole string is numeric */
221 die("Invalid netmask");
225 /* This needs to be special cased, although I don't fully
226 * understand why -pdw
228 options.netfiltermask.s_addr = htonl(0xffffffffl);
231 u_int32_t mm = 0xffffffffl;
233 options.netfiltermask.s_addr = htonl(~mm);
237 else if (inet_aton(mask, &options.netfiltermask) == 0) {
238 die("Invalid netmask\n");
240 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
242 options.netfilter = 1;
247 * Print usage information. */
248 static void usage(FILE *fp) {
250 "iftop: display bandwidth usage on an interface by host\n"
252 "Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]\n"
253 " [-F net/mask] [-G net6/mask6]\n"
255 " -h display this message\n"
256 " -n don't do hostname lookups\n"
257 " -N don't convert port numbers to services\n"
258 " -p run in promiscuous mode (show traffic between other\n"
259 " hosts on the same network segment)\n"
260 " -b don't display a bar graph of traffic\n"
261 " -B Display bandwidth in bytes\n"
262 " -i interface listen on named interface\n"
263 " -f filter code use filter code to select packets to count\n"
264 " (default: none, but only IP packets are counted)\n"
265 " -F net/mask show traffic flows in/out of IPv4 network\n"
266 " -G net6/mask6 show traffic flows in/out of IPv6 network\n"
267 " -l display and count link-local IPv6 traffic (default: off)\n"
268 " -P show ports as well as hosts\n"
269 " -m limit sets the upper limit for the bandwidth scale\n"
270 " -c config file specifies an alternative configuration file\n"
272 "iftop, version " IFTOP_VERSION "\n"
273 "copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
277 void options_read_args(int argc, char **argv) {
281 while ((opt = getopt(argc, argv, optstr)) != -1) {
288 config_set_string("dns-resolution","false");
292 config_set_string("port-resolution","false");
296 config_set_string("interface", optarg);
300 config_set_string("filter-code", optarg);
304 config_set_string("link-local", "true");
308 config_set_string("promiscuous", "true");
312 config_set_string("port-display", "on");
316 config_set_string("net-filter", optarg);
320 config_set_string("net-filter6", optarg);
324 config_set_string("max-bandwidth", optarg);
328 config_set_string("show-bars", "false");
332 config_set_string("use-bytes", "true");
336 xfree(options.config_file);
337 options.config_file = xstrdup(optarg);
338 options.config_file_specified = 1;
342 fprintf(stderr, "iftop: unknown option -%c\n", optopt);
347 fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
354 if (optind != argc) {
355 fprintf(stderr, "iftop: found arguments following options\n");
356 fprintf(stderr, "*** some options have changed names since v0.9 ***\n");
362 /* options_config_get_string:
363 * Gets a value from the config, sets *value to a copy of the value, if
364 * found. Leaves the option unchanged otherwise. */
365 int options_config_get_string(const char *name, char** value) {
367 s = config_get_string(name);
375 int options_config_get_bool(const char *name, int* value) {
376 if(config_get_string(name)) {
377 *value = config_get_bool(name);
383 int options_config_get_int(const char *name, int* value) {
384 if(config_get_string(name)) {
385 config_get_int(name, value);
391 int options_config_get_enum(char *name, config_enumeration_type* enumeration, int *result) {
393 if(config_get_string(name)) {
394 if(config_get_enum(name, enumeration, &i)) {
402 int options_config_get_promiscuous() {
403 if(config_get_string("promiscuous")) {
404 options.promiscuous = config_get_bool("promiscuous");
405 if(options.promiscuous) {
406 /* User has explicitly requested promiscuous mode, so don't be
408 options.promiscuous_but_choosy = 0;
415 int options_config_get_bw_rate(char *directive, long long* result) {
420 s = config_get_string(directive);
422 units = s + strspn(s, "0123456789");
423 if(strlen(units) > 1) {
424 fprintf(stderr, "Invalid units in value: %s\n", s);
427 if(strlen(units) == 1) {
428 if(*units == 'k' || *units == 'K') {
431 else if(*units == 'm' || *units == 'M') {
434 else if(*units == 'g' || *units == 'G') {
435 mult = 1024 * 1024 * 1024;
437 else if(*units == 'b' || *units == 'B') {
438 /* bits => mult = 1 */
441 fprintf(stderr, "Invalid units in value: %s\n", s);
446 if(sscanf(s, "%lld", &value) != 1) {
447 fprintf(stderr, "Error reading rate: %s\n", s);
449 options.max_bandwidth = value * mult;
456 * Read the net filter option.
458 int options_config_get_net_filter() {
460 s = config_get_string("net-filter");
464 options.netfilter = 0;
466 mask = strchr(s, '/');
468 fprintf(stderr, "Could not parse net/mask: %s\n", s);
473 if (inet_aton(s, &options.netfilternet) == 0) {
474 fprintf(stderr, "Invalid network address: %s\n", s);
477 /* Accept a netmask like /24 or /255.255.255.0. */
478 if (mask[strspn(mask, "0123456789")] == '\0') {
479 /* Whole string is numeric */
483 fprintf(stderr, "Invalid netmask length: %s\n", mask);
487 /* This needs to be special cased, although I don't fully
488 * understand why -pdw
490 options.netfiltermask.s_addr = htonl(0xffffffffl);
493 u_int32_t mm = 0xffffffffl;
495 options.netfiltermask.s_addr = htonl(~mm);
498 options.netfilter = 1;
501 if (inet_aton(mask, &options.netfiltermask) != 0)
502 options.netfilter = 1;
504 fprintf(stderr, "Invalid netmask: %s\n", s);
508 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
515 * Read the net filter IPv6 option.
517 int options_config_get_net_filter6() {
521 s = config_get_string("net-filter6");
525 options.netfilter6 = 0;
527 mask = strchr(s, '/');
529 fprintf(stderr, "Could not parse IPv6 net/prefix: %s\n", s);
534 if (inet_pton(AF_INET6, s, &options.netfilter6net) == 0) {
535 fprintf(stderr, "Invalid IPv6 network address: %s\n", s);
538 /* Accept prefix lengths and address expressions. */
539 if (mask[strspn(mask, "0123456789")] == '\0') {
540 /* Whole string is numeric */
544 if (n > 128 || n < 1) {
545 fprintf(stderr, "Invalid IPv6 prefix length: %s\n", mask);
549 const uint8_t mm = 0xff;
555 for (j=0; j < bl; ++j)
556 options.netfilter6mask.s6_addr[j] = mm;
559 options.netfilter6mask.s6_addr[bl] = part;
560 options.netfilter6 = 1;
564 if (inet_pton(AF_INET6, mask, &options.netfilter6mask) != 0)
565 options.netfilter6 = 1;
567 fprintf(stderr, "Invalid IPv6 netmask: %s\n", s);
571 /* Prepare any comparison by masking the provided filtered net. */
572 for (j=0; j < 16; ++j)
573 options.netfilter6net.s6_addr[j] &= options.netfilter6mask.s6_addr[j];
580 void options_make() {
581 options_config_get_string("interface", &options.interface);
582 options_config_get_bool("dns-resolution", &options.dnsresolution);
583 options_config_get_bool("port-resolution", &options.portresolution);
584 options_config_get_string("filter-code", &options.filtercode);
585 options_config_get_bool("show-bars", &options.showbars);
586 options_config_get_promiscuous();
587 options_config_get_bool("hide-source", &options.aggregate_src);
588 options_config_get_bool("hide-destination", &options.aggregate_dest);
589 options_config_get_bool("use-bytes", &options.bandwidth_in_bytes);
590 options_config_get_enum("sort", sort_enumeration, (int*)&options.sort);
591 options_config_get_enum("line-display", linedisplay_enumeration, (int*)&options.linedisplay);
592 options_config_get_bool("show-totals", &options.show_totals);
593 options_config_get_bool("log-scale", &options.log_scale);
594 options_config_get_bw_rate("max-bandwidth", &options.max_bandwidth);
595 options_config_get_enum("port-display", showports_enumeration, (int*)&options.showports);
596 options_config_get_string("screen-filter", &options.screenfilter);
597 options_config_get_bool("link-local", &options.link_local);
598 options_config_get_net_filter();
599 options_config_get_net_filter6();