1 /* $NetBSD: arp.c,v 1.53 2015/07/31 04:02:40 ozaki-r Exp $ */
2
3 /*
4 * Copyright (c) 1984, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Sun Microsystems, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
38 The Regents of the University of California. All rights reserved.");
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)arp.c 8.3 (Berkeley) 4/28/95";
44 #else
45 __RCSID("$NetBSD: arp.c,v 1.53 2015/07/31 04:02:40 ozaki-r Exp $");
46 #endif
47 #endif /* not lint */
48
49 /*
50 * arp - display, set, and delete arp table entries
51 */
52
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/ioctl.h>
58
59 #include <net/if.h>
60 #include <net/if_dl.h>
61 #include <net/if_ether.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/if_inarp.h>
66 #include <arpa/inet.h>
67
68 #include <err.h>
69 #include <errno.h>
70 #include <netdb.h>
71 #include <nlist.h>
72 #include <paths.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <string.h>
76 #include <unistd.h>
77 #include <ifaddrs.h>
78
79 #include "prog_ops.h"
80
81 static int is_llinfo(const struct sockaddr_dl *, int);
82 static int delete(const char *, const char *);
83 static void dump(uint32_t);
84 static void delete_all(void);
85 static void sdl_print(const struct sockaddr_dl *);
86 static int getifname(u_int16_t, char *, size_t);
87 static int atosdl(const char *s, struct sockaddr_dl *sdl);
88 static int file(const char *);
89 static void get(const char *);
90 static int getinetaddr(const char *, struct in_addr *);
91 static int getsocket(void);
92 static struct rt_msghdr *
93 rtmsg(const int, const int, const struct sockaddr_inarp *,
94 const struct sockaddr_dl *);
95 static int set(int, char **);
96 static void usage(void) __dead;
97
98 static int aflag, nflag, vflag;
99 static struct sockaddr_in so_mask = {
100 .sin_len = 8,
101 .sin_addr = {
102 .s_addr = 0xffffffff
103 }
104 };
105 static struct sockaddr_inarp blank_sin = {
106 .sin_len = sizeof(blank_sin),
107 .sin_family = AF_INET
108 };
109 static struct sockaddr_dl blank_sdl = {
110 .sdl_len = sizeof(blank_sdl),
111 .sdl_family = AF_LINK
112 };
113
114 static int expire_time, flags, export_only, doing_proxy, found_entry;
115
116 int
main(int argc,char ** argv)117 main(int argc, char **argv)
118 {
119 int ch;
120 int op = 0;
121
122 setprogname(argv[0]);
123
124 while ((ch = getopt(argc, argv, "andsfv")) != -1)
125 switch((char)ch) {
126 case 'a':
127 aflag = 1;
128 break;
129 case 'd':
130 case 's':
131 case 'f':
132 if (op)
133 usage();
134 op = ch;
135 break;
136 case 'n':
137 nflag = 1;
138 break;
139 case 'v':
140 vflag = 1;
141 break;
142 default:
143 usage();
144 }
145 argc -= optind;
146 argv += optind;
147
148 if (!op && aflag)
149 op = 'a';
150
151 if (prog_init && prog_init() == -1)
152 err(1, "init failed");
153
154 switch((char)op) {
155 case 'a':
156 dump(0);
157 break;
158 case 'd':
159 if (aflag && argc == 0)
160 delete_all();
161 else {
162 if (aflag || argc < 1 || argc > 2)
163 usage();
164 (void)delete(argv[0], argv[1]);
165 }
166 break;
167 case 's':
168 if (argc < 2 || argc > 7)
169 usage();
170 return (set(argc, argv) ? 1 : 0);
171 case 'f':
172 if (argc != 1)
173 usage();
174 return (file(argv[0]));
175 default:
176 if (argc != 1)
177 usage();
178 get(argv[0]);
179 break;
180 }
181 return (0);
182 }
183
184 /*
185 * Process a file to set standard arp entries
186 */
187 static int
file(const char * name)188 file(const char *name)
189 {
190 char *line, *argv[5];
191 int i, retval;
192 FILE *fp;
193
194 if (!strcmp(name, "-")) {
195 fp = stdin;
196 } else {
197 fp = fopen(name, "r");
198 if (fp == NULL) {
199 err(1, "Cannot open %s", name);
200 }
201 }
202 retval = 0;
203 for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
204 char **ap, *inputstring;
205
206 inputstring = line;
207 for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
208 (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
209 if (**ap != '\0')
210 ap++;
211 }
212 i = ap - argv;
213 if (i < 2) {
214 warnx("bad line: %s", line);
215 retval = 1;
216 continue;
217 }
218 if (set(i, argv))
219 retval = 1;
220 }
221 if (fp != stdin)
222 (void)fclose(fp);
223 return retval;
224 }
225
226 static int
getsocket(void)227 getsocket(void)
228 {
229 int s;
230 s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
231 if (s < 0)
232 err(1, "socket");
233 return s;
234 }
235
236 static int
getlink(const char * name,struct sockaddr_dl * sdl)237 getlink(const char *name, struct sockaddr_dl *sdl)
238 {
239 struct ifaddrs *ifap, *ifa;
240
241 if (getifaddrs(&ifap) != 0) {
242 warn("getifaddrs");
243 return 0;
244 }
245
246 for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
247 if (ifa->ifa_addr->sa_family != AF_LINK)
248 continue;
249 if (strcmp(ifa->ifa_name, name) != 0)
250 continue;
251 memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
252 freeifaddrs(ifap);
253 return 1;
254 }
255 freeifaddrs(ifap);
256 return 0;
257 }
258
259 /*
260 * Set an individual arp entry
261 */
262 static int
set(int argc,char ** argv)263 set(int argc, char **argv)
264 {
265 struct sockaddr_inarp *sina;
266 struct sockaddr_dl *sdl;
267 struct rt_msghdr *rtm;
268 char *host = argv[0], *eaddr;
269 struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
270 struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */
271 int s;
272
273 eaddr = argv[1];
274
275 s = getsocket();
276 argc -= 2;
277 argv += 2;
278
279 if (getinetaddr(host, &sin_m.sin_addr) == -1)
280 return (1);
281 if (atosdl(eaddr, &sdl_m))
282 warnx("invalid link-level address '%s'", eaddr);
283 doing_proxy = flags = export_only = expire_time = 0;
284 for (; argc-- > 0; argv++) {
285 if (strncmp(argv[0], "temp", 4) == 0) {
286 struct timeval timev;
287 (void)gettimeofday(&timev, 0);
288 expire_time = timev.tv_sec + 20 * 60;
289 }
290 else if (strncmp(argv[0], "pub", 3) == 0) {
291 flags |= RTF_ANNOUNCE;
292 doing_proxy = SIN_PROXY;
293 if (argc && strncmp(argv[1], "pro", 3) == 0) {
294 export_only = 1;
295 argc--; argv++;
296 }
297 } else if (strncmp(argv[0], "trail", 5) == 0) {
298 warnx("%s: Sending trailers is no longer supported",
299 host);
300 } else if (strcmp(argv[0], "ifscope") == 0) {
301 if (argc == 0) {
302 warnx("missing interface for ifscope");
303 continue;
304 }
305 argc--;
306 argv++;
307 if (!getlink(argv[0], &sdl_m))
308 warnx("cannot get link address for %s", argv[0]);
309 }
310
311 }
312 if (memcmp(&sdl_m, &blank_sdl, sizeof(blank_sdl)))
313 goto out;
314 tryagain:
315 rtm = rtmsg(s, RTM_GET, &sin_m, &sdl_m);
316 if (rtm == NULL) {
317 warn("%s", host);
318 return (1);
319 }
320 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
321 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
322 (char *)(void *)sina);
323 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
324 if (is_llinfo(sdl, rtm->rtm_flags))
325 goto overwrite;
326 if (doing_proxy == 0) {
327 warnx("set: can only proxy for %s", host);
328 return (1);
329 }
330 if (sin_m.sin_other & SIN_PROXY) {
331 warnx("set: proxy entry exists for non 802 device");
332 return (1);
333 }
334 sin_m.sin_other = SIN_PROXY;
335 export_only = 1;
336 goto tryagain;
337 }
338 overwrite:
339 if (sdl->sdl_family != AF_LINK) {
340 warnx("cannot intuit interface index and type for %s",
341 host);
342 return (1);
343 }
344 sdl_m.sdl_type = sdl->sdl_type;
345 sdl_m.sdl_index = sdl->sdl_index;
346 out:
347 sin_m.sin_other = 0;
348 if (doing_proxy && export_only)
349 sin_m.sin_other = SIN_PROXY;
350 rtm = rtmsg(s, RTM_ADD, &sin_m, &sdl_m);
351 if (vflag)
352 (void)printf("%s (%s) added\n", host, eaddr);
353 return (rtm == NULL) ? 1 : 0;
354 }
355
356 /*
357 * Display an individual arp entry
358 */
359 static void
get(const char * host)360 get(const char *host)
361 {
362 struct sockaddr_inarp sin = blank_sin; /* struct copy */
363
364 if (getinetaddr(host, &sin.sin_addr) == -1)
365 exit(1);
366 dump(sin.sin_addr.s_addr);
367 if (found_entry == 0)
368 errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr));
369 }
370
371
372 static int
is_llinfo(const struct sockaddr_dl * sdl,int rtflags)373 is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
374 {
375 if (sdl->sdl_family != AF_LINK ||
376 (rtflags & (RTF_LLINFO|RTF_GATEWAY)) != RTF_LLINFO)
377 return 0;
378
379 switch (sdl->sdl_type) {
380 case IFT_ETHER:
381 case IFT_FDDI:
382 case IFT_ISO88023:
383 case IFT_ISO88024:
384 case IFT_ISO88025:
385 case IFT_ARCNET:
386 return 1;
387 default:
388 return 0;
389 }
390 }
391
392 /*
393 * Delete an arp entry
394 */
395 int
delete(const char * host,const char * info)396 delete(const char *host, const char *info)
397 {
398 struct sockaddr_inarp *sina;
399 struct sockaddr_dl *sdl;
400 struct rt_msghdr *rtm;
401 struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
402 struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */
403 int s;
404
405 s = getsocket();
406 if (info && strncmp(info, "pro", 3) == 0)
407 sin_m.sin_other = SIN_PROXY;
408 if (getinetaddr(host, &sin_m.sin_addr) == -1)
409 return (1);
410 tryagain:
411 rtm = rtmsg(s, RTM_GET, &sin_m, &sdl_m);
412 if (rtm == NULL) {
413 warn("%s", host);
414 return (1);
415 }
416 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
417 sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
418 (char *)(void *)sina);
419 if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr &&
420 is_llinfo(sdl, rtm->rtm_flags))
421 goto delete;
422 if (sin_m.sin_other & SIN_PROXY) {
423 warnx("delete: can't locate %s", host);
424 return (1);
425 } else {
426 sin_m.sin_other = SIN_PROXY;
427 goto tryagain;
428 }
429 delete:
430 if (sdl->sdl_family != AF_LINK) {
431 (void)warnx("cannot locate %s", host);
432 return (1);
433 }
434 rtm = rtmsg(s, RTM_DELETE, &sin_m, sdl);
435 if (rtm == NULL)
436 return (1);
437 if (vflag)
438 (void)printf("%s (%s) deleted\n", host,
439 inet_ntoa(sin_m.sin_addr));
440 return (0);
441 }
442
443 /*
444 * Dump the entire arp table
445 */
446 void
dump(uint32_t addr)447 dump(uint32_t addr)
448 {
449 int mib[6];
450 size_t needed;
451 char ifname[IFNAMSIZ];
452 char *lim, *buf, *next;
453 const char *host;
454 struct rt_msghdr *rtm;
455 struct sockaddr_inarp *sina;
456 struct sockaddr_dl *sdl;
457 struct hostent *hp;
458
459 mib[0] = CTL_NET;
460 mib[1] = PF_ROUTE;
461 mib[2] = 0;
462 mib[3] = AF_INET;
463 mib[4] = NET_RT_FLAGS;
464 mib[5] = RTF_LLINFO;
465 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
466 err(1, "route-sysctl-estimate");
467 if (needed == 0)
468 return;
469 if ((buf = malloc(needed)) == NULL)
470 err(1, "malloc");
471 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
472 err(1, "actual retrieval of routing table");
473 lim = buf + needed;
474 for (next = buf; next < lim; next += rtm->rtm_msglen) {
475 rtm = (struct rt_msghdr *)(void *)next;
476 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
477 sdl = (struct sockaddr_dl *)(void *)
478 (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
479 if (addr) {
480 if (addr != sina->sin_addr.s_addr)
481 continue;
482 found_entry = 1;
483 }
484 if (nflag == 0)
485 hp = gethostbyaddr((const char *)(void *)
486 &(sina->sin_addr),
487 sizeof sina->sin_addr, AF_INET);
488 else
489 hp = NULL;
490
491 host = hp ? hp->h_name : "?";
492
493 (void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
494 if (sdl->sdl_alen)
495 sdl_print(sdl);
496 else
497 (void)printf("(incomplete)");
498
499 if (sdl->sdl_index) {
500 if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
501 (void)printf(" on %s", ifname);
502 }
503
504 if (rtm->rtm_rmx.rmx_expire == 0)
505 (void)printf(" permanent");
506 if (sina->sin_other & SIN_PROXY)
507 (void)printf(" published (proxy only)");
508 if (rtm->rtm_addrs & RTA_NETMASK) {
509 sina = (struct sockaddr_inarp *)(void *)
510 (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
511 if (sina->sin_addr.s_addr == 0xffffffff)
512 (void)printf(" published");
513 if (sina->sin_len != 8)
514 (void)printf("(weird)");
515 }
516 (void)printf("\n");
517 }
518 free(buf);
519 }
520
521 /*
522 * Delete the entire arp table
523 */
524 void
delete_all(void)525 delete_all(void)
526 {
527 int mib[6];
528 size_t needed;
529 char addr[sizeof("000.000.000.000\0")];
530 char *lim, *buf, *next;
531 struct rt_msghdr *rtm;
532 struct sockaddr_inarp *sina;
533
534 mib[0] = CTL_NET;
535 mib[1] = PF_ROUTE;
536 mib[2] = 0;
537 mib[3] = AF_INET;
538 mib[4] = NET_RT_FLAGS;
539 mib[5] = RTF_LLINFO;
540 if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
541 err(1, "route-sysctl-estimate");
542 if (needed == 0)
543 return;
544 if ((buf = malloc(needed)) == NULL)
545 err(1, "malloc");
546 if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
547 err(1, "actual retrieval of routing table");
548 lim = buf + needed;
549 for (next = buf; next < lim; next += rtm->rtm_msglen) {
550 rtm = (struct rt_msghdr *)(void *)next;
551 sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
552 (void)snprintf(addr, sizeof(addr), "%s",
553 inet_ntoa(sina->sin_addr));
554 (void)delete(addr, NULL);
555 }
556 free(buf);
557 }
558
559 void
sdl_print(const struct sockaddr_dl * sdl)560 sdl_print(const struct sockaddr_dl *sdl)
561 {
562 char hbuf[NI_MAXHOST];
563
564 if (getnameinfo((const struct sockaddr *)(const void *)sdl,
565 (socklen_t)sdl->sdl_len,
566 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
567 (void)printf("<invalid>");
568 else
569 (void)printf("%s", hbuf);
570 }
571
572 static int
atosdl(const char * ss,struct sockaddr_dl * sdl)573 atosdl(const char *ss, struct sockaddr_dl *sdl)
574 {
575 int i;
576 unsigned long b;
577 char *endp;
578 char *p;
579 char *t, *r;
580
581 p = LLADDR(sdl);
582 endp = ((char *)(void *)sdl) + sdl->sdl_len;
583 i = 0;
584
585 b = strtoul(ss, &t, 16);
586 if (b > 255 || t == ss)
587 return 1;
588
589 *p++ = (char)b;
590 ++i;
591 while ((p < endp) && (*t++ == ':')) {
592 b = strtoul(t, &r, 16);
593 if (b > 255 || r == t)
594 break;
595 *p++ = (char)b;
596 ++i;
597 t = r;
598 }
599 sdl->sdl_alen = i;
600
601 return 0;
602 }
603
604 static void
usage(void)605 usage(void)
606 {
607 const char *progname;
608
609 progname = getprogname();
610 (void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
611 (void)fprintf(stderr, " %s [-nv] -a\n", progname);
612 (void)fprintf(stderr, " %s [-v] -d [-a|hostname [pub [proxy]]]\n",
613 progname);
614 (void)fprintf(stderr, " %s -s hostname ether_addr [temp] [pub [proxy]]\n",
615 progname);
616 (void)fprintf(stderr, " %s -f filename\n", progname);
617 exit(1);
618 }
619
620 static struct rt_msghdr *
rtmsg(const int s,const int cmd,const struct sockaddr_inarp * sin,const struct sockaddr_dl * sdl)621 rtmsg(const int s, const int cmd, const struct sockaddr_inarp *sin,
622 const struct sockaddr_dl *sdl)
623 {
624 static int seq;
625 struct rt_msghdr *rtm;
626 char *cp;
627 int l;
628 static struct {
629 struct rt_msghdr m_rtm;
630 char m_space[512];
631 } m_rtmsg;
632 pid_t pid;
633
634 rtm = &m_rtmsg.m_rtm;
635 cp = m_rtmsg.m_space;
636 errno = 0;
637
638 if (cmd == RTM_DELETE)
639 goto doit;
640 (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
641 rtm->rtm_flags = flags;
642 rtm->rtm_version = RTM_VERSION;
643
644 switch (cmd) {
645 default:
646 errx(1, "internal wrong cmd");
647 /*NOTREACHED*/
648 case RTM_ADD:
649 rtm->rtm_addrs |= RTA_GATEWAY;
650 rtm->rtm_rmx.rmx_expire = expire_time;
651 rtm->rtm_inits = RTV_EXPIRE;
652 rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
653 if (doing_proxy) {
654 if (!export_only) {
655 rtm->rtm_addrs |= RTA_NETMASK;
656 rtm->rtm_flags &= ~RTF_HOST;
657 }
658 }
659 /* FALLTHROUGH */
660 case RTM_GET:
661 rtm->rtm_addrs |= RTA_DST;
662 }
663
664 #define NEXTADDR(w, s) \
665 if (rtm->rtm_addrs & (w)) { \
666 (void)memcpy(cp, &s, \
667 (size_t)((const struct sockaddr *)&s)->sa_len); \
668 RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \
669 }
670
671 NEXTADDR(RTA_DST, *sin);
672 NEXTADDR(RTA_GATEWAY, *sdl);
673 NEXTADDR(RTA_NETMASK, so_mask);
674
675 rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
676 doit:
677 l = rtm->rtm_msglen;
678 rtm->rtm_seq = ++seq;
679 rtm->rtm_type = cmd;
680 #ifdef __minix
681 /*
682 * Borrow from the future by setting the "this is a link-local request"
683 * flag on all routing socket requests. IMPORTANT: this change may be
684 * dropped with the resync to NetBSD 8 as it will do the same thing,
685 * although slightly differently (and hence may not create a conflict).
686 */
687 rtm->rtm_flags |= RTF_LLDATA;
688 #endif /* __minix */
689 if (prog_write(s, &m_rtmsg, (size_t)l) < 0) {
690 if (errno != ESRCH || cmd != RTM_DELETE) {
691 warn("writing to routing socket");
692 return NULL;
693 }
694 }
695
696 pid = prog_getpid();
697 do {
698 l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg));
699 } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
700 if (l < 0)
701 warn("read from routing socket");
702 return rtm;
703 }
704
705 static int
getinetaddr(const char * host,struct in_addr * inap)706 getinetaddr(const char *host, struct in_addr *inap)
707 {
708 struct hostent *hp;
709
710 if (inet_aton(host, inap) == 1)
711 return (0);
712 if ((hp = gethostbyname(host)) == NULL) {
713 warnx("%s: %s", host, hstrerror(h_errno));
714 return (-1);
715 }
716 (void)memcpy(inap, hp->h_addr, sizeof(*inap));
717 return (0);
718 }
719
720 static int
getifname(u_int16_t ifindex,char * ifname,size_t l)721 getifname(u_int16_t ifindex, char *ifname, size_t l)
722 {
723 int i;
724 struct ifaddrs *addr;
725 const struct sockaddr_dl *sdl = NULL;
726 static struct ifaddrs* ifaddrs = NULL;
727
728 if (ifaddrs == NULL) {
729 i = getifaddrs(&ifaddrs);
730 if (i != 0)
731 err(1, "getifaddrs");
732 }
733
734 for (addr = ifaddrs; addr; addr = addr->ifa_next) {
735 if (addr->ifa_addr == NULL ||
736 addr->ifa_addr->sa_family != AF_LINK)
737 continue;
738
739 sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
740 if (sdl && sdl->sdl_index == ifindex) {
741 (void) strlcpy(ifname, addr->ifa_name, l);
742 return 0;
743 }
744 }
745
746 return -1;
747 }
748