Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.66
      1  1.66       roy /*	$NetBSD: arp.c,v 1.66 2020/09/11 15:28:29 roy Exp $ */
      2  1.10    chopps 
      3   1.1       cgd /*
      4   1.5   mycroft  * Copyright (c) 1984, 1993
      5   1.5   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * This code is derived from software contributed to Berkeley by
      8   1.1       cgd  * Sun Microsystems, Inc.
      9   1.1       cgd  *
     10   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1       cgd  * modification, are permitted provided that the following conditions
     12   1.1       cgd  * are met:
     13   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.38       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       cgd  *    may be used to endorse or promote products derived from this software
     20   1.1       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       cgd  * SUCH DAMAGE.
     33   1.1       cgd  */
     34   1.1       cgd 
     35  1.19     lukem #include <sys/cdefs.h>
     36   1.1       cgd #ifndef lint
     37  1.47     lukem __COPYRIGHT("@(#) Copyright (c) 1984, 1993\
     38  1.47     lukem  The Regents of the University of California.  All rights reserved.");
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #ifndef lint
     42  1.19     lukem #if 0
     43  1.19     lukem static char sccsid[] = "@(#)arp.c	8.3 (Berkeley) 4/28/95";
     44  1.19     lukem #else
     45  1.66       roy __RCSID("$NetBSD: arp.c,v 1.66 2020/09/11 15:28:29 roy Exp $");
     46  1.19     lukem #endif
     47   1.1       cgd #endif /* not lint */
     48   1.1       cgd 
     49   1.1       cgd /*
     50   1.1       cgd  * arp - display, set, and delete arp table entries
     51   1.1       cgd  */
     52   1.1       cgd 
     53   1.1       cgd #include <sys/param.h>
     54   1.1       cgd #include <sys/file.h>
     55   1.1       cgd #include <sys/socket.h>
     56   1.5   mycroft #include <sys/sysctl.h>
     57  1.29      fair #include <sys/ioctl.h>
     58   1.5   mycroft 
     59   1.5   mycroft #include <net/if.h>
     60   1.5   mycroft #include <net/if_dl.h>
     61  1.17        is #include <net/if_ether.h>
     62   1.5   mycroft #include <net/if_types.h>
     63  1.66       roy #include <net/nd.h>
     64   1.5   mycroft #include <net/route.h>
     65   1.1       cgd #include <netinet/in.h>
     66  1.66       roy #include <netinet/in_var.h>
     67  1.17        is #include <netinet/if_inarp.h>
     68   1.2   mycroft #include <arpa/inet.h>
     69   1.1       cgd 
     70  1.15     mikel #include <err.h>
     71  1.15     mikel #include <errno.h>
     72   1.5   mycroft #include <netdb.h>
     73   1.1       cgd #include <nlist.h>
     74  1.15     mikel #include <paths.h>
     75   1.1       cgd #include <stdio.h>
     76   1.8    chopps #include <stdlib.h>
     77  1.12       cgd #include <string.h>
     78  1.15     mikel #include <unistd.h>
     79  1.35     rafal #include <ifaddrs.h>
     80   1.1       cgd 
     81  1.52     ozaki #include "prog_ops.h"
     82  1.52     ozaki 
     83  1.43    dyoung static int is_llinfo(const struct sockaddr_dl *, int);
     84  1.58     ozaki static int delete_one(struct rt_msghdr *);
     85  1.44  christos static void dump(uint32_t);
     86  1.58     ozaki static void delete(const char *, const char *);
     87  1.44  christos static void sdl_print(const struct sockaddr_dl *);
     88  1.44  christos static int getifname(u_int16_t, char *, size_t);
     89  1.60  christos static int atosdl(const char *, struct sockaddr_dl *);
     90  1.44  christos static int file(const char *);
     91  1.44  christos static void get(const char *);
     92  1.44  christos static int getinetaddr(const char *, struct in_addr *);
     93  1.53     ozaki static int getsocket(void);
     94  1.60  christos static int getetheraddr(struct in_addr, struct sockaddr_dl *);
     95  1.60  christos static struct rt_msghdr * rtmsg(const int, const int,  struct rt_msghdr *,
     96  1.60  christos 	const struct sockaddr_inarp *, const struct sockaddr_dl *);
     97  1.66       roy static struct in_nbrinfo * getnbrinfo(const char *, struct in_addr *);
     98  1.66       roy static const char * sec2str(time_t);
     99  1.44  christos static int set(int, char **);
    100  1.45     perry static void usage(void) __dead;
    101  1.17        is 
    102  1.30    atatat static int aflag, nflag, vflag;
    103  1.56       ryo static struct sockaddr_in so_mask = {
    104  1.44  christos 	.sin_len = 8,
    105  1.44  christos 	.sin_addr = {
    106  1.44  christos 		.s_addr = 0xffffffff
    107  1.44  christos 	}
    108  1.44  christos };
    109  1.44  christos static struct sockaddr_inarp blank_sin = {
    110  1.44  christos 	.sin_len = sizeof(blank_sin),
    111  1.44  christos 	.sin_family = AF_INET
    112  1.44  christos };
    113  1.44  christos static struct sockaddr_dl blank_sdl = {
    114  1.44  christos 	.sdl_len = sizeof(blank_sdl),
    115  1.44  christos 	.sdl_family = AF_LINK
    116  1.44  christos };
    117  1.44  christos 
    118  1.44  christos static int expire_time, flags, export_only, doing_proxy, found_entry;
    119  1.16     mikel 
    120   1.8    chopps int
    121  1.41   xtraeme main(int argc, char **argv)
    122   1.1       cgd {
    123   1.1       cgd 	int ch;
    124  1.15     mikel 	int op = 0;
    125  1.34        tv 
    126  1.34        tv 	setprogname(argv[0]);
    127   1.1       cgd 
    128  1.23       mrg 	while ((ch = getopt(argc, argv, "andsfv")) != -1)
    129   1.1       cgd 		switch((char)ch) {
    130   1.5   mycroft 		case 'a':
    131  1.30    atatat 			aflag = 1;
    132  1.30    atatat 			break;
    133   1.1       cgd 		case 'd':
    134  1.15     mikel 		case 's':
    135  1.15     mikel 		case 'f':
    136  1.15     mikel 			if (op)
    137   1.1       cgd 				usage();
    138  1.15     mikel 			op = ch;
    139  1.15     mikel 			break;
    140   1.5   mycroft 		case 'n':
    141   1.5   mycroft 			nflag = 1;
    142   1.8    chopps 			break;
    143  1.23       mrg 		case 'v':
    144  1.23       mrg 			vflag = 1;
    145  1.23       mrg 			break;
    146   1.1       cgd 		default:
    147   1.1       cgd 			usage();
    148   1.1       cgd 		}
    149  1.15     mikel 	argc -= optind;
    150  1.15     mikel 	argv += optind;
    151  1.15     mikel 
    152  1.30    atatat 	if (!op && aflag)
    153  1.30    atatat 		op = 'a';
    154  1.30    atatat 
    155  1.52     ozaki 	if (prog_init && prog_init() == -1)
    156  1.52     ozaki 		err(1, "init failed");
    157  1.52     ozaki 
    158  1.15     mikel 	switch((char)op) {
    159  1.15     mikel 	case 'a':
    160  1.15     mikel 		dump(0);
    161  1.15     mikel 		break;
    162  1.15     mikel 	case 'd':
    163  1.30    atatat 		if (aflag && argc == 0)
    164  1.58     ozaki 			delete(NULL, NULL);
    165  1.30    atatat 		else {
    166  1.30    atatat 			if (aflag || argc < 1 || argc > 2)
    167  1.30    atatat 				usage();
    168  1.58     ozaki 			delete(argv[0], argv[1]);
    169  1.30    atatat 		}
    170  1.15     mikel 		break;
    171  1.15     mikel 	case 's':
    172  1.51  christos 		if (argc < 2 || argc > 7)
    173  1.15     mikel 			usage();
    174  1.15     mikel 		return (set(argc, argv) ? 1 : 0);
    175  1.15     mikel 	case 'f':
    176  1.15     mikel 		if (argc != 1)
    177  1.15     mikel 			usage();
    178  1.15     mikel 		return (file(argv[0]));
    179  1.15     mikel 	default:
    180  1.15     mikel 		if (argc != 1)
    181  1.15     mikel 			usage();
    182  1.15     mikel 		get(argv[0]);
    183  1.15     mikel 		break;
    184  1.15     mikel 	}
    185   1.8    chopps 	return (0);
    186   1.1       cgd }
    187   1.1       cgd 
    188   1.1       cgd /*
    189   1.1       cgd  * Process a file to set standard arp entries
    190   1.1       cgd  */
    191  1.44  christos static int
    192  1.44  christos file(const char *name)
    193   1.1       cgd {
    194  1.44  christos 	char *line, *argv[5];
    195   1.8    chopps 	int i, retval;
    196   1.1       cgd 	FILE *fp;
    197   1.1       cgd 
    198  1.49  dholland 	if (!strcmp(name, "-")) {
    199  1.49  dholland 		fp = stdin;
    200  1.49  dholland 	} else {
    201  1.49  dholland 		fp = fopen(name, "r");
    202  1.49  dholland 		if (fp == NULL) {
    203  1.49  dholland 			err(1, "Cannot open %s", name);
    204  1.49  dholland 		}
    205  1.49  dholland 	}
    206   1.1       cgd 	retval = 0;
    207  1.44  christos 	for (; (line = fparseln(fp, NULL, NULL, NULL, 0)) != NULL; free(line)) {
    208  1.44  christos 		char **ap, *inputstring;
    209  1.44  christos 
    210  1.44  christos 		inputstring = line;
    211  1.44  christos 		for (ap = argv; ap < &argv[sizeof(argv) / sizeof(argv[0])] &&
    212  1.44  christos 		    (*ap = stresep(&inputstring, " \t", '\\')) != NULL;) {
    213  1.56       ryo 			if (**ap != '\0')
    214  1.44  christos 				ap++;
    215  1.44  christos 		}
    216  1.44  christos 		i = ap - argv;
    217   1.1       cgd 		if (i < 2) {
    218   1.8    chopps 			warnx("bad line: %s", line);
    219   1.1       cgd 			retval = 1;
    220   1.1       cgd 			continue;
    221   1.1       cgd 		}
    222  1.44  christos 		if (set(i, argv))
    223   1.1       cgd 			retval = 1;
    224   1.1       cgd 	}
    225  1.49  dholland 	if (fp != stdin)
    226  1.49  dholland 		(void)fclose(fp);
    227  1.44  christos 	return retval;
    228   1.1       cgd }
    229   1.1       cgd 
    230  1.53     ozaki static int
    231  1.41   xtraeme getsocket(void)
    232   1.8    chopps {
    233  1.53     ozaki 	int s;
    234  1.52     ozaki 	s = prog_socket(PF_ROUTE, SOCK_RAW, 0);
    235   1.8    chopps 	if (s < 0)
    236   1.8    chopps 		err(1, "socket");
    237  1.53     ozaki 	return s;
    238   1.5   mycroft }
    239   1.5   mycroft 
    240  1.50  christos static int
    241  1.50  christos getlink(const char *name, struct sockaddr_dl *sdl)
    242  1.50  christos {
    243  1.50  christos 	struct ifaddrs *ifap, *ifa;
    244  1.50  christos 
    245  1.50  christos 	if (getifaddrs(&ifap) != 0) {
    246  1.50  christos 		warn("getifaddrs");
    247  1.50  christos 		return 0;
    248  1.50  christos 	}
    249  1.50  christos 
    250  1.50  christos 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    251  1.50  christos 		if (ifa->ifa_addr->sa_family != AF_LINK)
    252  1.50  christos 			continue;
    253  1.50  christos 		if (strcmp(ifa->ifa_name, name) != 0)
    254  1.50  christos 			continue;
    255  1.50  christos 		memcpy(sdl, ifa->ifa_addr, sizeof(*sdl));
    256  1.50  christos 		freeifaddrs(ifap);
    257  1.50  christos 		return 1;
    258  1.50  christos 	}
    259  1.50  christos 	freeifaddrs(ifap);
    260  1.50  christos 	return 0;
    261  1.50  christos }
    262  1.50  christos 
    263   1.1       cgd /*
    264  1.56       ryo  * Set an individual arp entry
    265   1.1       cgd  */
    266  1.44  christos static int
    267  1.41   xtraeme set(int argc, char **argv)
    268   1.1       cgd {
    269  1.41   xtraeme 	struct sockaddr_inarp *sina;
    270  1.55     ozaki 	struct sockaddr_dl *sdl = NULL;
    271  1.19     lukem 	struct rt_msghdr *rtm;
    272   1.8    chopps 	char *host = argv[0], *eaddr;
    273  1.53     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    274  1.53     ozaki 	struct sockaddr_dl sdl_m = blank_sdl; /* struct copy */
    275  1.53     ozaki 	int s;
    276   1.8    chopps 
    277   1.8    chopps 	eaddr = argv[1];
    278   1.1       cgd 
    279  1.53     ozaki 	s = getsocket();
    280   1.1       cgd 	argc -= 2;
    281   1.1       cgd 	argv += 2;
    282  1.53     ozaki 
    283  1.64  dholland 	if (getinetaddr(host, &sin_m.sin_addr) == -1) {
    284  1.64  dholland 		prog_close(s);
    285   1.8    chopps 		return (1);
    286  1.64  dholland 	}
    287  1.61  christos 	if (strcmp(eaddr, "auto") != 0 && atosdl(eaddr, &sdl_m))
    288  1.17        is 		warnx("invalid link-level address '%s'", eaddr);
    289   1.5   mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    290  1.50  christos 	for (; argc-- > 0; argv++) {
    291   1.5   mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    292  1.41   xtraeme 			struct timeval timev;
    293  1.41   xtraeme 			(void)gettimeofday(&timev, 0);
    294  1.41   xtraeme 			expire_time = timev.tv_sec + 20 * 60;
    295   1.5   mycroft 		}
    296   1.5   mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    297   1.5   mycroft 			flags |= RTF_ANNOUNCE;
    298  1.54     ozaki 			doing_proxy = 1;
    299  1.46     seanb 			if (argc && strncmp(argv[1], "pro", 3) == 0) {
    300  1.56       ryo 				export_only = 1;
    301  1.56       ryo 				argc--; argv++;
    302  1.46     seanb 			}
    303   1.5   mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    304  1.44  christos 			warnx("%s: Sending trailers is no longer supported",
    305  1.44  christos 			    host);
    306  1.50  christos 		} else if (strcmp(argv[0], "ifscope") == 0) {
    307  1.50  christos 			if (argc == 0) {
    308  1.50  christos 				warnx("missing interface for ifscope");
    309  1.50  christos 				continue;
    310  1.50  christos 			}
    311  1.50  christos 			argc--;
    312  1.50  christos 			argv++;
    313  1.50  christos 			if (!getlink(argv[0], &sdl_m))
    314  1.50  christos 				warnx("cannot get link address for %s", argv[0]);
    315   1.5   mycroft 		}
    316  1.50  christos 
    317   1.1       cgd 	}
    318  1.61  christos 	if (doing_proxy && strcmp(eaddr, "auto") == 0) {
    319  1.64  dholland 		if (getetheraddr(sin_m.sin_addr, &sdl_m) == -1) {
    320  1.64  dholland 			prog_close(s);
    321  1.60  christos 			return 1;
    322  1.64  dholland 		}
    323  1.60  christos 	}
    324   1.5   mycroft tryagain:
    325  1.58     ozaki 	rtm = rtmsg(s, RTM_GET, NULL, &sin_m, &sdl_m);
    326  1.53     ozaki 	if (rtm == NULL) {
    327   1.8    chopps 		warn("%s", host);
    328  1.64  dholland 		prog_close(s);
    329   1.5   mycroft 		return (1);
    330   1.1       cgd 	}
    331  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    332  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    333  1.44  christos 	    (char *)(void *)sina);
    334  1.41   xtraeme 	if (sina->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    335  1.43    dyoung 		if (is_llinfo(sdl, rtm->rtm_flags))
    336   1.5   mycroft 			goto overwrite;
    337   1.5   mycroft 		if (doing_proxy == 0) {
    338  1.44  christos 			warnx("set: can only proxy for %s", host);
    339  1.64  dholland 			prog_close(s);
    340   1.5   mycroft 			return (1);
    341   1.5   mycroft 		}
    342   1.5   mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    343  1.44  christos 			warnx("set: proxy entry exists for non 802 device");
    344  1.64  dholland 			prog_close(s);
    345   1.8    chopps 			return (1);
    346   1.5   mycroft 		}
    347   1.5   mycroft 		sin_m.sin_other = SIN_PROXY;
    348   1.5   mycroft 		export_only = 1;
    349   1.5   mycroft 		goto tryagain;
    350   1.5   mycroft 	}
    351   1.5   mycroft overwrite:
    352   1.5   mycroft 	if (sdl->sdl_family != AF_LINK) {
    353  1.44  christos 		warnx("cannot intuit interface index and type for %s",
    354   1.8    chopps 		    host);
    355  1.64  dholland 		prog_close(s);
    356   1.5   mycroft 		return (1);
    357   1.1       cgd 	}
    358   1.5   mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    359   1.5   mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    360  1.53     ozaki 	sin_m.sin_other = 0;
    361  1.53     ozaki 	if (doing_proxy && export_only)
    362  1.53     ozaki 		sin_m.sin_other = SIN_PROXY;
    363  1.58     ozaki 	rtm = rtmsg(s, RTM_ADD, NULL, &sin_m, &sdl_m);
    364  1.23       mrg 	if (vflag)
    365  1.23       mrg 		(void)printf("%s (%s) added\n", host, eaddr);
    366  1.64  dholland 	prog_close(s);
    367  1.53     ozaki 	return (rtm == NULL) ? 1 : 0;
    368   1.1       cgd }
    369   1.1       cgd 
    370   1.1       cgd /*
    371   1.1       cgd  * Display an individual arp entry
    372   1.1       cgd  */
    373  1.44  christos static void
    374  1.41   xtraeme get(const char *host)
    375   1.1       cgd {
    376  1.53     ozaki 	struct sockaddr_inarp sin = blank_sin; /* struct copy */
    377   1.1       cgd 
    378  1.53     ozaki 	if (getinetaddr(host, &sin.sin_addr) == -1)
    379   1.8    chopps 		exit(1);
    380  1.53     ozaki 	dump(sin.sin_addr.s_addr);
    381  1.44  christos 	if (found_entry == 0)
    382  1.53     ozaki 		errx(1, "%s (%s) -- no entry", host, inet_ntoa(sin.sin_addr));
    383   1.1       cgd }
    384   1.1       cgd 
    385  1.43    dyoung 
    386  1.43    dyoung static int
    387  1.43    dyoung is_llinfo(const struct sockaddr_dl *sdl, int rtflags)
    388  1.43    dyoung {
    389  1.43    dyoung 	if (sdl->sdl_family != AF_LINK ||
    390  1.55     ozaki 	    (rtflags & (RTF_LLDATA|RTF_GATEWAY)) != RTF_LLDATA)
    391  1.43    dyoung 		return 0;
    392  1.43    dyoung 
    393  1.43    dyoung 	switch (sdl->sdl_type) {
    394  1.43    dyoung 	case IFT_ETHER:
    395  1.43    dyoung 	case IFT_FDDI:
    396  1.43    dyoung 	case IFT_ISO88023:
    397  1.43    dyoung 	case IFT_ISO88024:
    398  1.43    dyoung 	case IFT_ISO88025:
    399  1.43    dyoung 	case IFT_ARCNET:
    400  1.43    dyoung 		return 1;
    401  1.43    dyoung 	default:
    402  1.43    dyoung 		return 0;
    403  1.43    dyoung 	}
    404  1.43    dyoung }
    405  1.43    dyoung 
    406   1.1       cgd /*
    407  1.56       ryo  * Delete an arp entry
    408   1.1       cgd  */
    409   1.8    chopps int
    410  1.58     ozaki delete_one(struct rt_msghdr *rtm)
    411   1.1       cgd {
    412  1.41   xtraeme 	struct sockaddr_inarp *sina;
    413  1.53     ozaki 	struct sockaddr_dl *sdl;
    414  1.53     ozaki 	int s;
    415   1.8    chopps 
    416  1.53     ozaki 	s = getsocket();
    417  1.44  christos 	sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    418  1.48  christos 	sdl = (struct sockaddr_dl *)(void *)(RT_ROUNDUP(sina->sin_len) +
    419  1.44  christos 	    (char *)(void *)sina);
    420  1.65    nonaka 	if (sdl->sdl_family != AF_LINK) {
    421  1.65    nonaka 		prog_close(s);
    422   1.5   mycroft 		return (1);
    423  1.65    nonaka 	}
    424  1.58     ozaki 	rtm = rtmsg(s, RTM_DELETE, rtm, sina, sdl);
    425  1.64  dholland 	prog_close(s);
    426  1.53     ozaki 	if (rtm == NULL)
    427   1.8    chopps 		return (1);
    428   1.8    chopps 	return (0);
    429   1.1       cgd }
    430   1.1       cgd 
    431   1.1       cgd /*
    432   1.1       cgd  * Dump the entire arp table
    433   1.1       cgd  */
    434   1.8    chopps void
    435  1.44  christos dump(uint32_t addr)
    436   1.1       cgd {
    437   1.5   mycroft 	int mib[6];
    438   1.5   mycroft 	size_t needed;
    439  1.29      fair 	char ifname[IFNAMSIZ];
    440  1.41   xtraeme 	char *lim, *buf, *next;
    441  1.41   xtraeme         const char *host;
    442   1.5   mycroft 	struct rt_msghdr *rtm;
    443  1.41   xtraeme 	struct sockaddr_inarp *sina;
    444   1.5   mycroft 	struct sockaddr_dl *sdl;
    445   1.1       cgd 	struct hostent *hp;
    446  1.66       roy 	struct timeval tim;
    447  1.66       roy 	struct in_nbrinfo *nbi;
    448   1.1       cgd 
    449   1.5   mycroft 	mib[0] = CTL_NET;
    450   1.5   mycroft 	mib[1] = PF_ROUTE;
    451   1.5   mycroft 	mib[2] = 0;
    452   1.5   mycroft 	mib[3] = AF_INET;
    453   1.5   mycroft 	mib[4] = NET_RT_FLAGS;
    454  1.55     ozaki 	mib[5] = 0;
    455  1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    456   1.8    chopps 		err(1, "route-sysctl-estimate");
    457  1.22      fvdl 	if (needed == 0)
    458  1.22      fvdl 		return;
    459   1.5   mycroft 	if ((buf = malloc(needed)) == NULL)
    460   1.8    chopps 		err(1, "malloc");
    461  1.52     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    462   1.8    chopps 		err(1, "actual retrieval of routing table");
    463   1.5   mycroft 	lim = buf + needed;
    464   1.5   mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    465  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    466  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    467  1.44  christos 		sdl = (struct sockaddr_dl *)(void *)
    468  1.48  christos 		    (RT_ROUNDUP(sina->sin_len) + (char *)(void *)sina);
    469   1.5   mycroft 		if (addr) {
    470  1.41   xtraeme 			if (addr != sina->sin_addr.s_addr)
    471   1.5   mycroft 				continue;
    472   1.5   mycroft 			found_entry = 1;
    473   1.5   mycroft 		}
    474   1.5   mycroft 		if (nflag == 0)
    475  1.44  christos 			hp = gethostbyaddr((const char *)(void *)
    476  1.44  christos 			    &(sina->sin_addr),
    477  1.41   xtraeme 			    sizeof sina->sin_addr, AF_INET);
    478   1.1       cgd 		else
    479  1.27  christos 			hp = NULL;
    480  1.27  christos 
    481  1.27  christos 		host = hp ? hp->h_name : "?";
    482  1.27  christos 
    483  1.41   xtraeme 		(void)printf("%s (%s) at ", host, inet_ntoa(sina->sin_addr));
    484   1.5   mycroft 		if (sdl->sdl_alen)
    485  1.17        is 			sdl_print(sdl);
    486   1.1       cgd 		else
    487   1.8    chopps 			(void)printf("(incomplete)");
    488  1.29      fair 
    489  1.29      fair 		if (sdl->sdl_index) {
    490  1.37    itojun 			if (getifname(sdl->sdl_index, ifname, sizeof(ifname)) == 0)
    491  1.44  christos 				(void)printf(" on %s", ifname);
    492  1.29      fair 		}
    493  1.29      fair 
    494   1.5   mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    495   1.8    chopps 			(void)printf(" permanent");
    496  1.41   xtraeme 		if (sina->sin_other & SIN_PROXY)
    497   1.8    chopps 			(void)printf(" published (proxy only)");
    498   1.5   mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    499  1.44  christos 			sina = (struct sockaddr_inarp *)(void *)
    500  1.48  christos 			    (RT_ROUNDUP(sdl->sdl_len) + (char *)(void *)sdl);
    501  1.41   xtraeme 			if (sina->sin_addr.s_addr == 0xffffffff)
    502   1.8    chopps 				(void)printf(" published");
    503  1.41   xtraeme 			if (sina->sin_len != 8)
    504  1.25       erh 				(void)printf("(weird)");
    505   1.5   mycroft 		}
    506  1.66       roy 
    507  1.66       roy 		if (sdl->sdl_index == 0)
    508  1.66       roy 			goto done;
    509  1.66       roy 		(void)gettimeofday(&tim, 0);
    510  1.66       roy 		nbi = getnbrinfo(ifname, &sina->sin_addr);
    511  1.66       roy 		if (nbi != NULL) {
    512  1.66       roy 			if (nbi->expire > tim.tv_sec) {
    513  1.66       roy 				(void)printf(" %s",
    514  1.66       roy 				    sec2str(nbi->expire - tim.tv_sec));
    515  1.66       roy 			} else if (nbi->expire == 0)
    516  1.66       roy 				(void)printf(" %s", "permanent");
    517  1.66       roy 			else
    518  1.66       roy 				(void)printf(" %s", "expired");
    519  1.66       roy 
    520  1.66       roy 			switch (nbi->state) {
    521  1.66       roy 			case ND_LLINFO_NOSTATE:
    522  1.66       roy 				(void)printf(" N");
    523  1.66       roy 				break;
    524  1.66       roy 			case ND_LLINFO_WAITDELETE:
    525  1.66       roy 				(void)printf(" W");
    526  1.66       roy 				break;
    527  1.66       roy 			case ND_LLINFO_INCOMPLETE:
    528  1.66       roy 				(void)printf(" I");
    529  1.66       roy 				break;
    530  1.66       roy 			case ND_LLINFO_REACHABLE:
    531  1.66       roy 				(void)printf(" R");
    532  1.66       roy 				break;
    533  1.66       roy 			case ND_LLINFO_STALE:
    534  1.66       roy 				(void)printf(" S");
    535  1.66       roy 				break;
    536  1.66       roy 			case ND_LLINFO_DELAY:
    537  1.66       roy 				(void)printf(" D");
    538  1.66       roy 				break;
    539  1.66       roy 			case ND_LLINFO_PROBE:
    540  1.66       roy 				(void)printf(" P");
    541  1.66       roy 				break;
    542  1.66       roy 			default:
    543  1.66       roy 				(void)printf(" ?");
    544  1.66       roy 				break;
    545  1.66       roy 			}
    546  1.66       roy 		}
    547  1.66       roy 
    548  1.66       roy done:
    549   1.8    chopps 		(void)printf("\n");
    550   1.1       cgd 	}
    551  1.39    itojun 	free(buf);
    552   1.1       cgd }
    553   1.1       cgd 
    554  1.30    atatat /*
    555  1.30    atatat  * Delete the entire arp table
    556  1.30    atatat  */
    557  1.30    atatat void
    558  1.58     ozaki delete(const char *host, const char *info)
    559  1.30    atatat {
    560  1.30    atatat 	int mib[6];
    561  1.63    nonaka 	char addr[sizeof("000.000.000.000\0")];
    562  1.30    atatat 	size_t needed;
    563  1.30    atatat 	char *lim, *buf, *next;
    564  1.30    atatat 	struct rt_msghdr *rtm;
    565  1.41   xtraeme 	struct sockaddr_inarp *sina;
    566  1.58     ozaki 	struct sockaddr_inarp sin_m = blank_sin; /* struct copy */
    567  1.62     ozaki 	bool found = false;
    568  1.58     ozaki 
    569  1.58     ozaki 	if (host != NULL) {
    570  1.58     ozaki 		int ret = getinetaddr(host, &sin_m.sin_addr);
    571  1.58     ozaki 		if (ret == -1)
    572  1.58     ozaki 			return;
    573  1.58     ozaki 	}
    574  1.58     ozaki 	if (info && strncmp(info, "pro", 3) == 0)
    575  1.58     ozaki 		sin_m.sin_other = SIN_PROXY;
    576  1.30    atatat 
    577  1.58     ozaki retry:
    578  1.30    atatat 	mib[0] = CTL_NET;
    579  1.30    atatat 	mib[1] = PF_ROUTE;
    580  1.30    atatat 	mib[2] = 0;
    581  1.30    atatat 	mib[3] = AF_INET;
    582  1.30    atatat 	mib[4] = NET_RT_FLAGS;
    583  1.58     ozaki 	mib[5] = RTF_LLDATA;
    584  1.52     ozaki 	if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    585  1.30    atatat 		err(1, "route-sysctl-estimate");
    586  1.30    atatat 	if (needed == 0)
    587  1.30    atatat 		return;
    588  1.30    atatat 	if ((buf = malloc(needed)) == NULL)
    589  1.30    atatat 		err(1, "malloc");
    590  1.58     ozaki 	if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
    591  1.58     ozaki 		free(buf);
    592  1.58     ozaki 		if (errno == ENOBUFS)
    593  1.58     ozaki 			goto retry;
    594  1.30    atatat 		err(1, "actual retrieval of routing table");
    595  1.58     ozaki 	}
    596  1.30    atatat 	lim = buf + needed;
    597  1.58     ozaki 
    598  1.30    atatat 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    599  1.58     ozaki 		int ret;
    600  1.44  christos 		rtm = (struct rt_msghdr *)(void *)next;
    601  1.44  christos 		sina = (struct sockaddr_inarp *)(void *)(rtm + 1);
    602  1.58     ozaki 		if (host != NULL &&
    603  1.58     ozaki 		    sina->sin_addr.s_addr != sin_m.sin_addr.s_addr)
    604  1.58     ozaki 			continue;
    605  1.62     ozaki 		found = true;
    606  1.58     ozaki 		ret = delete_one(rtm);
    607  1.58     ozaki 		if (vflag && ret == 0) {
    608  1.63    nonaka 			snprintf(addr, sizeof(addr), "%s",
    609  1.58     ozaki 			    inet_ntoa(sina->sin_addr));
    610  1.63    nonaka 			(void)printf("%s (%s) deleted\n",
    611  1.63    nonaka 			    host != NULL ? host : addr, addr);
    612  1.58     ozaki 		}
    613  1.30    atatat 	}
    614  1.62     ozaki 	if (host != NULL && !found)
    615  1.62     ozaki 		warnx("delete: can't locate %s", host);
    616  1.39    itojun 	free(buf);
    617  1.30    atatat }
    618  1.30    atatat 
    619   1.8    chopps void
    620  1.41   xtraeme sdl_print(const struct sockaddr_dl *sdl)
    621  1.17        is {
    622  1.32     bjh21 	char hbuf[NI_MAXHOST];
    623  1.17        is 
    624  1.44  christos 	if (getnameinfo((const struct sockaddr *)(const void *)sdl,
    625  1.44  christos 	    (socklen_t)sdl->sdl_len,
    626  1.32     bjh21 	    hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
    627  1.44  christos 		(void)printf("<invalid>");
    628  1.32     bjh21 	else
    629  1.44  christos 		(void)printf("%s", hbuf);
    630  1.17        is }
    631  1.17        is 
    632  1.44  christos static int
    633  1.41   xtraeme atosdl(const char *ss, struct sockaddr_dl *sdl)
    634   1.1       cgd {
    635  1.17        is 	int i;
    636  1.44  christos 	unsigned long b;
    637  1.44  christos 	char *endp;
    638  1.56       ryo 	char *p;
    639  1.17        is 	char *t, *r;
    640  1.17        is 
    641  1.17        is 	p = LLADDR(sdl);
    642  1.44  christos 	endp = ((char *)(void *)sdl) + sdl->sdl_len;
    643  1.17        is 	i = 0;
    644  1.56       ryo 
    645  1.44  christos 	b = strtoul(ss, &t, 16);
    646  1.44  christos 	if (b > 255 || t == ss)
    647  1.17        is 		return 1;
    648  1.17        is 
    649  1.44  christos 	*p++ = (char)b;
    650  1.17        is 	++i;
    651  1.17        is 	while ((p < endp) && (*t++ == ':')) {
    652  1.44  christos 		b = strtoul(t, &r, 16);
    653  1.44  christos 		if (b > 255 || r == t)
    654  1.17        is 			break;
    655  1.44  christos 		*p++ = (char)b;
    656  1.17        is 		++i;
    657  1.17        is 		t = r;
    658  1.17        is 	}
    659  1.17        is 	sdl->sdl_alen = i;
    660  1.17        is 
    661  1.17        is 	return 0;
    662   1.1       cgd }
    663   1.1       cgd 
    664  1.44  christos static void
    665  1.41   xtraeme usage(void)
    666   1.1       cgd {
    667  1.33     pooka 	const char *progname;
    668  1.20     lukem 
    669  1.33     pooka 	progname = getprogname();
    670  1.44  christos 	(void)fprintf(stderr, "Usage: %s [-n] hostname\n", progname);
    671  1.56       ryo 	(void)fprintf(stderr, "       %s [-nv] -a\n", progname);
    672  1.59    nonaka 	(void)fprintf(stderr, "       %s [-v] -d [-a|hostname [proxy]]\n",
    673  1.44  christos 	    progname);
    674  1.46     seanb 	(void)fprintf(stderr, "       %s -s hostname ether_addr [temp] [pub [proxy]]\n",
    675  1.44  christos 	    progname);
    676  1.44  christos 	(void)fprintf(stderr, "       %s -f filename\n", progname);
    677   1.5   mycroft 	exit(1);
    678   1.5   mycroft }
    679   1.5   mycroft 
    680  1.53     ozaki static struct rt_msghdr *
    681  1.58     ozaki rtmsg(const int s, const int cmd, struct rt_msghdr *_rtm,
    682  1.58     ozaki     const struct sockaddr_inarp *sin, const struct sockaddr_dl *sdl)
    683   1.5   mycroft {
    684   1.5   mycroft 	static int seq;
    685  1.58     ozaki 	struct rt_msghdr *rtm = _rtm;
    686  1.19     lukem 	char *cp;
    687  1.19     lukem 	int l;
    688  1.53     ozaki 	static struct {
    689  1.53     ozaki 		struct	rt_msghdr m_rtm;
    690  1.53     ozaki 		char	m_space[512];
    691  1.53     ozaki 	} m_rtmsg;
    692  1.53     ozaki 	pid_t pid;
    693   1.5   mycroft 
    694   1.5   mycroft 	errno = 0;
    695  1.58     ozaki 	if (rtm != NULL) {
    696  1.58     ozaki 		memcpy(&m_rtmsg, rtm, rtm->rtm_msglen);
    697  1.58     ozaki 		rtm = &m_rtmsg.m_rtm;
    698   1.5   mycroft 		goto doit;
    699  1.55     ozaki 	}
    700   1.8    chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    701  1.58     ozaki 	rtm = &m_rtmsg.m_rtm;
    702  1.58     ozaki 	cp = m_rtmsg.m_space;
    703  1.58     ozaki 
    704   1.5   mycroft 	rtm->rtm_flags = flags;
    705   1.5   mycroft 	rtm->rtm_version = RTM_VERSION;
    706   1.5   mycroft 
    707   1.5   mycroft 	switch (cmd) {
    708   1.5   mycroft 	default:
    709   1.8    chopps 		errx(1, "internal wrong cmd");
    710   1.8    chopps 		/*NOTREACHED*/
    711   1.5   mycroft 	case RTM_ADD:
    712   1.5   mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    713   1.5   mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    714   1.5   mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    715  1.55     ozaki 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC | RTF_LLDATA);
    716   1.5   mycroft 		if (doing_proxy) {
    717  1.53     ozaki 			if (!export_only) {
    718   1.5   mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    719   1.5   mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    720   1.5   mycroft 			}
    721   1.5   mycroft 		}
    722  1.55     ozaki 		rtm->rtm_addrs |= RTA_DST;
    723  1.55     ozaki 		break;
    724   1.5   mycroft 	case RTM_GET:
    725  1.55     ozaki 		rtm->rtm_flags |= RTF_LLDATA;
    726  1.57     ozaki 		rtm->rtm_addrs |= RTA_DST | RTA_GATEWAY;
    727   1.5   mycroft 	}
    728  1.25       erh 
    729   1.5   mycroft #define NEXTADDR(w, s) \
    730   1.5   mycroft 	if (rtm->rtm_addrs & (w)) { \
    731  1.44  christos 		(void)memcpy(cp, &s, \
    732  1.53     ozaki 		    (size_t)((const struct sockaddr *)&s)->sa_len); \
    733  1.53     ozaki 		RT_ADVANCE(cp, ((const struct sockaddr *)&s)); \
    734  1.25       erh 	}
    735   1.5   mycroft 
    736  1.53     ozaki 	NEXTADDR(RTA_DST, *sin);
    737  1.53     ozaki 	NEXTADDR(RTA_GATEWAY, *sdl);
    738   1.5   mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    739   1.5   mycroft 
    740  1.44  christos 	rtm->rtm_msglen = cp - (char *)(void *)&m_rtmsg;
    741   1.5   mycroft doit:
    742   1.5   mycroft 	l = rtm->rtm_msglen;
    743   1.5   mycroft 	rtm->rtm_seq = ++seq;
    744   1.5   mycroft 	rtm->rtm_type = cmd;
    745  1.52     ozaki 	if (prog_write(s, &m_rtmsg, (size_t)l) < 0) {
    746   1.5   mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    747   1.8    chopps 			warn("writing to routing socket");
    748  1.53     ozaki 			return NULL;
    749   1.5   mycroft 		}
    750   1.5   mycroft 	}
    751  1.53     ozaki 
    752  1.53     ozaki 	pid = prog_getpid();
    753   1.5   mycroft 	do {
    754  1.52     ozaki 		l = prog_read(s, &m_rtmsg, sizeof(m_rtmsg));
    755   1.5   mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    756   1.5   mycroft 	if (l < 0)
    757   1.8    chopps 		warn("read from routing socket");
    758  1.53     ozaki 	return rtm;
    759   1.5   mycroft }
    760   1.5   mycroft 
    761  1.44  christos static int
    762  1.41   xtraeme getinetaddr(const char *host, struct in_addr *inap)
    763   1.5   mycroft {
    764   1.8    chopps 	struct hostent *hp;
    765   1.8    chopps 
    766   1.9    chopps 	if (inet_aton(host, inap) == 1)
    767   1.8    chopps 		return (0);
    768   1.8    chopps 	if ((hp = gethostbyname(host)) == NULL) {
    769  1.31       chs 		warnx("%s: %s", host, hstrerror(h_errno));
    770   1.8    chopps 		return (-1);
    771   1.8    chopps 	}
    772   1.8    chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    773   1.8    chopps 	return (0);
    774  1.29      fair }
    775  1.29      fair 
    776  1.44  christos static int
    777  1.41   xtraeme getifname(u_int16_t ifindex, char *ifname, size_t l)
    778  1.29      fair {
    779  1.35     rafal 	int i;
    780  1.35     rafal 	struct ifaddrs *addr;
    781  1.60  christos 	const struct sockaddr_dl *sdl;
    782  1.53     ozaki 	static struct ifaddrs* ifaddrs = NULL;
    783  1.29      fair 
    784  1.35     rafal 	if (ifaddrs == NULL) {
    785  1.35     rafal 		i = getifaddrs(&ifaddrs);
    786  1.35     rafal 		if (i != 0)
    787  1.35     rafal 			err(1, "getifaddrs");
    788  1.29      fair 	}
    789  1.29      fair 
    790  1.35     rafal 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    791  1.56       ryo 		if (addr->ifa_addr == NULL ||
    792  1.35     rafal 		    addr->ifa_addr->sa_family != AF_LINK)
    793  1.29      fair 			continue;
    794  1.29      fair 
    795  1.44  christos 		sdl = (const struct sockaddr_dl *)(void *)addr->ifa_addr;
    796  1.29      fair 		if (sdl && sdl->sdl_index == ifindex) {
    797  1.37    itojun 			(void) strlcpy(ifname, addr->ifa_name, l);
    798  1.29      fair 			return 0;
    799  1.29      fair 		}
    800  1.29      fair 	}
    801  1.29      fair 
    802  1.29      fair 	return -1;
    803   1.1       cgd }
    804  1.60  christos 
    805  1.60  christos static int
    806  1.60  christos getetheraddr(struct in_addr ipaddr, struct sockaddr_dl *sdl)
    807  1.60  christos {
    808  1.60  christos 	struct ifaddrs *ifaddrs, *addr;
    809  1.60  christos 	in_addr_t ina, mask;
    810  1.60  christos 	char ifname[IFNAMSIZ];
    811  1.60  christos 
    812  1.60  christos 	if (getifaddrs(&ifaddrs) != 0) {
    813  1.60  christos 		warn("getifaddrs");
    814  1.60  christos 		return -1;
    815  1.60  christos 	}
    816  1.60  christos 
    817  1.60  christos 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    818  1.60  christos 		if (addr->ifa_addr == NULL ||
    819  1.60  christos 		    addr->ifa_addr->sa_family != AF_INET)
    820  1.60  christos 			continue;
    821  1.60  christos 		if ((addr->ifa_flags & (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|
    822  1.60  christos 		    IFF_LOOPBACK|IFF_NOARP)) != (IFF_UP|IFF_BROADCAST))
    823  1.60  christos 			continue;
    824  1.60  christos 
    825  1.60  christos 		mask = ((struct sockaddr_in *)(void *)addr->ifa_netmask)->sin_addr.s_addr;
    826  1.60  christos 		ina = ((struct sockaddr_in *)(void *)addr->ifa_addr)->sin_addr.s_addr;
    827  1.60  christos 		if ((ipaddr.s_addr & mask) != (ina & mask))
    828  1.60  christos 			continue;
    829  1.60  christos 		strlcpy(ifname, addr->ifa_name, sizeof(ifname));
    830  1.60  christos 		break;
    831  1.60  christos 	}
    832  1.60  christos 	if (addr == NULL) {
    833  1.60  christos 		warnx("No interface matched %s", inet_ntoa(ipaddr));
    834  1.60  christos 		freeifaddrs(ifaddrs);
    835  1.60  christos 		return -1;
    836  1.60  christos 	}
    837  1.60  christos 
    838  1.60  christos 	for (addr = ifaddrs; addr; addr = addr->ifa_next) {
    839  1.60  christos 		if (addr->ifa_addr == NULL ||
    840  1.60  christos 		    addr->ifa_addr->sa_family != AF_LINK)
    841  1.60  christos 			continue;
    842  1.60  christos 		if (strcmp(ifname, addr->ifa_name) != 0)
    843  1.60  christos 			continue;
    844  1.60  christos 		memcpy(sdl, addr->ifa_addr, sizeof(*sdl));
    845  1.60  christos 		freeifaddrs(ifaddrs);
    846  1.60  christos 		return 0;
    847  1.60  christos 	}
    848  1.60  christos 	warnx("No link address for interface %s", ifname);
    849  1.60  christos 	freeifaddrs(ifaddrs);
    850  1.60  christos 	return -1;
    851  1.60  christos }
    852  1.66       roy 
    853  1.66       roy static struct in_nbrinfo *
    854  1.66       roy getnbrinfo(const char *ifname, struct in_addr *addr)
    855  1.66       roy {
    856  1.66       roy 	static struct in_nbrinfo nbi, *nbip;
    857  1.66       roy 	int s;
    858  1.66       roy 
    859  1.66       roy 	if ((s = prog_socket(AF_INET, SOCK_DGRAM, 0)) == -1)
    860  1.66       roy 		err(1, "socket");
    861  1.66       roy 
    862  1.66       roy 	(void)memset(&nbi, 0, sizeof(nbi));
    863  1.66       roy 	(void)strlcpy(nbi.ifname, ifname, sizeof(nbi.ifname));
    864  1.66       roy 	nbi.addr = *addr;
    865  1.66       roy 	if (prog_ioctl(s, SIOCGNBRINFO, &nbi) == -1) {
    866  1.66       roy 		warn("ioctl(SIOCGNBRINFO)");
    867  1.66       roy 		nbip = NULL;
    868  1.66       roy 	} else
    869  1.66       roy 		nbip = &nbi;
    870  1.66       roy 	(void)prog_close(s);
    871  1.66       roy 
    872  1.66       roy 	return nbip;
    873  1.66       roy }
    874  1.66       roy 
    875  1.66       roy static const char *
    876  1.66       roy sec2str(time_t total)
    877  1.66       roy {
    878  1.66       roy 	static char result[256];
    879  1.66       roy 	int days, hours, mins, secs;
    880  1.66       roy 	int first = 1;
    881  1.66       roy 	char *p = result;
    882  1.66       roy 	char *ep = &result[sizeof(result)];
    883  1.66       roy 	int n;
    884  1.66       roy 
    885  1.66       roy 	days = total / 3600 / 24;
    886  1.66       roy 	hours = (total / 3600) % 24;
    887  1.66       roy 	mins = (total / 60) % 60;
    888  1.66       roy 	secs = total % 60;
    889  1.66       roy 
    890  1.66       roy 	if (days) {
    891  1.66       roy 		first = 0;
    892  1.66       roy 		n = snprintf(p, (size_t)(ep - p), "%dd", days);
    893  1.66       roy 		if (n < 0 || n >= ep - p)
    894  1.66       roy 			return "?";
    895  1.66       roy 		p += n;
    896  1.66       roy 	}
    897  1.66       roy 	if (!first || hours) {
    898  1.66       roy 		first = 0;
    899  1.66       roy 		n = snprintf(p, (size_t)(ep - p), "%dh", hours);
    900  1.66       roy 		if (n < 0 || n >= ep - p)
    901  1.66       roy 			return "?";
    902  1.66       roy 		p += n;
    903  1.66       roy 	}
    904  1.66       roy 	if (!first || mins) {
    905  1.66       roy 		first = 0;
    906  1.66       roy 		n = snprintf(p, (size_t)(ep - p), "%dm", mins);
    907  1.66       roy 		if (n < 0 || n >= ep - p)
    908  1.66       roy 			return "?";
    909  1.66       roy 		p += n;
    910  1.66       roy 	}
    911  1.66       roy 	(void)snprintf(p, (size_t)(ep - p), "%ds", secs);
    912  1.66       roy 
    913  1.66       roy 	return(result);
    914  1.66       roy }
    915