Home | History | Annotate | Line # | Download | only in arp
arp.c revision 1.12
      1  1.12      cgd /*	$NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd 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.12      cgd static char *rcsid = "$NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd 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.5  mycroft #include <netdb.h>
     68   1.1      cgd #include <errno.h>
     69   1.8   chopps #include <err.h>
     70   1.1      cgd #include <nlist.h>
     71   1.1      cgd #include <stdio.h>
     72   1.8   chopps #include <stdlib.h>
     73  1.12      cgd #include <string.h>
     74   1.1      cgd #include <paths.h>
     75   1.1      cgd 
     76   1.8   chopps int delete __P((const char *, const char *));
     77   1.8   chopps void dump __P((u_long));
     78   1.8   chopps int ether_aton __P((const char *, u_char *));
     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.1      cgd 
     99   1.5  mycroft 	pid = getpid();
    100   1.6   chopps 	while ((ch = getopt(argc, argv, "andsf")) != EOF)
    101   1.1      cgd 		switch((char)ch) {
    102   1.5  mycroft 		case 'a':
    103   1.5  mycroft 			dump(0);
    104   1.8   chopps 			return (0);
    105   1.1      cgd 		case 'd':
    106   1.5  mycroft 			if (argc < 3 || argc > 4)
    107   1.1      cgd 				usage();
    108   1.8   chopps 			(void)delete(argv[2], argv[3]);
    109   1.8   chopps 			return (0);
    110   1.5  mycroft 		case 'n':
    111   1.5  mycroft 			nflag = 1;
    112   1.8   chopps 			break;
    113   1.1      cgd 		case 's':
    114   1.1      cgd 			if (argc < 4 || argc > 7)
    115   1.1      cgd 				usage();
    116   1.8   chopps 			return (set(argc-2, &argv[2]) ? 1 : 0);
    117   1.6   chopps 		case 'f':
    118   1.6   chopps 			if (argc != 3)
    119   1.6   chopps 				usage();
    120   1.8   chopps 			return (file(argv[2]));
    121   1.1      cgd 		case '?':
    122   1.1      cgd 		default:
    123   1.1      cgd 			usage();
    124   1.1      cgd 		}
    125   1.8   chopps 	if (argc == 2 || (argc == 3 && nflag))
    126   1.8   chopps 		get(argv[argc - 1]);
    127   1.8   chopps 	else
    128   1.1      cgd 		usage();
    129   1.8   chopps 	return (0);
    130   1.1      cgd }
    131   1.1      cgd 
    132   1.1      cgd /*
    133   1.1      cgd  * Process a file to set standard arp entries
    134   1.1      cgd  */
    135   1.8   chopps int
    136   1.1      cgd file(name)
    137   1.1      cgd 	char *name;
    138   1.1      cgd {
    139   1.8   chopps 	char line[100], arg[5][50], *args[5];
    140   1.8   chopps 	int i, retval;
    141   1.1      cgd 	FILE *fp;
    142   1.1      cgd 
    143   1.8   chopps 	if ((fp = fopen(name, "r")) == NULL)
    144   1.8   chopps 		err(1, "cannot open %s", name);
    145   1.1      cgd 	args[0] = &arg[0][0];
    146   1.1      cgd 	args[1] = &arg[1][0];
    147   1.1      cgd 	args[2] = &arg[2][0];
    148   1.1      cgd 	args[3] = &arg[3][0];
    149   1.1      cgd 	args[4] = &arg[4][0];
    150   1.1      cgd 	retval = 0;
    151   1.8   chopps 	while (fgets(line, 100, fp) != NULL) {
    152   1.1      cgd 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
    153   1.1      cgd 		    arg[3], arg[4]);
    154   1.1      cgd 		if (i < 2) {
    155   1.8   chopps 			warnx("bad line: %s", line);
    156   1.1      cgd 			retval = 1;
    157   1.1      cgd 			continue;
    158   1.1      cgd 		}
    159   1.1      cgd 		if (set(i, args))
    160   1.1      cgd 			retval = 1;
    161   1.1      cgd 	}
    162   1.1      cgd 	fclose(fp);
    163   1.1      cgd 	return (retval);
    164   1.1      cgd }
    165   1.1      cgd 
    166   1.8   chopps void
    167   1.8   chopps getsocket()
    168   1.8   chopps {
    169   1.8   chopps 	if (s >= 0)
    170   1.8   chopps 		return;
    171   1.8   chopps 	s = socket(PF_ROUTE, SOCK_RAW, 0);
    172   1.8   chopps 	if (s < 0)
    173   1.8   chopps 		err(1, "socket");
    174   1.5  mycroft }
    175   1.5  mycroft 
    176   1.5  mycroft struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
    177   1.5  mycroft struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
    178   1.5  mycroft struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
    179   1.5  mycroft int	expire_time, flags, export_only, doing_proxy, found_entry;
    180   1.5  mycroft struct	{
    181   1.5  mycroft 	struct	rt_msghdr m_rtm;
    182   1.5  mycroft 	char	m_space[512];
    183   1.5  mycroft }	m_rtmsg;
    184   1.5  mycroft 
    185   1.1      cgd /*
    186   1.1      cgd  * Set an individual arp entry
    187   1.1      cgd  */
    188   1.8   chopps int
    189   1.1      cgd set(argc, argv)
    190   1.1      cgd 	int argc;
    191   1.1      cgd 	char **argv;
    192   1.1      cgd {
    193   1.8   chopps 	register struct sockaddr_inarp *sin;
    194   1.5  mycroft 	register struct sockaddr_dl *sdl;
    195   1.8   chopps 	register struct rt_msghdr *rtm;
    196   1.1      cgd 	u_char *ea;
    197   1.8   chopps 	char *host = argv[0], *eaddr;
    198   1.8   chopps 
    199   1.8   chopps 	sin = &sin_m;
    200   1.8   chopps 	rtm = &(m_rtmsg.m_rtm);
    201   1.8   chopps 	eaddr = argv[1];
    202   1.1      cgd 
    203   1.5  mycroft 	getsocket();
    204   1.1      cgd 	argc -= 2;
    205   1.1      cgd 	argv += 2;
    206   1.8   chopps 	sdl_m = blank_sdl;		/* struct copy */
    207   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    208   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    209   1.8   chopps 		return (1);
    210   1.5  mycroft 	ea = (u_char *)LLADDR(&sdl_m);
    211   1.5  mycroft 	if (ether_aton(eaddr, ea) == 0)
    212   1.5  mycroft 		sdl_m.sdl_alen = 6;
    213   1.5  mycroft 	doing_proxy = flags = export_only = expire_time = 0;
    214   1.1      cgd 	while (argc-- > 0) {
    215   1.5  mycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
    216   1.5  mycroft 			struct timeval time;
    217   1.8   chopps 			(void)gettimeofday(&time, 0);
    218   1.5  mycroft 			expire_time = time.tv_sec + 20 * 60;
    219   1.5  mycroft 		}
    220   1.5  mycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
    221   1.5  mycroft 			flags |= RTF_ANNOUNCE;
    222   1.5  mycroft 			doing_proxy = SIN_PROXY;
    223   1.5  mycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
    224   1.8   chopps 			(void)printf(
    225   1.8   chopps 			    "%s: Sending trailers is no longer supported\n",
    226   1.8   chopps 			     host);
    227   1.5  mycroft 		}
    228   1.1      cgd 		argv++;
    229   1.1      cgd 	}
    230   1.5  mycroft tryagain:
    231   1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    232   1.8   chopps 		warn("%s", host);
    233   1.5  mycroft 		return (1);
    234   1.1      cgd 	}
    235   1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    236   1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    237   1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    238   1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    239   1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    240   1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    241   1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    242   1.5  mycroft 		case IFT_ISO88024: case IFT_ISO88025:
    243   1.5  mycroft 			goto overwrite;
    244   1.5  mycroft 		}
    245   1.5  mycroft 		if (doing_proxy == 0) {
    246   1.8   chopps 			(void)printf("set: can only proxy for %s\n", host);
    247   1.5  mycroft 			return (1);
    248   1.5  mycroft 		}
    249   1.5  mycroft 		if (sin_m.sin_other & SIN_PROXY) {
    250   1.8   chopps 			(void)printf(
    251   1.8   chopps 			    "set: proxy entry exists for non 802 device\n");
    252   1.8   chopps 			return (1);
    253   1.5  mycroft 		}
    254   1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    255   1.5  mycroft 		export_only = 1;
    256   1.5  mycroft 		goto tryagain;
    257   1.5  mycroft 	}
    258   1.5  mycroft overwrite:
    259   1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    260   1.8   chopps 		(void)printf("cannot intuit interface index and type for %s\n",
    261   1.8   chopps 		    host);
    262   1.5  mycroft 		return (1);
    263   1.1      cgd 	}
    264   1.5  mycroft 	sdl_m.sdl_type = sdl->sdl_type;
    265   1.5  mycroft 	sdl_m.sdl_index = sdl->sdl_index;
    266   1.5  mycroft 	return (rtmsg(RTM_ADD));
    267   1.1      cgd }
    268   1.1      cgd 
    269   1.1      cgd /*
    270   1.1      cgd  * Display an individual arp entry
    271   1.1      cgd  */
    272   1.8   chopps void
    273   1.1      cgd get(host)
    274   1.8   chopps 	const char *host;
    275   1.1      cgd {
    276   1.8   chopps 	struct sockaddr_inarp *sin;
    277   1.1      cgd 	u_char *ea;
    278   1.1      cgd 
    279   1.8   chopps 	sin = &sin_m;
    280   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    281   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    282   1.8   chopps 		exit(1);
    283   1.5  mycroft 	dump(sin->sin_addr.s_addr);
    284   1.5  mycroft 	if (found_entry == 0) {
    285   1.8   chopps 		(void)printf("%s (%s) -- no entry\n", host,
    286   1.8   chopps 		    inet_ntoa(sin->sin_addr));
    287   1.1      cgd 		exit(1);
    288   1.1      cgd 	}
    289   1.1      cgd }
    290   1.1      cgd 
    291   1.1      cgd /*
    292   1.1      cgd  * Delete an arp entry
    293   1.1      cgd  */
    294   1.8   chopps int
    295   1.5  mycroft delete(host, info)
    296   1.8   chopps 	const char *host;
    297   1.8   chopps 	const char *info;
    298   1.1      cgd {
    299   1.8   chopps 	register struct sockaddr_inarp *sin;
    300   1.8   chopps 	register struct rt_msghdr *rtm;
    301   1.5  mycroft 	struct sockaddr_dl *sdl;
    302   1.5  mycroft 	u_char *ea;
    303   1.5  mycroft 	char *eaddr;
    304   1.1      cgd 
    305   1.8   chopps 	sin = &sin_m;
    306   1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    307   1.8   chopps 
    308   1.5  mycroft 	if (info && strncmp(info, "pro", 3) )
    309   1.5  mycroft 		export_only = 1;
    310   1.5  mycroft 	getsocket();
    311   1.8   chopps 	sin_m = blank_sin;		/* struct copy */
    312   1.8   chopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
    313   1.8   chopps 		return (1);
    314   1.5  mycroft tryagain:
    315   1.5  mycroft 	if (rtmsg(RTM_GET) < 0) {
    316   1.8   chopps 		warn("%s", host);
    317   1.5  mycroft 		return (1);
    318   1.5  mycroft 	}
    319   1.5  mycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
    320   1.5  mycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
    321   1.5  mycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
    322   1.5  mycroft 		if (sdl->sdl_family == AF_LINK &&
    323   1.5  mycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
    324   1.5  mycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
    325   1.5  mycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
    326   1.5  mycroft 		case IFT_ISO88024: case IFT_ISO88025:
    327   1.5  mycroft 			goto delete;
    328   1.5  mycroft 		}
    329   1.1      cgd 	}
    330   1.5  mycroft 	if (sin_m.sin_other & SIN_PROXY) {
    331   1.8   chopps 		warnx("delete: can't locate %s", host);
    332   1.5  mycroft 		return (1);
    333   1.5  mycroft 	} else {
    334   1.5  mycroft 		sin_m.sin_other = SIN_PROXY;
    335   1.5  mycroft 		goto tryagain;
    336   1.5  mycroft 	}
    337   1.5  mycroft delete:
    338   1.5  mycroft 	if (sdl->sdl_family != AF_LINK) {
    339   1.8   chopps 		(void)printf("cannot locate %s\n", host);
    340   1.5  mycroft 		return (1);
    341   1.1      cgd 	}
    342   1.8   chopps 	if (rtmsg(RTM_DELETE))
    343   1.8   chopps 		return (1);
    344   1.8   chopps 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
    345   1.8   chopps 	return (0);
    346   1.1      cgd }
    347   1.1      cgd 
    348   1.1      cgd /*
    349   1.1      cgd  * Dump the entire arp table
    350   1.1      cgd  */
    351   1.8   chopps void
    352   1.5  mycroft dump(addr)
    353   1.8   chopps 	u_long addr;
    354   1.1      cgd {
    355   1.5  mycroft 	int mib[6];
    356   1.5  mycroft 	size_t needed;
    357   1.8   chopps 	char *host, *lim, *buf, *next;
    358   1.5  mycroft 	struct rt_msghdr *rtm;
    359   1.5  mycroft 	struct sockaddr_inarp *sin;
    360   1.5  mycroft 	struct sockaddr_dl *sdl;
    361   1.1      cgd 	extern int h_errno;
    362   1.1      cgd 	struct hostent *hp;
    363   1.1      cgd 
    364   1.5  mycroft 	mib[0] = CTL_NET;
    365   1.5  mycroft 	mib[1] = PF_ROUTE;
    366   1.5  mycroft 	mib[2] = 0;
    367   1.5  mycroft 	mib[3] = AF_INET;
    368   1.5  mycroft 	mib[4] = NET_RT_FLAGS;
    369   1.5  mycroft 	mib[5] = RTF_LLINFO;
    370   1.5  mycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
    371   1.8   chopps 		err(1, "route-sysctl-estimate");
    372   1.5  mycroft 	if ((buf = malloc(needed)) == NULL)
    373   1.8   chopps 		err(1, "malloc");
    374   1.5  mycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
    375   1.8   chopps 		err(1, "actual retrieval of routing table");
    376   1.5  mycroft 	lim = buf + needed;
    377   1.5  mycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
    378   1.5  mycroft 		rtm = (struct rt_msghdr *)next;
    379   1.5  mycroft 		sin = (struct sockaddr_inarp *)(rtm + 1);
    380   1.5  mycroft 		sdl = (struct sockaddr_dl *)(sin + 1);
    381   1.5  mycroft 		if (addr) {
    382   1.5  mycroft 			if (addr != sin->sin_addr.s_addr)
    383   1.5  mycroft 				continue;
    384   1.5  mycroft 			found_entry = 1;
    385   1.5  mycroft 		}
    386   1.5  mycroft 		if (nflag == 0)
    387   1.5  mycroft 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
    388   1.5  mycroft 			    sizeof sin->sin_addr, AF_INET);
    389   1.1      cgd 		else
    390   1.1      cgd 			hp = 0;
    391   1.1      cgd 		if (hp)
    392   1.1      cgd 			host = hp->h_name;
    393   1.1      cgd 		else {
    394   1.1      cgd 			host = "?";
    395   1.1      cgd 			if (h_errno == TRY_AGAIN)
    396   1.5  mycroft 				nflag = 1;
    397   1.1      cgd 		}
    398   1.8   chopps 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
    399   1.5  mycroft 		if (sdl->sdl_alen)
    400   1.5  mycroft 			ether_print(LLADDR(sdl));
    401   1.1      cgd 		else
    402   1.8   chopps 			(void)printf("(incomplete)");
    403   1.5  mycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
    404   1.8   chopps 			(void)printf(" permanent");
    405   1.5  mycroft 		if (sin->sin_other & SIN_PROXY)
    406   1.8   chopps 			(void)printf(" published (proxy only)");
    407   1.5  mycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
    408   1.5  mycroft 			sin = (struct sockaddr_inarp *)
    409   1.5  mycroft 				(sdl->sdl_len + (char *)sdl);
    410   1.5  mycroft 			if (sin->sin_addr.s_addr == 0xffffffff)
    411   1.8   chopps 				(void)printf(" published");
    412   1.5  mycroft 			if (sin->sin_len != 8)
    413   1.8   chopps 				(void)printf("(wierd)");
    414   1.5  mycroft 		}
    415   1.8   chopps 		(void)printf("\n");
    416   1.1      cgd 	}
    417   1.1      cgd }
    418   1.1      cgd 
    419   1.8   chopps void
    420   1.1      cgd ether_print(cp)
    421   1.8   chopps 	const u_char *cp;
    422   1.1      cgd {
    423   1.8   chopps 	(void)printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4],
    424   1.8   chopps 	    cp[5]);
    425   1.1      cgd }
    426   1.1      cgd 
    427   1.8   chopps int
    428   1.1      cgd ether_aton(a, n)
    429   1.8   chopps 	const char *a;
    430   1.1      cgd 	u_char *n;
    431   1.1      cgd {
    432   1.1      cgd 	int i, o[6];
    433   1.1      cgd 
    434   1.8   chopps 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], &o[3], &o[4],
    435   1.8   chopps 	    &o[5]);
    436   1.1      cgd 	if (i != 6) {
    437   1.8   chopps 		warnx("invalid Ethernet address '%s'", a);
    438   1.1      cgd 		return (1);
    439   1.1      cgd 	}
    440   1.1      cgd 	for (i=0; i<6; i++)
    441   1.1      cgd 		n[i] = o[i];
    442   1.1      cgd 	return (0);
    443   1.1      cgd }
    444   1.1      cgd 
    445   1.8   chopps void
    446   1.1      cgd usage()
    447   1.1      cgd {
    448   1.8   chopps 	(void)fprintf(stderr, "usage: arp [-n] hostname\n");
    449   1.8   chopps 	(void)fprintf(stderr, "usage: arp [-n] -a\n");
    450   1.8   chopps 	(void)fprintf(stderr, "usage: arp -d hostname\n");
    451   1.8   chopps 	(void)fprintf(stderr,
    452   1.8   chopps 	    "usage: arp -s hostname ether_addr [temp] [pub]\n");
    453   1.8   chopps 	(void)fprintf(stderr, "usage: arp -f filename\n");
    454   1.5  mycroft 	exit(1);
    455   1.5  mycroft }
    456   1.5  mycroft 
    457   1.8   chopps int
    458   1.5  mycroft rtmsg(cmd)
    459   1.8   chopps 	int cmd;
    460   1.5  mycroft {
    461   1.5  mycroft 	static int seq;
    462   1.5  mycroft 	int rlen;
    463   1.8   chopps 	register struct rt_msghdr *rtm;
    464   1.8   chopps 	register char *cp;
    465   1.5  mycroft 	register int l;
    466   1.5  mycroft 
    467   1.8   chopps 	rtm = &m_rtmsg.m_rtm;
    468   1.8   chopps 	cp = m_rtmsg.m_space;
    469   1.5  mycroft 	errno = 0;
    470   1.8   chopps 
    471   1.5  mycroft 	if (cmd == RTM_DELETE)
    472   1.5  mycroft 		goto doit;
    473   1.8   chopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
    474   1.5  mycroft 	rtm->rtm_flags = flags;
    475   1.5  mycroft 	rtm->rtm_version = RTM_VERSION;
    476   1.5  mycroft 
    477   1.5  mycroft 	switch (cmd) {
    478   1.5  mycroft 	default:
    479   1.8   chopps 		errx(1, "internal wrong cmd");
    480   1.8   chopps 		/*NOTREACHED*/
    481   1.5  mycroft 	case RTM_ADD:
    482   1.5  mycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
    483   1.5  mycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
    484   1.5  mycroft 		rtm->rtm_inits = RTV_EXPIRE;
    485   1.5  mycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
    486   1.5  mycroft 		sin_m.sin_other = 0;
    487   1.5  mycroft 		if (doing_proxy) {
    488   1.5  mycroft 			if (export_only)
    489   1.5  mycroft 				sin_m.sin_other = SIN_PROXY;
    490   1.5  mycroft 			else {
    491   1.5  mycroft 				rtm->rtm_addrs |= RTA_NETMASK;
    492   1.5  mycroft 				rtm->rtm_flags &= ~RTF_HOST;
    493   1.5  mycroft 			}
    494   1.5  mycroft 		}
    495   1.5  mycroft 		/* FALLTHROUGH */
    496   1.5  mycroft 	case RTM_GET:
    497   1.5  mycroft 		rtm->rtm_addrs |= RTA_DST;
    498   1.5  mycroft 	}
    499   1.5  mycroft #define NEXTADDR(w, s) \
    500   1.5  mycroft 	if (rtm->rtm_addrs & (w)) { \
    501   1.8   chopps 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
    502   1.5  mycroft 
    503   1.5  mycroft 	NEXTADDR(RTA_DST, sin_m);
    504   1.5  mycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
    505   1.5  mycroft 	NEXTADDR(RTA_NETMASK, so_mask);
    506   1.5  mycroft 
    507   1.5  mycroft 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
    508   1.5  mycroft doit:
    509   1.5  mycroft 	l = rtm->rtm_msglen;
    510   1.5  mycroft 	rtm->rtm_seq = ++seq;
    511   1.5  mycroft 	rtm->rtm_type = cmd;
    512   1.5  mycroft 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
    513   1.5  mycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
    514   1.8   chopps 			warn("writing to routing socket");
    515   1.5  mycroft 			return (-1);
    516   1.5  mycroft 		}
    517   1.5  mycroft 	}
    518   1.5  mycroft 	do {
    519   1.5  mycroft 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
    520   1.5  mycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
    521   1.5  mycroft 	if (l < 0)
    522   1.8   chopps 		warn("read from routing socket");
    523   1.5  mycroft 	return (0);
    524   1.5  mycroft }
    525   1.5  mycroft 
    526   1.8   chopps int
    527   1.8   chopps getinetaddr(host, inap)
    528   1.8   chopps 	const char *host;
    529   1.8   chopps 	struct in_addr *inap;
    530   1.5  mycroft {
    531   1.8   chopps 	extern char *__progname;	/* Program name, from crt0. */
    532   1.8   chopps 	struct hostent *hp;
    533   1.8   chopps 	u_long addr;
    534   1.8   chopps 
    535   1.9   chopps 	if (inet_aton(host, inap) == 1)
    536   1.8   chopps 		return (0);
    537   1.8   chopps 	if ((hp = gethostbyname(host)) == NULL) {
    538   1.8   chopps 		(void)fprintf(stderr, "%s: %s: ", __progname, host);
    539   1.8   chopps 		herror(NULL);
    540   1.8   chopps 		return (-1);
    541   1.8   chopps 	}
    542   1.8   chopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
    543   1.8   chopps 	return (0);
    544   1.1      cgd }
    545