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