From 2555a9c485cdff12816ae571754dc213c075712f Mon Sep 17 00:00:00 2001 From: "Eric S. Raymond" Date: Mon, 4 Aug 1997 14:10:57 +0000 Subject: Ian T. Zimmerman's `interface' option. svn path=/trunk/; revision=1228 --- interface.c | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/interface.c b/interface.c index b379786b..58f6e0a4 100644 --- a/interface.c +++ b/interface.c @@ -45,10 +45,24 @@ static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname, int namelen = strlen(ifname); struct ifreq request; char *cp, buffer[256]; + int found = 0; /* initialize result */ memset((char *) ifinfo, 0, sizeof(ifinfo_t)); + /* get the packet I/O counts */ + while (fgets(buffer, sizeof(buffer) - 1, stats_file)) { + for (cp = buffer; *cp && *cp == ' '; ++cp); + if (!strncmp(cp, ifname, namelen) && + cp[namelen] == ':') { + cp += namelen + 1; + sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d", + &ifinfo->rx_packets, &ifinfo->tx_packets); + found = 1; + } + } + if (!found) return (FALSE); + /* see if the interface is up */ strcpy(request.ifr_name, ifname); if (ioctl(socket_fd, SIOCGIFFLAGS, &request) < 0) @@ -70,21 +84,12 @@ static int _get_ifinfo_(int socket_fd, FILE *stats_file, const char *ifname, /* get the netmask */ strcpy(request.ifr_name, ifname); - if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) - ifinfo->netmask = ((struct sockaddr_in *) - (&request.ifr_netmask))->sin_addr; + if (ioctl(socket_fd, SIOCGIFNETMASK, &request) >= 0) { + ifinfo->netmask = ((struct sockaddr_in *) + (&request.ifr_netmask))->sin_addr; + return (TRUE); + } - /* get the packet I/O counts */ - while (fgets(buffer, sizeof(buffer) - 1, stats_file)) { - for (cp = buffer; *cp && *cp == ' '; ++cp); - if (!strncmp(cp, ifname, namelen) && - cp[namelen] == ':') { - cp += namelen + 1; - sscanf(cp, "%d %*d %*d %*d %*d %d %*d %*d %*d %*d %*d", - &ifinfo->rx_packets, &ifinfo->tx_packets); - return(TRUE); - } - } return(FALSE); } -- cgit v1.2.3