]> git.saurik.com Git - apple/network_cmds.git/blob - ifconfig.tproj/ifconfig.c
0165a69c750f039137a8ccd0de6579ac65c3f61a
[apple/network_cmds.git] / ifconfig.tproj / ifconfig.c
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
43 #endif
44 static const char rcsid[] =
45 "$Id: ifconfig.c,v 1.1.1.2 2000/01/11 01:48:49 wsanchez Exp $";
46 #endif /* not lint */
47
48 #include <sys/param.h>
49 #include <sys/ioctl.h>
50 #include <sys/socket.h>
51 #include <sys/sysctl.h>
52 #include <sys/time.h>
53
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/if_dl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59
60 /* IP */
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <arpa/inet.h>
64 #include <netdb.h>
65
66 /* OSI */
67
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <stdio.h>
73 #include <stdlib.h>
74 #include <string.h>
75 #include <unistd.h>
76
77 #include "ifconfig.h"
78
79 struct ifreq ifr, ridreq;
80 struct ifaliasreq addreq;
81 struct sockaddr_in netmask;
82
83
84 char name[32];
85 int flags;
86 int metric;
87 int mtu;
88 int setaddr;
89 int setipdst;
90 int doalias;
91 int clearaddr;
92 int newaddr = 1;
93
94 struct afswtch;
95
96 void Perror __P((const char *cmd));
97
98 int ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
99 void notealias __P((const char *, int, int, const struct afswtch *afp));
100 void printb __P((const char *s, unsigned value, const char *bits));
101 void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
102 void status __P((const struct afswtch *afp, int addrcount,
103 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
104 struct ifa_msghdr *ifam));
105 void usage __P((void));
106
107 typedef void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
108
109 c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
110 c_func setifipdst;
111 c_func setifflags, setifmetric, setifmtu;
112
113
114 #define NEXTARG 0xffffff
115
116 const
117 struct cmd {
118 const char *c_name;
119 int c_parameter; /* NEXTARG means next argv */
120 void (*c_func) __P((const char *, int, int, const struct afswtch *afp));
121 } cmds[] = {
122 { "up", IFF_UP, setifflags } ,
123 { "down", -IFF_UP, setifflags },
124 { "arp", -IFF_NOARP, setifflags },
125 { "-arp", IFF_NOARP, setifflags },
126 { "debug", IFF_DEBUG, setifflags },
127 { "-debug", -IFF_DEBUG, setifflags },
128 { "alias", IFF_UP, notealias },
129 { "-alias", -IFF_UP, notealias },
130 { "delete", -IFF_UP, notealias },
131 #ifdef notdef
132 #define EN_SWABIPS 0x1000
133 { "swabips", EN_SWABIPS, setifflags },
134 { "-swabips", -EN_SWABIPS, setifflags },
135 #endif
136 { "netmask", NEXTARG, setifnetmask },
137 { "range", NEXTARG, 0 },
138 { "phase", NEXTARG, 0 },
139 { "metric", NEXTARG, setifmetric },
140 { "broadcast", NEXTARG, setifbroadaddr },
141 { "ipdst", NEXTARG, setifipdst },
142 { "link0", IFF_LINK0, setifflags },
143 { "-link0", -IFF_LINK0, setifflags },
144 { "link1", IFF_LINK1, setifflags },
145 { "-link1", -IFF_LINK1, setifflags },
146 { "link2", IFF_LINK2, setifflags },
147 { "-link2", -IFF_LINK2, setifflags },
148 #if USE_IF_MEDIA
149 { "media", NEXTARG, setmedia },
150 { "mediaopt", NEXTARG, setmediaopt },
151 { "-mediaopt", NEXTARG, unsetmediaopt },
152 #endif
153 { "normal", -IFF_LINK0, setifflags },
154 { "compress", IFF_LINK0, setifflags },
155 { "noicmp", IFF_LINK1, setifflags },
156 { "mtu", NEXTARG, setifmtu },
157 { 0, 0, setifaddr },
158 { 0, 0, setifdstaddr },
159 };
160
161 /*
162 * XNS support liberally adapted from code written at the University of
163 * Maryland principally by James O'Toole and Chris Torek.
164 */
165 typedef void af_status __P((int, struct rt_addrinfo *));
166 typedef void af_getaddr __P((const char *, int));
167
168 af_status in_status, ipx_status, at_status, ether_status;
169 af_getaddr in_getaddr, ipx_getaddr, at_getaddr;
170
171 /* Known address families */
172 const
173 struct afswtch {
174 const char *af_name;
175 short af_af;
176 af_status *af_status;
177 af_getaddr *af_getaddr;
178 u_long af_difaddr;
179 u_long af_aifaddr;
180 caddr_t af_ridreq;
181 caddr_t af_addreq;
182 } afs[] = {
183 #define C(x) ((caddr_t) &x)
184 { "inet", AF_INET, in_status, in_getaddr,
185 SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
186 { "ether", AF_INET, ether_status, NULL }, /* XXX not real!! */
187 #if 0 /* XXX conflicts with the media command */
188 #if USE_IF_MEDIA
189 { "media", AF_INET, media_status, NULL }, /* XXX not real!! */
190 #endif
191 #endif
192 { 0, 0, 0, 0 }
193 };
194
195 /*
196 * Expand the compacted form of addresses as returned via the
197 * configuration read via sysctl().
198 */
199
200 #define ROUNDUP(a) \
201 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
202 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
203
204 void
205 rt_xaddrs(cp, cplim, rtinfo)
206 caddr_t cp, cplim;
207 struct rt_addrinfo *rtinfo;
208 {
209 struct sockaddr *sa;
210 int i;
211
212 memset(rtinfo->rti_info, 0, sizeof(rtinfo->rti_info));
213 for (i = 0; (i < RTAX_MAX) && (cp < cplim); i++) {
214 if ((rtinfo->rti_addrs & (1 << i)) == 0)
215 continue;
216 rtinfo->rti_info[i] = sa = (struct sockaddr *)cp;
217 ADVANCE(cp, sa);
218 }
219 }
220
221
222 void
223 usage()
224 {
225 fprintf(stderr, "%s\n%s\n%s\n%s\n",
226 "usage: ifconfig interface address_family [address [dest_address]]",
227 " [parameters]",
228 " ifconfig -a [-d] [-u] [address_family]",
229 " ifconfig -l [-d] [-u] [address_family]");
230 exit(1);
231 }
232
233 int
234 main(argc, argv)
235 int argc;
236 char *const *argv;
237 {
238 int c;
239 int all, namesonly, downonly, uponly;
240 int foundit = 0, need_nl = 0;
241 const struct afswtch *afp = 0;
242 int addrcount;
243 struct if_msghdr *ifm, *nextifm;
244 struct ifa_msghdr *ifam;
245 struct sockaddr_dl *sdl;
246 char *buf, *lim, *next;
247
248
249 size_t needed;
250 int mib[6];
251
252 /* Parse leading line options */
253 all = downonly = uponly = namesonly = 0;
254 while ((c = getopt(argc, argv, "adlmu")) != -1) {
255 switch (c) {
256 case 'a': /* scan all interfaces */
257 all++;
258 break;
259 case 'l': /* scan interface names only */
260 namesonly++;
261 break;
262 case 'd': /* restrict scan to "down" interfaces */
263 downonly++;
264 break;
265 case 'u': /* restrict scan to "up" interfaces */
266 uponly++;
267 break;
268 case 'm': /* show media choices in status */
269 /* ignored for compatibility */
270 break;
271 default:
272 usage();
273 break;
274 }
275 }
276 argc -= optind;
277 argv += optind;
278
279 /* -l cannot be used with -a or -m */
280 if (namesonly && all)
281 usage();
282
283 /* nonsense.. */
284 if (uponly && downonly)
285 usage();
286
287 /* -a and -l allow an address family arg to limit the output */
288 if (all || namesonly) {
289 if (argc > 1)
290 usage();
291
292 if (argc == 1) {
293 for (afp = afs; afp->af_name; afp++)
294 if (strcmp(afp->af_name, *argv) == 0) {
295 argc--, argv++;
296 break;
297 }
298 if (afp->af_name == NULL)
299 usage();
300 /* leave with afp non-zero */
301 }
302 } else {
303 /* not listing, need an argument */
304 if (argc < 1)
305 usage();
306
307 strncpy(name, *argv, sizeof(name));
308 argc--, argv++;
309 }
310
311 /* Check for address family */
312 if (argc > 0) {
313 for (afp = afs; afp->af_name; afp++)
314 if (strcmp(afp->af_name, *argv) == 0) {
315 argc--, argv++;
316 break;
317 }
318 if (afp->af_name == NULL)
319 afp = NULL; /* not a family, NULL */
320 }
321
322 mib[0] = CTL_NET;
323 mib[1] = PF_ROUTE;
324 mib[2] = 0;
325 mib[3] = 0; /* address family */
326 mib[4] = NET_RT_IFLIST;
327 mib[5] = 0;
328
329 /* if particular family specified, only ask about it */
330 if (afp)
331 mib[3] = afp->af_af;
332
333 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
334 errx(1, "iflist-sysctl-estimate");
335 if ((buf = malloc(needed)) == NULL)
336 errx(1, "malloc");
337 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
338 errx(1, "actual retrieval of interface table");
339 lim = buf + needed;
340
341 next = buf;
342 while (next < lim) {
343
344 ifm = (struct if_msghdr *)next;
345
346 if (ifm->ifm_type == RTM_IFINFO) {
347 sdl = (struct sockaddr_dl *)(ifm + 1);
348 flags = ifm->ifm_flags;
349 } else {
350 fprintf(stderr, "out of sync parsing NET_RT_IFLIST\n");
351 fprintf(stderr, "expected %d, got %d\n", RTM_IFINFO,
352 ifm->ifm_type);
353 fprintf(stderr, "msglen = %d\n", ifm->ifm_msglen);
354 fprintf(stderr, "buf:%p, next:%p, lim:%p\n", buf, next,
355 lim);
356 exit (1);
357 }
358
359 next += ifm->ifm_msglen;
360 ifam = NULL;
361 addrcount = 0;
362 while (next < lim) {
363
364 nextifm = (struct if_msghdr *)next;
365
366 if (nextifm->ifm_type != RTM_NEWADDR)
367 break;
368
369 if (ifam == NULL)
370 ifam = (struct ifa_msghdr *)nextifm;
371
372 addrcount++;
373 next += nextifm->ifm_msglen;
374 }
375
376 if (all || namesonly) {
377 if (uponly)
378 if ((flags & IFF_UP) == 0)
379 continue; /* not up */
380 if (downonly)
381 if (flags & IFF_UP)
382 continue; /* not down */
383 strncpy(name, sdl->sdl_data, sdl->sdl_nlen);
384 name[sdl->sdl_nlen] = '\0';
385 if (namesonly) {
386 if (afp == NULL ||
387 afp->af_status != ether_status ||
388 sdl->sdl_type == IFT_ETHER) {
389 if (need_nl)
390 putchar(' ');
391 fputs(name, stdout);
392 need_nl++;
393 }
394 continue;
395 }
396 } else {
397 if (strlen(name) != sdl->sdl_nlen)
398 continue; /* not same len */
399 if (strncmp(name, sdl->sdl_data, sdl->sdl_nlen) != 0)
400 continue; /* not same name */
401 }
402
403 if (argc > 0)
404 ifconfig(argc, argv, afp);
405 else
406 status(afp, addrcount, sdl, ifm, ifam);
407
408 if (all == 0 && namesonly == 0) {
409 foundit++; /* flag it as 'done' */
410 break;
411 }
412 }
413 free(buf);
414
415 if (namesonly && need_nl > 0)
416 putchar('\n');
417
418 if (all == 0 && namesonly == 0 && foundit == 0)
419 errx(1, "interface %s does not exist", name);
420
421
422 exit (0);
423 }
424
425
426 int
427 ifconfig(argc, argv, afp)
428 int argc;
429 char *const *argv;
430 const struct afswtch *afp;
431 {
432 int s;
433
434 if (afp == NULL)
435 afp = &afs[0];
436 ifr.ifr_addr.sa_family = afp->af_af;
437 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
438
439 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
440 err(1, "socket");
441
442 while (argc > 0) {
443 register const struct cmd *p;
444
445 for (p = cmds; p->c_name; p++)
446 if (strcmp(*argv, p->c_name) == 0)
447 break;
448 if (p->c_name == 0 && setaddr)
449 p++; /* got src, do dst */
450 if (p->c_func) {
451 if (p->c_parameter == NEXTARG) {
452 if (argv[1] == NULL)
453 errx(1, "'%s' requires argument",
454 p->c_name);
455 (*p->c_func)(argv[1], 0, s, afp);
456 argc--, argv++;
457 } else
458 (*p->c_func)(*argv, p->c_parameter, s, afp);
459 }
460 argc--, argv++;
461 }
462
463 if (clearaddr) {
464 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
465 warnx("interface %s cannot change %s addresses!",
466 name, afp->af_name);
467 clearaddr = NULL;
468 }
469 }
470 if (clearaddr) {
471 int ret;
472 strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name);
473 if ((ret = ioctl(s, afp->af_difaddr, afp->af_ridreq)) < 0) {
474 if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
475 /* means no previous address for interface */
476 } else
477 Perror("ioctl (SIOCDIFADDR)");
478 }
479 }
480 if (newaddr) {
481 if (afp->af_ridreq == NULL || afp->af_difaddr == 0) {
482 warnx("interface %s cannot change %s addresses!",
483 name, afp->af_name);
484 newaddr = NULL;
485 }
486 }
487 if (newaddr) {
488 strncpy(afp->af_addreq, name, sizeof ifr.ifr_name);
489 if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0)
490 Perror("ioctl (SIOCAIFADDR)");
491 }
492 close(s);
493 return(0);
494 }
495 #define RIDADDR 0
496 #define ADDR 1
497 #define MASK 2
498 #define DSTADDR 3
499
500 /*ARGSUSED*/
501 void
502 setifaddr(addr, param, s, afp)
503 const char *addr;
504 int param;
505 int s;
506 const struct afswtch *afp;
507 {
508 /*
509 * Delay the ioctl to set the interface addr until flags are all set.
510 * The address interpretation may depend on the flags,
511 * and the flags may change when the address is set.
512 */
513 setaddr++;
514 if (doalias == 0)
515 clearaddr = 1;
516 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
517 }
518
519 void
520 setifnetmask(addr, dummy, s, afp)
521 const char *addr;
522 int dummy ;
523 int s;
524 const struct afswtch *afp;
525 {
526 (*afp->af_getaddr)(addr, MASK);
527 }
528
529 void
530 setifbroadaddr(addr, dummy, s, afp)
531 const char *addr;
532 int dummy ;
533 int s;
534 const struct afswtch *afp;
535 {
536 (*afp->af_getaddr)(addr, DSTADDR);
537 }
538
539 void
540 setifipdst(addr, dummy, s, afp)
541 const char *addr;
542 int dummy ;
543 int s;
544 const struct afswtch *afp;
545 {
546 in_getaddr(addr, DSTADDR);
547 setipdst++;
548 clearaddr = 0;
549 newaddr = 0;
550 }
551 #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
552
553 void
554 notealias(addr, param, s, afp)
555 const char *addr;
556 int param;
557 int s;
558 const struct afswtch *afp;
559 {
560 if (setaddr && doalias == 0 && param < 0)
561 bcopy((caddr_t)rqtosa(af_addreq),
562 (caddr_t)rqtosa(af_ridreq),
563 rqtosa(af_addreq)->sa_len);
564 doalias = param;
565 if (param < 0) {
566 clearaddr = 1;
567 newaddr = 0;
568 } else
569 clearaddr = 0;
570 }
571
572 /*ARGSUSED*/
573 void
574 setifdstaddr(addr, param, s, afp)
575 const char *addr;
576 int param ;
577 int s;
578 const struct afswtch *afp;
579 {
580 (*afp->af_getaddr)(addr, DSTADDR);
581 }
582
583 void
584 setifflags(vname, value, s, afp)
585 const char *vname;
586 int value;
587 int s;
588 const struct afswtch *afp;
589 {
590 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
591 Perror("ioctl (SIOCGIFFLAGS)");
592 exit(1);
593 }
594 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
595 flags = ifr.ifr_flags;
596
597 if (value < 0) {
598 value = -value;
599 flags &= ~value;
600 } else
601 flags |= value;
602 ifr.ifr_flags = flags;
603 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
604 Perror(vname);
605 }
606
607 void
608 setifmetric(val, dummy, s, afp)
609 const char *val;
610 int dummy ;
611 int s;
612 const struct afswtch *afp;
613 {
614 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
615 ifr.ifr_metric = atoi(val);
616 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
617 warn("ioctl (set metric)");
618 }
619
620 void
621 setifmtu(val, dummy, s, afp)
622 const char *val;
623 int dummy ;
624 int s;
625 const struct afswtch *afp;
626 {
627 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
628 ifr.ifr_mtu = atoi(val);
629 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
630 warn("ioctl (set mtu)");
631 }
632
633
634 #define IFFBITS \
635 "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6b6\7RUNNING" \
636 "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
637 "\20MULTICAST"
638
639 /*
640 * Print the status of the interface. If an address family was
641 * specified, show it and it only; otherwise, show them all.
642 */
643 void
644 status(afp, addrcount, sdl, ifm, ifam)
645 const struct afswtch *afp;
646 int addrcount;
647 struct sockaddr_dl *sdl;
648 struct if_msghdr *ifm;
649 struct ifa_msghdr *ifam;
650 {
651 const struct afswtch *p = NULL;
652 struct rt_addrinfo info;
653 int allfamilies, s;
654
655 if (afp == NULL) {
656 allfamilies = 1;
657 afp = &afs[0];
658 } else
659 allfamilies = 0;
660
661 ifr.ifr_addr.sa_family = afp->af_af;
662 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
663
664 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
665 err(1, "socket");
666
667 /*
668 * XXX is it we are doing a SIOCGIFMETRIC etc for one family.
669 * is it possible that the metric and mtu can be different for
670 * each family? If so, we have a format problem, because the
671 * metric and mtu is printed on the global the flags line.
672 */
673 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
674 warn("ioctl (SIOCGIFMETRIC)");
675 else
676 metric = ifr.ifr_metric;
677
678 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
679 warn("ioctl (SIOCGIFMTU)");
680 else
681 mtu = ifr.ifr_mtu;
682
683 printf("%s: ", name);
684 printb("flags", flags, IFFBITS);
685 if (metric)
686 printf(" metric %d", metric);
687 if (mtu)
688 printf(" mtu %d", mtu);
689 putchar('\n');
690
691 while (addrcount > 0) {
692
693 info.rti_addrs = ifam->ifam_addrs;
694
695 /* Expand the compacted addresses */
696 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
697 &info);
698
699 if (!allfamilies) {
700 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
701 #if USE_IF_MEDIA
702 afp->af_status != media_status &&
703 #endif
704 afp->af_status != ether_status) {
705 p = afp;
706 (*p->af_status)(s, &info);
707 }
708 } else for (p = afs; p->af_name; p++) {
709 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
710 #if USE_IF_MEDIA
711 p->af_status != media_status &&
712 #endif
713 p->af_status != ether_status)
714 (*p->af_status)(s, &info);
715 }
716 addrcount--;
717 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
718 }
719 if (allfamilies || afp->af_status == ether_status)
720 ether_status(s, (struct rt_addrinfo *)sdl);
721 #if USE_IF_MEDIA
722 if (allfamilies || afp->af_status == media_status)
723 media_status(s, NULL);
724 #endif
725 if (!allfamilies && !p &&
726 afp->af_status != ether_status)
727 warnx("%s has no %s interface address!", name, afp->af_name);
728
729 close(s);
730 return;
731 }
732
733 void
734 in_status(s, info)
735 int s ;
736 struct rt_addrinfo * info;
737 {
738 struct sockaddr_in *sin, null_sin;
739
740 memset(&null_sin, 0, sizeof(null_sin));
741
742 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
743 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
744
745 if (flags & IFF_POINTOPOINT) {
746 /* note RTAX_BRD overlap with IFF_BROADCAST */
747 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
748 if (!sin)
749 sin = &null_sin;
750 printf("--> %s ", inet_ntoa(sin->sin_addr));
751 }
752
753 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
754 if (!sin)
755 sin = &null_sin;
756 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
757
758 if (flags & IFF_BROADCAST) {
759 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
760 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
761 if (sin && sin->sin_addr.s_addr != 0)
762 printf("broadcast %s", inet_ntoa(sin->sin_addr));
763 }
764 putchar('\n');
765 }
766
767 void
768 ether_status(s, info)
769 int s ;
770 struct rt_addrinfo *info;
771 {
772 char *cp;
773 int n;
774 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
775
776 cp = (char *)LLADDR(sdl);
777 if ((n = sdl->sdl_alen) > 0) {
778 if (sdl->sdl_type == IFT_ETHER)
779 printf ("\tether ");
780 else
781 printf ("\tlladdr ");
782 while (--n >= 0)
783 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
784 putchar('\n');
785 }
786 }
787
788 void
789 Perror(cmd)
790 const char *cmd;
791 {
792 switch (errno) {
793
794 case ENXIO:
795 errx(1, "%s: no such interface", cmd);
796 break;
797
798 case EPERM:
799 errx(1, "%s: permission denied", cmd);
800 break;
801
802 default:
803 err(1, "%s", cmd);
804 }
805 }
806
807 #define SIN(x) ((struct sockaddr_in *) &(x))
808 struct sockaddr_in *sintab[] = {
809 SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
810 SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
811
812 void
813 in_getaddr(s, which)
814 const char *s;
815 int which;
816 {
817 register struct sockaddr_in *sin = sintab[which];
818 struct hostent *hp;
819 struct netent *np;
820
821 sin->sin_len = sizeof(*sin);
822 if (which != MASK)
823 sin->sin_family = AF_INET;
824
825 if (inet_aton(s, &sin->sin_addr))
826 return;
827 if ((hp = gethostbyname(s)) != 0)
828 bcopy(hp->h_addr, (char *)&sin->sin_addr,
829 MIN(hp->h_length, sizeof(sin->sin_addr)));
830 else if ((np = getnetbyname(s)) != 0)
831 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
832 else
833 errx(1, "%s: bad value", s);
834 }
835
836 /*
837 * Print a value a la the %b format of the kernel's printf
838 */
839 void
840 printb(s, v, bits)
841 const char *s;
842 register unsigned v;
843 register const char *bits;
844 {
845 register int i, any = 0;
846 register char c;
847
848 if (bits && *bits == 8)
849 printf("%s=%o", s, v);
850 else
851 printf("%s=%x", s, v);
852 bits++;
853 if (bits) {
854 putchar('<');
855 while ((i = *bits++) != '\0') {
856 if (v & (1 << (i-1))) {
857 if (any)
858 putchar(',');
859 any = 1;
860 for (; (c = *bits) > 32; bits++)
861 putchar(c);
862 } else
863 for (; *bits > 32; bits++)
864 ;
865 }
866 putchar('>');
867 }
868 }
869
870