Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
March 03, 2009
file 279 lines (239 sloc) 7.763 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*
* Copyright (c) 2008, The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google, Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>

#define PROC_NET_DEV "/proc/net/dev"

#define MAX_IF 8 /* max interfaces we can handle */

#ifndef PAGE_SIZE
# define PAGE_SIZE 4096
#endif

#define _STR(s) #s
#define STR(s) _STR(s)

struct if_stats {
    char name[IFNAMSIZ];

    unsigned int mtu;

    unsigned int rx_bytes;
    unsigned int rx_packets;
    unsigned int rx_errors;
    unsigned int rx_dropped;

    unsigned int tx_bytes;
    unsigned int tx_packets;
    unsigned int tx_errors;
    unsigned int tx_dropped;
};

static int get_mtu(const char *if_name)
{
    struct ifreq ifr;
    int s, ret;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    memset(&ifr, 0, sizeof(struct ifreq));
    ifr.ifr_addr.sa_family = AF_INET;
    strcpy(ifr.ifr_name, if_name);

    ret = ioctl(s, SIOCGIFMTU, &ifr);
    if (ret < 0) {
        perror("ioctl");
        exit(EXIT_FAILURE);
    }

    ret = close(s);
    if (ret < 0) {
        perror("close");
        exit(EXIT_FAILURE);
    }

    return ifr.ifr_mtu;
}

static int get_interfaces(struct if_stats *ifs)
{
    char buf[PAGE_SIZE];
    char *p;
    int ret, nr, fd;

    fd = open(PROC_NET_DEV, O_RDONLY);
    if (fd < 0) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    ret = read(fd, buf, sizeof(buf) - 1);
    if (ret < 0) {
        perror("read");
        exit(EXIT_FAILURE);
    } else if (!ret) {
        fprintf(stderr, "reading " PROC_NET_DEV " returned premature EOF\n");
        exit(EXIT_FAILURE);
    }
    buf[ret] = '\0';

    /* skip down to the third line */
    p = strchr(buf, '\n');
    if (!p) {
        fprintf(stderr, "parsing " PROC_NET_DEV " failed unexpectedly\n");
        exit(EXIT_FAILURE);
    }
    p = strchr(p + 1, '\n');
    if (!p) {
        fprintf(stderr, "parsing " PROC_NET_DEV " failed unexpectedly\n");
        exit(EXIT_FAILURE);
    }
    p += 1;

    /*
* Key:
* if: (Rx) bytes packets errs drop fifo frame compressed multicast \
* (Tx) bytes packets errs drop fifo colls carrier compressed
*/
    for (nr = 0; nr < MAX_IF; nr++) {
        char *c;

        ret = sscanf(p, "%" STR(IFNAMSIZ) "s", ifs->name);
        if (ret != 1) {
            fprintf(stderr, "parsing " PROC_NET_DEV " failed unexpectedly\n");
            exit(EXIT_FAILURE);
        }

        /*
* This works around a bug in the proc file where large interface names
* or Rx byte counts eat the delimiter, breaking sscanf.
*/
        c = strchr(ifs->name, ':');
        if (c)
            *c = '\0';

        p = strchr(p, ':') + 1;

        ret = sscanf(p, "%u %u %u %u %*u %*u %*u %*u %u %u %u %u %*u %*u "
                     "%*u %*u\n", &ifs->rx_bytes, &ifs->rx_packets,
                     &ifs->rx_errors, &ifs->rx_dropped, &ifs->tx_bytes,
                     &ifs->tx_packets, &ifs->tx_errors, &ifs->tx_dropped);
        if (ret != 8) {
            fprintf(stderr, "parsing " PROC_NET_DEV " failed unexpectedly\n");
            exit(EXIT_FAILURE);
        }

        ifs->mtu = get_mtu(ifs->name);

        p = strchr(p, '\n') + 1;
        if (*p == '\0') {
            nr++;
            break;
        }

        ifs++;
    }

    ret = close(fd);
    if (ret) {
        perror("close");
        exit(EXIT_FAILURE);
    }

    return nr;
}

static void print_header(void)
{
    printf(" Rx Tx\n");
    printf("%-8s %-5s %-10s %-8s %-5s %-5s %-10s %-8s %-5s %-5s\n",
           "name", "MTU", "bytes", "packets", "errs", "drpd", "bytes",
           "packets", "errs", "drpd");
}

static int print_interfaces(struct if_stats *old, struct if_stats *new, int nr)
{
    int i = 0;

    while (nr--) {
        if (old->rx_packets || old->tx_packets) {
            printf("%-8s %-5u %-10u %-8u %-5u %-5u %-10u %-8u %-5u %-5u\n",
                   new->name, new->mtu,
                   new->rx_bytes - old->rx_bytes,
                   new->rx_packets - old->rx_packets,
                   new->rx_errors - old->rx_errors,
                   new->rx_dropped - old->rx_dropped,
                   new->tx_bytes - old->tx_bytes,
                   new->tx_packets - old->tx_packets,
                   new->tx_errors - old->tx_errors,
                   new->tx_dropped - old->tx_dropped);
            i++;
        }
        old++;
        new++;
    }

    return i;
}

static void usage(const char *cmd)
{
    fprintf(stderr, "usage: %s [ -r repeats] [ -d delay ]\n", cmd);
}

int iftop_main(int argc, char *argv[])
{
    struct if_stats ifs[2][MAX_IF];
    int count = 0, header_interval = 22, delay = 1, i;
    unsigned int toggle = 0;

    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-d")) {
            if (i >= argc - 1) {
                fprintf(stderr, "Option -d requires an argument.\n");
                exit(EXIT_FAILURE);
            }
            delay = atoi(argv[i++]);
            if (!delay)
                delay = 1;
            continue;
        }
        if (!strcmp(argv[i], "-r")) {
            if (i >= argc - 1) {
                fprintf(stderr, "Option -r requires an argument.\n");
                exit(EXIT_FAILURE);
            }
            header_interval = atoi(argv[i++]);
            if (header_interval < MAX_IF)
                header_interval = MAX_IF;
            continue;
        }
        if (!strcmp(argv[i], "-h")) {
            usage(argv[0]);
            exit(EXIT_SUCCESS);
        }
        usage(argv[0]);
        exit(EXIT_FAILURE);
    }

    get_interfaces(ifs[!toggle]);
    if (header_interval)
        print_header();
    while (1) {
        int nr;

        sleep(delay);
        nr = get_interfaces(ifs[toggle]);
        if (header_interval && count + nr > header_interval) {
            print_header();
            count = 0;
        }
        count += print_interfaces(ifs[!toggle], ifs[toggle], nr);
        toggle = !toggle;
    }

    return 0;
}
Something went wrong with that request. Please try again.