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

chris's avatar
chris committed
7 8
#include <sys/types.h>

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

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

#include "iftop.h"
pdw's avatar
pdw committed
21 22
#include "options.h"

chris's avatar
chris committed
23 24 25 26
#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
27 28
options_t options;

pdw's avatar
pdw committed
29
char optstr[] = "+i:f:nN:hpbBP";
pdw's avatar
pdw committed
30 31 32

/* Global options. */

chris's avatar
chris committed
33 34 35 36 37 38
/* 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[] = {
39
            "lo:",
chris's avatar
chris committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            "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
55
static char *get_first_interface(void) {
56
    struct if_nameindex * nameindex;
chris's avatar
chris committed
57
    char *i = NULL;
58
    int j = 0;
chris's avatar
chris committed
59
    /* Use if_nameindex(3) instead? */
60 61 62 63 64 65 66 67 68

    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
69 70
            break;
        }
71
        j++;
chris's avatar
chris committed
72
    }
73
    if_freenameindex(nameindex);
chris's avatar
chris committed
74 75 76
    return i;
}

chris's avatar
chris committed
77
static void set_defaults() {
chris's avatar
chris committed
78 79 80 81 82 83
    /* 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
84 85 86 87 88
    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;
89
    options.portresolution = 1;
pdw's avatar
pdw committed
90
    options.promiscuous = 0;
pdw's avatar
pdw committed
91
    options.showbars = 1;
92
    options.showports = OPTION_PORTS_OFF;
93 94
    options.aggregate_src = 0;
    options.aggregate_dest = 0;
pdw's avatar
pdw committed
95
    options.paused = 0;
pdw's avatar
pdw committed
96
    options.showhelp = 0;
97
    options.bandwidth_in_bytes = 0;
pdw's avatar
pdw committed
98
    options.sort = OPTION_SORT_DIV2;
99
    options.screenfilter = NULL;
100
    options.freezeorder = 0;
pdw's avatar
pdw committed
101 102
    options.linedisplay = OPTION_LINEDISPLAY_TWO_LINE;
    options.screen_offset = 0;
pdw's avatar
pdw committed
103 104
}

chris's avatar
chris committed
105
static void die(char *msg) {
pdw's avatar
pdw committed
106 107 108 109
    fprintf(stderr, msg);
    exit(1);
}

chris's avatar
chris committed
110
static void set_net_filter(char* arg) {
pdw's avatar
pdw committed
111 112
    char* mask;

chris's avatar
chris committed
113 114
    mask = strchr(arg, '/');
    if (mask == NULL) {
pdw's avatar
pdw committed
115 116 117 118
        die("Could not parse net/mask\n");
    }
    *mask = '\0';
    mask++;
chris's avatar
chris committed
119
    if (inet_aton(arg, &options.netfilternet) == 0)
pdw's avatar
pdw committed
120
        die("Invalid network address\n");
chris's avatar
chris committed
121
    /* Accept a netmask like /24 or /255.255.255.0. */
122 123
    if (mask[strspn(mask, "0123456789")] == '\0') {
        /* Whole string is numeric */
chris's avatar
chris committed
124 125
        int n;
        n = atoi(mask);
126
        if (n > 32) {
chris's avatar
chris committed
127
            die("Invalid netmask");
128
        }
chris's avatar
chris committed
129
        else {
130 131 132 133 134 135 136 137 138 139 140
            if(n == 32) {
              /* This needs to be special cased, although I don't fully 
               * understand why -pdw 
               */
              options.netfiltermask.s_addr = htonl(0xffffffffl);
            }
            else {
              uint32_t mm = 0xffffffffl;
              mm >>= n;
              options.netfiltermask.s_addr = htonl(~mm);
            }
chris's avatar
chris committed
141
        }
142 143
    } 
    else if (inet_aton(mask, &options.netfiltermask) == 0) {
chris's avatar
chris committed
144
        die("Invalid netmask\n");
145 146
    }
    options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
pdw's avatar
pdw committed
147 148 149 150 151 152 153

    options.netfilter = 1;

}

/* usage:
 * Print usage information. */
chris's avatar
chris committed
154
static void usage(FILE *fp) {
pdw's avatar
pdw committed
155 156 157
    fprintf(fp,
"iftop: display bandwidth usage on an interface by host\n"
"\n"
158
"Synopsis: iftop -h | [-npbBP] [-i interface] [-f filter code] [-N net/mask]\n"
pdw's avatar
pdw committed
159 160
"\n"
"   -h                  display this message\n"
161
"   -n                  don't do hostname lookups\n"
pdw's avatar
pdw committed
162 163
"   -p                  run in promiscuous mode (show traffic between other\n"
"                       hosts on the same network segment)\n"
chris's avatar
chris committed
164
"   -b                  don't display a bar graph of traffic\n"
165
"   -B                  Display bandwidth in bytes\n"
chris's avatar
chris committed
166
"   -i interface        listen on named interface\n"
pdw's avatar
pdw committed
167 168
"   -f filter code      use filter code to select packets to count\n"
"                       (default: none, but only IP packets are counted)\n"
169
"   -N net/mask         show traffic flows in/out of network\n"
170
"   -P                  show ports as well as hosts\n"
pdw's avatar
pdw committed
171
"\n"
pdw's avatar
pdw committed
172 173
"iftop, version " IFTOP_VERSION "\n"
"copyright (c) 2002 Paul Warren <pdw@ex-parrot.com> and contributors\n"
pdw's avatar
pdw committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            );
}

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

189
            case 'n':
pdw's avatar
pdw committed
190 191 192 193 194 195 196 197
                options.dnsresolution = 0;
                break;

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

            case 'f':
chris's avatar
chris committed
198
                options.filtercode = xstrdup(optarg);
pdw's avatar
pdw committed
199 200 201 202 203 204
                break;

            case 'p':
                options.promiscuous = 1;
                break;

205 206 207 208
            case 'P':
                options.showports = OPTION_PORTS_ON;
                break;

209
            case 'N':
pdw's avatar
pdw committed
210 211 212
                set_net_filter(optarg);
                break;

chris's avatar
chris committed
213 214 215 216
            case 'b':
                options.showbars = 0;
                break;

217 218 219
            case 'B':
                options.bandwidth_in_bytes = 1;
                break;
220

pdw's avatar
pdw committed
221 222 223 224 225 226 227 228 229 230 231 232
            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
233 234 235 236 237 238
    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
239
}