Import to git/gerrit
[profile/ivi/iftop.git] / options.c
1 /*
2  * options.c:
3  *
4  *
5  */
6
7 #include "config.h"
8
9 #include <sys/types.h>
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15
16 #include <sys/ioctl.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <net/if.h>
21
22 #include "iftop.h"
23 #include "options.h"
24 #include "cfgfile.h"
25 #include "integers.h"
26
27 #if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON)
28 #   define inet_aton(a, b)  inet_pton(AF_INET, (a), (b))
29 #endif
30
31 options_t options;
32
33 char optstr[] = "+i:f:nNF:G:lhpbBPm:c:";
34
35 /* Global options. */
36
37 /* Selecting an interface on which to listen: */
38
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[] = {
43             "lo:",
44             "lo",
45             "stf",     /* pseudo-device 6to4 tunnel interface */
46             "gif",     /* psuedo-device generic tunnel interface */
47             "dummy",
48             "vmnet",
49             "wmaster", /* wmaster0 is an internal-use interface for mac80211, a Linux WiFi API. */
50             NULL       /* last entry must be NULL */
51         };
52
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 },
59         { NULL, -1 }
60 };
61
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 },
67         { NULL, -1 }
68 };
69
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 },
75         { NULL, -1 }
76 };
77
78 static int is_bad_interface_name(char *i) {
79     char **p;
80     for (p = bad_interface_names; *p; ++p)
81         if (strncmp(i, *p, strlen(*p)) == 0)
82             return 1;
83     return 0;
84 }
85
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;
90     struct ifreq ifr;
91     char *i = NULL;
92     int j = 0;
93     int s;
94     /* Use if_nameindex(3) instead? */
95
96     nameindex = if_nameindex();
97     if(nameindex == NULL) {
98         return NULL;
99     }
100
101     s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
102
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);
108                 break;
109             }
110         }
111         j++;
112     }
113     if_freenameindex(nameindex);
114     return i;
115 }
116
117 void options_set_defaults() {
118     char *s;
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";
124
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;
138 #else
139     options.promiscuous = 0;
140     options.promiscuous_but_choosy = 0;
141 #endif
142     options.showbars = 1;
143     options.showports = OPTION_PORTS_OFF;
144     options.aggregate_src = 0;
145     options.aggregate_dest = 0;
146     options.paused = 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
159     /* Figure out the name for the config file */
160     s = getenv("HOME");
161     if(s != NULL) {
162         int i = strlen(s) + 9 + 1;
163         options.config_file = xmalloc(i);
164         snprintf(options.config_file,i,"%s/.iftoprc",s);
165     }
166     else {
167         options.config_file = xstrdup("iftoprc");
168     }
169     options.config_file_specified = 0;
170     
171 }
172
173 static void die(char *msg) {
174     fprintf(stderr, "%s", msg);
175     exit(1);
176 }
177
178 static void set_max_bandwidth(char* arg) {
179     char* units;
180     long long mult = 1;
181     long long value;
182     units = arg + strspn(arg, "0123456789");
183     if(strlen(units) > 1) {
184         die("Invalid units\n");
185     }
186     if(strlen(units) == 1) {
187         if(*units == 'k' || *units == 'K') {
188             mult = 1024;
189         }
190         else if(*units == 'm' || *units == 'M') {
191             mult = 1024 * 1024;
192         }
193         else if(*units == 'g' || *units == 'G') {
194             mult = 1024 * 1024 * 1024;
195         }
196     }
197     *units = '\0';
198     if(sscanf(arg, "%lld", &value) != 1) {
199         die("Error reading max bandwidth\n");
200     }
201     options.max_bandwidth = value * mult;
202 }
203
204 static void set_net_filter(char* arg) {
205     char* mask;
206
207     mask = strchr(arg, '/');
208     if (mask == NULL) {
209         die("Could not parse net/mask\n");
210     }
211     *mask = '\0';
212     mask++;
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 */
218         int n;
219         n = atoi(mask);
220         if (n > 32) {
221             die("Invalid netmask");
222         }
223         else {
224             if(n == 32) {
225               /* This needs to be special cased, although I don't fully 
226                * understand why -pdw 
227                */
228               options.netfiltermask.s_addr = htonl(0xffffffffl);
229             }
230             else {
231               u_int32_t mm = 0xffffffffl;
232               mm >>= n;
233               options.netfiltermask.s_addr = htonl(~mm);
234             }
235         }
236     } 
237     else if (inet_aton(mask, &options.netfiltermask) == 0) {
238         die("Invalid netmask\n");
239     }
240     options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
241
242     options.netfilter = 1;
243
244 }
245
246 /* usage:
247  * Print usage information. */
248 static void usage(FILE *fp) {
249     fprintf(fp,
250 "iftop: display bandwidth usage on an interface by host\n"
251 "\n"
252 "Synopsis: iftop -h | [-npblNBP] [-i interface] [-f filter code]\n"
253 "                               [-F net/mask] [-G net6/mask6]\n"
254 "\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"
271 "\n"
272 "iftop, version " IFTOP_VERSION "\n"
273 "copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
274             );
275 }
276
277 void options_read_args(int argc, char **argv) {
278     int opt;
279
280     opterr = 0;
281     while ((opt = getopt(argc, argv, optstr)) != -1) {
282         switch (opt) {
283             case 'h':
284                 usage(stdout);
285                 exit(0);
286
287             case 'n':
288                 config_set_string("dns-resolution","false");
289                 break;
290
291             case 'N':
292                 config_set_string("port-resolution","false");
293                 break;
294
295             case 'i':
296                 config_set_string("interface", optarg);
297                 break;
298
299             case 'f':
300                 config_set_string("filter-code", optarg);
301                 break;
302
303             case 'l':
304                 config_set_string("link-local", "true");
305                 break;
306
307             case 'p':
308                 config_set_string("promiscuous", "true");
309                 break;
310
311             case 'P':
312                 config_set_string("port-display", "on");
313                 break;
314
315             case 'F':
316                 config_set_string("net-filter", optarg);
317                 break;
318             
319             case 'G':
320                 config_set_string("net-filter6", optarg);
321                 break;
322
323             case 'm':
324                 config_set_string("max-bandwidth", optarg);
325                 break;
326
327             case 'b':
328                 config_set_string("show-bars", "false");
329                 break;
330
331             case 'B':
332                 config_set_string("use-bytes", "true");
333                 break;
334
335             case 'c':
336                 xfree(options.config_file);
337                 options.config_file = xstrdup(optarg);
338                 options.config_file_specified = 1;
339                 break;
340
341             case '?':
342                 fprintf(stderr, "iftop: unknown option -%c\n", optopt);
343                 usage(stderr);
344                 exit(1);
345
346             case ':':
347                 fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
348                 usage(stderr);
349                 exit(1);
350         }
351     }
352
353
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");
357         usage(stderr);
358         exit(1);
359     }
360 }
361
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) {
366     char *s;
367     s = config_get_string(name);
368     if(s != NULL) {
369         *value = xstrdup(s);
370         return 1;
371     }
372     return 0;
373 }
374
375 int options_config_get_bool(const char *name, int* value) {
376     if(config_get_string(name)) {
377         *value = config_get_bool(name);
378         return 1;
379     }
380     return 0;
381 }
382
383 int options_config_get_int(const char *name, int* value) {
384     if(config_get_string(name)) {
385         config_get_int(name, value);
386         return 1;
387     }
388     return 0;
389 }
390
391 int options_config_get_enum(char *name, config_enumeration_type* enumeration, int *result) {
392     int i;
393     if(config_get_string(name)) {
394         if(config_get_enum(name, enumeration, &i)) {
395             *result = i; 
396             return 1;
397         }
398     }
399     return 0;
400 }
401
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
407              * choosy */
408             options.promiscuous_but_choosy = 0;
409         }
410         return 1;
411     }
412     return 0;
413 }
414
415 int options_config_get_bw_rate(char *directive, long long* result) {
416     char* units;
417     long long mult = 1;
418     long long value;
419     char *s;
420     s = config_get_string(directive);
421     if(s) {
422         units = s + strspn(s, "0123456789");
423         if(strlen(units) > 1) {
424             fprintf(stderr, "Invalid units in value: %s\n", s);
425             return 0;
426         }
427         if(strlen(units) == 1) {
428             if(*units == 'k' || *units == 'K') {
429                 mult = 1024;
430             }
431             else if(*units == 'm' || *units == 'M') {
432                 mult = 1024 * 1024;
433             }
434             else if(*units == 'g' || *units == 'G') {
435                 mult = 1024 * 1024 * 1024;
436             }
437             else if(*units == 'b' || *units == 'B') {
438                 /* bits => mult = 1 */
439             }
440             else {
441                 fprintf(stderr, "Invalid units in value: %s\n", s);
442                 return 0;
443             }
444         }
445         *units = '\0';
446         if(sscanf(s, "%lld", &value) != 1) {
447             fprintf(stderr, "Error reading rate: %s\n", s);
448         }
449         options.max_bandwidth = value * mult;
450         return 1;
451     }
452     return 0;
453 }
454
455 /*
456  * Read the net filter option.  
457  */
458 int options_config_get_net_filter() {
459     char* s;
460     s = config_get_string("net-filter");
461     if(s) {
462         char* mask;
463
464         options.netfilter = 0;
465
466         mask = strchr(s, '/');
467         if (mask == NULL) {
468             fprintf(stderr, "Could not parse net/mask: %s\n", s);
469             return 0;
470         }
471         *mask = '\0';
472         mask++;
473         if (inet_aton(s, &options.netfilternet) == 0) {
474             fprintf(stderr, "Invalid network address: %s\n", s);
475             return 0;
476         }
477         /* Accept a netmask like /24 or /255.255.255.0. */
478         if (mask[strspn(mask, "0123456789")] == '\0') {
479             /* Whole string is numeric */
480             int n;
481             n = atoi(mask);
482             if (n > 32) {
483                 fprintf(stderr, "Invalid netmask length: %s\n", mask);
484             }
485             else {
486                 if(n == 32) {
487                   /* This needs to be special cased, although I don't fully 
488                    * understand why -pdw 
489                    */
490                   options.netfiltermask.s_addr = htonl(0xffffffffl);
491                 }
492                 else {
493                   u_int32_t mm = 0xffffffffl;
494                   mm >>= n;
495                   options.netfiltermask.s_addr = htonl(~mm);
496                 }
497             }
498             options.netfilter = 1;
499         } 
500         else {
501             if (inet_aton(mask, &options.netfiltermask) != 0)
502                 options.netfilter = 1;
503             else {
504                 fprintf(stderr, "Invalid netmask: %s\n", s);
505                 return 0;
506             }
507         }
508         options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
509         return 1;
510     }
511     return 0;
512 }
513
514 /*
515  * Read the net filter IPv6 option.  
516  */
517 int options_config_get_net_filter6() {
518     char* s;
519     int j;
520
521     s = config_get_string("net-filter6");
522     if(s) {
523         char* mask;
524
525         options.netfilter6 = 0;
526
527         mask = strchr(s, '/');
528         if (mask == NULL) {
529             fprintf(stderr, "Could not parse IPv6 net/prefix: %s\n", s);
530             return 0;
531         }
532         *mask = '\0';
533         mask++;
534         if (inet_pton(AF_INET6, s, &options.netfilter6net) == 0) {
535             fprintf(stderr, "Invalid IPv6 network address: %s\n", s);
536             return 0;
537         }
538         /* Accept prefix lengths and address expressions. */
539         if (mask[strspn(mask, "0123456789")] == '\0') {
540             /* Whole string is numeric */
541             unsigned int n;
542
543             n = atoi(mask);
544             if (n > 128 || n < 1) {
545                 fprintf(stderr, "Invalid IPv6 prefix length: %s\n", mask);
546             }
547             else {
548                 int bl, rem;
549                 const uint8_t mm = 0xff;
550                 uint8_t part = mm;
551
552                 bl = n / 8;
553                 rem = n % 8;
554                 part <<= 8 - rem;
555                 for (j=0; j < bl; ++j)
556                     options.netfilter6mask.s6_addr[j] = mm;
557
558                 if (rem > 0)
559                     options.netfilter6mask.s6_addr[bl] = part;
560                 options.netfilter6 = 1;
561               }
562         }
563         else {
564             if (inet_pton(AF_INET6, mask, &options.netfilter6mask) != 0)
565                 options.netfilter6 = 1;
566             else {
567                 fprintf(stderr, "Invalid IPv6 netmask: %s\n", s);
568                 return 0;
569             }
570         }
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];
574
575         return 1;
576     }
577     return 0;
578 }
579
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();
600 };