Home | History | Annotate | Line # | Download | only in netstat
if.c revision 1.84
      1  1.84  christos /*	$NetBSD: if.c,v 1.84 2016/07/13 22:01:12 christos 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.84  christos __RCSID("$NetBSD: if.c,v 1.84 2016/07/13 22:01:12 christos 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.64      elad 	int ift_dr;			/* drops */
     84  1.64      elad };
     85  1.64      elad 
     86  1.82       mrg static void set_lines(void);
     87  1.73  christos static void print_addr(struct sockaddr *, struct sockaddr **, struct if_data *,
     88  1.73  christos     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.8   mycroft static void catchalarm __P((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.64      elad bool	signalled;			/* set if alarm goes off "early" */
    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.64      elad 			       "%8.8s %5.5s %8.8s %5.5s %5.5s",
    156  1.64      elad 			       "Name", "Mtu", "Network", "Address", "Ipkts", "Ierrs",
    157  1.64      elad 			       "Opkts", "Oerrs", "Colls");
    158  1.64      elad 		}
    159  1.64      elad 		if (tflag)
    160  1.64      elad 			printf(" %4.4s", "Time");
    161  1.64      elad 		if (dflag)
    162  1.64      elad 			printf(" %5.5s", "Drops");
    163  1.64      elad 		putchar('\n');
    164  1.64      elad 	}
    165  1.64      elad }
    166  1.64      elad 
    167  1.64      elad static void
    168  1.64      elad intpr_sysctl(void)
    169  1.64      elad {
    170  1.64      elad 	struct if_msghdr *ifm;
    171  1.64      elad 	int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
    172  1.64      elad 	char *buf = NULL, *next, *lim, *cp;
    173  1.64      elad 	struct rt_msghdr *rtm;
    174  1.64      elad 	struct ifa_msghdr *ifam;
    175  1.64      elad 	struct if_data *ifd = NULL;
    176  1.64      elad 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    177  1.64      elad 	struct sockaddr_dl *sdl;
    178  1.64      elad 	uint64_t total = 0;
    179  1.64      elad 	size_t len;
    180  1.84  christos 	int did = 1, rtax = 0, n;
    181  1.64      elad 	char name[IFNAMSIZ + 1];	/* + 1 for `*' */
    182  1.64      elad 
    183  1.70     pooka 	if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
    184  1.64      elad 		err(1, "sysctl");
    185  1.64      elad 	if ((buf = malloc(len)) == NULL)
    186  1.64      elad 		err(1, NULL);
    187  1.70     pooka 	if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
    188  1.64      elad 		err(1, "sysctl");
    189  1.64      elad 
    190  1.64      elad 	intpr_header();
    191  1.64      elad 
    192  1.64      elad 	lim = buf + len;
    193  1.64      elad 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    194  1.64      elad 		rtm = (struct rt_msghdr *)next;
    195  1.64      elad 		if (rtm->rtm_version != RTM_VERSION)
    196  1.64      elad 			continue;
    197  1.64      elad 		switch (rtm->rtm_type) {
    198  1.64      elad 		case RTM_IFINFO:
    199  1.64      elad 			total = 0;
    200  1.64      elad 			ifm = (struct if_msghdr *)next;
    201  1.64      elad 			ifd = &ifm->ifm_data;
    202  1.64      elad 
    203  1.64      elad 			sa = (struct sockaddr *)(ifm + 1);
    204  1.64      elad 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
    205  1.64      elad 
    206  1.64      elad 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
    207  1.64      elad 			if (sdl == NULL || sdl->sdl_family != AF_LINK) {
    208  1.64      elad 				continue;
    209  1.64      elad 			}
    210  1.64      elad 			bzero(name, sizeof(name));
    211  1.64      elad 			if (sdl->sdl_nlen >= IFNAMSIZ)
    212  1.64      elad 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
    213  1.64      elad 			else if (sdl->sdl_nlen > 0)
    214  1.64      elad 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
    215  1.64      elad 
    216  1.64      elad 			if (interface != 0 && strcmp(name, interface) != 0)
    217  1.64      elad 				continue;
    218  1.64      elad 
    219  1.64      elad 			/* mark inactive interfaces with a '*' */
    220  1.64      elad 			cp = strchr(name, '\0');
    221  1.64      elad 			if ((ifm->ifm_flags & IFF_UP) == 0)
    222  1.64      elad 				*cp++ = '*';
    223  1.64      elad 			*cp = '\0';
    224  1.64      elad 
    225  1.64      elad 			if (qflag) {
    226  1.64      elad 				total = ifd->ifi_ibytes + ifd->ifi_obytes +
    227  1.64      elad 				    ifd->ifi_ipackets + ifd->ifi_ierrors +
    228  1.64      elad 				    ifd->ifi_opackets + ifd->ifi_oerrors +
    229  1.64      elad 				    ifd->ifi_collisions;
    230  1.64      elad 				if (tflag)
    231  1.64      elad 					total += 0; // XXX-elad ifnet.if_timer;
    232  1.64      elad 				if (dflag)
    233  1.64      elad 					total += 0; // XXX-elad ifnet.if_snd.ifq_drops;
    234  1.64      elad 				if (total == 0)
    235  1.64      elad 					continue;
    236  1.64      elad 			}
    237  1.84  christos 			/* Skip the first one */
    238  1.84  christos 			if (did) {
    239  1.84  christos 				did = 0;
    240  1.84  christos 				continue;
    241  1.84  christos 			}
    242  1.84  christos 			rtax = RTAX_IFP;
    243  1.64      elad 			break;
    244  1.64      elad 		case RTM_NEWADDR:
    245  1.64      elad 			if (qflag && total == 0)
    246  1.64      elad 				continue;
    247  1.64      elad 			if (interface != 0 && strcmp(name, interface) != 0)
    248  1.64      elad 				continue;
    249  1.64      elad 			ifam = (struct ifa_msghdr *)next;
    250  1.64      elad 			if ((ifam->ifam_addrs & (RTA_NETMASK | RTA_IFA |
    251  1.64      elad 			    RTA_BRD)) == 0)
    252  1.64      elad 				break;
    253  1.64      elad 
    254  1.64      elad 			sa = (struct sockaddr *)(ifam + 1);
    255  1.64      elad 
    256  1.64      elad 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
    257  1.84  christos 			rtax = RTAX_IFA;
    258  1.84  christos 			did = 1;
    259  1.64      elad 			break;
    260  1.84  christos 		default:
    261  1.84  christos 			continue;
    262  1.64      elad 		}
    263  1.84  christos 		if (vflag)
    264  1.84  christos 			n = strlen(name) < 5 ? 5 : strlen(name);
    265  1.84  christos 		else
    266  1.84  christos 			n = 5;
    267  1.84  christos 
    268  1.84  christos 		printf("%-*.*s %-5" PRIu64 " ", n, n, name, ifd->ifi_mtu);
    269  1.84  christos 		print_addr(rti_info[rtax], rti_info, ifd, NULL);
    270  1.64      elad 	}
    271  1.64      elad }
    272  1.64      elad 
    273  1.73  christos union ifaddr_u {
    274  1.73  christos 	struct ifaddr ifa;
    275  1.73  christos 	struct in_ifaddr in;
    276  1.73  christos #ifdef INET6
    277  1.73  christos 	struct in6_ifaddr in6;
    278  1.73  christos #endif /* INET6 */
    279  1.73  christos };
    280  1.73  christos 
    281  1.64      elad static void
    282  1.64      elad intpr_kvm(u_long ifnetaddr, void (*pfunc)(const char *))
    283  1.64      elad {
    284   1.1       cgd 	struct ifnet ifnet;
    285  1.73  christos 	union ifaddr_u ifaddr;
    286   1.7       cgd 	u_long ifaddraddr;
    287  1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    288  1.43     enami 	char name[IFNAMSIZ + 1];	/* + 1 for `*' */
    289   1.1       cgd 
    290   1.1       cgd 	if (ifnetaddr == 0) {
    291   1.1       cgd 		printf("ifnet: symbol not defined\n");
    292   1.1       cgd 		return;
    293   1.1       cgd 	}
    294  1.15   thorpej 
    295  1.15   thorpej 	/*
    296  1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    297  1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    298  1.15   thorpej 	 * to the first list element.
    299  1.15   thorpej 	 */
    300  1.15   thorpej 	if (kread(ifnetaddr, (char *)&ifhead, sizeof ifhead))
    301   1.8   mycroft 		return;
    302  1.15   thorpej 	ifnetaddr = (u_long)ifhead.tqh_first;
    303  1.15   thorpej 
    304  1.64      elad 	intpr_header();
    305  1.64      elad 
    306   1.1       cgd 	ifaddraddr = 0;
    307   1.1       cgd 	while (ifnetaddr || ifaddraddr) {
    308  1.25     lukem 		char *cp;
    309  1.64      elad 		int n;
    310   1.1       cgd 
    311   1.1       cgd 		if (ifaddraddr == 0) {
    312  1.15   thorpej 			if (kread(ifnetaddr, (char *)&ifnet, sizeof ifnet))
    313   1.8   mycroft 				return;
    314  1.25     lukem 			memmove(name, ifnet.if_xname, IFNAMSIZ);
    315  1.15   thorpej 			name[IFNAMSIZ - 1] = '\0';	/* sanity */
    316  1.10   mycroft 			ifnetaddr = (u_long)ifnet.if_list.tqe_next;
    317  1.16   thorpej 			if (interface != 0 && strcmp(name, interface) != 0)
    318   1.1       cgd 				continue;
    319  1.25     lukem 			cp = strchr(name, '\0');
    320  1.34    itojun 
    321  1.34    itojun 			if (pfunc) {
    322  1.34    itojun 				(*pfunc)(name);
    323  1.34    itojun 				continue;
    324  1.34    itojun 			}
    325  1.34    itojun 
    326  1.10   mycroft 			if ((ifnet.if_flags & IFF_UP) == 0)
    327   1.1       cgd 				*cp++ = '*';
    328   1.1       cgd 			*cp = '\0';
    329  1.10   mycroft 			ifaddraddr = (u_long)ifnet.if_addrlist.tqh_first;
    330   1.1       cgd 		}
    331  1.41    itojun 		if (vflag)
    332  1.41    itojun 			n = strlen(name) < 5 ? 5 : strlen(name);
    333  1.41    itojun 		else
    334  1.41    itojun 			n = 5;
    335  1.41    itojun 		printf("%-*.*s %-5llu ", n, n, name,
    336  1.33    bouyer 		    (unsigned long long)ifnet.if_mtu);
    337   1.1       cgd 		if (ifaddraddr == 0) {
    338  1.21  christos 			printf("%-13.13s ", "none");
    339  1.23     mikel 			printf("%-17.17s ", "none");
    340   1.1       cgd 		} else {
    341  1.64      elad 			struct sockaddr *sa;
    342  1.64      elad 
    343   1.8   mycroft 			if (kread(ifaddraddr, (char *)&ifaddr, sizeof ifaddr)) {
    344   1.8   mycroft 				ifaddraddr = 0;
    345   1.8   mycroft 				continue;
    346   1.8   mycroft 			}
    347   1.1       cgd #define CP(x) ((char *)(x))
    348   1.8   mycroft 			cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) +
    349  1.56    itojun 			    CP(&ifaddr);
    350  1.56    itojun 			sa = (struct sockaddr *)cp;
    351  1.73  christos 			print_addr(sa, (void *)&ifaddr, &ifnet.if_data, &ifnet);
    352  1.64      elad 		}
    353  1.64      elad 		ifaddraddr = (u_long)ifaddr.ifa.ifa_list.tqe_next;
    354  1.64      elad 	}
    355  1.64      elad 
    356  1.64      elad }
    357  1.64      elad 
    358  1.64      elad static void
    359  1.84  christos ia6_print(struct in6_addr *ia)
    360  1.84  christos {
    361  1.84  christos 	struct sockaddr_in6 as6;
    362  1.84  christos 	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
    363  1.84  christos 	int n;
    364  1.84  christos 
    365  1.84  christos 	memset(&as6, 0, sizeof(as6));
    366  1.84  christos 	as6.sin6_len = sizeof(struct sockaddr_in6);
    367  1.84  christos 	as6.sin6_family = AF_INET6;
    368  1.84  christos 	as6.sin6_addr = *ia;
    369  1.84  christos 	inet6_getscopeid(&as6, INET6_IS_ADDR_MC_LINKLOCAL);
    370  1.84  christos 	if (getnameinfo((struct sockaddr *)&as6, as6.sin6_len, hbuf,
    371  1.84  christos 	    sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) {
    372  1.84  christos 		strlcpy(hbuf, "??", sizeof(hbuf));
    373  1.84  christos 	}
    374  1.84  christos 	if (vflag)
    375  1.84  christos 		n = strlen(hbuf) < 17 ? 17 : strlen(hbuf);
    376  1.84  christos 	else
    377  1.84  christos 		n = 17;
    378  1.84  christos 	printf("\n%25s %-*.*s ", "", n, n, hbuf);
    379  1.84  christos }
    380  1.84  christos 
    381  1.84  christos static void
    382  1.73  christos print_addr(struct sockaddr *sa, struct sockaddr **rtinfo, struct if_data *ifd,
    383  1.73  christos     struct ifnet *ifnet)
    384  1.64      elad {
    385  1.64      elad 	char hexsep = '.';		/* for hexprint */
    386  1.64      elad 	static const char hexfmt[] = "%02x%c";	/* for hexprint */
    387  1.64      elad 	char hbuf[NI_MAXHOST];		/* for getnameinfo() */
    388  1.64      elad #ifdef INET6
    389  1.64      elad 	const int niflag = NI_NUMERICHOST;
    390  1.67    plunky 	struct sockaddr_in6 *sin6, *netmask6;
    391  1.64      elad #endif
    392  1.81  christos 	struct sockaddr_in netmask;
    393  1.64      elad 	struct sockaddr_in *sin;
    394  1.64      elad 	char *cp;
    395  1.64      elad 	int n, m;
    396  1.64      elad 
    397  1.64      elad 	switch (sa->sa_family) {
    398  1.64      elad 	case AF_UNSPEC:
    399  1.64      elad 		printf("%-13.13s ", "none");
    400  1.64      elad 		printf("%-17.17s ", "none");
    401  1.64      elad 		break;
    402  1.64      elad 	case AF_INET:
    403  1.64      elad 		sin = (struct sockaddr_in *)sa;
    404  1.64      elad 		if (use_sysctl) {
    405  1.81  christos 			netmask = *((struct sockaddr_in *)rtinfo[RTAX_NETMASK]);
    406  1.64      elad 		} else {
    407  1.64      elad 			struct in_ifaddr *ifaddr_in = (void *)rtinfo;
    408  1.81  christos 			netmask.sin_addr.s_addr = ifaddr_in->ia_subnetmask;
    409  1.64      elad 		}
    410  1.81  christos 		cp = netname4(sin, &netmask, nflag);
    411  1.64      elad 		if (vflag)
    412  1.64      elad 			n = strlen(cp) < 13 ? 13 : strlen(cp);
    413  1.64      elad 		else
    414  1.64      elad 			n = 13;
    415  1.64      elad 		printf("%-*.*s ", n, n, cp);
    416  1.80  christos 		cp = routename4(sin->sin_addr.s_addr, nflag);
    417  1.64      elad 		if (vflag)
    418  1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    419  1.64      elad 		else
    420  1.64      elad 			n = 17;
    421  1.64      elad 		printf("%-*.*s ", n, n, cp);
    422  1.64      elad 
    423  1.84  christos 		if (!aflag)
    424  1.84  christos 			break;
    425  1.84  christos 		if (ifnet) {
    426  1.64      elad 			u_long multiaddr;
    427  1.64      elad 			struct in_multi inm;
    428  1.73  christos 			union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
    429  1.64      elad 
    430  1.64      elad 			multiaddr = (u_long)
    431  1.73  christos 			    ifaddr->in.ia_multiaddrs.lh_first;
    432  1.64      elad 			while (multiaddr != 0) {
    433  1.64      elad 				kread(multiaddr, (char *)&inm,
    434  1.64      elad 				   sizeof inm);
    435  1.64      elad 				printf("\n%25s %-17.17s ", "",
    436  1.64      elad 				   routename4(
    437  1.80  christos 				      inm.inm_addr.s_addr, nflag));
    438  1.64      elad 				multiaddr =
    439  1.64      elad 				   (u_long)inm.inm_list.le_next;
    440  1.64      elad 			}
    441  1.84  christos 		} else {
    442  1.84  christos 			// XXX: Sysctl/ioctl to get multicast addresses
    443  1.64      elad 		}
    444  1.64      elad 		break;
    445  1.32    itojun #ifdef INET6
    446  1.64      elad 	case AF_INET6:
    447  1.64      elad 		sin6 = (struct sockaddr_in6 *)sa;
    448  1.79  christos 		inet6_getscopeid(sin6, INET6_IS_ADDR_LINKLOCAL);
    449  1.54    itojun #ifdef __KAME__
    450  1.78  christos 		if (!vflag)
    451  1.78  christos 			sin6->sin6_scope_id = 0;
    452  1.34    itojun #endif
    453  1.64      elad 
    454  1.64      elad 		if (use_sysctl) {
    455  1.64      elad 			netmask6 = (struct sockaddr_in6 *)rtinfo[RTAX_NETMASK];
    456  1.64      elad 		} else {
    457  1.64      elad 			struct in6_ifaddr *ifaddr_in6 = (void *)rtinfo;
    458  1.64      elad 			netmask6 = &ifaddr_in6->ia_prefixmask;
    459  1.64      elad 		}
    460  1.64      elad 
    461  1.80  christos 		cp = netname6(sin6, netmask6, nflag);
    462  1.64      elad 		if (vflag)
    463  1.64      elad 			n = strlen(cp) < 13 ? 13 : strlen(cp);
    464  1.64      elad 		else
    465  1.64      elad 			n = 13;
    466  1.64      elad 		printf("%-*.*s ", n, n, cp);
    467  1.64      elad 		if (getnameinfo((struct sockaddr *)sin6,
    468  1.64      elad 				sin6->sin6_len,
    469  1.64      elad 				hbuf, sizeof(hbuf), NULL, 0,
    470  1.64      elad 				niflag) != 0) {
    471  1.64      elad 			strlcpy(hbuf, "?", sizeof(hbuf));
    472  1.64      elad 		}
    473  1.64      elad 		cp = hbuf;
    474  1.64      elad 		if (vflag)
    475  1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    476  1.64      elad 		else
    477  1.64      elad 			n = 17;
    478  1.64      elad 		printf("%-*.*s ", n, n, cp);
    479  1.64      elad 
    480  1.84  christos 		if (!aflag)
    481  1.84  christos 			break;
    482  1.84  christos 		if (ifnet) {
    483  1.64      elad 			u_long multiaddr;
    484  1.64      elad 			struct in6_multi inm;
    485  1.73  christos 			union ifaddr_u *ifaddr = (union ifaddr_u *)rtinfo;
    486  1.35    itojun 
    487  1.84  christos 			multiaddr = (u_long)ifaddr->in6.ia6_multiaddrs.lh_first;
    488  1.64      elad 			while (multiaddr != 0) {
    489  1.84  christos 				kread(multiaddr, (char *)&inm, sizeof inm);
    490  1.84  christos 				ia6_print(&inm.in6m_addr);
    491  1.84  christos 				multiaddr = (u_long)inm.in6m_entry.le_next;
    492   1.1       cgd 			}
    493  1.84  christos 		} else {
    494  1.84  christos 			// XXX: Sysctl/ioctl to get multicast addresses
    495   1.1       cgd 		}
    496  1.64      elad 		break;
    497  1.64      elad #endif /*INET6*/
    498  1.64      elad #ifndef SMALL
    499  1.64      elad 	case AF_APPLETALK:
    500  1.64      elad 		printf("atalk:%-7.7s ",
    501  1.64      elad 		       atalk_print(sa,0x10));
    502  1.64      elad 		printf("%-17.17s ", atalk_print(sa,0x0b));
    503  1.64      elad 		break;
    504  1.64      elad #endif
    505  1.64      elad 	case AF_LINK:
    506  1.64      elad 		printf("%-13.13s ", "<Link>");
    507  1.64      elad 		if (getnameinfo(sa, sa->sa_len,
    508  1.64      elad 		    hbuf, sizeof(hbuf), NULL, 0,
    509  1.64      elad 		    NI_NUMERICHOST) != 0) {
    510  1.64      elad 			strlcpy(hbuf, "?", sizeof(hbuf));
    511  1.26       kml 		}
    512  1.64      elad 		cp = hbuf;
    513  1.64      elad 		if (vflag)
    514  1.64      elad 			n = strlen(cp) < 17 ? 17 : strlen(cp);
    515  1.64      elad 		else
    516  1.64      elad 			n = 17;
    517  1.64      elad 		printf("%-*.*s ", n, n, cp);
    518  1.64      elad 		break;
    519  1.64      elad 
    520  1.64      elad 	default:
    521  1.64      elad 		m = printf("(%d)", sa->sa_family);
    522  1.64      elad 		for (cp = sa->sa_len + (char *)sa;
    523  1.64      elad 			--cp > sa->sa_data && (*cp == 0);) {}
    524  1.64      elad 		n = cp - sa->sa_data + 1;
    525  1.64      elad 		cp = sa->sa_data;
    526  1.64      elad 
    527  1.64      elad 		while (--n >= 0)
    528  1.64      elad 			m += printf(hexfmt, *cp++ & 0xff,
    529  1.64      elad 				    n > 0 ? hexsep : ' ');
    530  1.64      elad 		m = 32 - m;
    531  1.64      elad 		while (m-- > 0)
    532  1.64      elad 			putchar(' ');
    533  1.64      elad 		break;
    534  1.64      elad 	}
    535  1.64      elad 
    536  1.64      elad 	if (bflag) {
    537  1.68     pooka 		char humbuf[HUMBUF_SIZE];
    538  1.68     pooka 
    539  1.68     pooka 		if (hflag && humanize_number(humbuf, sizeof(humbuf),
    540  1.68     pooka 		    ifd->ifi_ibytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    541  1.68     pooka 			printf("%10s ", humbuf);
    542  1.68     pooka 		else
    543  1.68     pooka 			printf("%10llu ", (unsigned long long)ifd->ifi_ibytes);
    544  1.68     pooka 
    545  1.68     pooka 		if (hflag && humanize_number(humbuf, sizeof(humbuf),
    546  1.68     pooka 		    ifd->ifi_obytes, "", HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    547  1.68     pooka 			printf("%10s", humbuf);
    548  1.68     pooka 		else
    549  1.68     pooka 			printf("%10llu", (unsigned long long)ifd->ifi_obytes);
    550  1.64      elad 	} else {
    551  1.64      elad 		printf("%8llu %5llu %8llu %5llu %5llu",
    552  1.64      elad 			(unsigned long long)ifd->ifi_ipackets,
    553  1.64      elad 			(unsigned long long)ifd->ifi_ierrors,
    554  1.64      elad 			(unsigned long long)ifd->ifi_opackets,
    555  1.64      elad 			(unsigned long long)ifd->ifi_oerrors,
    556  1.64      elad 			(unsigned long long)ifd->ifi_collisions);
    557   1.1       cgd 	}
    558  1.64      elad 	if (tflag)
    559  1.73  christos 		printf(" %4d", ifnet ? ifnet->if_timer : 0);
    560  1.64      elad 	if (dflag)
    561  1.73  christos 		printf(" %5d", ifnet ? ifnet->if_snd.ifq_drops : 0);
    562  1.64      elad 	putchar('\n');
    563  1.64      elad }
    564  1.64      elad 
    565  1.64      elad static void
    566  1.64      elad iftot_banner(struct iftot *ift)
    567  1.64      elad {
    568  1.64      elad 	if (bflag)
    569  1.64      elad 		printf("%7.7s in %8.8s %6.6s out %5.5s",
    570  1.64      elad 		    ift->ift_name, " ",
    571  1.64      elad 		    ift->ift_name, " ");
    572  1.64      elad 	else
    573  1.64      elad 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
    574  1.64      elad 		    ift->ift_name, " ",
    575  1.64      elad 		    ift->ift_name, " ", " ");
    576  1.64      elad 	if (dflag)
    577  1.64      elad 		printf(" %5.5s", " ");
    578  1.64      elad 
    579  1.64      elad 	if (bflag)
    580  1.64      elad 		printf("  %7.7s in %8.8s %6.6s out %5.5s",
    581  1.64      elad 		    "total", " ", "total", " ");
    582  1.64      elad 	else
    583  1.64      elad 		printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
    584  1.64      elad 		    "total", " ", "total", " ", " ");
    585  1.64      elad 	if (dflag)
    586  1.64      elad 		printf(" %5.5s", " ");
    587  1.64      elad 	putchar('\n');
    588  1.64      elad 	if (bflag)
    589  1.64      elad 		printf("%10.10s %8.8s %10.10s %5.5s",
    590  1.64      elad 		    "bytes", " ", "bytes", " ");
    591  1.64      elad 	else
    592  1.64      elad 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
    593  1.64      elad 		    "packets", "errs", "packets", "errs", "colls");
    594  1.64      elad 	if (dflag)
    595  1.64      elad 		printf(" %5.5s", "drops");
    596  1.64      elad 
    597  1.64      elad 	if (bflag)
    598  1.64      elad 		printf("  %10.10s %8.8s %10.10s %5.5s",
    599  1.64      elad 		    "bytes", " ", "bytes", " ");
    600  1.64      elad 	else
    601  1.64      elad 		printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
    602  1.64      elad 		    "packets", "errs", "packets", "errs", "colls");
    603  1.64      elad 	if (dflag)
    604  1.64      elad 		printf(" %5.5s", "drops");
    605  1.64      elad 	putchar('\n');
    606  1.64      elad 	fflush(stdout);
    607   1.1       cgd }
    608   1.1       cgd 
    609  1.64      elad static void
    610  1.64      elad iftot_print(struct iftot *cur, struct iftot *old)
    611  1.64      elad {
    612  1.64      elad 	if (bflag)
    613  1.75   msaitoh 		printf("%10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
    614  1.66  pgoyette 		    cur->ift_ib - old->ift_ib, " ",
    615  1.66  pgoyette 		    cur->ift_ob - old->ift_ob, " ");
    616  1.64      elad 	else
    617  1.75   msaitoh 		printf("%8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
    618  1.66  pgoyette 		    cur->ift_ip - old->ift_ip,
    619  1.66  pgoyette 		    cur->ift_ie - old->ift_ie,
    620  1.66  pgoyette 		    cur->ift_op - old->ift_op,
    621  1.66  pgoyette 		    cur->ift_oe - old->ift_oe,
    622  1.66  pgoyette 		    cur->ift_co - old->ift_co);
    623  1.64      elad 	if (dflag)
    624  1.64      elad 		printf(" %5llu",
    625  1.64      elad 		    /* XXX ifnet.if_snd.ifq_drops - ip->ift_dr); */
    626  1.64      elad 		    0LL);
    627  1.64      elad }
    628  1.64      elad 
    629  1.64      elad static void
    630  1.64      elad iftot_print_sum(struct iftot *cur, struct iftot *old)
    631  1.64      elad {
    632  1.64      elad 	if (bflag)
    633  1.75   msaitoh 		printf("  %10" PRIu64 " %8.8s %10" PRIu64 " %5.5s",
    634  1.66  pgoyette 		    cur->ift_ib - old->ift_ib, " ",
    635  1.66  pgoyette 		    cur->ift_ob - old->ift_ob, " ");
    636  1.64      elad 	else
    637  1.75   msaitoh 		printf("  %8" PRIu64 " %5" PRIu64 " %8" PRIu64 " %5" PRIu64 " %5" PRIu64,
    638  1.66  pgoyette 		    cur->ift_ip - old->ift_ip,
    639  1.66  pgoyette 		    cur->ift_ie - old->ift_ie,
    640  1.66  pgoyette 		    cur->ift_op - old->ift_op,
    641  1.66  pgoyette 		    cur->ift_oe - old->ift_oe,
    642  1.66  pgoyette 		    cur->ift_co - old->ift_co);
    643  1.64      elad 
    644  1.64      elad 	if (dflag)
    645  1.64      elad 		printf(" %5llu", (unsigned long long)(cur->ift_dr - old->ift_dr));
    646  1.64      elad }
    647  1.64      elad 
    648  1.72     joerg __dead static void
    649  1.64      elad sidewaysintpr_sysctl(unsigned interval)
    650  1.64      elad {
    651  1.64      elad 	sigset_t emptyset;
    652  1.82       mrg 	unsigned line;
    653  1.82       mrg 
    654  1.82       mrg 	set_lines();
    655  1.64      elad 
    656  1.64      elad 	fetchifs();
    657  1.64      elad 	if (ip_cur.ift_name[0] == '\0') {
    658  1.64      elad 		fprintf(stderr, "%s: %s: unknown interface\n",
    659  1.64      elad 		    getprogname(), interface);
    660  1.64      elad 		exit(1);
    661  1.64      elad 	}
    662  1.64      elad 
    663  1.64      elad 	(void)signal(SIGALRM, catchalarm);
    664  1.64      elad 	signalled = 0;
    665  1.64      elad 	(void)alarm(interval);
    666  1.64      elad banner:
    667  1.64      elad 	iftot_banner(&ip_cur);
    668  1.64      elad 
    669  1.64      elad 	line = 0;
    670  1.64      elad 	bzero(&ip_old, sizeof(ip_old));
    671  1.64      elad 	bzero(&sum_old, sizeof(sum_old));
    672  1.64      elad loop:
    673  1.64      elad 	bzero(&sum_cur, sizeof(sum_cur));
    674  1.64      elad 
    675  1.64      elad 	fetchifs();
    676  1.64      elad 
    677  1.64      elad 	iftot_print(&ip_cur, &ip_old);
    678  1.64      elad 
    679  1.64      elad 	ip_old = ip_cur;
    680  1.64      elad 
    681  1.64      elad 	iftot_print_sum(&sum_cur, &sum_old);
    682  1.64      elad 
    683  1.64      elad 	sum_old = sum_cur;
    684   1.1       cgd 
    685  1.64      elad 	putchar('\n');
    686  1.64      elad 	fflush(stdout);
    687  1.64      elad 	line++;
    688  1.64      elad 	sigemptyset(&emptyset);
    689  1.64      elad 	if (!signalled)
    690  1.64      elad 		sigsuspend(&emptyset);
    691  1.64      elad 	signalled = 0;
    692  1.64      elad 	(void)alarm(interval);
    693  1.82       mrg 	if (line == redraw_lines)
    694  1.64      elad 		goto banner;
    695  1.64      elad 	goto loop;
    696  1.64      elad 	/*NOTREACHED*/
    697  1.64      elad }
    698   1.1       cgd 
    699   1.8   mycroft static void
    700  1.64      elad sidewaysintpr_kvm(unsigned interval, u_long off)
    701   1.1       cgd {
    702  1.57     ragge 	struct itimerval it;
    703   1.1       cgd 	struct ifnet ifnet;
    704   1.7       cgd 	u_long firstifnet;
    705  1.25     lukem 	struct iftot *ip, *total;
    706  1.82       mrg 	unsigned line;
    707   1.1       cgd 	struct iftot *lastif, *sum, *interesting;
    708  1.15   thorpej 	struct ifnet_head ifhead;	/* TAILQ_HEAD */
    709   1.1       cgd 	int oldmask;
    710   1.1       cgd 
    711  1.82       mrg 	set_lines();
    712  1.82       mrg 
    713  1.15   thorpej 	/*
    714  1.15   thorpej 	 * Find the pointer to the first ifnet structure.  Replace
    715  1.15   thorpej 	 * the pointer to the TAILQ_HEAD with the actual pointer
    716  1.15   thorpej 	 * to the first list element.
    717  1.15   thorpej 	 */
    718  1.15   thorpej 	if (kread(off, (char *)&ifhead, sizeof ifhead))
    719   1.8   mycroft 		return;
    720  1.15   thorpej 	firstifnet = (u_long)ifhead.tqh_first;
    721  1.15   thorpej 
    722   1.1       cgd 	lastif = iftot;
    723   1.1       cgd 	sum = iftot + MAXIF - 1;
    724   1.1       cgd 	total = sum - 1;
    725  1.17       cgd 	interesting = (interface == NULL) ? iftot : NULL;
    726   1.1       cgd 	for (off = firstifnet, ip = iftot; off;) {
    727   1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet))
    728   1.8   mycroft 			break;
    729  1.25     lukem 		memset(ip->ift_name, 0, sizeof(ip->ift_name));
    730  1.31   mycroft 		snprintf(ip->ift_name, IFNAMSIZ, "%s", ifnet.if_xname);
    731  1.19   thorpej 		if (interface && strcmp(ifnet.if_xname, interface) == 0)
    732   1.1       cgd 			interesting = ip;
    733   1.1       cgd 		ip++;
    734   1.1       cgd 		if (ip >= iftot + MAXIF - 2)
    735   1.1       cgd 			break;
    736  1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    737  1.17       cgd 	}
    738  1.17       cgd 	if (interesting == NULL) {
    739  1.17       cgd 		fprintf(stderr, "%s: %s: unknown interface\n",
    740  1.47       cgd 		    getprogname(), interface);
    741  1.17       cgd 		exit(1);
    742   1.1       cgd 	}
    743   1.1       cgd 	lastif = ip;
    744   1.1       cgd 
    745   1.1       cgd 	(void)signal(SIGALRM, catchalarm);
    746  1.64      elad 	signalled = false;
    747  1.57     ragge 
    748  1.57     ragge 	it.it_interval.tv_sec = it.it_value.tv_sec = interval;
    749  1.57     ragge 	it.it_interval.tv_usec = it.it_value.tv_usec = 0;
    750  1.57     ragge 	setitimer(ITIMER_REAL, &it, NULL);
    751  1.57     ragge 
    752   1.1       cgd banner:
    753  1.31   mycroft 	if (bflag)
    754  1.31   mycroft 		printf("%7.7s in %8.8s %6.6s out %5.5s",
    755  1.31   mycroft 		    interesting->ift_name, " ",
    756  1.31   mycroft 		    interesting->ift_name, " ");
    757  1.31   mycroft 	else
    758  1.31   mycroft 		printf("%5.5s in %5.5s%5.5s out %5.5s %5.5s",
    759  1.31   mycroft 		    interesting->ift_name, " ",
    760  1.31   mycroft 		    interesting->ift_name, " ", " ");
    761  1.31   mycroft 	if (dflag)
    762  1.31   mycroft 		printf(" %5.5s", " ");
    763   1.1       cgd 	if (lastif - iftot > 0) {
    764  1.31   mycroft 		if (bflag)
    765  1.31   mycroft 			printf("  %7.7s in %8.8s %6.6s out %5.5s",
    766  1.31   mycroft 			    "total", " ", "total", " ");
    767  1.31   mycroft 		else
    768  1.31   mycroft 			printf("  %5.5s in %5.5s%5.5s out %5.5s %5.5s",
    769  1.31   mycroft 			    "total", " ", "total", " ", " ");
    770   1.1       cgd 		if (dflag)
    771  1.31   mycroft 			printf(" %5.5s", " ");
    772   1.1       cgd 	}
    773   1.1       cgd 	for (ip = iftot; ip < iftot + MAXIF; ip++) {
    774   1.1       cgd 		ip->ift_ip = 0;
    775  1.26       kml 		ip->ift_ib = 0;
    776   1.1       cgd 		ip->ift_ie = 0;
    777   1.1       cgd 		ip->ift_op = 0;
    778  1.26       kml 		ip->ift_ob = 0;
    779   1.1       cgd 		ip->ift_oe = 0;
    780   1.1       cgd 		ip->ift_co = 0;
    781   1.1       cgd 		ip->ift_dr = 0;
    782   1.1       cgd 	}
    783   1.1       cgd 	putchar('\n');
    784  1.31   mycroft 	if (bflag)
    785  1.26       kml 		printf("%10.10s %8.8s %10.10s %5.5s",
    786  1.31   mycroft 		    "bytes", " ", "bytes", " ");
    787  1.31   mycroft 	else
    788  1.31   mycroft 		printf("%8.8s %5.5s %8.8s %5.5s %5.5s",
    789  1.31   mycroft 		    "packets", "errs", "packets", "errs", "colls");
    790   1.1       cgd 	if (dflag)
    791  1.31   mycroft 		printf(" %5.5s", "drops");
    792  1.29      ross 	if (lastif - iftot > 0) {
    793  1.31   mycroft 		if (bflag)
    794  1.31   mycroft 			printf("  %10.10s %8.8s %10.10s %5.5s",
    795  1.31   mycroft 			    "bytes", " ", "bytes", " ");
    796  1.31   mycroft 		else
    797  1.31   mycroft 			printf("  %8.8s %5.5s %8.8s %5.5s %5.5s",
    798  1.31   mycroft 			    "packets", "errs", "packets", "errs", "colls");
    799  1.31   mycroft 		if (dflag)
    800  1.31   mycroft 			printf(" %5.5s", "drops");
    801  1.29      ross 	}
    802   1.1       cgd 	putchar('\n');
    803   1.1       cgd 	fflush(stdout);
    804   1.1       cgd 	line = 0;
    805   1.1       cgd loop:
    806   1.1       cgd 	sum->ift_ip = 0;
    807  1.26       kml 	sum->ift_ib = 0;
    808   1.1       cgd 	sum->ift_ie = 0;
    809   1.1       cgd 	sum->ift_op = 0;
    810  1.26       kml 	sum->ift_ob = 0;
    811   1.1       cgd 	sum->ift_oe = 0;
    812   1.1       cgd 	sum->ift_co = 0;
    813   1.1       cgd 	sum->ift_dr = 0;
    814   1.1       cgd 	for (off = firstifnet, ip = iftot; off && ip < lastif; ip++) {
    815   1.8   mycroft 		if (kread(off, (char *)&ifnet, sizeof ifnet)) {
    816   1.8   mycroft 			off = 0;
    817   1.8   mycroft 			continue;
    818   1.8   mycroft 		}
    819   1.1       cgd 		if (ip == interesting) {
    820  1.26       kml 			if (bflag) {
    821  1.68     pooka 				char humbuf[HUMBUF_SIZE];
    822  1.68     pooka 
    823  1.68     pooka 				if (hflag && humanize_number(humbuf,
    824  1.68     pooka 				    sizeof(humbuf),
    825  1.68     pooka 				    ifnet.if_ibytes - ip->ift_ib, "",
    826  1.68     pooka 				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    827  1.68     pooka 					printf("%10s %8.8s ", humbuf, " ");
    828  1.68     pooka 				else
    829  1.68     pooka 					printf("%10llu %8.8s ",
    830  1.68     pooka 					    (unsigned long long)
    831  1.68     pooka 					    (ifnet.if_ibytes-ip->ift_ib), " ");
    832  1.68     pooka 
    833  1.68     pooka 				if (hflag && humanize_number(humbuf,
    834  1.68     pooka 				    sizeof(humbuf),
    835  1.68     pooka 				    ifnet.if_obytes - ip->ift_ob, "",
    836  1.68     pooka 				    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    837  1.68     pooka 					printf("%10s %5.5s", humbuf, " ");
    838  1.68     pooka 				else
    839  1.68     pooka 					printf("%10llu %5.5s",
    840  1.68     pooka 					    (unsigned long long)
    841  1.68     pooka 					    (ifnet.if_obytes-ip->ift_ob), " ");
    842  1.26       kml 			} else {
    843  1.33    bouyer 				printf("%8llu %5llu %8llu %5llu %5llu",
    844  1.33    bouyer 				    (unsigned long long)
    845  1.33    bouyer 					(ifnet.if_ipackets - ip->ift_ip),
    846  1.33    bouyer 				    (unsigned long long)
    847  1.33    bouyer 					(ifnet.if_ierrors - ip->ift_ie),
    848  1.33    bouyer 				    (unsigned long long)
    849  1.33    bouyer 					(ifnet.if_opackets - ip->ift_op),
    850  1.33    bouyer 				    (unsigned long long)
    851  1.33    bouyer 					(ifnet.if_oerrors - ip->ift_oe),
    852  1.33    bouyer 				    (unsigned long long)
    853  1.33    bouyer 					(ifnet.if_collisions - ip->ift_co));
    854  1.26       kml 			}
    855   1.1       cgd 			if (dflag)
    856  1.33    bouyer 				printf(" %5llu",
    857  1.33    bouyer 				    (unsigned long long)
    858  1.33    bouyer 					(ifnet.if_snd.ifq_drops - ip->ift_dr));
    859   1.1       cgd 		}
    860   1.1       cgd 		ip->ift_ip = ifnet.if_ipackets;
    861  1.26       kml 		ip->ift_ib = ifnet.if_ibytes;
    862   1.1       cgd 		ip->ift_ie = ifnet.if_ierrors;
    863   1.1       cgd 		ip->ift_op = ifnet.if_opackets;
    864  1.26       kml 		ip->ift_ob = ifnet.if_obytes;
    865   1.1       cgd 		ip->ift_oe = ifnet.if_oerrors;
    866   1.1       cgd 		ip->ift_co = ifnet.if_collisions;
    867   1.1       cgd 		ip->ift_dr = ifnet.if_snd.ifq_drops;
    868   1.1       cgd 		sum->ift_ip += ip->ift_ip;
    869  1.26       kml 		sum->ift_ib += ip->ift_ib;
    870   1.1       cgd 		sum->ift_ie += ip->ift_ie;
    871   1.1       cgd 		sum->ift_op += ip->ift_op;
    872  1.26       kml 		sum->ift_ob += ip->ift_ob;
    873   1.1       cgd 		sum->ift_oe += ip->ift_oe;
    874   1.1       cgd 		sum->ift_co += ip->ift_co;
    875   1.1       cgd 		sum->ift_dr += ip->ift_dr;
    876  1.10   mycroft 		off = (u_long)ifnet.if_list.tqe_next;
    877   1.1       cgd 	}
    878   1.1       cgd 	if (lastif - iftot > 0) {
    879  1.26       kml 		if (bflag) {
    880  1.68     pooka 			char humbuf[HUMBUF_SIZE];
    881  1.68     pooka 
    882  1.68     pooka 			if (hflag && humanize_number(humbuf,
    883  1.68     pooka 			    sizeof(humbuf), sum->ift_ib - total->ift_ib, "",
    884  1.68     pooka 			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    885  1.69     enami 				printf("  %10s %8.8s ", humbuf, " ");
    886  1.68     pooka 			else
    887  1.69     enami 				printf("  %10llu %8.8s ",
    888  1.68     pooka 				    (unsigned long long)
    889  1.68     pooka 				    (sum->ift_ib - total->ift_ib), " ");
    890  1.68     pooka 
    891  1.68     pooka 			if (hflag && humanize_number(humbuf,
    892  1.68     pooka 			    sizeof(humbuf), sum->ift_ob -  total->ift_ob, "",
    893  1.68     pooka 			    HN_AUTOSCALE, HN_NOSPACE | HN_B) > 0)
    894  1.68     pooka 				printf("%10s %5.5s", humbuf, " ");
    895  1.68     pooka 			else
    896  1.68     pooka 				printf("%10llu %5.5s",
    897  1.68     pooka 				    (unsigned long long)
    898  1.68     pooka 				    (sum->ift_ob - total->ift_ob), " ");
    899  1.26       kml 		} else {
    900  1.33    bouyer 			printf("  %8llu %5llu %8llu %5llu %5llu",
    901  1.33    bouyer 			    (unsigned long long)
    902  1.33    bouyer 				(sum->ift_ip - total->ift_ip),
    903  1.33    bouyer 			    (unsigned long long)
    904  1.33    bouyer 				(sum->ift_ie - total->ift_ie),
    905  1.33    bouyer 			    (unsigned long long)
    906  1.33    bouyer 				(sum->ift_op - total->ift_op),
    907  1.33    bouyer 			    (unsigned long long)
    908  1.33    bouyer 				(sum->ift_oe - total->ift_oe),
    909  1.33    bouyer 			    (unsigned long long)
    910  1.33    bouyer 				(sum->ift_co - total->ift_co));
    911  1.26       kml 		}
    912   1.1       cgd 		if (dflag)
    913  1.33    bouyer 			printf(" %5llu",
    914  1.33    bouyer 			    (unsigned long long)(sum->ift_dr - total->ift_dr));
    915   1.1       cgd 	}
    916   1.1       cgd 	*total = *sum;
    917   1.1       cgd 	putchar('\n');
    918   1.1       cgd 	fflush(stdout);
    919   1.1       cgd 	line++;
    920   1.1       cgd 	oldmask = sigblock(sigmask(SIGALRM));
    921   1.1       cgd 	if (! signalled) {
    922   1.1       cgd 		sigpause(0);
    923   1.1       cgd 	}
    924   1.1       cgd 	sigsetmask(oldmask);
    925  1.64      elad 	signalled = false;
    926  1.82       mrg 	if (line == redraw_lines)
    927   1.1       cgd 		goto banner;
    928   1.1       cgd 	goto loop;
    929   1.1       cgd 	/*NOTREACHED*/
    930   1.1       cgd }
    931   1.1       cgd 
    932   1.1       cgd /*
    933  1.64      elad  * Print a running summary of interface statistics.
    934  1.64      elad  * Repeat display every interval seconds, showing statistics
    935  1.64      elad  * collected over that interval.  Assumes that interval is non-zero.
    936  1.64      elad  * First line printed at top of screen is always cumulative.
    937  1.64      elad  */
    938  1.64      elad static void
    939  1.74      matt sidewaysintpr(unsigned int interval, u_long off)
    940  1.64      elad {
    941  1.64      elad 
    942  1.64      elad 	if (use_sysctl) {
    943  1.64      elad 		sidewaysintpr_sysctl(interval);
    944  1.64      elad 	} else {
    945  1.64      elad 		sidewaysintpr_kvm(interval, off);
    946  1.64      elad 	}
    947  1.64      elad }
    948  1.64      elad 
    949  1.64      elad /*
    950   1.1       cgd  * Called if an interval expires before sidewaysintpr has completed a loop.
    951   1.1       cgd  * Sets a flag to not wait for the alarm.
    952   1.1       cgd  */
    953   1.8   mycroft static void
    954  1.74      matt catchalarm(int signo)
    955   1.1       cgd {
    956  1.28       mrg 
    957  1.64      elad 	signalled = true;
    958  1.64      elad }
    959  1.64      elad 
    960  1.64      elad static void
    961  1.64      elad get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
    962  1.64      elad {
    963  1.64      elad 	int i;
    964  1.64      elad 
    965  1.64      elad 	for (i = 0; i < RTAX_MAX; i++) {
    966  1.64      elad 		if (addrs & (1 << i)) {
    967  1.64      elad 			rti_info[i] = sa;
    968  1.71    martin 			sa = (struct sockaddr *)((char *)(sa) +
    969  1.71    martin 			    RT_ROUNDUP(sa->sa_len));
    970  1.64      elad 		} else
    971  1.64      elad 			rti_info[i] = NULL;
    972  1.64      elad 	}
    973  1.64      elad }
    974  1.64      elad 
    975  1.64      elad static void
    976  1.64      elad fetchifs(void)
    977  1.64      elad {
    978  1.64      elad 	struct if_msghdr *ifm;
    979  1.64      elad 	int mib[6] = { CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0 };
    980  1.64      elad 	struct rt_msghdr *rtm;
    981  1.64      elad 	struct if_data *ifd = NULL;
    982  1.64      elad 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    983  1.64      elad 	struct sockaddr_dl *sdl;
    984  1.64      elad 	char *buf, *next, *lim;
    985  1.64      elad 	char name[IFNAMSIZ];
    986  1.64      elad 	size_t len;
    987  1.64      elad 
    988  1.70     pooka 	if (prog_sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
    989  1.64      elad 		err(1, "sysctl");
    990  1.64      elad 	if ((buf = malloc(len)) == NULL)
    991  1.64      elad 		err(1, NULL);
    992  1.70     pooka 	if (prog_sysctl(mib, 6, buf, &len, NULL, 0) == -1)
    993  1.64      elad 		err(1, "sysctl");
    994  1.64      elad 
    995  1.64      elad 	lim = buf + len;
    996  1.64      elad 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    997  1.64      elad 		rtm = (struct rt_msghdr *)next;
    998  1.64      elad 		if (rtm->rtm_version != RTM_VERSION)
    999  1.64      elad 			continue;
   1000  1.64      elad 		switch (rtm->rtm_type) {
   1001  1.64      elad 		case RTM_IFINFO:
   1002  1.64      elad 			ifm = (struct if_msghdr *)next;
   1003  1.64      elad 			ifd = &ifm->ifm_data;
   1004  1.64      elad 
   1005  1.64      elad 			sa = (struct sockaddr *)(ifm + 1);
   1006  1.64      elad 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
   1007  1.64      elad 
   1008  1.64      elad 			sdl = (struct sockaddr_dl *)rti_info[RTAX_IFP];
   1009  1.64      elad 			if (sdl == NULL || sdl->sdl_family != AF_LINK)
   1010  1.64      elad 				continue;
   1011  1.64      elad 			bzero(name, sizeof(name));
   1012  1.64      elad 			if (sdl->sdl_nlen >= IFNAMSIZ)
   1013  1.64      elad 				memcpy(name, sdl->sdl_data, IFNAMSIZ - 1);
   1014  1.64      elad 			else if (sdl->sdl_nlen > 0)
   1015  1.64      elad 				memcpy(name, sdl->sdl_data, sdl->sdl_nlen);
   1016  1.64      elad 
   1017  1.64      elad 			if (interface != 0 && !strcmp(name, interface)) {
   1018  1.64      elad 				strlcpy(ip_cur.ift_name, name,
   1019  1.64      elad 				    sizeof(ip_cur.ift_name));
   1020  1.64      elad 				ip_cur.ift_ip = ifd->ifi_ipackets;
   1021  1.64      elad 				ip_cur.ift_ib = ifd->ifi_ibytes;
   1022  1.64      elad 				ip_cur.ift_ie = ifd->ifi_ierrors;
   1023  1.64      elad 				ip_cur.ift_op = ifd->ifi_opackets;
   1024  1.64      elad 				ip_cur.ift_ob = ifd->ifi_obytes;
   1025  1.64      elad 				ip_cur.ift_oe = ifd->ifi_oerrors;
   1026  1.64      elad 				ip_cur.ift_co = ifd->ifi_collisions;
   1027  1.64      elad 				ip_cur.ift_dr = 0;
   1028  1.64      elad 				    /* XXX-elad ifnet.if_snd.ifq_drops */
   1029  1.64      elad 			}
   1030  1.64      elad 
   1031  1.64      elad 			sum_cur.ift_ip += ifd->ifi_ipackets;
   1032  1.64      elad 			sum_cur.ift_ib += ifd->ifi_ibytes;
   1033  1.64      elad 			sum_cur.ift_ie += ifd->ifi_ierrors;
   1034  1.64      elad 			sum_cur.ift_op += ifd->ifi_opackets;
   1035  1.64      elad 			sum_cur.ift_ob += ifd->ifi_obytes;
   1036  1.64      elad 			sum_cur.ift_oe += ifd->ifi_oerrors;
   1037  1.64      elad 			sum_cur.ift_co += ifd->ifi_collisions;
   1038  1.64      elad 			sum_cur.ift_dr += 0; /* XXX-elad ifnet.if_snd.ifq_drops */
   1039  1.64      elad 			break;
   1040  1.64      elad 		}
   1041  1.64      elad 	}
   1042  1.64      elad 	if (interface == NULL) {
   1043  1.64      elad 		strlcpy(ip_cur.ift_name, name,
   1044  1.64      elad 		    sizeof(ip_cur.ift_name));
   1045  1.64      elad 		ip_cur.ift_ip = ifd->ifi_ipackets;
   1046  1.64      elad 		ip_cur.ift_ib = ifd->ifi_ibytes;
   1047  1.64      elad 		ip_cur.ift_ie = ifd->ifi_ierrors;
   1048  1.64      elad 		ip_cur.ift_op = ifd->ifi_opackets;
   1049  1.64      elad 		ip_cur.ift_ob = ifd->ifi_obytes;
   1050  1.64      elad 		ip_cur.ift_oe = ifd->ifi_oerrors;
   1051  1.64      elad 		ip_cur.ift_co = ifd->ifi_collisions;
   1052  1.64      elad 		ip_cur.ift_dr = 0;
   1053  1.64      elad 		    /* XXX-elad ifnet.if_snd.ifq_drops */
   1054  1.64      elad 	}
   1055   1.1       cgd }
   1056