xref: /netbsd-src/usr.sbin/arp/arp.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: arp.c,v 1.58 2017/06/28 08:17:50 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.58 2017/06/28 08:17:50 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_one(struct rt_msghdr *);
83 static void dump(uint32_t);
84 static void delete(const char *, const char *);
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,  struct rt_msghdr *,
94 	    const struct sockaddr_inarp *, 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
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(NULL, NULL);
161 		else {
162 			if (aflag || argc < 1 || argc > 2)
163 				usage();
164 			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
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
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
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
263 set(int argc, char **argv)
264 {
265 	struct sockaddr_inarp *sina;
266 	struct sockaddr_dl *sdl = NULL;
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 = 1;
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 tryagain:
313 	rtm = rtmsg(s, RTM_GET, NULL, &sin_m, &sdl_m);
314 	if (rtm == NULL) {
315 		warn("%s", host);
316 		return (1);
317 	}
318 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
319 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
320 	    (char *)(void *)sina);
321 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
322 		if (is_llinfo(sdl, rtm->rtm_flags))
323 			goto overwrite;
324 		if (doing_proxy == 0) {
325 			warnx("set: can only proxy for %s", host);
326 			return (1);
327 		}
328 		if (sin_m.sin_other & SIN_PROXY) {
329 			warnx("set: proxy entry exists for non 802 device");
330 			return (1);
331 		}
332 		sin_m.sin_other = SIN_PROXY;
333 		export_only = 1;
334 		goto tryagain;
335 	}
336 overwrite:
337 	if (sdl->sdl_family != AF_LINK) {
338 		warnx("cannot intuit interface index and type for %s",
339 		    host);
340 		return (1);
341 	}
342 	sdl_m.sdl_type = sdl->sdl_type;
343 	sdl_m.sdl_index = sdl->sdl_index;
344 	sin_m.sin_other = 0;
345 	if (doing_proxy && export_only)
346 		sin_m.sin_other = SIN_PROXY;
347 	rtm = rtmsg(s, RTM_ADD, NULL, &sin_m, &sdl_m);
348 	if (vflag)
349 		(void)printf("%s (%s) added\n", host, eaddr);
350 	return (rtm == NULL) ? 1 : 0;
351 }
352 
353 /*
354  * Display an individual arp entry
355  */
356 static void
357 get(const char *host)
358 {
359 	struct sockaddr_inarp sin = blank_sin; /* struct copy */
360 
361 	if (getinetaddr(host, &sin.sin_addr) == -1)
362 		exit(1);
363 	dump(sin.sin_addr.s_addr);
364 	if (found_entry == 0)
365 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr));
366 }
367 
368 
369 static int
370 is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
371 {
372 	if (sdl->sdl_family != AF_LINK ||
373 	    (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
374 		return 0;
375 
376 	switch (sdl->sdl_type) {
377 	case IFT_ETHER:
378 	case IFT_FDDI:
379 	case IFT_ISO88023:
380 	case IFT_ISO88024:
381 	case IFT_ISO88025:
382 	case IFT_ARCNET:
383 		return 1;
384 	default:
385 		return 0;
386 	}
387 }
388 
389 /*
390  * Delete an arp entry
391  */
392 int
393 delete_one(struct rt_msghdr *rtm)
394 {
395 	struct sockaddr_inarp *sina;
396 	struct sockaddr_dl *sdl;
397 	int s;
398 
399 	s = getsocket();
400 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
401 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
402 	    (char *)(void *)sina);
403 	if (sdl->sdl_family != AF_LINK)
404 		return (1);
405 	rtm = rtmsg(s, RTM_DELETE, rtm, sina, sdl);
406 	if (rtm == NULL)
407 		return (1);
408 	return (0);
409 }
410 
411 /*
412  * Dump the entire arp table
413  */
414 void
415 dump(uint32_t addr)
416 {
417 	int mib[6];
418 	size_t needed;
419 	char ifname[IFNAMSIZ];
420 	char *lim, *buf, *next;
421         const char *host;
422 	struct rt_msghdr *rtm;
423 	struct sockaddr_inarp *sina;
424 	struct sockaddr_dl *sdl;
425 	struct hostent *hp;
426 
427 	mib[0] = CTL_NET;
428 	mib[1] = PF_ROUTE;
429 	mib[2] = 0;
430 	mib[3] = AF_INET;
431 	mib[4] = NET_RT_FLAGS;
432 	mib[5] = 0;
433 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
434 		err(1, "route-sysctl-estimate");
435 	if (needed == 0)
436 		return;
437 	if ((buf = malloc(needed)) == NULL)
438 		err(1, "malloc");
439 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
440 		err(1, "actual retrieval of routing table");
441 	lim = buf + needed;
442 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
443 		rtm = (struct rt_msghdr *)(void *)next;
444 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
445 		sdl = (struct sockaddr_dl *)(void *)
446 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
447 		if (addr) {
448 			if (addr != sina->sin_addr.s_addr)
449 				continue;
450 			found_entry = 1;
451 		}
452 		if (nflag == 0)
453 			hp = gethostbyaddr((const char *)(void *)
454 			    &(sina->sin_addr),
455 			    sizeof sina->sin_addr, AF_INET);
456 		else
457 			hp = NULL;
458 
459 		host = hp ? hp->h_name : "?";
460 
461 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
462 		if (sdl->sdl_alen)
463 			sdl_print(sdl);
464 		else
465 			(void)printf("(incomplete)");
466 
467 		if (sdl->sdl_index) {
468 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
469 				(void)printf(" on %s", ifname);
470 		}
471 
472 		if (rtm->rtm_rmx.rmx_expire == 0)
473 			(void)printf(" permanent");
474 		if (sina->sin_other & SIN_PROXY)
475 			(void)printf(" published (proxy only)");
476 		if (rtm->rtm_addrs & RTA_NETMASK) {
477 			sina = (struct sockaddr_inarp *)(void *)
478 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
479 			if (sina->sin_addr.s_addr == 0xffffffff)
480 				(void)printf(" published");
481 			if (sina->sin_len != 8)
482 				(void)printf("(weird)");
483 		}
484 		(void)printf("\n");
485 	}
486 	free(buf);
487 }
488 
489 /*
490  * Delete the entire arp table
491  */
492 void
493 delete(const char *host, const char *info)
494 {
495 	int mib[6];
496 	size_t needed;
497 	char *lim, *buf, *next;
498 	struct rt_msghdr *rtm;
499 	struct sockaddr_inarp *sina;
500 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
501 
502 	if (host != NULL) {
503 		int ret = getinetaddr(host, &sin_m.sin_addr);
504 		if (ret == -1)
505 			return;
506 	}
507 	if (info && strncmp(info, "pro", 3) == 0)
508 		sin_m.sin_other = SIN_PROXY;
509 
510 retry:
511 	mib[0] = CTL_NET;
512 	mib[1] = PF_ROUTE;
513 	mib[2] = 0;
514 	mib[3] = AF_INET;
515 	mib[4] = NET_RT_FLAGS;
516 	mib[5] = RTF_LLDATA;
517 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
518 		err(1, "route-sysctl-estimate");
519 	if (needed == 0)
520 		return;
521 	if ((buf = malloc(needed)) == NULL)
522 		err(1, "malloc");
523 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
524 		free(buf);
525 		if (errno == ENOBUFS)
526 			goto retry;
527 		err(1, "actual retrieval of routing table");
528 	}
529 	lim = buf + needed;
530 
531 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
532 		int ret;
533 		rtm = (struct rt_msghdr *)(void *)next;
534 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
535 		if (host != NULL &&
536 		    sina->sin_addr.s_addr != sin_m.sin_addr.s_addr)
537 			continue;
538 		ret = delete_one(rtm);
539 		if (vflag && ret == 0) {
540 			(void)printf("%s (%s) deleted\n", host,
541 			    inet_ntoa(sina->sin_addr));
542 		}
543 	}
544 	free(buf);
545 }
546 
547 void
548 sdl_print(const struct sockaddr_dl *sdl)
549 {
550 	char hbuf[NI_MAXHOST];
551 
552 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
553 	    (socklen_t)sdl->sdl_len,
554 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
555 		(void)printf("<invalid>");
556 	else
557 		(void)printf("%s", hbuf);
558 }
559 
560 static int
561 atosdl(const char *ss, struct sockaddr_dl *sdl)
562 {
563 	int i;
564 	unsigned long b;
565 	char *endp;
566 	char *p;
567 	char *t, *r;
568 
569 	p = LLADDR(sdl);
570 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
571 	i = 0;
572 
573 	b = strtoul(ss, &t, 16);
574 	if (b > 255 || t == ss)
575 		return 1;
576 
577 	*p++ = (char)b;
578 	++i;
579 	while ((p < endp) && (*t++ == ':')) {
580 		b = strtoul(t, &r, 16);
581 		if (b > 255 || r == t)
582 			break;
583 		*p++ = (char)b;
584 		++i;
585 		t = r;
586 	}
587 	sdl->sdl_alen = i;
588 
589 	return 0;
590 }
591 
592 static void
593 usage(void)
594 {
595 	const char *progname;
596 
597 	progname = getprogname();
598 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
599 	(void)fprintf(stderr, "       %s [-nv] -a\n", progname);
600 	(void)fprintf(stderr, "       %s [-v] -d [-a|hostname [pub [proxy]]]\n",
601 	    progname);
602 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
603 	    progname);
604 	(void)fprintf(stderr, "       %s -f filename\n", progname);
605 	exit(1);
606 }
607 
608 static struct rt_msghdr *
609 rtmsg(const int s, const int cmd, struct rt_msghdr *_rtm,
610     const struct sockaddr_inarp *sin, const struct sockaddr_dl *sdl)
611 {
612 	static int seq;
613 	struct rt_msghdr *rtm = _rtm;
614 	char *cp;
615 	int l;
616 	static struct {
617 		struct	rt_msghdr m_rtm;
618 		char	m_space[512];
619 	} m_rtmsg;
620 	pid_t pid;
621 
622 	errno = 0;
623 	if (rtm != NULL) {
624 		memcpy(&m_rtmsg, rtm, rtm->rtm_msglen);
625 		rtm = &m_rtmsg.m_rtm;
626 		goto doit;
627 	}
628 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
629 	rtm = &m_rtmsg.m_rtm;
630 	cp = m_rtmsg.m_space;
631 
632 	rtm->rtm_flags = flags;
633 	rtm->rtm_version = RTM_VERSION;
634 
635 	switch (cmd) {
636 	default:
637 		errx(1, "internal wrong cmd");
638 		/*NOTREACHED*/
639 	case RTM_ADD:
640 		rtm->rtm_addrs |= RTA_GATEWAY;
641 		rtm->rtm_rmx.rmx_expire = expire_time;
642 		rtm->rtm_inits = RTV_EXPIRE;
643 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
644 		if (doing_proxy) {
645 			if (!export_only) {
646 				rtm->rtm_addrs |= RTA_NETMASK;
647 				rtm->rtm_flags &= ~RTF_HOST;
648 			}
649 		}
650 		rtm->rtm_addrs |= RTA_DST;
651 		break;
652 	case RTM_GET:
653 		rtm->rtm_flags |= RTF_LLDATA;
654 		rtm->rtm_addrs |= RTA_DST | RTA_GATEWAY;
655 	}
656 
657 #define NEXTADDR(w, s) \
658 	if (rtm->rtm_addrs & (w)) { \
659 		(void)memcpy(cp, &s, \
660 		    (size_t)((const struct sockaddr *)&s)->sa_len); \
661 		RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \
662 	}
663 
664 	NEXTADDR(RTA_DST, *sin);
665 	NEXTADDR(RTA_GATEWAY, *sdl);
666 	NEXTADDR(RTA_NETMASK, so_mask);
667 
668 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
669 doit:
670 	l = rtm->rtm_msglen;
671 	rtm->rtm_seq = ++seq;
672 	rtm->rtm_type = cmd;
673 	if (prog_write(s, &m_rtmsg, (size_t)l) < 0) {
674 		if (errno != ESRCH || cmd != RTM_DELETE) {
675 			warn("writing to routing socket");
676 			return NULL;
677 		}
678 	}
679 
680 	pid = prog_getpid();
681 	do {
682 		l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg));
683 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
684 	if (l < 0)
685 		warn("read from routing socket");
686 	return rtm;
687 }
688 
689 static int
690 getinetaddr(const char *host, struct in_addr *inap)
691 {
692 	struct hostent *hp;
693 
694 	if (inet_aton(host, inap) == 1)
695 		return (0);
696 	if ((hp = gethostbyname(host)) == NULL) {
697 		warnx("%s: %s", host, hstrerror(h_errno));
698 		return (-1);
699 	}
700 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
701 	return (0);
702 }
703 
704 static int
705 getifname(u_int16_t ifindex, char *ifname, size_t l)
706 {
707 	int i;
708 	struct ifaddrs *addr;
709 	const struct sockaddr_dl *sdl = NULL;
710 	static struct ifaddrs* ifaddrs = NULL;
711 
712 	if (ifaddrs == NULL) {
713 		i = getifaddrs(&ifaddrs);
714 		if (i != 0)
715 			err(1, "getifaddrs");
716 	}
717 
718 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
719 		if (addr->ifa_addr == NULL ||
720 		    addr->ifa_addr->sa_family != AF_LINK)
721 			continue;
722 
723 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
724 		if (sdl && sdl->sdl_index == ifindex) {
725 			(void) strlcpy(ifname, addr->ifa_name, l);
726 			return 0;
727 		}
728 	}
729 
730 	return -1;
731 }
732