Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.15
      1  1.15    mikel /*	$NetBSD: arp.c,v 1.15 1997/02/07 05:07:13 mikel 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.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.1      cgd  *    must display the following acknowledgement:
     20   1.1      cgd  *	This product includes software developed by the University of
     21   1.1      cgd  *	California, Berkeley and its contributors.
     22   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.1      cgd  *    may be used to endorse or promote products derived from this software
     24   1.1      cgd  *    without specific prior written permission.
     25   1.1      cgd  *
     26   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1      cgd  * SUCH DAMAGE.
     37   1.1      cgd  */
     38   1.1      cgd 
     39   1.1      cgd #ifndef lint
     40   1.5  mycroft static char copyright[] =
     41   1.5  mycroft "@(#) Copyright (c) 1984, 1993\n\
     42   1.5  mycroft 	The Regents of the University of California.  All rights reserved.\n";
     43   1.1      cgd #endif /* not lint */
     44   1.1      cgd 
     45   1.1      cgd #ifndef lint
     46   1.5  mycroft /*static char sccsid[] = "from: @(#)arp.c	8.2 (Berkeley) 1/2/94";*/
     47  1.15    mikel static char *rcsid = "$NetBSD: arp.c,v 1.15 1997/02/07 05:07:13 mikel Exp $";
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd /*
     51   1.1      cgd  * arp - display, set, and delete arp table entries
     52   1.1      cgd  */
     53   1.1      cgd 
     54   1.1      cgd #include <sys/param.h>
     55   1.1      cgd #include <sys/file.h>
     56   1.1      cgd #include <sys/socket.h>
     57   1.5  mycroft #include <sys/sysctl.h>
     58   1.5  mycroft 
     59   1.5  mycroft #include <net/if.h>
     60   1.5  mycroft #include <net/if_dl.h>
     61   1.5  mycroft #include <net/if_types.h>
     62   1.5  mycroft #include <net/route.h>
     63   1.1      cgd #include <netinet/in.h>
     64   1.1      cgd #include <netinet/if_ether.h>
     65   1.2  mycroft #include <arpa/inet.h>
     66   1.1      cgd 
     67  1.15    mikel #include <err.h>
     68  1.15    mikel #include <errno.h>
     69   1.5  mycroft #include <netdb.h>
     70   1.1      cgd #include <nlist.h>
     71  1.15    mikel #include <paths.h>
     72   1.1      cgd #include <stdio.h>
     73   1.8   chopps #include <stdlib.h>
     74  1.12      cgd #include <string.h>
     75  1.15    mikel #include <unistd.h>
     76   1.1      cgd 
     77   1.8   chopps int delete __P((const char *, const char *));
     78   1.8   chopps void dump __P((u_long));
     79   1.8   chopps void ether_print __P((const u_char *));
     80   1.8   chopps int file __P((char *));
     81   1.8   chopps void get __P((const char *));
     82   1.8   chopps int getinetaddr __P((const char *, struct in_addr *));
     83   1.8   chopps void getsocket __P((void));
     84   1.8   chopps int rtmsg __P((int));
     85   1.8   chopps int set __P((int, char **));
     86   1.8   chopps void usage __P((void));
     87   1.8   chopps 
     88   1.5  mycroft static int pid;
     89   1.5  mycroft static int nflag;
     90   1.5  mycroft static int s = -1;
     91   1.1      cgd 
     92   1.8   chopps int
     93   1.1      cgd main(argc, argv)
     94   1.1      cgd 	int argc;
     95   1.1      cgd 	char **argv;
     96   1.1      cgd {
     97   1.1      cgd 	int ch;
     98  1.15    mikel 	int op = 0;
     99   1.1      cgd 
    100   1.5  mycroft 	pid = getpid();
    101  1.15    mikel 
    102  1.15    mikel 	while ((ch = getopt(argc, argv, "andsf")) != -1)
    103   1.1      cgd 		switch((char)ch) {
    104   1.5  mycroft 		case 'a':
    105   1.1      cgd 		case 'd':
    106  1.15    mikel 		case 's':
    107  1.15    mikel 		case 'f':
    108  1.15    mikel 			if (op)
    109   1.1      cgd 				usage();
    110  1.15    mikel 			op = ch;
    111  1.15    mikel 			break;
    112   1.5  mycroft 		case 'n':
    113   1.5  mycroft 			nflag = 1;
    114   1.8   chopps 			break;
    115   1.1      cgd 		default:
    116   1.1      cgd 			usage();
    117   1.1      cgd 		}
    118  1.15    mikel 	argc -= optind;
    119  1.15    mikel 	argv += optind;
    120  1.15    mikel 
    121  1.15    mikel 	switch((char)op) {
    122  1.15    mikel 	case 'a':
    123  1.15    mikel 		dump(0);
    124  1.15    mikel 		break;
    125  1.15    mikel 	case 'd':
    126  1.15    mikel 		if (argc < 1 || argc > 2)
    127  1.15    mikel 			usage();
    128  1.15    mikel 		(void)delete(argv[0], argv[1]);
    129  1.15    mikel 		break;
    130  1.15    mikel 	case 's':
    131  1.15    mikel 		if (argc < 2 || argc > 5)
    132  1.15    mikel 			usage();
    133  1.15    mikel 		return (set(argc, argv) ? 1 : 0);
    134  1.15    mikel 	case 'f':
    135  1.15    mikel 		if (argc != 1)
    136  1.15    mikel 			usage();
    137  1.15    mikel 		return (file(argv[0]));
    138  1.15    mikel 	default:
    139  1.15    mikel 		if (argc != 1)
    140  1.15    mikel 			usage();
    141  1.15    mikel 		get(argv[0]);
    142  1.15    mikel 		break;
    143  1.15    mikel 	}
    144   1.8   chopps 	return (0);
    145   1.1      cgd }
    146   1.1      cgd 
    147   1.1      cgd /*
    148   1.1      cgd  * Process a file to set standard arp entries
    149   1.1      cgd  */
    150   1.8   chopps int
    151   1.1      cgd file(name)
    152   1.1      cgd 	char *name;
    153   1.1      cgd {
    154   1.8   chopps 	char line[100], arg[5][50], *args[5];
    155   1.8   chopps 	int i, retval;
    156   1.1      cgd 	FILE *fp;
    157   1.1      cgd 
    158   1.8   chopps 	if ((fp = fopen(name, "r")) == NULL)
    159   1.8   chopps 		err(1, "cannot open %s", name);
    160   1.1      cgd 	args[0] = &arg[0][0];
    161   1.1      cgd 	args[1] = &arg[1][0];
    162   1.1      cgd 	args[2] = &arg[2][0];
    163   1.1      cgd 	args[3] = &arg[3][0];
    164   1.1      cgd 	args[4] = &arg[4][0];
    165   1.1      cgd 	retval = 0;
    166   1.8   chopps 	while (fgets(line, 100, fp) != NULL) {
    167   1.1      cgd 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    168   1.1      cgd 		    arg[3], arg[4]);
    169   1.1      cgd 		if (i < 2) {
    170   1.8   chopps 			warnx("bad line: %s", line);
    171   1.1      cgd 			retval = 1;
    172   1.1      cgd 			continue;
    173   1.1      cgd 		}
    174   1.1      cgd 		if (set(i, args))
    175   1.1      cgd 			retval = 1;
    176   1.1      cgd 	}
    177   1.1      cgd 	fclose(fp);
    178   1.1      cgd 	return (retval);
    179   1.1      cgd }
    180   1.1      cgd 
    181   1.8   chopps void
    182   1.8   chopps getsocket()
    183   1.8   chopps {
    184   1.8   chopps 	if (s >= 0)
    185   1.8   chopps 		return;
    186   1.8   chopps 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    187   1.8   chopps 	if (s < 0)
    188   1.8   chopps 		err(1, "socket");
    189   1.5  mycroft }
    190   1.5  mycroft 
    191   1.5  mycroft struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    192   1.5  mycroft struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    193   1.5  mycroft struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    194   1.5  mycroft int	expire_time, flags, export_only, doing_proxy, found_entry;
    195   1.5  mycroft struct	{
    196   1.5  mycroft 	struct	rt_msghdr m_rtm;
    197   1.5  mycroft 	char	m_space[512];
    198   1.5  mycroft }	m_rtmsg;
    199   1.5  mycroft 
    200   1.1      cgd /*
    201   1.1      cgd  * Set an individual arp entry
    202   1.1      cgd  */
    203   1.8   chopps int
    204   1.1      cgd set(argc, argv)
    205   1.1      cgd 	int argc;
    206   1.1      cgd 	char **argv;
    207   1.1      cgd {
    208   1.8   chopps 	register struct sockaddr_inarp *sin;
    209   1.5  mycroft 	register struct sockaddr_dl *sdl;
    210   1.8   chopps 	register struct rt_msghdr *rtm;
    211  1.14    mikel 	struct ether_addr *ea;
    212   1.8   chopps 	char *host = argv[0], *eaddr;
    213   1.8   chopps 
    214   1.8   chopps 	sin = &sin_m;
    215   1.8   chopps 	rtm = &(m_rtmsg.m_rtm);
    216   1.8   chopps 	eaddr = argv[1];
    217   1.1      cgd 
    218   1.5  mycroft 	getsocket();
    219   1.1      cgd 	argc -= 2;
    220   1.1      cgd 	argv += 2;
    221   1.8   chopps 	sdl_m = blank_sdl;		/* struct copy */
    222   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    223   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    224   1.8   chopps 		return (1);
    225  1.14    mikel 	ea = ether_aton(eaddr);
    226  1.14    mikel 	if (ea != NULL) {
    227  1.14    mikel 		(void)memcpy(LLADDR(&sdl_m), ea, sizeof(struct ether_addr));
    228  1.14    mikel 		sdl_m.sdl_alen = sizeof(struct ether_addr);
    229  1.14    mikel 	}
    230  1.14    mikel 	else
    231  1.14    mikel 		warnx("invalid Ethernet address '%s'", eaddr);
    232   1.5  mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    233   1.1      cgd 	while (argc-- > 0) {
    234   1.5  mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    235   1.5  mycroft 			struct timeval time;
    236   1.8   chopps 			(void)gettimeofday(&time, 0);
    237   1.5  mycroft 			expire_time = time.tv_sec + 20 * 60;
    238   1.5  mycroft 		}
    239   1.5  mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    240   1.5  mycroft 			flags |= RTF_ANNOUNCE;
    241   1.5  mycroft 			doing_proxy = SIN_PROXY;
    242   1.5  mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    243   1.8   chopps 			(void)printf(
    244   1.8   chopps 			    "%s: Sending trailers is no longer supported\n",
    245   1.8   chopps 			     host);
    246   1.5  mycroft 		}
    247   1.1      cgd 		argv++;
    248   1.1      cgd 	}
    249   1.5  mycroft tryagain:
    250   1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    251   1.8   chopps 		warn("%s", host);
    252   1.5  mycroft 		return (1);
    253   1.1      cgd 	}
    254   1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    255   1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    256   1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    257   1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    258   1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    259   1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    260   1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    261   1.5  mycroft 		case IFT_ISO88024: case IFT_ISO88025:
    262   1.5  mycroft 			goto overwrite;
    263   1.5  mycroft 		}
    264   1.5  mycroft 		if (doing_proxy == 0) {
    265   1.8   chopps 			(void)printf("set: can only proxy for %s\n", host);
    266   1.5  mycroft 			return (1);
    267   1.5  mycroft 		}
    268   1.5  mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    269   1.8   chopps 			(void)printf(
    270   1.8   chopps 			    "set: proxy entry exists for non 802 device\n");
    271   1.8   chopps 			return (1);
    272   1.5  mycroft 		}
    273   1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    274   1.5  mycroft 		export_only = 1;
    275   1.5  mycroft 		goto tryagain;
    276   1.5  mycroft 	}
    277   1.5  mycroft overwrite:
    278   1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    279   1.8   chopps 		(void)printf("cannot intuit interface index and type for %s\n",
    280   1.8   chopps 		    host);
    281   1.5  mycroft 		return (1);
    282   1.1      cgd 	}
    283   1.5  mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    284   1.5  mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    285   1.5  mycroft 	return (rtmsg(RTM_ADD));
    286   1.1      cgd }
    287   1.1      cgd 
    288   1.1      cgd /*
    289   1.1      cgd  * Display an individual arp entry
    290   1.1      cgd  */
    291   1.8   chopps void
    292   1.1      cgd get(host)
    293   1.8   chopps 	const char *host;
    294   1.1      cgd {
    295   1.8   chopps 	struct sockaddr_inarp *sin;
    296   1.1      cgd 
    297   1.8   chopps 	sin = &sin_m;
    298   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    299   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    300   1.8   chopps 		exit(1);
    301   1.5  mycroft 	dump(sin->sin_addr.s_addr);
    302   1.5  mycroft 	if (found_entry == 0) {
    303   1.8   chopps 		(void)printf("%s (%s) -- no entry\n", host,
    304   1.8   chopps 		    inet_ntoa(sin->sin_addr));
    305   1.1      cgd 		exit(1);
    306   1.1      cgd 	}
    307   1.1      cgd }
    308   1.1      cgd 
    309   1.1      cgd /*
    310   1.1      cgd  * Delete an arp entry
    311   1.1      cgd  */
    312   1.8   chopps int
    313   1.5  mycroft delete(host, info)
    314   1.8   chopps 	const char *host;
    315   1.8   chopps 	const char *info;
    316   1.1      cgd {
    317   1.8   chopps 	register struct sockaddr_inarp *sin;
    318   1.8   chopps 	register struct rt_msghdr *rtm;
    319   1.5  mycroft 	struct sockaddr_dl *sdl;
    320   1.1      cgd 
    321   1.8   chopps 	sin = &sin_m;
    322   1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    323   1.8   chopps 
    324   1.5  mycroft 	if (info && strncmp(info, "pro", 3) )
    325   1.5  mycroft 		export_only = 1;
    326   1.5  mycroft 	getsocket();
    327   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    328   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    329   1.8   chopps 		return (1);
    330   1.5  mycroft tryagain:
    331   1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    332   1.8   chopps 		warn("%s", host);
    333   1.5  mycroft 		return (1);
    334   1.5  mycroft 	}
    335   1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    336   1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    337   1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    338   1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    339   1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    340   1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    341   1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    342   1.5  mycroft 		case IFT_ISO88024: case IFT_ISO88025:
    343   1.5  mycroft 			goto delete;
    344   1.5  mycroft 		}
    345   1.1      cgd 	}
    346   1.5  mycroft 	if (sin_m.sin_other & SIN_PROXY) {
    347   1.8   chopps 		warnx("delete: can't locate %s", host);
    348   1.5  mycroft 		return (1);
    349   1.5  mycroft 	} else {
    350   1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    351   1.5  mycroft 		goto tryagain;
    352   1.5  mycroft 	}
    353   1.5  mycroft delete:
    354   1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    355   1.8   chopps 		(void)printf("cannot locate %s\n", host);
    356   1.5  mycroft 		return (1);
    357   1.1      cgd 	}
    358   1.8   chopps 	if (rtmsg(RTM_DELETE))
    359   1.8   chopps 		return (1);
    360   1.8   chopps 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
    361   1.8   chopps 	return (0);
    362   1.1      cgd }
    363   1.1      cgd 
    364   1.1      cgd /*
    365   1.1      cgd  * Dump the entire arp table
    366   1.1      cgd  */
    367   1.8   chopps void
    368   1.5  mycroft dump(addr)
    369   1.8   chopps 	u_long addr;
    370   1.1      cgd {
    371   1.5  mycroft 	int mib[6];
    372   1.5  mycroft 	size_t needed;
    373   1.8   chopps 	char *host, *lim, *buf, *next;
    374   1.5  mycroft 	struct rt_msghdr *rtm;
    375   1.5  mycroft 	struct sockaddr_inarp *sin;
    376   1.5  mycroft 	struct sockaddr_dl *sdl;
    377   1.1      cgd 	extern int h_errno;
    378   1.1      cgd 	struct hostent *hp;
    379   1.1      cgd 
    380   1.5  mycroft 	mib[0] = CTL_NET;
    381   1.5  mycroft 	mib[1] = PF_ROUTE;
    382   1.5  mycroft 	mib[2] = 0;
    383   1.5  mycroft 	mib[3] = AF_INET;
    384   1.5  mycroft 	mib[4] = NET_RT_FLAGS;
    385   1.5  mycroft 	mib[5] = RTF_LLINFO;
    386   1.5  mycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    387   1.8   chopps 		err(1, "route-sysctl-estimate");
    388   1.5  mycroft 	if ((buf = malloc(needed)) == NULL)
    389   1.8   chopps 		err(1, "malloc");
    390   1.5  mycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    391   1.8   chopps 		err(1, "actual retrieval of routing table");
    392   1.5  mycroft 	lim = buf + needed;
    393   1.5  mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    394   1.5  mycroft 		rtm = (struct rt_msghdr *)next;
    395   1.5  mycroft 		sin = (struct sockaddr_inarp *)(rtm + 1);
    396   1.5  mycroft 		sdl = (struct sockaddr_dl *)(sin + 1);
    397   1.5  mycroft 		if (addr) {
    398   1.5  mycroft 			if (addr != sin->sin_addr.s_addr)
    399   1.5  mycroft 				continue;
    400   1.5  mycroft 			found_entry = 1;
    401   1.5  mycroft 		}
    402   1.5  mycroft 		if (nflag == 0)
    403   1.5  mycroft 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    404   1.5  mycroft 			    sizeof sin->sin_addr, AF_INET);
    405   1.1      cgd 		else
    406   1.1      cgd 			hp = 0;
    407   1.1      cgd 		if (hp)
    408   1.1      cgd 			host = hp->h_name;
    409   1.1      cgd 		else {
    410   1.1      cgd 			host = "?";
    411   1.1      cgd 			if (h_errno == TRY_AGAIN)
    412   1.5  mycroft 				nflag = 1;
    413   1.1      cgd 		}
    414   1.8   chopps 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    415   1.5  mycroft 		if (sdl->sdl_alen)
    416   1.5  mycroft 			ether_print(LLADDR(sdl));
    417   1.1      cgd 		else
    418   1.8   chopps 			(void)printf("(incomplete)");
    419   1.5  mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    420   1.8   chopps 			(void)printf(" permanent");
    421   1.5  mycroft 		if (sin->sin_other & SIN_PROXY)
    422   1.8   chopps 			(void)printf(" published (proxy only)");
    423   1.5  mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    424   1.5  mycroft 			sin = (struct sockaddr_inarp *)
    425   1.5  mycroft 				(sdl->sdl_len + (char *)sdl);
    426   1.5  mycroft 			if (sin->sin_addr.s_addr == 0xffffffff)
    427   1.8   chopps 				(void)printf(" published");
    428   1.5  mycroft 			if (sin->sin_len != 8)
    429   1.8   chopps 				(void)printf("(wierd)");
    430   1.5  mycroft 		}
    431   1.8   chopps 		(void)printf("\n");
    432   1.1      cgd 	}
    433   1.1      cgd }
    434   1.1      cgd 
    435   1.8   chopps void
    436   1.1      cgd ether_print(cp)
    437   1.8   chopps 	const u_char *cp;
    438   1.1      cgd {
    439   1.8   chopps 	(void)printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4],
    440   1.8   chopps 	    cp[5]);
    441   1.1      cgd }
    442   1.1      cgd 
    443   1.8   chopps void
    444   1.1      cgd usage()
    445   1.1      cgd {
    446   1.8   chopps 	(void)fprintf(stderr, "usage: arp [-n] hostname\n");
    447   1.8   chopps 	(void)fprintf(stderr, "usage: arp [-n] -a\n");
    448   1.8   chopps 	(void)fprintf(stderr, "usage: arp -d hostname\n");
    449   1.8   chopps 	(void)fprintf(stderr,
    450   1.8   chopps 	    "usage: arp -s hostname ether_addr [temp] [pub]\n");
    451   1.8   chopps 	(void)fprintf(stderr, "usage: arp -f filename\n");
    452   1.5  mycroft 	exit(1);
    453   1.5  mycroft }
    454   1.5  mycroft 
    455   1.8   chopps int
    456   1.5  mycroft rtmsg(cmd)
    457   1.8   chopps 	int cmd;
    458   1.5  mycroft {
    459   1.5  mycroft 	static int seq;
    460   1.5  mycroft 	int rlen;
    461   1.8   chopps 	register struct rt_msghdr *rtm;
    462   1.8   chopps 	register char *cp;
    463   1.5  mycroft 	register int l;
    464   1.5  mycroft 
    465   1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    466   1.8   chopps 	cp = m_rtmsg.m_space;
    467   1.5  mycroft 	errno = 0;
    468   1.8   chopps 
    469   1.5  mycroft 	if (cmd == RTM_DELETE)
    470   1.5  mycroft 		goto doit;
    471   1.8   chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    472   1.5  mycroft 	rtm->rtm_flags = flags;
    473   1.5  mycroft 	rtm->rtm_version = RTM_VERSION;
    474   1.5  mycroft 
    475   1.5  mycroft 	switch (cmd) {
    476   1.5  mycroft 	default:
    477   1.8   chopps 		errx(1, "internal wrong cmd");
    478   1.8   chopps 		/*NOTREACHED*/
    479   1.5  mycroft 	case RTM_ADD:
    480   1.5  mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    481   1.5  mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    482   1.5  mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    483   1.5  mycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    484   1.5  mycroft 		sin_m.sin_other = 0;
    485   1.5  mycroft 		if (doing_proxy) {
    486   1.5  mycroft 			if (export_only)
    487   1.5  mycroft 				sin_m.sin_other = SIN_PROXY;
    488   1.5  mycroft 			else {
    489   1.5  mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    490   1.5  mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    491   1.5  mycroft 			}
    492   1.5  mycroft 		}
    493   1.5  mycroft 		/* FALLTHROUGH */
    494   1.5  mycroft 	case RTM_GET:
    495   1.5  mycroft 		rtm->rtm_addrs |= RTA_DST;
    496   1.5  mycroft 	}
    497   1.5  mycroft #define NEXTADDR(w, s) \
    498   1.5  mycroft 	if (rtm->rtm_addrs & (w)) { \
    499   1.8   chopps 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    500   1.5  mycroft 
    501   1.5  mycroft 	NEXTADDR(RTA_DST, sin_m);
    502   1.5  mycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
    503   1.5  mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    504   1.5  mycroft 
    505   1.5  mycroft 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    506   1.5  mycroft doit:
    507   1.5  mycroft 	l = rtm->rtm_msglen;
    508   1.5  mycroft 	rtm->rtm_seq = ++seq;
    509   1.5  mycroft 	rtm->rtm_type = cmd;
    510   1.5  mycroft 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    511   1.5  mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    512   1.8   chopps 			warn("writing to routing socket");
    513   1.5  mycroft 			return (-1);
    514   1.5  mycroft 		}
    515   1.5  mycroft 	}
    516   1.5  mycroft 	do {
    517   1.5  mycroft 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    518   1.5  mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    519   1.5  mycroft 	if (l < 0)
    520   1.8   chopps 		warn("read from routing socket");
    521   1.5  mycroft 	return (0);
    522   1.5  mycroft }
    523   1.5  mycroft 
    524   1.8   chopps int
    525   1.8   chopps getinetaddr(host, inap)
    526   1.8   chopps 	const char *host;
    527   1.8   chopps 	struct in_addr *inap;
    528   1.5  mycroft {
    529   1.8   chopps 	struct hostent *hp;
    530   1.8   chopps 
    531   1.9   chopps 	if (inet_aton(host, inap) == 1)
    532   1.8   chopps 		return (0);
    533   1.8   chopps 	if ((hp = gethostbyname(host)) == NULL) {
    534  1.13  mycroft 		warnx("%s: %s\n", host, hstrerror(h_errno));
    535   1.8   chopps 		return (-1);
    536   1.8   chopps 	}
    537   1.8   chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    538   1.8   chopps 	return (0);
    539   1.1      cgd }
    540