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

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

#include <sys/ioctl.h>
13 14
#include <sys/socket.h>
#include <net/if.h>
chris's avatar
chris committed
15 16

#include "iftop.h"
pdw's avatar
pdw committed
17 18 19 20
#include "options.h"

options_t options;

21
char optstr[] = "+i:f:n:NhpbBP";
pdw's avatar
pdw committed
22 23 24

/* Global options. */

chris's avatar
chris committed
25 26 27 28 29 30
/* 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[] = {
31
            "lo:",
chris's avatar
chris committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
            "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
47 48 49
static char *get_first_interface(void) {
    int s, size = 1;
    struct ifreq *ifr;
pdw's avatar
pdw committed
50
    struct ifconf ifc = {0};
chris's avatar
chris committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    char *i = NULL;
    /* Use if_nameindex(3) instead? */
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
        return NULL;
    ifc.ifc_len = sizeof *ifr;
    do {
        ++size;
        ifc.ifc_req = xrealloc(ifc.ifc_req, size * sizeof *ifc.ifc_req);
        ifc.ifc_len = size * sizeof *ifc.ifc_req;
        if (ioctl(s, SIOCGIFCONF, &ifc) == -1) {
            perror("SIOCGIFCONF");
            return NULL;
        }
    } while (size * sizeof *ifc.ifc_req <= ifc.ifc_len);
    /* Ugly. */
    for (ifr = ifc.ifc_req; (char*)ifr < (char*)ifc.ifc_req + ifc.ifc_len; ++ifr) {
chris's avatar
chris committed
67
        if (strcmp(ifr->ifr_name, "lo") != 0 && !is_bad_interface_name(ifr->ifr_name)
chris's avatar
chris committed
68 69 70 71 72 73 74 75 76
            && ioctl(s, SIOCGIFFLAGS, ifr) == 0 && ifr->ifr_flags & IFF_UP) {
            i = xstrdup(ifr->ifr_name);
            break;
        }
    }
    xfree(ifc.ifc_req);
    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
}

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

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

chris's avatar
chris committed
111 112
    mask = strchr(arg, '/');
    if (mask == NULL) {
pdw's avatar
pdw committed
113 114 115 116
        die("Could not parse net/mask\n");
    }
    *mask = '\0';
    mask++;
chris's avatar
chris committed
117
    if (inet_aton(arg, &options.netfilternet) == 0)
pdw's avatar
pdw committed
118
        die("Invalid network address\n");
chris's avatar
chris committed
119
    /* Accept a netmask like /24 or /255.255.255.0. */
120 121
    if (mask[strspn(mask, "0123456789")] == '\0') {
        /* Whole string is numeric */
chris's avatar
chris committed
122 123
        int n;
        n = atoi(mask);
124
        if (n > 32) {
chris's avatar
chris committed
125
            die("Invalid netmask");
126
        }
chris's avatar
chris committed
127
        else {
128 129 130 131 132 133 134 135 136 137 138
            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
139
        }
140 141
    } 
    else if (inet_aton(mask, &options.netfiltermask) == 0) {
chris's avatar
chris committed
142
        die("Invalid netmask\n");
143 144
    }
    options.netfilternet.s_addr = options.netfilternet.s_addr & options.netfiltermask.s_addr;
pdw's avatar
pdw committed
145 146 147 148 149 150 151

    options.netfilter = 1;

}

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

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

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

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

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

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

203 204 205 206
            case 'P':
                options.showports = OPTION_PORTS_ON;
                break;

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

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

215 216 217
            case 'B':
                options.bandwidth_in_bytes = 1;
                break;
218

pdw's avatar
pdw committed
219 220 221 222 223 224 225 226 227 228 229 230
            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);
        }
    }

chris's avatar
chris committed
231 232
    if (optind != argc)
        fprintf(stderr, "iftop: warning: ignored arguments following options\n");
pdw's avatar
pdw committed
233
}