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