Home | History | Annotate | Line # | Download | only in netstat
if.c revision 1.94.12.2
      1  1.94.12.2    martin /*	$NetBSD: if.c,v 1.94.12.2 2020/04/08 14:09:17 martin Exp $	*/
      2       1.13   thorpej 
      3        1.1       cgd /*
      4        1.8   mycroft  * Copyright (c) 1983, 1988, 1993
      5        1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.55       agc  * 3. Neither the name of the University nor the names of its contributors
     16        1.1       cgd  *    may be used to endorse or promote products derived from this software
     17        1.1       cgd  *    without specific prior written permission.
     18        1.1       cgd  *
     19        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29        1.1       cgd  * SUCH DAMAGE.
     30        1.1       cgd  */
     31        1.1       cgd 
     32       1.25     lukem #include <sys/cdefs.h>
     33        1.1       cgd #ifndef lint
     34       1.13   thorpej #if 0
     35       1.13   thorpej static char sccsid[] = "from: @(#)if.c	8.2 (Berkeley) 2/21/94";
     36       1.13   thorpej #else
     37  1.94.12.2    martin __RCSID("$NetBSD: if.c,v 1.94.12.2 2020/04/08 14:09:17 martin Exp $");
     38       1.13   thorpej #endif
     39        1.1       cgd #endif /* not lint */
     40        1.1       cgd 
     41       1.64      elad #include <sys/param.h>
     42        1.1       cgd #include <sys/types.h>
     43        1.8   mycroft #include <sys/protosw.h>
     44        1.1       cgd #include <sys/socket.h>
     45       1.57     ragge #include <sys/time.h>
     46       1.64      elad #include <sys/sysctl.h>
     47       1.82       mrg #include <sys/ioctl.h>
     48        1.1       cgd 
     49        1.1       cgd #include <net/if.h>
     50        1.1       cgd #include <net/if_dl.h>
     51       1.20   thorpej #include <net/if_types.h>
     52       1.64      elad #include <net/route.h>
     53        1.1       cgd #include <netinet/in.h>
     54        1.1       cgd #include <netinet/in_var.h>
     55        1.8   mycroft #include <arpa/inet.h>
     56        1.1       cgd 
     57       1.59    rpaulo #include <kvm.h>
     58        1.8   mycroft #include <signal.h>
     59        1.1       cgd #include <stdio.h>
     60       1.42      matt #include <stdlib.h>
     61        1.8   mycroft #include <string.h>
     62        1.8   mycroft #include <unistd.h>
     63       1.34    itojun #include <netdb.h>
     64       1.64      elad #include <err.h>
     65        1.8   mycroft 
     66        1.8   mycroft #include "netstat.h"
     67       1.80  christos #include "rtutil.h"
     68       1.70     pooka #include "prog_ops.h"
     69        1.1       cgd 
     70       1.64      elad #define	MAXIF	100
     71       1.64      elad 
     72       1.68     pooka #define HUMBUF_SIZE 7
     73       1.68     pooka 
     74       1.64      elad struct	iftot {
     75       1.64      elad 	char ift_name[IFNAMSIZ];	/* interface name */
     76       1.64      elad 	u_quad_t ift_ip;		/* input packets */
     77       1.64      elad 	u_quad_t ift_ib;		/* input bytes */
     78       1.64      elad 	u_quad_t ift_ie;		/* input errors */
     79       1.64      elad 	u_quad_t ift_op;		/* output packets */
     80       1.64      elad 	u_quad_t ift_ob;		/* output bytes */
     81       1.64      elad 	u_quad_t ift_oe;		/* output errors */
     82       1.64      elad 	u_quad_t ift_co;		/* collisions */
     83       1.89  christos 	u_quad_t ift_dr;		/* drops */
     84       1.64      elad };
     85       1.64      elad 
     86       1.82       mrg static void set_lines(void);
     87       1.91     ozaki static void print_addr(const int, struct sockaddr *, struct sockaddr **,
     88       1.85  christos     struct if_data *, struct ifnet *);
     89       1.64      elad static void sidewaysintpr(u_int, u_long);
     90       1.64      elad 
     91       1.64      elad static void iftot_banner(struct iftot *);
     92       1.64      elad static void iftot_print_sum(struct iftot *, struct iftot *);
     93       1.64      elad static void iftot_print(struct iftot *, struct iftot *);
     94        1.1       cgd 
     95       1.87  christos static void catchalarm(int);
     96       1.64      elad static void get_rtaddrs(int, struct sockaddr *, struct sockaddr **);
     97       1.64      elad static void fetchifs(void);
     98       1.64      elad 
     99       1.64      elad static void intpr_sysctl(void);
    100       1.64      elad static void intpr_kvm(u_long, void (*)(const char *));
    101       1.64      elad 
    102       1.64      elad struct iftot iftot[MAXIF], ip_cur, ip_old, sum_cur, sum_old;
    103       1.90  dholland static sig_atomic_t signalled;		/* set when alarm goes off */
    104       1.32    itojun 
    105       1.82       mrg static unsigned redraw_lines = 21;
    106       1.82       mrg 
    107       1.82       mrg static void
    108       1.82       mrg set_lines(void)
    109       1.82       mrg {
    110       1.82       mrg 	static bool first = true;
    111       1.82       mrg 	struct ttysize ts;
    112       1.82       mrg 
    113       1.82       mrg 	if (!first)
    114       1.82       mrg 		return;
    115       1.82       mrg 	first = false;
    116       1.82       mrg 	if (ioctl(STDOUT_FILENO, TIOCGSIZE, &ts) != -1 && ts.ts_lines)
    117       1.82       mrg 		redraw_lines = ts.ts_lines - 3;
    118       1.82       mrg }
    119       1.82       mrg 
    120       1.82       mrg 
    121        1.1       cgd /*
    122        1.1       cgd  * Print a description of the network interfaces.
    123       1.15   thorpej  * NOTE: ifnetaddr is the location of the kernel global "ifnet",
    124       1.15   thorpej  * which is a TAILQ_HEAD.
    125        1.1       cgd  */
    126        1.8   mycroft void
    127       1.74      matt intpr(int interval, u_long ifnetaddr, void (*pfunc)(const char *))
    128        1.1       cgd {
    129       1.64      elad 
    130       1.64      elad 	if (interval) {
    131       1.64      elad 		sidewaysintpr((unsigned)interval, ifnetaddr);
    132       1.64      elad 		return;
    133       1.64      elad 	}
    134       1.64      elad 
    135       1.64      elad 	if (use_sysctl) {
    136       1.64      elad 		intpr_sysctl();
    137       1.64      elad 	} else {
    138       1.64      elad 		intpr_kvm(ifnetaddr, pfunc);
    139       1.64      elad 	}
    140       1.64      elad 
    141       1.64      elad }
    142       1.64      elad 
    143       1.64      elad static void
    144       1.64      elad intpr_header(void)
    145       1.64      elad {
    146       1.64      elad 
    147       1.83  christos 	if (!sflag && !pflag) {
    148       1.64      elad 		if (bflag) {
    149       1.64      elad 			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
    150       1.64      elad 			       "%10.10s %10.10s",
    151       1.64      elad 			       "Name", "Mtu", "Network", "Address",
    152       1.64      elad 			       "Ibytes", "Obytes");
    153       1.64      elad 		} else {
    154       1.64      elad 			printf("%-5.5s %-5.5s %-13.13s %-17.17s "
    155  1.94.12.1  christos 			    "%8.8s %5.5s",
    156  1.94.12.1  christos 			    "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs");
    157  1.94.12.1  christos 			if (dflag)
    158  1.94.12.1  christos 				printf(" %6.6s", "Idrops");
    159  1.94.12.1  christos 			printf(" %8.8s %5.5s %5.5s",
    160  1.94.12.1  christos 			    "Opkts", "Oerrs", "Colls");
    161       1.64      elad 		}
    162  1.94.12.1  christos 		if (dflag)
    163  1.94.12.1  christos 			printf(" %6.6s", "Odrops");
    164       1.64      elad 		if (tflag)
    165       1.64      elad 			printf(" %4.4s", "Time");
    166       1.64      elad 		putchar('\n');
    167       1.64      elad 	}
    168       1.64      elad }
    169       1.64      elad 
    170       1.64      elad static void
    171       1.64      elad intpr_sysctl(void)
    172       1.64      elad {
    173       1.64      elad 	struct if_msghdr *ifm;
    174       1.64      elad 	int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
    175       1.93       mrg 	static char *buf = NULL;
    176       1.93       mrg 	static size_t olen;
    177       1.93       mrg 	char *next, *lim, *cp;
    178       1.64      elad 	struct rt_msghdr *rtm;
    179       1.64      elad 	struct ifa_msghdr *ifam;
    180       1.64      elad 	struct if_data *ifd = NULL;
    181       1.64      elad 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    182       1.64      elad 	struct sockaddr_dl *sdl;
    183       1.64      elad 	uint64_t total = 0;
    184       1.64      elad 	size_t len;
    185       1.84  christos 	int did = 1, rtax = 0, n;
    186       1.64      elad 	char name[IFNAMSIZ + 1];	/* + 1 for `*' */
    187       1.91     ozaki 	int ifindex = 0;
    188       1.64      elad 
    189       1.70     pooka 	if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
    190       1.64      elad 		err(1, "sysctl");
    191       1.93       mrg 	if (len > olen) {
    192       1.93       mrg 		free(buf);
    193       1.93       mrg 		if ((buf = malloc(len)) == NULL)
    194       1.93       mrg 			err(1, NULL);
    195       1.93       mrg 		olen = len;
    196       1.93       mrg 	}
    197       1.70     pooka 	if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
    198       1.64      elad 		err(1, "sysctl");
    199       1.64      elad 
    200       1.64      elad 	intpr_header();
    201       1.64      elad 
    202       1.64      elad 	lim = buf + len;
    203       1.64      elad 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    204       1.64      elad 		rtm = (struct rt_msghdr *)next;
    205       1.64      elad 		if (rtm->rtm_version != RTM_VERSION)
    206       1.64      elad 			continue;
    207       1.64      elad 		switch (rtm->rtm_type) {
    208       1.64      elad 		case RTM_IFINFO:
    209       1.64      elad 			total = 0;
    210       1.64      elad 			ifm = (struct if_msghdr *)next;
    211       1.64      elad 			ifd = &ifm->ifm_data;
    212       1.64      elad 
    213       1.64      elad 			sa = (struct sockaddr *)(ifm + 1);
    214       1.64      elad 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
    215       1.64      elad 
    216       1.64      elad 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
    217       1.64      elad 			if (sdl == NULL || sdl->sdl_family != AF_LINK) {
    218       1.64      elad 				continue;
    219       1.64      elad 			}
    220       1.64      elad 			bzero(name, sizeof(name));
    221       1.64      elad 			if (sdl->sdl_nlen >= IFNAMSIZ)
    222       1.64      elad 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
    223       1.64      elad 			else if (sdl->sdl_nlen > 0)
    224       1.64      elad 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
    225       1.64      elad 
    226       1.64      elad 			if (interface != 0 && strcmp(name, interface) != 0)
    227       1.64      elad 				continue;
    228       1.64      elad 
    229       1.91     ozaki 			ifindex = sdl->sdl_index;
    230       1.91     ozaki 
    231       1.64      elad 			/* mark inactive interfaces with a '*' */
    232       1.64      elad 			cp = strchr(name, '\0');
    233       1.64      elad 			if ((ifm->ifm_flags & IFF_UP) == 0)
    234       1.64      elad 				*cp++ = '*';
    235       1.64      elad 			*cp = '\0';
    236       1.64      elad 
    237       1.64      elad 			if (qflag) {
    238       1.64      elad 				total = ifd->ifi_ibytes + ifd->ifi_obytes +
    239       1.64      elad 				    ifd->ifi_ipackets + ifd->ifi_ierrors +
    240       1.64      elad 				    ifd->ifi_opackets + ifd->ifi_oerrors +
    241       1.64      elad 				    ifd->ifi_collisions;
    242       1.64      elad 				if (dflag)
    243       1.88  christos 					total += ifd->ifi_iqdrops;
    244       1.64      elad 				if (total == 0)
    245       1.64      elad 					continue;
    246       1.64      elad 			}
    247       1.84  christos 			/* Skip the first one */
    248       1.84  christos 			if (did) {
    249       1.84  christos 				did = 0;
    250       1.84  christos 				continue;
    251       1.84  christos 			}
    252       1.84  christos 			rtax = RTAX_IFP;
    253       1.64      elad 			break;
    254       1.64      elad 		case RTM_NEWADDR:
    255       1.64      elad 			if (qflag && total == 0)
    256       1.64      elad 				continue;
    257       1.64      elad 			if (interface != 0 && strcmp(name, interface) != 0)
    258       1.64      elad 				continue;
    259       1.64      elad 			ifam = (struct ifa_msghdr *)next;
    260       1.64      elad 			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
    261       1.64      elad 			    RTA_BRD)) == 0)
    262       1.64      elad 				break;
    263       1.64      elad 
    264       1.64      elad 			sa = (struct sockaddr *)(ifam + 1);
    265       1.64      elad 
    266       1.64      elad 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
    267       1.84  christos 			rtax = RTAX_IFA;
    268       1.84  christos 			did = 1;
    269       1.64      elad 			break;
    270       1.84  christos 		default:
    271       1.84  christos 			continue;
    272       1.64      elad 		}
    273       1.84  christos 		if (vflag)
    274       1.84  christos 			n = strlen(name) < 5 ? 5 : strlen(name);
    275       1.84  christos 		else
    276       1.84  christos 			n = 5;
    277       1.84  christos 
    278       1.84  christos 		printf("%-*.*s %-5" PRIu64 " ", n, n, name, ifd->ifi_mtu);
    279       1.91     ozaki 		print_addr(ifindex, rti_info[rtax], rti_info, ifd, NULL);
    280       1.64      elad 	}
    281       1.64      elad }
    282       1.64      elad 
    283       1.73  christos union ifaddr_u {
    284       1.73  christos 	struct ifaddr ifa;
    285       1.73  christos 	struct in_ifaddr in;
    286       1.73  christos #ifdef INET6
    287       1.73  christos 	struct in6_ifaddr in6;
    288       1.73  christos #endif /* INET6 */
    289       1.73  christos };
    290       1.73  christos 
    291       1.64      elad static void
    292  1.94.12.2    martin ifnet_to_ifdata_kvm(const struct ifnet * const ifp, struct if_data * const ifd)
    293  1.94.12.2    martin {
    294  1.94.12.2    martin 
    295  1.94.12.2    martin 	/*
    296  1.94.12.2    martin 	 * Interface statistics are no longer kept in struct ifnet,
    297  1.94.12.2    martin 	 * and thus an if_data is no longer embedded in struct ifnet.
    298  1.94.12.2    martin 	 * We cannot read stats via kvm without chasing per-cpu data,
    299  1.94.12.2    martin 	 * and maybe someday we could do that.  But for now, this is
    300  1.94.12.2    martin 	 * what we have.
    301  1.94.12.2    martin 	 *
    302  1.94.12.2    martin 	 * Just copy the fields that do exist.
    303  1.94.12.2    martin 	 */
    304  1.94.12.2    martin 	memset(ifd, 0, sizeof(*ifd));
    305  1.94.12.2    martin 	ifd->ifi_type = ifp->if_type;
    306  1.94.12.2    martin 	ifd->ifi_addrlen = ifp->if_addrlen;
    307  1.94.12.2    martin 	ifd->ifi_hdrlen = ifp->if_hdrlen;
    308  1.94.12.2    martin 	ifd->ifi_link_state = ifp->if_link_state;
    309  1.94.12.2    martin 	ifd->ifi_mtu = ifp->if_mtu;
    310  1.94.12.2    martin 	ifd->ifi_metric = ifp->if_metric;
    311  1.94.12.2    martin 	ifd->ifi_baudrate = ifp->if_baudrate;
    312  1.94.12.2    martin 	ifd->ifi_lastchange = ifp->if_lastchange;
    313  1.94.12.2    martin }
    314  1.94.12.2    martin 
    315  1.94.12.2    martin static void
    316       1.64      elad intpr_kvm(u_long ifnetaddr, void (*pfunc)(const char *))
    317       1.64      elad {
    318        1.1       cgd 	struct ifnet ifnet;
    319  1.94.12.2    martin 	struct if_data ifd;
    320       1.73  christos 	union ifaddr_u ifaddr;
    321        1.7       cgd 	u_long ifaddraddr;
    322       1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    323       1.43     enami 	char name[IFNAMSIZ + 1];	/* + 1 for `*' */
    324        1.1       cgd 
    325        1.1       cgd 	if (ifnetaddr == 0) {
    326        1.1       cgd 		printf("ifnet: symbol not defined\n");
    327        1.1       cgd 		return;
    328        1.1       cgd 	}
    329       1.15   thorpej 
    330       1.15   thorpej 	/*
    331       1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    332       1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    333       1.15   thorpej 	 * to the first list element.
    334       1.15   thorpej 	 */
    335       1.15   thorpej 	if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
    336        1.8   mycroft 		return;
    337       1.15   thorpej 	ifnetaddr = (u_long)ifhead.tqh_first;
    338       1.15   thorpej 
    339       1.64      elad 	intpr_header();
    340       1.64      elad 
    341        1.1       cgd 	ifaddraddr = 0;
    342        1.1       cgd 	while (ifnetaddr || ifaddraddr) {
    343       1.25     lukem 		char *cp;
    344       1.64      elad 		int n;
    345        1.1       cgd 
    346        1.1       cgd 		if (ifaddraddr == 0) {
    347       1.15   thorpej 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
    348        1.8   mycroft 				return;
    349       1.25     lukem 			memmove(name, ifnet.if_xname, IFNAMSIZ);
    350       1.15   thorpej 			name[IFNAMSIZ - 1] = '\0';	/* sanity */
    351       1.10   mycroft 			ifnetaddr = (u_long)ifnet.if_list.tqe_next;
    352       1.16   thorpej 			if (interface != 0 && strcmp(name, interface) != 0)
    353        1.1       cgd 				continue;
    354       1.25     lukem 			cp = strchr(name, '\0');
    355       1.34    itojun 
    356       1.34    itojun 			if (pfunc) {
    357       1.34    itojun 				(*pfunc)(name);
    358       1.34    itojun 				continue;
    359       1.34    itojun 			}
    360       1.34    itojun 
    361       1.10   mycroft 			if ((ifnet.if_flags & IFF_UP) == 0)
    362        1.1       cgd 				*cp++ = '*';
    363        1.1       cgd 			*cp = '\0';
    364       1.10   mycroft 			ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
    365        1.1       cgd 		}
    366       1.41    itojun 		if (vflag)
    367       1.41    itojun 			n = strlen(name) < 5 ? 5 : strlen(name);
    368       1.41    itojun 		else
    369       1.41    itojun 			n = 5;
    370       1.41    itojun 		printf("%-*.*s %-5llu ", n, n, name,
    371       1.33    bouyer 		    (unsigned long long)ifnet.if_mtu);
    372        1.1       cgd 		if (ifaddraddr == 0) {
    373       1.21  christos 			printf("%-13.13s ", "none");
    374       1.23     mikel 			printf("%-17.17s ", "none");
    375        1.1       cgd 		} else {
    376       1.64      elad 			struct sockaddr *sa;
    377       1.64      elad 
    378        1.8   mycroft 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
    379        1.8   mycroft 				ifaddraddr = 0;
    380        1.8   mycroft 				continue;
    381        1.8   mycroft 			}
    382        1.1       cgd #define CP(x) ((char *)(x))
    383        1.8   mycroft 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
    384       1.56    itojun 			    CP(&ifaddr);
    385       1.56    itojun 			sa = (struct sockaddr *)cp;
    386  1.94.12.2    martin 			ifnet_to_ifdata_kvm(&ifnet, &ifd);
    387       1.91     ozaki 			print_addr(ifnet.if_index, sa, (void *)&ifaddr,
    388  1.94.12.2    martin 			    &ifd, &ifnet);
    389       1.64      elad 		}
    390       1.64      elad 		ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
    391       1.64      elad 	}
    392       1.64      elad 
    393       1.64      elad }
    394       1.64      elad 
    395       1.64      elad static void
    396       1.91     ozaki mc_print(const int ifindex, const size_t ias, const char *oid, int *mcast_oids,
    397       1.86  christos     void (*pr)(const void *))
    398       1.84  christos {
    399       1.85  christos 	uint8_t *mcast_addrs, *p;
    400       1.86  christos 	const size_t incr = 2 * ias + sizeof(uint32_t);
    401       1.85  christos 	size_t len;
    402       1.85  christos 
    403       1.85  christos 	if (mcast_oids[0] == 0) {
    404       1.86  christos 		size_t oidlen = 4;
    405       1.86  christos 		if (sysctlnametomib(oid, mcast_oids, &oidlen) == -1) {
    406       1.86  christos 			warnx("'%s' not found", oid);
    407       1.85  christos 			return;
    408       1.85  christos 		}
    409       1.85  christos 		if (oidlen != 3) {
    410       1.86  christos 			warnx("Wrong OID path for '%s'", oid);
    411       1.85  christos 			return;
    412       1.85  christos 		}
    413       1.85  christos 	}
    414       1.86  christos 
    415       1.86  christos 	if (mcast_oids[3] == ifindex)
    416       1.86  christos 		return;
    417       1.85  christos 	mcast_oids[3] = ifindex;
    418       1.85  christos 
    419       1.85  christos 	mcast_addrs = asysctl(mcast_oids, 4, &len);
    420       1.85  christos 	if (mcast_addrs == NULL && len != 0) {
    421       1.86  christos 		warn("failed to read '%s'", oid);
    422       1.85  christos 		return;
    423       1.85  christos 	}
    424       1.85  christos 	if (len) {
    425       1.85  christos 		p = mcast_addrs;
    426       1.85  christos 		while (len >= incr) {
    427       1.86  christos 			(*pr)((p + ias));
    428       1.85  christos 			p += incr;
    429       1.85  christos 			len -= incr;
    430       1.85  christos 		}
    431       1.85  christos 	}
    432       1.85  christos 	free(mcast_addrs);
    433       1.85  christos }
    434       1.85  christos 
    435       1.86  christos #ifdef INET6
    436       1.86  christos static void
    437       1.86  christos ia6_print(const struct in6_addr *ia)
    438       1.86  christos {
    439       1.86  christos 	struct sockaddr_in6 as6;
    440       1.86  christos 	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
    441       1.86  christos 	int n;
    442       1.86  christos 
    443       1.86  christos 	memset(&as6, 0, sizeof(as6));
    444       1.86  christos 	as6.sin6_len = sizeof(struct sockaddr_in6);
    445       1.86  christos 	as6.sin6_family = AF_INET6;
    446       1.86  christos 	as6.sin6_addr = *ia;
    447       1.86  christos 	inet6_getscopeid(&as6, INET6_IS_ADDR_MC_LINKLOCAL);
    448       1.86  christos 	if (getnameinfo((struct sockaddr *)&as6, as6.sin6_len, hbuf,
    449       1.86  christos 	    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
    450       1.86  christos 		strlcpy(hbuf, "??", sizeof(hbuf));
    451       1.86  christos 	}
    452       1.86  christos 	if (vflag)
    453       1.86  christos 		n = strlen(hbuf) < 17 ? 17 : strlen(hbuf);
    454       1.86  christos 	else
    455       1.86  christos 		n = 17;
    456       1.86  christos 	printf("\n%25s %-*.*s ", "", n, n, hbuf);
    457       1.86  christos }
    458       1.86  christos 
    459       1.86  christos static void
    460       1.91     ozaki mc6_print(const int ifindex)
    461       1.86  christos {
    462       1.86  christos 	static int mcast_oids[4];
    463       1.86  christos 
    464       1.91     ozaki 	mc_print(ifindex, sizeof(struct in6_addr), "net.inet6.multicast",
    465       1.86  christos 	    mcast_oids, (void (*)(const void *))ia6_print);
    466       1.86  christos }
    467       1.86  christos #endif
    468       1.86  christos 
    469       1.85  christos static void
    470       1.85  christos ia4_print(const struct in_addr *ia)
    471       1.85  christos {
    472       1.85  christos 	printf("\n%25s %-17.17s ", "", routename4(ia->s_addr, nflag));
    473       1.85  christos }
    474       1.85  christos 
    475       1.85  christos static void
    476       1.91     ozaki mc4_print(const int ifindex)
    477       1.85  christos {
    478       1.85  christos 	static int mcast_oids[4];
    479       1.85  christos 
    480       1.91     ozaki 	mc_print(ifindex, sizeof(struct in_addr), "net.inet.multicast",
    481       1.86  christos 	    mcast_oids, (void (*)(const void *))ia4_print);
    482       1.85  christos }
    483       1.85  christos 
    484       1.85  christos static void
    485       1.91     ozaki print_addr(const int ifindex, struct sockaddr *sa,
    486       1.91     ozaki     struct sockaddr **rtinfo, struct if_data *ifd, struct ifnet *ifnet)
    487       1.64      elad {
    488       1.64      elad 	char hexsep = '.';		/* for hexprint */
    489       1.64      elad 	static const char hexfmt[] = "%02x%c";	/* for hexprint */
    490       1.64      elad 	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
    491       1.64      elad #ifdef INET6
    492       1.64      elad 	const int niflag = NI_NUMERICHOST;
    493       1.67    plunky 	struct sockaddr_in6 *sin6, *netmask6;
    494       1.64      elad #endif
    495       1.81  christos 	struct sockaddr_in netmask;
    496       1.64      elad 	struct sockaddr_in *sin;
    497       1.64      elad 	char *cp;
    498       1.64      elad 	int n, m;
    499       1.64      elad 
    500       1.64      elad 	switch (sa->sa_family) {
    501       1.64      elad 	case AF_UNSPEC:
    502       1.64      elad 		printf("%-13.13s ", "none");
    503       1.64      elad 		printf("%-17.17s ", "none");
    504       1.64      elad 		break;
    505       1.64      elad 	case AF_INET:
    506       1.64      elad 		sin = (struct sockaddr_in *)sa;
    507       1.64      elad 		if (use_sysctl) {
    508       1.81  christos 			netmask = *((struct sockaddr_in *)rtinfo[RTAX_NETMASK]);
    509       1.64      elad 		} else {
    510       1.64      elad 			struct in_ifaddr *ifaddr_in = (void *)rtinfo;
    511       1.81  christos 			netmask.sin_addr.s_addr = ifaddr_in->ia_subnetmask;
    512       1.64      elad 		}
    513       1.81  christos 		cp = netname4(sin, &netmask, nflag);
    514       1.64      elad 		if (vflag)
    515       1.64      elad 			n = strlen(cp) < 13 ? 13 : strlen(cp);
    516       1.64      elad 		else
    517       1.64      elad 			n = 13;
    518       1.64      elad 		printf("%-*.*s ", n, n, cp);
    519       1.80  christos 		cp = routename4(sin->sin_addr.s_addr, nflag);
    520       1.64      elad 		if (vflag)
    521       1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    522       1.64      elad 		else
    523       1.64      elad 			n = 17;
    524       1.64      elad 		printf("%-*.*s ", n, n, cp);
    525       1.64      elad 
    526       1.84  christos 		if (!aflag)
    527       1.84  christos 			break;
    528       1.84  christos 		if (ifnet) {
    529       1.64      elad 			u_long multiaddr;
    530       1.64      elad 			struct in_multi inm;
    531       1.73  christos 			union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
    532       1.64      elad 
    533       1.85  christos 			multiaddr = (u_long)ifaddr->in.ia_multiaddrs.lh_first;
    534       1.64      elad 			while (multiaddr != 0) {
    535       1.85  christos 				kread(multiaddr, (char *)&inm, sizeof inm);
    536       1.85  christos 				ia4_print(&inm.inm_addr);
    537       1.85  christos 				multiaddr = (u_long)inm.inm_list.le_next;
    538       1.64      elad 			}
    539       1.84  christos 		} else {
    540       1.91     ozaki 			mc4_print(ifindex);
    541       1.64      elad 		}
    542       1.64      elad 		break;
    543       1.32    itojun #ifdef INET6
    544       1.64      elad 	case AF_INET6:
    545       1.64      elad 		sin6 = (struct sockaddr_in6 *)sa;
    546       1.79  christos 		inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    547       1.54    itojun #ifdef __KAME__
    548       1.78  christos 		if (!vflag)
    549       1.78  christos 			sin6->sin6_scope_id = 0;
    550       1.34    itojun #endif
    551       1.64      elad 
    552       1.64      elad 		if (use_sysctl) {
    553       1.64      elad 			netmask6 = (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK];
    554       1.64      elad 		} else {
    555       1.64      elad 			struct in6_ifaddr *ifaddr_in6 = (void *)rtinfo;
    556       1.64      elad 			netmask6 = &ifaddr_in6->ia_prefixmask;
    557       1.64      elad 		}
    558       1.64      elad 
    559       1.80  christos 		cp = netname6(sin6, netmask6, nflag);
    560       1.64      elad 		if (vflag)
    561       1.64      elad 			n = strlen(cp) < 13 ? 13 : strlen(cp);
    562       1.64      elad 		else
    563       1.64      elad 			n = 13;
    564       1.64      elad 		printf("%-*.*s ", n, n, cp);
    565       1.64      elad 		if (getnameinfo((struct sockaddr *)sin6,
    566       1.64      elad 				sin6->sin6_len,
    567       1.64      elad 				hbuf, sizeof(hbuf), NULL, 0,
    568       1.64      elad 				niflag) != 0) {
    569       1.64      elad 			strlcpy(hbuf, "?", sizeof(hbuf));
    570       1.64      elad 		}
    571       1.64      elad 		cp = hbuf;
    572       1.64      elad 		if (vflag)
    573       1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    574       1.64      elad 		else
    575       1.64      elad 			n = 17;
    576       1.64      elad 		printf("%-*.*s ", n, n, cp);
    577       1.64      elad 
    578       1.84  christos 		if (!aflag)
    579       1.84  christos 			break;
    580       1.84  christos 		if (ifnet) {
    581       1.64      elad 			u_long multiaddr;
    582       1.64      elad 			struct in6_multi inm;
    583       1.73  christos 			union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
    584       1.35    itojun 
    585       1.94     ozaki 			multiaddr = (u_long)ifaddr->in6._ia6_multiaddrs.lh_first;
    586       1.64      elad 			while (multiaddr != 0) {
    587       1.84  christos 				kread(multiaddr, (char *)&inm, sizeof inm);
    588       1.84  christos 				ia6_print(&inm.in6m_addr);
    589       1.84  christos 				multiaddr = (u_long)inm.in6m_entry.le_next;
    590        1.1       cgd 			}
    591       1.84  christos 		} else {
    592       1.91     ozaki 			mc6_print(ifindex);
    593        1.1       cgd 		}
    594       1.64      elad 		break;
    595       1.64      elad #endif /*INET6*/
    596       1.64      elad #ifndef SMALL
    597       1.64      elad 	case AF_APPLETALK:
    598       1.64      elad 		printf("atalk:%-7.7s ",
    599       1.64      elad 		       atalk_print(sa,0x10));
    600       1.64      elad 		printf("%-17.17s ", atalk_print(sa,0x0b));
    601       1.64      elad 		break;
    602       1.64      elad #endif
    603       1.64      elad 	case AF_LINK:
    604       1.64      elad 		printf("%-13.13s ", "<Link>");
    605       1.64      elad 		if (getnameinfo(sa, sa->sa_len,
    606       1.64      elad 		    hbuf, sizeof(hbuf), NULL, 0,
    607       1.64      elad 		    NI_NUMERICHOST) != 0) {
    608       1.64      elad 			strlcpy(hbuf, "?", sizeof(hbuf));
    609       1.26       kml 		}
    610       1.64      elad 		cp = hbuf;
    611       1.64      elad 		if (vflag)
    612       1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    613       1.64      elad 		else
    614       1.64      elad 			n = 17;
    615       1.64      elad 		printf("%-*.*s ", n, n, cp);
    616       1.64      elad 		break;
    617       1.64      elad 
    618       1.64      elad 	default:
    619       1.64      elad 		m = printf("(%d)", sa->sa_family);
    620       1.64      elad 		for (cp = sa->sa_len + (char *)sa;
    621       1.64      elad 			--cp > sa->sa_data && (*cp == 0);) {}
    622       1.64      elad 		n = cp - sa->sa_data + 1;
    623       1.64      elad 		cp = sa->sa_data;
    624       1.64      elad 
    625       1.64      elad 		while (--n >= 0)
    626       1.64      elad 			m += printf(hexfmt, *cp++ & 0xff,
    627       1.64      elad 				    n > 0 ? hexsep : ' ');
    628       1.64      elad 		m = 32 - m;
    629       1.64      elad 		while (m-- > 0)
    630       1.64      elad 			putchar(' ');
    631       1.64      elad 		break;
    632       1.64      elad 	}
    633       1.64      elad 
    634       1.64      elad 	if (bflag) {
    635       1.68     pooka 		char humbuf[HUMBUF_SIZE];
    636       1.68     pooka 
    637       1.68     pooka 		if (hflag && humanize_number(humbuf, sizeof(humbuf),
    638       1.68     pooka 		    ifd->ifi_ibytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    639       1.68     pooka 			printf("%10s ", humbuf);
    640       1.68     pooka 		else
    641       1.68     pooka 			printf("%10llu ", (unsigned long long)ifd->ifi_ibytes);
    642       1.68     pooka 
    643       1.68     pooka 		if (hflag && humanize_number(humbuf, sizeof(humbuf),
    644       1.68     pooka 		    ifd->ifi_obytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    645       1.68     pooka 			printf("%10s", humbuf);
    646       1.68     pooka 		else
    647       1.68     pooka 			printf("%10llu", (unsigned long long)ifd->ifi_obytes);
    648       1.64      elad 	} else {
    649  1.94.12.1  christos 		printf("%8llu %5llu",
    650       1.64      elad 			(unsigned long long)ifd->ifi_ipackets,
    651  1.94.12.1  christos 			(unsigned long long)ifd->ifi_ierrors);
    652  1.94.12.1  christos 		if (dflag)
    653  1.94.12.1  christos 			printf(" %6" PRIu64, ifd->ifi_iqdrops);
    654  1.94.12.1  christos 		printf(" %8llu %5llu %5llu",
    655       1.64      elad 			(unsigned long long)ifd->ifi_opackets,
    656       1.64      elad 			(unsigned long long)ifd->ifi_oerrors,
    657       1.64      elad 			(unsigned long long)ifd->ifi_collisions);
    658        1.1       cgd 	}
    659  1.94.12.1  christos 	if (dflag)
    660  1.94.12.1  christos 		printf(" %6lld", ifnet ?
    661  1.94.12.1  christos 		    (unsigned long long)ifnet->if_snd.ifq_drops : 0);
    662       1.64      elad 	if (tflag)
    663       1.73  christos 		printf(" %4d", ifnet ? ifnet->if_timer : 0);
    664       1.64      elad 	putchar('\n');
    665       1.64      elad }
    666       1.64      elad 
    667       1.64      elad static void
    668       1.64      elad iftot_banner(struct iftot *ift)
    669       1.64      elad {
    670       1.64      elad 	if (bflag)
    671       1.64      elad 		printf("%7.7s in %8.8s %6.6s out %5.5s",
    672       1.64      elad 		    ift->ift_name, " ",
    673       1.64      elad 		    ift->ift_name, " ");
    674       1.64      elad 	else
    675       1.64      elad 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
    676       1.64      elad 		    ift->ift_name, " ",
    677       1.64      elad 		    ift->ift_name, " ", " ");
    678       1.64      elad 	if (dflag)
    679       1.64      elad 		printf(" %5.5s", " ");
    680       1.64      elad 
    681       1.64      elad 	if (bflag)
    682       1.64      elad 		printf("  %7.7s in %8.8s %6.6s out %5.5s",
    683       1.64      elad 		    "total", " ", "total", " ");
    684       1.64      elad 	else
    685       1.64      elad 		printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
    686       1.64      elad 		    "total", " ", "total", " ", " ");
    687       1.64      elad 	if (dflag)
    688       1.64      elad 		printf(" %5.5s", " ");
    689       1.64      elad 	putchar('\n');
    690       1.64      elad 	if (bflag)
    691       1.64      elad 		printf("%10.10s %8.8s %10.10s %5.5s",
    692       1.64      elad 		    "bytes", " ", "bytes", " ");
    693       1.64      elad 	else
    694       1.64      elad 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
    695       1.64      elad 		    "packets", "errs", "packets", "errs", "colls");
    696       1.64      elad 	if (dflag)
    697       1.64      elad 		printf(" %5.5s", "drops");
    698       1.64      elad 
    699       1.64      elad 	if (bflag)
    700       1.64      elad 		printf("  %10.10s %8.8s %10.10s %5.5s",
    701       1.64      elad 		    "bytes", " ", "bytes", " ");
    702       1.64      elad 	else
    703       1.64      elad 		printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
    704       1.64      elad 		    "packets", "errs", "packets", "errs", "colls");
    705       1.64      elad 	if (dflag)
    706       1.64      elad 		printf(" %5.5s", "drops");
    707       1.64      elad 	putchar('\n');
    708       1.64      elad 	fflush(stdout);
    709        1.1       cgd }
    710        1.1       cgd 
    711       1.64      elad static void
    712       1.64      elad iftot_print(struct iftot *cur, struct iftot *old)
    713       1.64      elad {
    714       1.64      elad 	if (bflag)
    715       1.75   msaitoh 		printf("%10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
    716       1.66  pgoyette 		    cur->ift_ib - old->ift_ib, " ",
    717       1.66  pgoyette 		    cur->ift_ob - old->ift_ob, " ");
    718       1.64      elad 	else
    719       1.75   msaitoh 		printf("%8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
    720       1.66  pgoyette 		    cur->ift_ip - old->ift_ip,
    721       1.66  pgoyette 		    cur->ift_ie - old->ift_ie,
    722       1.66  pgoyette 		    cur->ift_op - old->ift_op,
    723       1.66  pgoyette 		    cur->ift_oe - old->ift_oe,
    724       1.66  pgoyette 		    cur->ift_co - old->ift_co);
    725       1.64      elad 	if (dflag)
    726       1.89  christos 		printf(" %5" PRIu64, cur->ift_dr - old->ift_dr);
    727       1.64      elad }
    728       1.64      elad 
    729       1.64      elad static void
    730       1.64      elad iftot_print_sum(struct iftot *cur, struct iftot *old)
    731       1.64      elad {
    732       1.64      elad 	if (bflag)
    733       1.75   msaitoh 		printf("  %10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
    734       1.66  pgoyette 		    cur->ift_ib - old->ift_ib, " ",
    735       1.66  pgoyette 		    cur->ift_ob - old->ift_ob, " ");
    736       1.64      elad 	else
    737       1.75   msaitoh 		printf("  %8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
    738       1.66  pgoyette 		    cur->ift_ip - old->ift_ip,
    739       1.66  pgoyette 		    cur->ift_ie - old->ift_ie,
    740       1.66  pgoyette 		    cur->ift_op - old->ift_op,
    741       1.66  pgoyette 		    cur->ift_oe - old->ift_oe,
    742       1.66  pgoyette 		    cur->ift_co - old->ift_co);
    743       1.64      elad 
    744       1.64      elad 	if (dflag)
    745       1.89  christos 		printf(" %5" PRIu64, cur->ift_dr - old->ift_dr);
    746       1.64      elad }
    747       1.64      elad 
    748       1.72     joerg __dead static void
    749       1.64      elad sidewaysintpr_sysctl(unsigned interval)
    750       1.64      elad {
    751       1.90  dholland 	struct itimerval it;
    752       1.64      elad 	sigset_t emptyset;
    753       1.90  dholland 	sigset_t noalrm;
    754       1.82       mrg 	unsigned line;
    755       1.82       mrg 
    756       1.82       mrg 	set_lines();
    757       1.64      elad 
    758       1.64      elad 	fetchifs();
    759       1.64      elad 	if (ip_cur.ift_name[0] == '\0') {
    760       1.64      elad 		fprintf(stderr, "%s: %s: unknown interface\n",
    761       1.64      elad 		    getprogname(), interface);
    762       1.64      elad 		exit(1);
    763       1.64      elad 	}
    764       1.64      elad 
    765       1.90  dholland 	sigemptyset(&emptyset);
    766       1.90  dholland 	sigemptyset(&noalrm);
    767       1.90  dholland 	sigaddset(&noalrm, SIGALRM);
    768       1.90  dholland 	sigprocmask(SIG_SETMASK, &noalrm, NULL);
    769       1.90  dholland 
    770       1.90  dholland 	signalled = 0;
    771       1.64      elad 	(void)signal(SIGALRM, catchalarm);
    772       1.90  dholland 
    773       1.90  dholland 	it.it_interval.tv_sec = it.it_value.tv_sec = interval;
    774       1.90  dholland 	it.it_interval.tv_usec = it.it_value.tv_usec = 0;
    775       1.90  dholland 	setitimer(ITIMER_REAL, &it, NULL);
    776       1.90  dholland 
    777       1.64      elad banner:
    778       1.64      elad 	iftot_banner(&ip_cur);
    779       1.64      elad 
    780       1.64      elad 	line = 0;
    781       1.64      elad 	bzero(&ip_old, sizeof(ip_old));
    782       1.64      elad 	bzero(&sum_old, sizeof(sum_old));
    783       1.64      elad loop:
    784       1.64      elad 	bzero(&sum_cur, sizeof(sum_cur));
    785       1.64      elad 
    786       1.64      elad 	fetchifs();
    787       1.64      elad 
    788       1.64      elad 	iftot_print(&ip_cur, &ip_old);
    789       1.64      elad 
    790       1.64      elad 	ip_old = ip_cur;
    791       1.64      elad 
    792       1.64      elad 	iftot_print_sum(&sum_cur, &sum_old);
    793       1.64      elad 
    794       1.64      elad 	sum_old = sum_cur;
    795        1.1       cgd 
    796       1.64      elad 	putchar('\n');
    797       1.64      elad 	fflush(stdout);
    798       1.64      elad 	line++;
    799       1.90  dholland 	if (signalled == 0) {
    800       1.64      elad 		sigsuspend(&emptyset);
    801       1.90  dholland 	}
    802       1.64      elad 	signalled = 0;
    803       1.82       mrg 	if (line == redraw_lines)
    804       1.64      elad 		goto banner;
    805       1.64      elad 	goto loop;
    806       1.64      elad 	/*NOTREACHED*/
    807       1.64      elad }
    808        1.1       cgd 
    809        1.8   mycroft static void
    810       1.64      elad sidewaysintpr_kvm(unsigned interval, u_long off)
    811        1.1       cgd {
    812       1.57     ragge 	struct itimerval it;
    813       1.90  dholland 	sigset_t emptyset;
    814       1.90  dholland 	sigset_t noalrm;
    815        1.1       cgd 	struct ifnet ifnet;
    816  1.94.12.2    martin 	struct if_data ifd;
    817        1.7       cgd 	u_long firstifnet;
    818       1.25     lukem 	struct iftot *ip, *total;
    819       1.82       mrg 	unsigned line;
    820        1.1       cgd 	struct iftot *lastif, *sum, *interesting;
    821       1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    822        1.1       cgd 
    823       1.82       mrg 	set_lines();
    824       1.82       mrg 
    825       1.15   thorpej 	/*
    826       1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    827       1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    828       1.15   thorpej 	 * to the first list element.
    829       1.15   thorpej 	 */
    830       1.15   thorpej 	if (kread(off, (char *)&ifhead, sizeof ifhead))
    831        1.8   mycroft 		return;
    832       1.15   thorpej 	firstifnet = (u_long)ifhead.tqh_first;
    833       1.15   thorpej 
    834        1.1       cgd 	lastif = iftot;
    835        1.1       cgd 	sum = iftot + MAXIF - 1;
    836        1.1       cgd 	total = sum - 1;
    837       1.17       cgd 	interesting = (interface == NULL) ? iftot : NULL;
    838        1.1       cgd 	for (off = firstifnet, ip = iftot; off;) {
    839        1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet))
    840        1.8   mycroft 			break;
    841       1.25     lukem 		memset(ip->ift_name, 0, sizeof(ip->ift_name));
    842       1.31   mycroft 		snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
    843       1.19   thorpej 		if (interface && strcmp(ifnet.if_xname, interface) == 0)
    844        1.1       cgd 			interesting = ip;
    845        1.1       cgd 		ip++;
    846        1.1       cgd 		if (ip >= iftot + MAXIF - 2)
    847        1.1       cgd 			break;
    848       1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    849       1.17       cgd 	}
    850       1.17       cgd 	if (interesting == NULL) {
    851       1.17       cgd 		fprintf(stderr, "%s: %s: unknown interface\n",
    852       1.47       cgd 		    getprogname(), interface);
    853       1.17       cgd 		exit(1);
    854        1.1       cgd 	}
    855        1.1       cgd 	lastif = ip;
    856        1.1       cgd 
    857       1.90  dholland 	sigemptyset(&emptyset);
    858       1.90  dholland 	sigemptyset(&noalrm);
    859       1.90  dholland 	sigaddset(&noalrm, SIGALRM);
    860       1.90  dholland 	sigprocmask(SIG_SETMASK, &noalrm, NULL);
    861       1.90  dholland 
    862       1.90  dholland 	signalled = 0;
    863        1.1       cgd 	(void)signal(SIGALRM, catchalarm);
    864       1.57     ragge 
    865       1.57     ragge 	it.it_interval.tv_sec = it.it_value.tv_sec = interval;
    866       1.57     ragge 	it.it_interval.tv_usec = it.it_value.tv_usec = 0;
    867       1.57     ragge 	setitimer(ITIMER_REAL, &it, NULL);
    868       1.57     ragge 
    869        1.1       cgd banner:
    870       1.31   mycroft 	if (bflag)
    871       1.31   mycroft 		printf("%7.7s in %8.8s %6.6s out %5.5s",
    872       1.31   mycroft 		    interesting->ift_name, " ",
    873       1.31   mycroft 		    interesting->ift_name, " ");
    874       1.31   mycroft 	else
    875       1.31   mycroft 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
    876       1.31   mycroft 		    interesting->ift_name, " ",
    877       1.31   mycroft 		    interesting->ift_name, " ", " ");
    878       1.31   mycroft 	if (dflag)
    879       1.31   mycroft 		printf(" %5.5s", " ");
    880        1.1       cgd 	if (lastif - iftot > 0) {
    881       1.31   mycroft 		if (bflag)
    882       1.31   mycroft 			printf("  %7.7s in %8.8s %6.6s out %5.5s",
    883       1.31   mycroft 			    "total", " ", "total", " ");
    884       1.31   mycroft 		else
    885       1.31   mycroft 			printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
    886       1.31   mycroft 			    "total", " ", "total", " ", " ");
    887        1.1       cgd 		if (dflag)
    888       1.31   mycroft 			printf(" %5.5s", " ");
    889        1.1       cgd 	}
    890        1.1       cgd 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
    891        1.1       cgd 		ip->ift_ip = 0;
    892       1.26       kml 		ip->ift_ib = 0;
    893        1.1       cgd 		ip->ift_ie = 0;
    894        1.1       cgd 		ip->ift_op = 0;
    895       1.26       kml 		ip->ift_ob = 0;
    896        1.1       cgd 		ip->ift_oe = 0;
    897        1.1       cgd 		ip->ift_co = 0;
    898        1.1       cgd 		ip->ift_dr = 0;
    899        1.1       cgd 	}
    900        1.1       cgd 	putchar('\n');
    901       1.31   mycroft 	if (bflag)
    902       1.26       kml 		printf("%10.10s %8.8s %10.10s %5.5s",
    903       1.31   mycroft 		    "bytes", " ", "bytes", " ");
    904       1.31   mycroft 	else
    905       1.31   mycroft 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
    906       1.31   mycroft 		    "packets", "errs", "packets", "errs", "colls");
    907        1.1       cgd 	if (dflag)
    908       1.31   mycroft 		printf(" %5.5s", "drops");
    909       1.29      ross 	if (lastif - iftot > 0) {
    910       1.31   mycroft 		if (bflag)
    911       1.31   mycroft 			printf("  %10.10s %8.8s %10.10s %5.5s",
    912       1.31   mycroft 			    "bytes", " ", "bytes", " ");
    913       1.31   mycroft 		else
    914       1.31   mycroft 			printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
    915       1.31   mycroft 			    "packets", "errs", "packets", "errs", "colls");
    916       1.31   mycroft 		if (dflag)
    917       1.31   mycroft 			printf(" %5.5s", "drops");
    918       1.29      ross 	}
    919        1.1       cgd 	putchar('\n');
    920        1.1       cgd 	fflush(stdout);
    921        1.1       cgd 	line = 0;
    922        1.1       cgd loop:
    923        1.1       cgd 	sum->ift_ip = 0;
    924       1.26       kml 	sum->ift_ib = 0;
    925        1.1       cgd 	sum->ift_ie = 0;
    926        1.1       cgd 	sum->ift_op = 0;
    927       1.26       kml 	sum->ift_ob = 0;
    928        1.1       cgd 	sum->ift_oe = 0;
    929        1.1       cgd 	sum->ift_co = 0;
    930        1.1       cgd 	sum->ift_dr = 0;
    931        1.1       cgd 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
    932        1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet)) {
    933        1.8   mycroft 			off = 0;
    934        1.8   mycroft 			continue;
    935        1.8   mycroft 		}
    936  1.94.12.2    martin 		ifnet_to_ifdata_kvm(&ifnet, &ifd);
    937        1.1       cgd 		if (ip == interesting) {
    938       1.26       kml 			if (bflag) {
    939       1.68     pooka 				char humbuf[HUMBUF_SIZE];
    940       1.68     pooka 
    941       1.68     pooka 				if (hflag && humanize_number(humbuf,
    942       1.68     pooka 				    sizeof(humbuf),
    943  1.94.12.2    martin 				    ifd.ifi_ibytes - ip->ift_ib, "",
    944       1.68     pooka 				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    945       1.68     pooka 					printf("%10s %8.8s ", humbuf, " ");
    946       1.68     pooka 				else
    947       1.68     pooka 					printf("%10llu %8.8s ",
    948       1.68     pooka 					    (unsigned long long)
    949  1.94.12.2    martin 					    (ifd.ifi_ibytes-ip->ift_ib), " ");
    950       1.68     pooka 
    951       1.68     pooka 				if (hflag && humanize_number(humbuf,
    952       1.68     pooka 				    sizeof(humbuf),
    953  1.94.12.2    martin 				    ifd.ifi_obytes - ip->ift_ob, "",
    954       1.68     pooka 				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    955       1.68     pooka 					printf("%10s %5.5s", humbuf, " ");
    956       1.68     pooka 				else
    957       1.68     pooka 					printf("%10llu %5.5s",
    958       1.68     pooka 					    (unsigned long long)
    959  1.94.12.2    martin 					    (ifd.ifi_obytes-ip->ift_ob), " ");
    960       1.26       kml 			} else {
    961       1.33    bouyer 				printf("%8llu %5llu %8llu %5llu %5llu",
    962       1.33    bouyer 				    (unsigned long long)
    963  1.94.12.2    martin 					(ifd.ifi_ipackets - ip->ift_ip),
    964       1.33    bouyer 				    (unsigned long long)
    965  1.94.12.2    martin 					(ifd.ifi_ierrors - ip->ift_ie),
    966       1.33    bouyer 				    (unsigned long long)
    967  1.94.12.2    martin 					(ifd.ifi_opackets - ip->ift_op),
    968       1.33    bouyer 				    (unsigned long long)
    969  1.94.12.2    martin 					(ifd.ifi_oerrors - ip->ift_oe),
    970       1.33    bouyer 				    (unsigned long long)
    971  1.94.12.2    martin 					(ifd.ifi_collisions - ip->ift_co));
    972       1.26       kml 			}
    973        1.1       cgd 			if (dflag)
    974       1.89  christos 				printf(" %5" PRIu64,
    975       1.89  christos 					ifnet.if_snd.ifq_drops - ip->ift_dr);
    976        1.1       cgd 		}
    977  1.94.12.2    martin 		ip->ift_ip = ifd.ifi_ipackets;
    978  1.94.12.2    martin 		ip->ift_ib = ifd.ifi_ibytes;
    979  1.94.12.2    martin 		ip->ift_ie = ifd.ifi_ierrors;
    980  1.94.12.2    martin 		ip->ift_op = ifd.ifi_opackets;
    981  1.94.12.2    martin 		ip->ift_ob = ifd.ifi_obytes;
    982  1.94.12.2    martin 		ip->ift_oe = ifd.ifi_oerrors;
    983  1.94.12.2    martin 		ip->ift_co = ifd.ifi_collisions;
    984        1.1       cgd 		ip->ift_dr = ifnet.if_snd.ifq_drops;
    985        1.1       cgd 		sum->ift_ip += ip->ift_ip;
    986       1.26       kml 		sum->ift_ib += ip->ift_ib;
    987        1.1       cgd 		sum->ift_ie += ip->ift_ie;
    988        1.1       cgd 		sum->ift_op += ip->ift_op;
    989       1.26       kml 		sum->ift_ob += ip->ift_ob;
    990        1.1       cgd 		sum->ift_oe += ip->ift_oe;
    991        1.1       cgd 		sum->ift_co += ip->ift_co;
    992        1.1       cgd 		sum->ift_dr += ip->ift_dr;
    993       1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    994        1.1       cgd 	}
    995        1.1       cgd 	if (lastif - iftot > 0) {
    996       1.26       kml 		if (bflag) {
    997       1.68     pooka 			char humbuf[HUMBUF_SIZE];
    998       1.68     pooka 
    999       1.68     pooka 			if (hflag && humanize_number(humbuf,
   1000       1.68     pooka 			    sizeof(humbuf), sum->ift_ib - total->ift_ib, "",
   1001       1.68     pooka 			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
   1002       1.69     enami 				printf("  %10s %8.8s ", humbuf, " ");
   1003       1.68     pooka 			else
   1004       1.69     enami 				printf("  %10llu %8.8s ",
   1005       1.68     pooka 				    (unsigned long long)
   1006       1.68     pooka 				    (sum->ift_ib - total->ift_ib), " ");
   1007       1.68     pooka 
   1008       1.68     pooka 			if (hflag && humanize_number(humbuf,
   1009       1.68     pooka 			    sizeof(humbuf), sum->ift_ob -  total->ift_ob, "",
   1010       1.68     pooka 			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
   1011       1.68     pooka 				printf("%10s %5.5s", humbuf, " ");
   1012       1.68     pooka 			else
   1013       1.68     pooka 				printf("%10llu %5.5s",
   1014       1.68     pooka 				    (unsigned long long)
   1015       1.68     pooka 				    (sum->ift_ob - total->ift_ob), " ");
   1016       1.26       kml 		} else {
   1017       1.33    bouyer 			printf("  %8llu %5llu %8llu %5llu %5llu",
   1018       1.33    bouyer 			    (unsigned long long)
   1019       1.33    bouyer 				(sum->ift_ip - total->ift_ip),
   1020       1.33    bouyer 			    (unsigned long long)
   1021       1.33    bouyer 				(sum->ift_ie - total->ift_ie),
   1022       1.33    bouyer 			    (unsigned long long)
   1023       1.33    bouyer 				(sum->ift_op - total->ift_op),
   1024       1.33    bouyer 			    (unsigned long long)
   1025       1.33    bouyer 				(sum->ift_oe - total->ift_oe),
   1026       1.33    bouyer 			    (unsigned long long)
   1027       1.33    bouyer 				(sum->ift_co - total->ift_co));
   1028       1.26       kml 		}
   1029        1.1       cgd 		if (dflag)
   1030       1.33    bouyer 			printf(" %5llu",
   1031       1.33    bouyer 			    (unsigned long long)(sum->ift_dr - total->ift_dr));
   1032        1.1       cgd 	}
   1033        1.1       cgd 	*total = *sum;
   1034        1.1       cgd 	putchar('\n');
   1035        1.1       cgd 	fflush(stdout);
   1036        1.1       cgd 	line++;
   1037       1.90  dholland 	if (signalled == 0) {
   1038       1.90  dholland 		sigsuspend(&emptyset);
   1039        1.1       cgd 	}
   1040       1.90  dholland 	signalled = 0;
   1041       1.82       mrg 	if (line == redraw_lines)
   1042        1.1       cgd 		goto banner;
   1043        1.1       cgd 	goto loop;
   1044        1.1       cgd 	/*NOTREACHED*/
   1045        1.1       cgd }
   1046        1.1       cgd 
   1047        1.1       cgd /*
   1048       1.64      elad  * Print a running summary of interface statistics.
   1049       1.64      elad  * Repeat display every interval seconds, showing statistics
   1050       1.64      elad  * collected over that interval.  Assumes that interval is non-zero.
   1051       1.64      elad  * First line printed at top of screen is always cumulative.
   1052       1.64      elad  */
   1053       1.64      elad static void
   1054       1.74      matt sidewaysintpr(unsigned int interval, u_long off)
   1055       1.64      elad {
   1056       1.64      elad 
   1057       1.64      elad 	if (use_sysctl) {
   1058       1.64      elad 		sidewaysintpr_sysctl(interval);
   1059       1.64      elad 	} else {
   1060       1.64      elad 		sidewaysintpr_kvm(interval, off);
   1061       1.64      elad 	}
   1062       1.64      elad }
   1063       1.64      elad 
   1064       1.64      elad /*
   1065        1.1       cgd  * Called if an interval expires before sidewaysintpr has completed a loop.
   1066        1.1       cgd  * Sets a flag to not wait for the alarm.
   1067        1.1       cgd  */
   1068        1.8   mycroft static void
   1069       1.74      matt catchalarm(int signo)
   1070        1.1       cgd {
   1071       1.28       mrg 
   1072       1.64      elad 	signalled = true;
   1073       1.64      elad }
   1074       1.64      elad 
   1075       1.64      elad static void
   1076       1.64      elad get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
   1077       1.64      elad {
   1078       1.64      elad 	int i;
   1079       1.64      elad 
   1080       1.64      elad 	for (i = 0; i < RTAX_MAX; i++) {
   1081       1.64      elad 		if (addrs & (1 << i)) {
   1082       1.64      elad 			rti_info[i] = sa;
   1083       1.71    martin 			sa = (struct sockaddr *)((char *)(sa) +
   1084       1.71    martin 			    RT_ROUNDUP(sa->sa_len));
   1085       1.64      elad 		} else
   1086       1.64      elad 			rti_info[i] = NULL;
   1087       1.64      elad 	}
   1088       1.64      elad }
   1089       1.64      elad 
   1090       1.64      elad static void
   1091       1.64      elad fetchifs(void)
   1092       1.64      elad {
   1093       1.64      elad 	struct if_msghdr *ifm;
   1094       1.64      elad 	int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
   1095       1.64      elad 	struct rt_msghdr *rtm;
   1096       1.64      elad 	struct if_data *ifd = NULL;
   1097       1.64      elad 	struct sockaddr *sa, *rti_info[RTAX_MAX];
   1098       1.64      elad 	struct sockaddr_dl *sdl;
   1099       1.93       mrg 	static char *buf = NULL;
   1100       1.93       mrg 	static size_t olen;
   1101       1.93       mrg 	char *next, *lim;
   1102       1.64      elad 	char name[IFNAMSIZ];
   1103       1.64      elad 	size_t len;
   1104       1.64      elad 
   1105       1.70     pooka 	if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
   1106       1.64      elad 		err(1, "sysctl");
   1107       1.93       mrg 	if (len > olen) {
   1108       1.93       mrg 		free(buf);
   1109       1.93       mrg 		if ((buf = malloc(len)) == NULL)
   1110       1.93       mrg 			err(1, NULL);
   1111       1.93       mrg 		olen = len;
   1112       1.93       mrg 	}
   1113       1.70     pooka 	if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
   1114       1.64      elad 		err(1, "sysctl");
   1115       1.64      elad 
   1116       1.64      elad 	lim = buf + len;
   1117       1.64      elad 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
   1118       1.64      elad 		rtm = (struct rt_msghdr *)next;
   1119       1.64      elad 		if (rtm->rtm_version != RTM_VERSION)
   1120       1.64      elad 			continue;
   1121       1.64      elad 		switch (rtm->rtm_type) {
   1122       1.64      elad 		case RTM_IFINFO:
   1123       1.64      elad 			ifm = (struct if_msghdr *)next;
   1124       1.64      elad 			ifd = &ifm->ifm_data;
   1125       1.64      elad 
   1126       1.64      elad 			sa = (struct sockaddr *)(ifm + 1);
   1127       1.64      elad 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
   1128       1.64      elad 
   1129       1.64      elad 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
   1130       1.64      elad 			if (sdl == NULL || sdl->sdl_family != AF_LINK)
   1131       1.64      elad 				continue;
   1132       1.64      elad 			bzero(name, sizeof(name));
   1133       1.64      elad 			if (sdl->sdl_nlen >= IFNAMSIZ)
   1134       1.64      elad 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
   1135       1.64      elad 			else if (sdl->sdl_nlen > 0)
   1136       1.64      elad 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
   1137       1.64      elad 
   1138       1.64      elad 			if (interface != 0 && !strcmp(name, interface)) {
   1139       1.64      elad 				strlcpy(ip_cur.ift_name, name,
   1140       1.64      elad 				    sizeof(ip_cur.ift_name));
   1141       1.64      elad 				ip_cur.ift_ip = ifd->ifi_ipackets;
   1142       1.64      elad 				ip_cur.ift_ib = ifd->ifi_ibytes;
   1143       1.64      elad 				ip_cur.ift_ie = ifd->ifi_ierrors;
   1144       1.64      elad 				ip_cur.ift_op = ifd->ifi_opackets;
   1145       1.64      elad 				ip_cur.ift_ob = ifd->ifi_obytes;
   1146       1.64      elad 				ip_cur.ift_oe = ifd->ifi_oerrors;
   1147       1.64      elad 				ip_cur.ift_co = ifd->ifi_collisions;
   1148       1.89  christos 				ip_cur.ift_dr = ifd->ifi_iqdrops;
   1149       1.64      elad 			}
   1150       1.64      elad 
   1151       1.64      elad 			sum_cur.ift_ip += ifd->ifi_ipackets;
   1152       1.64      elad 			sum_cur.ift_ib += ifd->ifi_ibytes;
   1153       1.64      elad 			sum_cur.ift_ie += ifd->ifi_ierrors;
   1154       1.64      elad 			sum_cur.ift_op += ifd->ifi_opackets;
   1155       1.64      elad 			sum_cur.ift_ob += ifd->ifi_obytes;
   1156       1.64      elad 			sum_cur.ift_oe += ifd->ifi_oerrors;
   1157       1.64      elad 			sum_cur.ift_co += ifd->ifi_collisions;
   1158       1.89  christos 			sum_cur.ift_dr += ifd->ifi_iqdrops;
   1159       1.64      elad 			break;
   1160       1.64      elad 		}
   1161       1.64      elad 	}
   1162       1.64      elad 	if (interface == NULL) {
   1163       1.64      elad 		strlcpy(ip_cur.ift_name, name,
   1164       1.64      elad 		    sizeof(ip_cur.ift_name));
   1165       1.64      elad 		ip_cur.ift_ip = ifd->ifi_ipackets;
   1166       1.64      elad 		ip_cur.ift_ib = ifd->ifi_ibytes;
   1167       1.64      elad 		ip_cur.ift_ie = ifd->ifi_ierrors;
   1168       1.64      elad 		ip_cur.ift_op = ifd->ifi_opackets;
   1169       1.64      elad 		ip_cur.ift_ob = ifd->ifi_obytes;
   1170       1.64      elad 		ip_cur.ift_oe = ifd->ifi_oerrors;
   1171       1.64      elad 		ip_cur.ift_co = ifd->ifi_collisions;
   1172       1.89  christos 		ip_cur.ift_dr = ifd->ifi_iqdrops;
   1173       1.64      elad 	}
   1174        1.1       cgd }
   1175