]> git.saurik.com Git - apple/network_cmds.git/blame - ifconfig.tproj/ifconfig.c
network_cmds-85.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / ifconfig.c
CommitLineData
b7080c8e
A
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
35static 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
42static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
43#endif
44static const char rcsid[] =
8052502f 45 "$Id: ifconfig.c,v 1.2 2001/08/03 16:27:01 vlubet Exp $";
b7080c8e
A
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
79struct ifreq ifr, ridreq;
80struct ifaliasreq addreq;
81struct sockaddr_in netmask;
82
83
84char name[32];
85int flags;
86int metric;
87int mtu;
88int setaddr;
89int setipdst;
90int doalias;
91int clearaddr;
8052502f 92int newaddr;
b7080c8e
A
93
94struct afswtch;
95
96void Perror __P((const char *cmd));
97
98int ifconfig __P((int argc, char *const *argv, const struct afswtch *afp));
99void notealias __P((const char *, int, int, const struct afswtch *afp));
100void printb __P((const char *s, unsigned value, const char *bits));
101void rt_xaddrs __P((caddr_t, caddr_t, struct rt_addrinfo *));
102void status __P((const struct afswtch *afp, int addrcount,
103 struct sockaddr_dl *sdl, struct if_msghdr *ifm,
104 struct ifa_msghdr *ifam));
105void usage __P((void));
106
107typedef void c_func __P((const char *cmd, int arg, int s, const struct afswtch *afp));
108
109c_func setifaddr, setifbroadaddr, setifdstaddr, setifnetmask;
110c_func setifipdst;
111c_func setifflags, setifmetric, setifmtu;
112
113
114#define NEXTARG 0xffffff
115
116const
117struct 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 */
165typedef void af_status __P((int, struct rt_addrinfo *));
166typedef void af_getaddr __P((const char *, int));
167
168af_status in_status, ipx_status, at_status, ether_status;
169af_getaddr in_getaddr, ipx_getaddr, at_getaddr;
170
171/* Known address families */
172const
173struct 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
204void
205rt_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
222void
223usage()
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
233int
234main(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
426int
427ifconfig(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*/
501void
502setifaddr(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 */
8052502f 513 newaddr = 1;
b7080c8e
A
514 setaddr++;
515 if (doalias == 0)
516 clearaddr = 1;
517 (*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
518}
519
520void
521setifnetmask(addr, dummy, s, afp)
522 const char *addr;
523 int dummy ;
524 int s;
525 const struct afswtch *afp;
526{
527 (*afp->af_getaddr)(addr, MASK);
528}
529
530void
531setifbroadaddr(addr, dummy, s, afp)
532 const char *addr;
533 int dummy ;
534 int s;
535 const struct afswtch *afp;
536{
537 (*afp->af_getaddr)(addr, DSTADDR);
538}
539
540void
541setifipdst(addr, dummy, s, afp)
542 const char *addr;
543 int dummy ;
544 int s;
545 const struct afswtch *afp;
546{
547 in_getaddr(addr, DSTADDR);
548 setipdst++;
549 clearaddr = 0;
550 newaddr = 0;
551}
552#define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr))
553
554void
555notealias(addr, param, s, afp)
556 const char *addr;
557 int param;
558 int s;
559 const struct afswtch *afp;
560{
561 if (setaddr && doalias == 0 && param < 0)
562 bcopy((caddr_t)rqtosa(af_addreq),
563 (caddr_t)rqtosa(af_ridreq),
564 rqtosa(af_addreq)->sa_len);
565 doalias = param;
566 if (param < 0) {
567 clearaddr = 1;
568 newaddr = 0;
569 } else
570 clearaddr = 0;
571}
572
573/*ARGSUSED*/
574void
575setifdstaddr(addr, param, s, afp)
576 const char *addr;
577 int param ;
578 int s;
579 const struct afswtch *afp;
580{
581 (*afp->af_getaddr)(addr, DSTADDR);
582}
583
584void
585setifflags(vname, value, s, afp)
586 const char *vname;
587 int value;
588 int s;
589 const struct afswtch *afp;
590{
591 if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
592 Perror("ioctl (SIOCGIFFLAGS)");
593 exit(1);
594 }
595 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
596 flags = ifr.ifr_flags;
597
598 if (value < 0) {
599 value = -value;
600 flags &= ~value;
601 } else
602 flags |= value;
603 ifr.ifr_flags = flags;
604 if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&ifr) < 0)
605 Perror(vname);
606}
607
608void
609setifmetric(val, dummy, s, afp)
610 const char *val;
611 int dummy ;
612 int s;
613 const struct afswtch *afp;
614{
615 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
616 ifr.ifr_metric = atoi(val);
617 if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
618 warn("ioctl (set metric)");
619}
620
621void
622setifmtu(val, dummy, s, afp)
623 const char *val;
624 int dummy ;
625 int s;
626 const struct afswtch *afp;
627{
628 strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name));
629 ifr.ifr_mtu = atoi(val);
630 if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0)
631 warn("ioctl (set mtu)");
632}
633
634
635#define IFFBITS \
636"\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6b6\7RUNNING" \
637"\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \
638"\20MULTICAST"
639
640/*
641 * Print the status of the interface. If an address family was
642 * specified, show it and it only; otherwise, show them all.
643 */
644void
645status(afp, addrcount, sdl, ifm, ifam)
646 const struct afswtch *afp;
647 int addrcount;
648 struct sockaddr_dl *sdl;
649 struct if_msghdr *ifm;
650 struct ifa_msghdr *ifam;
651{
652 const struct afswtch *p = NULL;
653 struct rt_addrinfo info;
654 int allfamilies, s;
655
656 if (afp == NULL) {
657 allfamilies = 1;
658 afp = &afs[0];
659 } else
660 allfamilies = 0;
661
662 ifr.ifr_addr.sa_family = afp->af_af;
663 strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
664
665 if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0)
666 err(1, "socket");
667
668 /*
669 * XXX is it we are doing a SIOCGIFMETRIC etc for one family.
670 * is it possible that the metric and mtu can be different for
671 * each family? If so, we have a format problem, because the
672 * metric and mtu is printed on the global the flags line.
673 */
674 if (ioctl(s, SIOCGIFMETRIC, (caddr_t)&ifr) < 0)
675 warn("ioctl (SIOCGIFMETRIC)");
676 else
677 metric = ifr.ifr_metric;
678
679 if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0)
680 warn("ioctl (SIOCGIFMTU)");
681 else
682 mtu = ifr.ifr_mtu;
683
684 printf("%s: ", name);
685 printb("flags", flags, IFFBITS);
686 if (metric)
687 printf(" metric %d", metric);
688 if (mtu)
689 printf(" mtu %d", mtu);
690 putchar('\n');
691
692 while (addrcount > 0) {
693
694 info.rti_addrs = ifam->ifam_addrs;
695
696 /* Expand the compacted addresses */
697 rt_xaddrs((char *)(ifam + 1), ifam->ifam_msglen + (char *)ifam,
698 &info);
699
700 if (!allfamilies) {
701 if (afp->af_af == info.rti_info[RTAX_IFA]->sa_family &&
702#if USE_IF_MEDIA
703 afp->af_status != media_status &&
704#endif
705 afp->af_status != ether_status) {
706 p = afp;
707 (*p->af_status)(s, &info);
708 }
709 } else for (p = afs; p->af_name; p++) {
710 if (p->af_af == info.rti_info[RTAX_IFA]->sa_family &&
711#if USE_IF_MEDIA
712 p->af_status != media_status &&
713#endif
714 p->af_status != ether_status)
715 (*p->af_status)(s, &info);
716 }
717 addrcount--;
718 ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
719 }
720 if (allfamilies || afp->af_status == ether_status)
721 ether_status(s, (struct rt_addrinfo *)sdl);
722#if USE_IF_MEDIA
723 if (allfamilies || afp->af_status == media_status)
724 media_status(s, NULL);
725#endif
726 if (!allfamilies && !p &&
727 afp->af_status != ether_status)
728 warnx("%s has no %s interface address!", name, afp->af_name);
729
730 close(s);
731 return;
732}
733
734void
735in_status(s, info)
736 int s ;
737 struct rt_addrinfo * info;
738{
739 struct sockaddr_in *sin, null_sin;
740
741 memset(&null_sin, 0, sizeof(null_sin));
742
743 sin = (struct sockaddr_in *)info->rti_info[RTAX_IFA];
744 printf("\tinet %s ", inet_ntoa(sin->sin_addr));
745
746 if (flags & IFF_POINTOPOINT) {
747 /* note RTAX_BRD overlap with IFF_BROADCAST */
748 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
749 if (!sin)
750 sin = &null_sin;
751 printf("--> %s ", inet_ntoa(sin->sin_addr));
752 }
753
754 sin = (struct sockaddr_in *)info->rti_info[RTAX_NETMASK];
755 if (!sin)
756 sin = &null_sin;
757 printf("netmask 0x%lx ", (unsigned long)ntohl(sin->sin_addr.s_addr));
758
759 if (flags & IFF_BROADCAST) {
760 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
761 sin = (struct sockaddr_in *)info->rti_info[RTAX_BRD];
762 if (sin && sin->sin_addr.s_addr != 0)
763 printf("broadcast %s", inet_ntoa(sin->sin_addr));
764 }
765 putchar('\n');
766}
767
768void
769ether_status(s, info)
770 int s ;
771 struct rt_addrinfo *info;
772{
773 char *cp;
774 int n;
775 struct sockaddr_dl *sdl = (struct sockaddr_dl *)info;
776
777 cp = (char *)LLADDR(sdl);
778 if ((n = sdl->sdl_alen) > 0) {
779 if (sdl->sdl_type == IFT_ETHER)
780 printf ("\tether ");
781 else
782 printf ("\tlladdr ");
783 while (--n >= 0)
784 printf("%02x%c",*cp++ & 0xff, n>0? ':' : ' ');
785 putchar('\n');
786 }
787}
788
789void
790Perror(cmd)
791 const char *cmd;
792{
793 switch (errno) {
794
795 case ENXIO:
796 errx(1, "%s: no such interface", cmd);
797 break;
798
799 case EPERM:
800 errx(1, "%s: permission denied", cmd);
801 break;
802
803 default:
804 err(1, "%s", cmd);
805 }
806}
807
808#define SIN(x) ((struct sockaddr_in *) &(x))
809struct sockaddr_in *sintab[] = {
810SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
811SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
812
813void
814in_getaddr(s, which)
815 const char *s;
816 int which;
817{
818 register struct sockaddr_in *sin = sintab[which];
819 struct hostent *hp;
820 struct netent *np;
821
822 sin->sin_len = sizeof(*sin);
823 if (which != MASK)
824 sin->sin_family = AF_INET;
825
826 if (inet_aton(s, &sin->sin_addr))
827 return;
828 if ((hp = gethostbyname(s)) != 0)
829 bcopy(hp->h_addr, (char *)&sin->sin_addr,
830 MIN(hp->h_length, sizeof(sin->sin_addr)));
831 else if ((np = getnetbyname(s)) != 0)
832 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
833 else
834 errx(1, "%s: bad value", s);
835}
836
837/*
838 * Print a value a la the %b format of the kernel's printf
839 */
840void
841printb(s, v, bits)
842 const char *s;
843 register unsigned v;
844 register const char *bits;
845{
846 register int i, any = 0;
847 register char c;
848
849 if (bits && *bits == 8)
850 printf("%s=%o", s, v);
851 else
852 printf("%s=%x", s, v);
853 bits++;
854 if (bits) {
855 putchar('<');
856 while ((i = *bits++) != '\0') {
857 if (v & (1 << (i-1))) {
858 if (any)
859 putchar(',');
860 any = 1;
861 for (; (c = *bits) > 32; bits++)
862 putchar(c);
863 } else
864 for (; *bits > 32; bits++)
865 ;
866 }
867 putchar('>');
868 }
869}
870
871