options.c 7.7 KB
Newer Older
pdw's avatar
pdw committed
1
2
3
4
5
6
/*
 * options.c:
 *
 *
 */

7
8
#include "config.h"

chris's avatar
""    
chris committed
9
10
#include <sys/types.h>

pdw's avatar
pdw committed
11
12
#include <stdio.h>
#include <string.h>
chris's avatar
""    
chris committed
13
14
15
16
#include <stdlib.h>
#include <unistd.h>

#include <sys/ioctl.h>
17
#include <sys/socket.h>
chris's avatar
""    
chris committed
18
19
#include <netinet/in.h>
#include <arpa/inet.h>
20
#include <net/if.h>
chris's avatar
""    
chris committed
21
22

#include "iftop.h"
pdw's avatar
pdw committed
23
24
#include "options.h"

chris's avatar
""    
chris committed
25
26
27
28
#if !defined(HAVE_INET_ATON) && defined(HAVE_INET_PTON)
#   define inet_aton(a, b)  inet_pton(AF_INET, (a), (b))
#endif

pdw's avatar
pdw committed
29
30
options_t options;

31
char optstr[] = "+i:f:nN:hpbBPm:";
pdw's avatar
pdw committed
32
33
34

/* Global options. */

chris's avatar
""    
chris committed
35
36
37
38
39
40
/* Selecting an interface on which to listen: */

/* This is a list of interface name prefixes which are `bad' in the sense
 * that they don't refer to interfaces of external type on which we are
 * likely to want to listen. We also compare candidate interfaces to lo. */
static char *bad_interface_names[] = {
41
            "lo:",
42
	    "lo",
pdw's avatar
pdw committed
43
44
	    "stf",     /* pseudo-device 6to4 tunnel interface */
	    "gif",     /* psuedo-device generic tunnel interface */
chris's avatar
""    
chris committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
            "dummy",
            "vmnet",
            NULL        /* last entry must be NULL */
        };

static int is_bad_interface_name(char *i) {
    char **p;
    for (p = bad_interface_names; *p; ++p)
        if (strncmp(i, *p, strlen(*p)) == 0)
            return 1;
    return 0;
}

/* This finds the first interface which is up and is not the loopback
 * interface or one of the interface types listed in bad_interface_names. */
chris's avatar
""    
chris committed
60
static char *get_first_interface(void) {
61
    struct if_nameindex * nameindex;
chris's avatar
""    
chris committed
62
    char *i = NULL;
63
    int j = 0;
chris's avatar
""    
chris committed
64
    /* Use if_nameindex(3) instead? */
65
66
67
68
69
70
71
72
73

    nameindex = if_nameindex();
    if(nameindex == NULL) {
      return NULL;
    }

    while(nameindex[j].if_index != 0) {
        if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) {
            i = xstrdup(nameindex[j].if_name);
chris's avatar
""    
chris committed
74
75
            break;
        }
76
        j++;
chris's avatar
""    
chris committed
77
    }
78
    if_freenameindex(nameindex);
chris's avatar
""    
chris committed
79
80
81
    return i;
}

chris's avatar
""    
chris committed
82
static void set_defaults() {
chris's avatar
""    
chris committed
83
84
85
86
87
88
    /* Should go through the list of interfaces, and find the first one which
     * is up and is not lo or dummy*. */
    options.interface = get_first_interface();
    if (!options.interface)
        options.interface = "eth0";

pdw's avatar
pdw committed
89
90
91
92
93
    options.filtercode = NULL;
    options.netfilter = 0;
    inet_aton("10.0.1.0", &options.netfilternet);
    inet_aton("255.255.255.0", &options.netfiltermask);
    options.dnsresolution = 1;
94
    options.portresolution = 1;
95
96
97
98
#ifdef NEED_PROMISCUOUS_FOR_OUTGOING
    options.promiscuous = 1;
    options.promiscuous_but_choosy = 1;
#else
pdw's avatar
pdw committed
99
    options.promiscuous = 0;
100
101
    options.promiscuous_but_choosy = 0;
#endif
pdw's avatar
pdw committed
102
    options.showbars = 1;
pdw's avatar
pdw committed
103
    options.showports = OPTION_PORTS_OFF;
104
105
    options.aggregate_src = 0;
    options.aggregate_dest = 0;
pdw's avatar
pdw committed
106
    options.paused = 0;
pdw's avatar
pdw committed
107
    options.showhelp = 0;
108
    options.bandwidth_in_bytes = 0;
pdw's avatar
pdw committed
109
    options.sort = OPTION_SORT_DIV2;
pdw's avatar
pdw committed
110
    options.screenfilter = NULL;
111
    options.freezeorder = 0;
pdw's avatar
pdw committed
112
113
    options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE;
    options.screen_offset = 0;
pdw's avatar
pdw committed
114
    options.show_totals = 0;
115
    options.max_bandwidth = 0; /* auto */
pdw's avatar
pdw committed
116
117
    options.log_scale = 0;
    options.bar_interval = 1;
pdw's avatar
pdw committed
118
119
}

chris's avatar
""    
chris committed
120
static void die(char *msg) {
pdw's avatar
pdw committed
121
122
123
124
    fprintf(stderr, msg);
    exit(1);
}

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
static void set_max_bandwidth(char* arg) {
    char* units;
    long long mult = 1;
    long long value;
    units = arg + strspn(arg, "0123456789");
    if(strlen(units) > 1) {
        die("Invalid units\n");
    }
    if(strlen(units) == 1) {
        if(*units == 'k' || *units == 'K') {
            mult = 1024;
        }
        else if(*units == 'm' || *units == 'M') {
            mult = 1024 * 1024;
        }
        else if(*units == 'g' || *units == 'G') {
            mult = 1024 * 1024 * 1024;
        }
    }
    *units = '\0';
    if(sscanf(arg, "%lld", &value) != 1) {
        die("Error reading max bandwidth\n");
    }
    options.max_bandwidth = value * mult;
}

chris's avatar
""    
chris committed
151
static void set_net_filter(char* arg) {
pdw's avatar
pdw committed
152
153
    char* mask;

chris's avatar
""    
chris committed
154
155
    mask = strchr(arg, '/');
    if (mask == NULL) {
pdw's avatar
pdw committed
156
157
158
159
        die("Could not parse net/mask\n");
    }
    *mask = '\0';
    mask++;
chris's avatar
""    
chris committed
160
    if (inet_aton(arg, &options.netfilternet) == 0)
pdw's avatar
pdw committed
161
        die("Invalid network address\n");
chris's avatar
""    
chris committed
162
    /* Accept a netmask like /24 or /255.255.255.0. */
163
164
    if (mask[strspn(mask, "0123456789")] == '\0') {
        /* Whole string is numeric */
chris's avatar
""    
chris committed
165
166
        int n;
        n = atoi(mask);
167
        if (n > 32) {
chris's avatar
""    
chris committed
168
            die("Invalid netmask");
169
        }
chris's avatar
""    
chris committed
170
        else {
171
172
173
174
175
176
177
            if(n == 32) {
              /* This needs to be special cased, although I don't fully 
               * understand why -pdw 
               */
              options.netfiltermask.s_addr = htonl(0xffffffffl);
            }
            else {
pdw's avatar
pdw committed
178
              u_int32_t mm = 0xffffffffl;
179
180
181
              mm >>= n;
              options.netfiltermask.s_addr = htonl(~mm);
            }
chris's avatar
""    
chris committed
182
        }
183
184
    } 
    else if (inet_aton(mask, &options.netfiltermask) == 0) {
chris's avatar
""    
chris committed
185
        die("Invalid netmask\n");
186
187
    }
    options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
pdw's avatar
pdw committed
188
189
190
191
192
193
194

    options.netfilter = 1;

}

/* usage:
 * Print usage information. */
chris's avatar
""    
chris committed
195
static void usage(FILE *fp) {
pdw's avatar
pdw committed
196
197
198
    fprintf(fp,
"iftop: display bandwidth usage on an interface by host\n"
"\n"
199
"Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter code] [-N net/mask]\n"
pdw's avatar
pdw committed
200
201
"\n"
"   -h                  display this message\n"
202
"   -n                  don't do hostname lookups\n"
pdw's avatar
pdw committed
203
204
"   -p                  run in promiscuous mode (show traffic between other\n"
"                       hosts on the same network segment)\n"
chris's avatar
""    
chris committed
205
"   -b                  don't display a bar graph of traffic\n"
206
"   -B                  Display bandwidth in bytes\n"
chris's avatar
""    
chris committed
207
"   -i interface        listen on named interface\n"
pdw's avatar
pdw committed
208
209
"   -f filter code      use filter code to select packets to count\n"
"                       (default: none, but only IP packets are counted)\n"
210
"   -N net/mask         show traffic flows in/out of network\n"
pdw's avatar
pdw committed
211
"   -P                  show ports as well as hosts\n"
212
"   -m limit            sets the upper limit for the bandwidth scale\n"
pdw's avatar
pdw committed
213
"\n"
pdw's avatar
pdw committed
214
215
"iftop, version " IFTOP_VERSION "\n"
"copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
pdw's avatar
pdw committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
            );
}

void options_read(int argc, char **argv) {
    int opt;

    set_defaults();

    opterr = 0;
    while ((opt = getopt(argc, argv, optstr)) != -1) {
        switch (opt) {
            case 'h':
                usage(stdout);
                exit(0);

231
            case 'n':
pdw's avatar
pdw committed
232
233
234
235
236
237
238
239
                options.dnsresolution = 0;
                break;

            case 'i':
                options.interface = optarg;
                break;

            case 'f':
chris's avatar
""    
chris committed
240
                options.filtercode = xstrdup(optarg);
pdw's avatar
pdw committed
241
242
243
244
                break;

            case 'p':
                options.promiscuous = 1;
245
                options.promiscuous_but_choosy = 0;
pdw's avatar
pdw committed
246
247
                break;

pdw's avatar
pdw committed
248
249
250
251
            case 'P':
                options.showports = OPTION_PORTS_ON;
                break;

252
            case 'N':
pdw's avatar
pdw committed
253
254
                set_net_filter(optarg);
                break;
255
256
257
258
259
            
            case 'm':
                set_max_bandwidth(optarg);
                break;

pdw's avatar
pdw committed
260

chris's avatar
""    
chris committed
261
262
263
264
            case 'b':
                options.showbars = 0;
                break;

265
266
267
            case 'B':
                options.bandwidth_in_bytes = 1;
                break;
pdw's avatar
pdw committed
268

pdw's avatar
pdw committed
269
270
271
272
273
274
275
276
277
278
279
280
            case '?':
                fprintf(stderr, "iftop: unknown option -%c\n", optopt);
                usage(stderr);
                exit(1);

            case ':':
                fprintf(stderr, "iftop: option -%c requires an argument\n", optopt);
                usage(stderr);
                exit(1);
        }
    }

pdw's avatar
0.10.    
pdw committed
281
282
283
284
285
286
    if (optind != argc) {
        fprintf(stderr, "iftop: found arguments following options\n");
        fprintf(stderr, "*** some options have changed names since v0.9 ***\n");
        usage(stderr);
        exit(1);
    }
pdw's avatar
pdw committed
287
}