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:lhpbBu:Pm: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 config_enumeration_type bandwidth_unit_enumeration[] = {
79 { "bits", OPTION_BW_BITS },
80 { "bytes", OPTION_BW_BYTES },
81 { "packets", OPTION_BW_PKTS },
85 static int is_bad_interface_name(char *i) {
87 for (p = bad_interface_names; *p; ++p)
88 if (strncmp(i, *p, strlen(*p)) == 0)
93 /* This finds the first interface which is up and is not the loopback
94 * interface or one of the interface types listed in bad_interface_names. */
95 static char *get_first_interface(void) {
96 struct if_nameindex * nameindex;
101 /* Use if_nameindex(3) instead? */
103 nameindex = if_nameindex();
104 if(nameindex == NULL) {
108 s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
110 while(nameindex[j].if_index != 0) {
111 if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) {
112 strncpy(ifr.ifr_name, nameindex[j].if_name, sizeof(ifr.ifr_name));
113 if ((s == -1) || (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) || (ifr.ifr_flags & IFF_UP)) {
114 i = xstrdup(nameindex[j].if_name);
120 if_freenameindex(nameindex);
124 void options_set_defaults() {
126 /* Should go through the list of interfaces, and find the first one which
127 * is up and is not lo or dummy*. */
128 options.interface = get_first_interface();
129 if (!options.interface)
130 options.interface = "eth0";
132 options.filtercode = NULL;
133 options.netfilter = 0;
134 inet_aton("10.0.1.0", &options.netfilternet);
135 inet_aton("255.255.255.0", &options.netfiltermask);
136 options.netfilter6 = 0;
137 inet_pton(AF_INET6, "fe80::", &options.netfilter6net); /* Link-local */
138 inet_pton(AF_INET6, "ffff::", &options.netfilter6mask);
139 options.link_local = 0;
140 options.dnsresolution = 1;
141 options.portresolution = 1;
142 #ifdef NEED_PROMISCUOUS_FOR_OUTGOING
143 options.promiscuous = 1;
144 options.promiscuous_but_choosy = 1;
146 options.promiscuous = 0;
147 options.promiscuous_but_choosy = 0;
149 options.showbars = 1;
150 options.showports = OPTION_PORTS_OFF;
151 options.aggregate_src = 0;
152 options.aggregate_dest = 0;
154 options.showhelp = 0;
155 options.bandwidth_unit = OPTION_BW_BITS;
156 options.sort = OPTION_SORT_DIV2;
157 options.screenfilter = NULL;
158 options.freezeorder = 0;
159 options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE;
160 options.screen_offset = 0;
161 options.show_totals = 0;
162 options.max_bandwidth = 0; /* auto */
163 options.log_scale = 0;
164 options.bar_interval = 1;
165 options.timed_output = 0;
166 options.no_curses = 0;
167 options.num_lines = 10;
169 /* Figure out the name for the config file */
172 int i = strlen(s) + 9 + 1;
173 options.config_file = xmalloc(i);
174 snprintf(options.config_file,i,"%s/.iftoprc",s);
177 options.config_file = xstrdup("iftoprc");
179 options.config_file_specified = 0;
184 * Print usage information. */
185 static void usage(FILE *fp) {
187 "iftop: display bandwidth usage on an interface by host\n"
189 "Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]\n"
190 " [-F net/mask] [-G net6/mask6]\n"
192 " -h display this message\n"
193 " -n don't do hostname lookups\n"
194 " -N don't convert port numbers to services\n"
195 " -p run in promiscuous mode (show traffic between other\n"
196 " hosts on the same network segment)\n"
197 " -b don't display a bar graph of traffic\n"
198 " -B display bandwidth in bytes\n"
199 " -a display bandwidth in packets\n"
200 " -i interface listen on named interface\n"
201 " -f filter code use filter code to select packets to count\n"
202 " (default: none, but only IP packets are counted)\n"
203 " -F net/mask show traffic flows in/out of IPv4 network\n"
204 " -G net6/mask6 show traffic flows in/out of IPv6 network\n"
205 " -l display and count link-local IPv6 traffic (default: off)\n"
206 " -P show ports as well as hosts\n"
207 " -m limit sets the upper limit for the bandwidth scale\n"
208 " -c config file specifies an alternative configuration file\n"
209 " -t use text interface without ncurses\n"
212 " -o 2s Sort by first column (2s traffic average)\n"
213 " -o 10s Sort by second column (10s traffic average) [default]\n"
214 " -o 40s Sort by third column (40s traffic average)\n"
215 " -o source Sort by source address\n"
216 " -o destination Sort by destination address\n"
218 " The following options are only available in combination with -t\n"
219 " -s num print one single text output afer num seconds, then quit\n"
220 " -L num number of lines to print\n"
222 "iftop, version " PACKAGE_VERSION "\n"
223 "copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
227 void options_read_args(int argc, char **argv) {
231 while ((opt = getopt(argc, argv, optstr)) != -1) {
238 config_set_string("dns-resolution","false");
242 config_set_string("port-resolution","false");
246 config_set_string("interface", optarg);
250 config_set_string("filter-code", optarg);
254 config_set_string("link-local", "true");
258 config_set_string("promiscuous", "true");
262 config_set_string("port-display", "on");
266 config_set_string("net-filter", optarg);
270 config_set_string("net-filter6", optarg);
274 config_set_string("max-bandwidth", optarg);
278 config_set_string("show-bars", "false");
282 config_set_string("bandwidth-unit", "bytes");
286 config_set_string("bandwidth-unit", optarg);
290 config_set_string("timed-output", optarg);
294 config_set_string("no-curses", "true");
298 config_set_string("num-lines", optarg);
302 config_set_string("sort", optarg);
306 xfree(options.config_file);
307 options.config_file = xstrdup(optarg);
308 options.config_file_specified = 1;
312 fprintf(stderr, "iftop: unknown option -%c\n", optopt);
317 fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
324 if (optind != argc) {
325 fprintf(stderr, "iftop: found arguments following options\n");
326 fprintf(stderr, "*** some options have changed names since v0.9 ***\n");
332 /* options_config_get_string:
333 * Gets a value from the config, sets *value to a copy of the value, if
334 * found. Leaves the option unchanged otherwise. */
335 int options_config_get_string(const char *name, char** value) {
337 s = config_get_string(name);
345 int options_config_get_bool(const char *name, int* value) {
346 if(config_get_string(name)) {
347 *value = config_get_bool(name);
353 int options_config_get_int(const char *name, int* value) {
354 if(config_get_string(name)) {
355 config_get_int(name, value);
361 int options_config_get_enum(char *name, config_enumeration_type* enumeration, int *result) {
363 if(config_get_string(name)) {
364 if(config_get_enum(name, enumeration, &i)) {
372 int options_config_get_promiscuous() {
373 if(config_get_string("promiscuous")) {
374 options.promiscuous = config_get_bool("promiscuous");
375 if(options.promiscuous) {
376 /* User has explicitly requested promiscuous mode, so don't be
378 options.promiscuous_but_choosy = 0;
385 int options_config_get_bw_unit() {
388 if (options_config_get_enum("bandwidth-unit", bandwidth_unit_enumeration,
389 (int*)&options.bandwidth_unit))
391 /* compatibility with use-bytes / -B */
392 if (options_config_get_bool("use-bytes", &i)) {
394 options.bandwidth_unit = OPTION_BW_BYTES;
396 options.bandwidth_unit = OPTION_BW_BITS;
402 int options_config_get_bw_rate(char *directive, long long* result) {
407 s = config_get_string(directive);
409 units = s + strspn(s, "0123456789");
410 if(strlen(units) > 1) {
411 fprintf(stderr, "Invalid units in value: %s\n", s);
414 if(strlen(units) == 1) {
415 if(*units == 'k' || *units == 'K') {
418 else if(*units == 'm' || *units == 'M') {
421 else if(*units == 'g' || *units == 'G') {
422 mult = 1024 * 1024 * 1024;
424 else if(*units == 'b' || *units == 'B') {
425 /* bits => mult = 1 */
428 fprintf(stderr, "Invalid units in value: %s\n", s);
433 if(sscanf(s, "%lld", &value) != 1) {
434 fprintf(stderr, "Error reading rate: %s\n", s);
436 options.max_bandwidth = value * mult;
443 * Read the net filter option.
445 int options_config_get_net_filter() {
447 s = config_get_string("net-filter");
451 options.netfilter = 0;
453 mask = strchr(s, '/');
455 fprintf(stderr, "Could not parse net/mask: %s\n", s);
460 if (inet_aton(s, &options.netfilternet) == 0) {
461 fprintf(stderr, "Invalid network address: %s\n", s);
464 /* Accept a netmask like /24 or /255.255.255.0. */
465 if (mask[strspn(mask, "0123456789")] == '\0') {
466 /* Whole string is numeric */
470 fprintf(stderr, "Invalid netmask length: %s\n", mask);
474 /* This needs to be special cased, although I don't fully
475 * understand why -pdw
477 options.netfiltermask.s_addr = htonl(0xffffffffl);
480 u_int32_t mm = 0xffffffffl;
482 options.netfiltermask.s_addr = htonl(~mm);
485 options.netfilter = 1;
488 if (inet_aton(mask, &options.netfiltermask) != 0)
489 options.netfilter = 1;
491 fprintf(stderr, "Invalid netmask: %s\n", s);
495 options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
502 * Read the net filter IPv6 option.
504 int options_config_get_net_filter6() {
508 s = config_get_string("net-filter6");
512 options.netfilter6 = 0;
514 mask = strchr(s, '/');
516 fprintf(stderr, "Could not parse IPv6 net/prefix: %s\n", s);
521 if (inet_pton(AF_INET6, s, &options.netfilter6net) == 0) {
522 fprintf(stderr, "Invalid IPv6 network address: %s\n", s);
525 /* Accept prefix lengths and address expressions. */
526 if (mask[strspn(mask, "0123456789")] == '\0') {
527 /* Whole string is numeric */
531 if (n > 128 || n < 1) {
532 fprintf(stderr, "Invalid IPv6 prefix length: %s\n", mask);
536 const uint8_t mm = 0xff;
542 for (j=0; j < bl; ++j)
543 options.netfilter6mask.s6_addr[j] = mm;
546 options.netfilter6mask.s6_addr[bl] = part;
547 options.netfilter6 = 1;
551 if (inet_pton(AF_INET6, mask, &options.netfilter6mask) != 0)
552 options.netfilter6 = 1;
554 fprintf(stderr, "Invalid IPv6 netmask: %s\n", s);
558 /* Prepare any comparison by masking the provided filtered net. */
559 for (j=0; j < 16; ++j)
560 options.netfilter6net.s6_addr[j] &= options.netfilter6mask.s6_addr[j];
567 void options_make() {
568 options_config_get_string("interface", &options.interface);
569 options_config_get_bool("dns-resolution", &options.dnsresolution);
570 options_config_get_bool("port-resolution", &options.portresolution);
571 options_config_get_string("filter-code", &options.filtercode);
572 options_config_get_bool("show-bars", &options.showbars);
573 options_config_get_promiscuous();
574 options_config_get_bool("hide-source", &options.aggregate_src);
575 options_config_get_bool("hide-destination", &options.aggregate_dest);
576 options_config_get_bw_unit();
577 options_config_get_enum("sort", sort_enumeration, (int*)&options.sort);
578 options_config_get_enum("line-display", linedisplay_enumeration, (int*)&options.linedisplay);
579 options_config_get_bool("show-totals", &options.show_totals);
580 options_config_get_bool("log-scale", &options.log_scale);
581 options_config_get_bw_rate("max-bandwidth", &options.max_bandwidth);
582 options_config_get_enum("port-display", showports_enumeration, (int*)&options.showports);
583 options_config_get_string("screen-filter", &options.screenfilter);
584 options_config_get_bool("link-local", &options.link_local);
585 options_config_get_int("timed-output", &options.timed_output);
586 options_config_get_bool("no-curses", &options.no_curses);
587 options_config_get_int("num-lines", &options.num_lines);
588 options_config_get_net_filter();
589 options_config_get_net_filter6();