options.c 7.69 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;
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;
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 116
    options.max_bandwidth = 0; /* auto */
    options.log_scale = 1; /* auto */
pdw's avatar
pdw committed
117 118
}

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

124 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
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
150
static void set_net_filter(char* arg) {
pdw's avatar
pdw committed
151 152
    char* mask;

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

    options.netfilter = 1;

}

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

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);

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

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

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

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

247 248 249 250
            case 'P':
                options.showports = OPTION_PORTS_ON;
                break;

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

pdw's avatar
pdw committed
259

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

264 265 266
            case 'B':
                options.bandwidth_in_bytes = 1;
                break;
267

pdw's avatar
pdw committed
268 269 270 271 272 273 274 275 276 277 278 279
            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
pdw committed
280 281 282 283 284 285
    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
286
}