xref: /netbsd/usr.sbin/arp/arp.c (revision 867b4b0f)
1*867b4b0fScgd /*	$NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd Exp $ */
27f624aaaSchopps 
361f28255Scgd /*
417c454feSmycroft  * Copyright (c) 1984, 1993
517c454feSmycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Sun Microsystems, Inc.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
1861f28255Scgd  * 3. All advertising materials mentioning features or use of this software
1961f28255Scgd  *    must display the following acknowledgement:
2061f28255Scgd  *	This product includes software developed by the University of
2161f28255Scgd  *	California, Berkeley and its contributors.
2261f28255Scgd  * 4. Neither the name of the University nor the names of its contributors
2361f28255Scgd  *    may be used to endorse or promote products derived from this software
2461f28255Scgd  *    without specific prior written permission.
2561f28255Scgd  *
2661f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2761f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2861f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2961f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3061f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3161f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3261f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3361f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3461f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3561f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3661f28255Scgd  * SUCH DAMAGE.
3761f28255Scgd  */
3861f28255Scgd 
3961f28255Scgd #ifndef lint
4017c454feSmycroft static char copyright[] =
4117c454feSmycroft "@(#) Copyright (c) 1984, 1993\n\
4217c454feSmycroft 	The Regents of the University of California.  All rights reserved.\n";
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd #ifndef lint
4617c454feSmycroft /*static char sccsid[] = "from: @(#)arp.c	8.2 (Berkeley) 1/2/94";*/
47*867b4b0fScgd static char *rcsid = "$NetBSD: arp.c,v 1.12 1995/04/24 13:25:18 cgd Exp $";
4861f28255Scgd #endif /* not lint */
4961f28255Scgd 
5061f28255Scgd /*
5161f28255Scgd  * arp - display, set, and delete arp table entries
5261f28255Scgd  */
5361f28255Scgd 
5461f28255Scgd #include <sys/param.h>
5561f28255Scgd #include <sys/file.h>
5661f28255Scgd #include <sys/socket.h>
5717c454feSmycroft #include <sys/sysctl.h>
5861f28255Scgd 
5961f28255Scgd #include <net/if.h>
6017c454feSmycroft #include <net/if_dl.h>
6117c454feSmycroft #include <net/if_types.h>
6217c454feSmycroft #include <net/route.h>
6317c454feSmycroft #include <netinet/in.h>
6461f28255Scgd #include <netinet/if_ether.h>
65804c4234Smycroft #include <arpa/inet.h>
6661f28255Scgd 
6717c454feSmycroft #include <netdb.h>
6861f28255Scgd #include <errno.h>
69c4516a41Schopps #include <err.h>
7061f28255Scgd #include <nlist.h>
7161f28255Scgd #include <stdio.h>
72c4516a41Schopps #include <stdlib.h>
73*867b4b0fScgd #include <string.h>
7461f28255Scgd #include <paths.h>
7561f28255Scgd 
76c4516a41Schopps int delete __P((const char *, const char *));
77c4516a41Schopps void dump __P((u_long));
78c4516a41Schopps int ether_aton __P((const char *, u_char *));
79c4516a41Schopps void ether_print __P((const u_char *));
80c4516a41Schopps int file __P((char *));
81c4516a41Schopps void get __P((const char *));
82c4516a41Schopps int getinetaddr __P((const char *, struct in_addr *));
83c4516a41Schopps void getsocket __P((void));
84c4516a41Schopps int rtmsg __P((int));
85c4516a41Schopps int set __P((int, char **));
86c4516a41Schopps void usage __P((void));
87c4516a41Schopps 
8817c454feSmycroft static int pid;
8917c454feSmycroft static int nflag;
9017c454feSmycroft static int s = -1;
9161f28255Scgd 
92c4516a41Schopps int
9361f28255Scgd main(argc, argv)
9461f28255Scgd 	int argc;
9561f28255Scgd 	char **argv;
9661f28255Scgd {
9761f28255Scgd 	int ch;
9861f28255Scgd 
9917c454feSmycroft 	pid = getpid();
1001aad640dSchopps 	while ((ch = getopt(argc, argv, "andsf")) != EOF)
10161f28255Scgd 		switch((char)ch) {
10217c454feSmycroft 		case 'a':
10317c454feSmycroft 			dump(0);
104c4516a41Schopps 			return (0);
10561f28255Scgd 		case 'd':
10617c454feSmycroft 			if (argc < 3 || argc > 4)
10761f28255Scgd 				usage();
108c4516a41Schopps 			(void)delete(argv[2], argv[3]);
109c4516a41Schopps 			return (0);
11017c454feSmycroft 		case 'n':
11117c454feSmycroft 			nflag = 1;
112c4516a41Schopps 			break;
11361f28255Scgd 		case 's':
11461f28255Scgd 			if (argc < 4 || argc > 7)
11561f28255Scgd 				usage();
116c4516a41Schopps 			return (set(argc-2, &argv[2]) ? 1 : 0);
1171aad640dSchopps 		case 'f':
1181aad640dSchopps 			if (argc != 3)
1191aad640dSchopps 				usage();
120c4516a41Schopps 			return (file(argv[2]));
12161f28255Scgd 		case '?':
12261f28255Scgd 		default:
12361f28255Scgd 			usage();
12461f28255Scgd 		}
125c4516a41Schopps 	if (argc == 2 || (argc == 3 && nflag))
126c4516a41Schopps 		get(argv[argc - 1]);
127c4516a41Schopps 	else
12861f28255Scgd 		usage();
129c4516a41Schopps 	return (0);
13061f28255Scgd }
13161f28255Scgd 
13261f28255Scgd /*
13361f28255Scgd  * Process a file to set standard arp entries
13461f28255Scgd  */
135c4516a41Schopps int
13661f28255Scgd file(name)
13761f28255Scgd 	char *name;
13861f28255Scgd {
13961f28255Scgd 	char line[100], arg[5][50], *args[5];
140c4516a41Schopps 	int i, retval;
141c4516a41Schopps 	FILE *fp;
14261f28255Scgd 
143c4516a41Schopps 	if ((fp = fopen(name, "r")) == NULL)
144c4516a41Schopps 		err(1, "cannot open %s", name);
14561f28255Scgd 	args[0] = &arg[0][0];
14661f28255Scgd 	args[1] = &arg[1][0];
14761f28255Scgd 	args[2] = &arg[2][0];
14861f28255Scgd 	args[3] = &arg[3][0];
14961f28255Scgd 	args[4] = &arg[4][0];
15061f28255Scgd 	retval = 0;
15161f28255Scgd 	while (fgets(line, 100, fp) != NULL) {
15261f28255Scgd 		i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
15361f28255Scgd 		    arg[3], arg[4]);
15461f28255Scgd 		if (i < 2) {
155c4516a41Schopps 			warnx("bad line: %s", line);
15661f28255Scgd 			retval = 1;
15761f28255Scgd 			continue;
15861f28255Scgd 		}
15961f28255Scgd 		if (set(i, args))
16061f28255Scgd 			retval = 1;
16161f28255Scgd 	}
16261f28255Scgd 	fclose(fp);
16361f28255Scgd 	return (retval);
16461f28255Scgd }
16561f28255Scgd 
166c4516a41Schopps void
167c4516a41Schopps getsocket()
168c4516a41Schopps {
169c4516a41Schopps 	if (s >= 0)
170c4516a41Schopps 		return;
17117c454feSmycroft 	s = socket(PF_ROUTE, SOCK_RAW, 0);
172c4516a41Schopps 	if (s < 0)
173c4516a41Schopps 		err(1, "socket");
17417c454feSmycroft }
17517c454feSmycroft 
17617c454feSmycroft struct	sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
17717c454feSmycroft struct	sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
17817c454feSmycroft struct	sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
17917c454feSmycroft int	expire_time, flags, export_only, doing_proxy, found_entry;
18017c454feSmycroft struct	{
18117c454feSmycroft 	struct	rt_msghdr m_rtm;
18217c454feSmycroft 	char	m_space[512];
18317c454feSmycroft }	m_rtmsg;
18417c454feSmycroft 
18561f28255Scgd /*
18661f28255Scgd  * Set an individual arp entry
18761f28255Scgd  */
188c4516a41Schopps int
18961f28255Scgd set(argc, argv)
19061f28255Scgd 	int argc;
19161f28255Scgd 	char **argv;
19261f28255Scgd {
193c4516a41Schopps 	register struct sockaddr_inarp *sin;
19417c454feSmycroft 	register struct sockaddr_dl *sdl;
195c4516a41Schopps 	register struct rt_msghdr *rtm;
19661f28255Scgd 	u_char *ea;
197c4516a41Schopps 	char *host = argv[0], *eaddr;
198c4516a41Schopps 
199c4516a41Schopps 	sin = &sin_m;
200c4516a41Schopps 	rtm = &(m_rtmsg.m_rtm);
201c4516a41Schopps 	eaddr = argv[1];
20261f28255Scgd 
20317c454feSmycroft 	getsocket();
20461f28255Scgd 	argc -= 2;
20561f28255Scgd 	argv += 2;
206c4516a41Schopps 	sdl_m = blank_sdl;		/* struct copy */
207c4516a41Schopps 	sin_m = blank_sin;		/* struct copy */
208c4516a41Schopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
20961f28255Scgd 		return (1);
21017c454feSmycroft 	ea = (u_char *)LLADDR(&sdl_m);
21117c454feSmycroft 	if (ether_aton(eaddr, ea) == 0)
21217c454feSmycroft 		sdl_m.sdl_alen = 6;
21317c454feSmycroft 	doing_proxy = flags = export_only = expire_time = 0;
21461f28255Scgd 	while (argc-- > 0) {
21517c454feSmycroft 		if (strncmp(argv[0], "temp", 4) == 0) {
21617c454feSmycroft 			struct timeval time;
217c4516a41Schopps 			(void)gettimeofday(&time, 0);
21817c454feSmycroft 			expire_time = time.tv_sec + 20 * 60;
21917c454feSmycroft 		}
22017c454feSmycroft 		else if (strncmp(argv[0], "pub", 3) == 0) {
22117c454feSmycroft 			flags |= RTF_ANNOUNCE;
22217c454feSmycroft 			doing_proxy = SIN_PROXY;
22317c454feSmycroft 		} else if (strncmp(argv[0], "trail", 5) == 0) {
224c4516a41Schopps 			(void)printf(
225c4516a41Schopps 			    "%s: Sending trailers is no longer supported\n",
22617c454feSmycroft 			     host);
22717c454feSmycroft 		}
22861f28255Scgd 		argv++;
22961f28255Scgd 	}
23017c454feSmycroft tryagain:
23117c454feSmycroft 	if (rtmsg(RTM_GET) < 0) {
232c4516a41Schopps 		warn("%s", host);
23317c454feSmycroft 		return (1);
23461f28255Scgd 	}
23517c454feSmycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
23617c454feSmycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
23717c454feSmycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
23817c454feSmycroft 		if (sdl->sdl_family == AF_LINK &&
23917c454feSmycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
24017c454feSmycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
24117c454feSmycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
24217c454feSmycroft 		case IFT_ISO88024: case IFT_ISO88025:
24317c454feSmycroft 			goto overwrite;
24417c454feSmycroft 		}
24517c454feSmycroft 		if (doing_proxy == 0) {
246c4516a41Schopps 			(void)printf("set: can only proxy for %s\n", host);
24717c454feSmycroft 			return (1);
24817c454feSmycroft 		}
24917c454feSmycroft 		if (sin_m.sin_other & SIN_PROXY) {
250c4516a41Schopps 			(void)printf(
251c4516a41Schopps 			    "set: proxy entry exists for non 802 device\n");
25217c454feSmycroft 			return (1);
25317c454feSmycroft 		}
25417c454feSmycroft 		sin_m.sin_other = SIN_PROXY;
25517c454feSmycroft 		export_only = 1;
25617c454feSmycroft 		goto tryagain;
25717c454feSmycroft 	}
25817c454feSmycroft overwrite:
25917c454feSmycroft 	if (sdl->sdl_family != AF_LINK) {
260c4516a41Schopps 		(void)printf("cannot intuit interface index and type for %s\n",
261c4516a41Schopps 		    host);
26217c454feSmycroft 		return (1);
26317c454feSmycroft 	}
26417c454feSmycroft 	sdl_m.sdl_type = sdl->sdl_type;
26517c454feSmycroft 	sdl_m.sdl_index = sdl->sdl_index;
26617c454feSmycroft 	return (rtmsg(RTM_ADD));
26761f28255Scgd }
26861f28255Scgd 
26961f28255Scgd /*
27061f28255Scgd  * Display an individual arp entry
27161f28255Scgd  */
272c4516a41Schopps void
27361f28255Scgd get(host)
274c4516a41Schopps 	const char *host;
27561f28255Scgd {
276c4516a41Schopps 	struct sockaddr_inarp *sin;
27761f28255Scgd 	u_char *ea;
27861f28255Scgd 
279c4516a41Schopps 	sin = &sin_m;
280c4516a41Schopps 	sin_m = blank_sin;		/* struct copy */
281c4516a41Schopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
28261f28255Scgd 		exit(1);
28317c454feSmycroft 	dump(sin->sin_addr.s_addr);
28417c454feSmycroft 	if (found_entry == 0) {
285c4516a41Schopps 		(void)printf("%s (%s) -- no entry\n", host,
286c4516a41Schopps 		    inet_ntoa(sin->sin_addr));
28761f28255Scgd 		exit(1);
28861f28255Scgd 	}
28961f28255Scgd }
29061f28255Scgd 
29161f28255Scgd /*
29261f28255Scgd  * Delete an arp entry
29361f28255Scgd  */
294c4516a41Schopps int
29517c454feSmycroft delete(host, info)
296c4516a41Schopps 	const char *host;
297c4516a41Schopps 	const char *info;
29861f28255Scgd {
299c4516a41Schopps 	register struct sockaddr_inarp *sin;
300c4516a41Schopps 	register struct rt_msghdr *rtm;
30117c454feSmycroft 	struct sockaddr_dl *sdl;
30217c454feSmycroft 	u_char *ea;
30317c454feSmycroft 	char *eaddr;
30461f28255Scgd 
305c4516a41Schopps 	sin = &sin_m;
306c4516a41Schopps 	rtm = &m_rtmsg.m_rtm;
307c4516a41Schopps 
30817c454feSmycroft 	if (info && strncmp(info, "pro", 3) )
30917c454feSmycroft 		export_only = 1;
31017c454feSmycroft 	getsocket();
311c4516a41Schopps 	sin_m = blank_sin;		/* struct copy */
312c4516a41Schopps 	if (getinetaddr(host, &sin->sin_addr) == -1)
31317c454feSmycroft 		return (1);
31417c454feSmycroft tryagain:
31517c454feSmycroft 	if (rtmsg(RTM_GET) < 0) {
316c4516a41Schopps 		warn("%s", host);
31717c454feSmycroft 		return (1);
31861f28255Scgd 	}
31917c454feSmycroft 	sin = (struct sockaddr_inarp *)(rtm + 1);
32017c454feSmycroft 	sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
32117c454feSmycroft 	if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
32217c454feSmycroft 		if (sdl->sdl_family == AF_LINK &&
32317c454feSmycroft 		    (rtm->rtm_flags & RTF_LLINFO) &&
32417c454feSmycroft 		    !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
32517c454feSmycroft 		case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
32617c454feSmycroft 		case IFT_ISO88024: case IFT_ISO88025:
32717c454feSmycroft 			goto delete;
32861f28255Scgd 		}
32917c454feSmycroft 	}
33017c454feSmycroft 	if (sin_m.sin_other & SIN_PROXY) {
331c4516a41Schopps 		warnx("delete: can't locate %s", host);
33217c454feSmycroft 		return (1);
33317c454feSmycroft 	} else {
33417c454feSmycroft 		sin_m.sin_other = SIN_PROXY;
33517c454feSmycroft 		goto tryagain;
33617c454feSmycroft 	}
33717c454feSmycroft delete:
33817c454feSmycroft 	if (sdl->sdl_family != AF_LINK) {
339c4516a41Schopps 		(void)printf("cannot locate %s\n", host);
34017c454feSmycroft 		return (1);
34117c454feSmycroft 	}
342c4516a41Schopps 	if (rtmsg(RTM_DELETE))
343c4516a41Schopps 		return (1);
344c4516a41Schopps 	(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
345c4516a41Schopps 	return (0);
34661f28255Scgd }
34761f28255Scgd 
34861f28255Scgd /*
34961f28255Scgd  * Dump the entire arp table
35061f28255Scgd  */
351c4516a41Schopps void
35217c454feSmycroft dump(addr)
35317c454feSmycroft 	u_long addr;
35461f28255Scgd {
35517c454feSmycroft 	int mib[6];
35617c454feSmycroft 	size_t needed;
357c4516a41Schopps 	char *host, *lim, *buf, *next;
35817c454feSmycroft 	struct rt_msghdr *rtm;
35917c454feSmycroft 	struct sockaddr_inarp *sin;
36017c454feSmycroft 	struct sockaddr_dl *sdl;
36161f28255Scgd 	extern int h_errno;
36261f28255Scgd 	struct hostent *hp;
36361f28255Scgd 
36417c454feSmycroft 	mib[0] = CTL_NET;
36517c454feSmycroft 	mib[1] = PF_ROUTE;
36617c454feSmycroft 	mib[2] = 0;
36717c454feSmycroft 	mib[3] = AF_INET;
36817c454feSmycroft 	mib[4] = NET_RT_FLAGS;
36917c454feSmycroft 	mib[5] = RTF_LLINFO;
37017c454feSmycroft 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
371c4516a41Schopps 		err(1, "route-sysctl-estimate");
37217c454feSmycroft 	if ((buf = malloc(needed)) == NULL)
373c4516a41Schopps 		err(1, "malloc");
37417c454feSmycroft 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
375c4516a41Schopps 		err(1, "actual retrieval of routing table");
37617c454feSmycroft 	lim = buf + needed;
37717c454feSmycroft 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
37817c454feSmycroft 		rtm = (struct rt_msghdr *)next;
37917c454feSmycroft 		sin = (struct sockaddr_inarp *)(rtm + 1);
38017c454feSmycroft 		sdl = (struct sockaddr_dl *)(sin + 1);
38117c454feSmycroft 		if (addr) {
38217c454feSmycroft 			if (addr != sin->sin_addr.s_addr)
38361f28255Scgd 				continue;
38417c454feSmycroft 			found_entry = 1;
38517c454feSmycroft 		}
38617c454feSmycroft 		if (nflag == 0)
38717c454feSmycroft 			hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
38817c454feSmycroft 			    sizeof sin->sin_addr, AF_INET);
38961f28255Scgd 		else
39061f28255Scgd 			hp = 0;
39161f28255Scgd 		if (hp)
39261f28255Scgd 			host = hp->h_name;
39361f28255Scgd 		else {
39461f28255Scgd 			host = "?";
39561f28255Scgd 			if (h_errno == TRY_AGAIN)
39617c454feSmycroft 				nflag = 1;
39761f28255Scgd 		}
398c4516a41Schopps 		(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
39917c454feSmycroft 		if (sdl->sdl_alen)
40017c454feSmycroft 			ether_print(LLADDR(sdl));
40161f28255Scgd 		else
402c4516a41Schopps 			(void)printf("(incomplete)");
40317c454feSmycroft 		if (rtm->rtm_rmx.rmx_expire == 0)
404c4516a41Schopps 			(void)printf(" permanent");
40517c454feSmycroft 		if (sin->sin_other & SIN_PROXY)
406c4516a41Schopps 			(void)printf(" published (proxy only)");
40717c454feSmycroft 		if (rtm->rtm_addrs & RTA_NETMASK) {
40817c454feSmycroft 			sin = (struct sockaddr_inarp *)
40917c454feSmycroft 				(sdl->sdl_len + (char *)sdl);
41017c454feSmycroft 			if (sin->sin_addr.s_addr == 0xffffffff)
411c4516a41Schopps 				(void)printf(" published");
41217c454feSmycroft 			if (sin->sin_len != 8)
413c4516a41Schopps 				(void)printf("(wierd)");
41417c454feSmycroft 		}
415c4516a41Schopps 		(void)printf("\n");
41661f28255Scgd 	}
41761f28255Scgd }
41861f28255Scgd 
419c4516a41Schopps void
42061f28255Scgd ether_print(cp)
421c4516a41Schopps 	const u_char *cp;
42261f28255Scgd {
423c4516a41Schopps 	(void)printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4],
424c4516a41Schopps 	    cp[5]);
42561f28255Scgd }
42661f28255Scgd 
427c4516a41Schopps int
42861f28255Scgd ether_aton(a, n)
429c4516a41Schopps 	const char *a;
43061f28255Scgd 	u_char *n;
43161f28255Scgd {
43261f28255Scgd 	int i, o[6];
43361f28255Scgd 
434c4516a41Schopps 	i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], &o[3], &o[4],
435c4516a41Schopps 	    &o[5]);
43661f28255Scgd 	if (i != 6) {
437c4516a41Schopps 		warnx("invalid Ethernet address '%s'", a);
43861f28255Scgd 		return (1);
43961f28255Scgd 	}
44061f28255Scgd 	for (i=0; i<6; i++)
44161f28255Scgd 		n[i] = o[i];
44261f28255Scgd 	return (0);
44361f28255Scgd }
44461f28255Scgd 
445c4516a41Schopps void
44661f28255Scgd usage()
44761f28255Scgd {
448c4516a41Schopps 	(void)fprintf(stderr, "usage: arp [-n] hostname\n");
449c4516a41Schopps 	(void)fprintf(stderr, "usage: arp [-n] -a\n");
450c4516a41Schopps 	(void)fprintf(stderr, "usage: arp -d hostname\n");
451c4516a41Schopps 	(void)fprintf(stderr,
452c4516a41Schopps 	    "usage: arp -s hostname ether_addr [temp] [pub]\n");
453c4516a41Schopps 	(void)fprintf(stderr, "usage: arp -f filename\n");
45461f28255Scgd 	exit(1);
45561f28255Scgd }
45617c454feSmycroft 
457c4516a41Schopps int
45817c454feSmycroft rtmsg(cmd)
459c4516a41Schopps 	int cmd;
46017c454feSmycroft {
46117c454feSmycroft 	static int seq;
46217c454feSmycroft 	int rlen;
463c4516a41Schopps 	register struct rt_msghdr *rtm;
464c4516a41Schopps 	register char *cp;
46517c454feSmycroft 	register int l;
46617c454feSmycroft 
467c4516a41Schopps 	rtm = &m_rtmsg.m_rtm;
468c4516a41Schopps 	cp = m_rtmsg.m_space;
46917c454feSmycroft 	errno = 0;
470c4516a41Schopps 
47117c454feSmycroft 	if (cmd == RTM_DELETE)
47217c454feSmycroft 		goto doit;
473c4516a41Schopps 	(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
47417c454feSmycroft 	rtm->rtm_flags = flags;
47517c454feSmycroft 	rtm->rtm_version = RTM_VERSION;
47617c454feSmycroft 
47717c454feSmycroft 	switch (cmd) {
47817c454feSmycroft 	default:
479c4516a41Schopps 		errx(1, "internal wrong cmd");
480c4516a41Schopps 		/*NOTREACHED*/
48117c454feSmycroft 	case RTM_ADD:
48217c454feSmycroft 		rtm->rtm_addrs |= RTA_GATEWAY;
48317c454feSmycroft 		rtm->rtm_rmx.rmx_expire = expire_time;
48417c454feSmycroft 		rtm->rtm_inits = RTV_EXPIRE;
48517c454feSmycroft 		rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
48617c454feSmycroft 		sin_m.sin_other = 0;
48717c454feSmycroft 		if (doing_proxy) {
48817c454feSmycroft 			if (export_only)
48917c454feSmycroft 				sin_m.sin_other = SIN_PROXY;
49017c454feSmycroft 			else {
49117c454feSmycroft 				rtm->rtm_addrs |= RTA_NETMASK;
49217c454feSmycroft 				rtm->rtm_flags &= ~RTF_HOST;
49317c454feSmycroft 			}
49417c454feSmycroft 		}
49517c454feSmycroft 		/* FALLTHROUGH */
49617c454feSmycroft 	case RTM_GET:
49717c454feSmycroft 		rtm->rtm_addrs |= RTA_DST;
49817c454feSmycroft 	}
49917c454feSmycroft #define NEXTADDR(w, s) \
50017c454feSmycroft 	if (rtm->rtm_addrs & (w)) { \
501c4516a41Schopps 		(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
50217c454feSmycroft 
50317c454feSmycroft 	NEXTADDR(RTA_DST, sin_m);
50417c454feSmycroft 	NEXTADDR(RTA_GATEWAY, sdl_m);
50517c454feSmycroft 	NEXTADDR(RTA_NETMASK, so_mask);
50617c454feSmycroft 
50717c454feSmycroft 	rtm->rtm_msglen = cp - (char *)&m_rtmsg;
50817c454feSmycroft doit:
50917c454feSmycroft 	l = rtm->rtm_msglen;
51017c454feSmycroft 	rtm->rtm_seq = ++seq;
51117c454feSmycroft 	rtm->rtm_type = cmd;
51217c454feSmycroft 	if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
51317c454feSmycroft 		if (errno != ESRCH || cmd != RTM_DELETE) {
514c4516a41Schopps 			warn("writing to routing socket");
51517c454feSmycroft 			return (-1);
51617c454feSmycroft 		}
51717c454feSmycroft 	}
51817c454feSmycroft 	do {
51917c454feSmycroft 		l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
52017c454feSmycroft 	} while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
52117c454feSmycroft 	if (l < 0)
522c4516a41Schopps 		warn("read from routing socket");
52317c454feSmycroft 	return (0);
52417c454feSmycroft }
52517c454feSmycroft 
526c4516a41Schopps int
527c4516a41Schopps getinetaddr(host, inap)
528c4516a41Schopps 	const char *host;
529c4516a41Schopps 	struct in_addr *inap;
53017c454feSmycroft {
531c4516a41Schopps 	extern char *__progname;	/* Program name, from crt0. */
532c4516a41Schopps 	struct hostent *hp;
533c4516a41Schopps 	u_long addr;
534c4516a41Schopps 
5359a1b7b98Schopps 	if (inet_aton(host, inap) == 1)
536c4516a41Schopps 		return (0);
537c4516a41Schopps 	if ((hp = gethostbyname(host)) == NULL) {
538c4516a41Schopps 		(void)fprintf(stderr, "%s: %s: ", __progname, host);
539c4516a41Schopps 		herror(NULL);
540c4516a41Schopps 		return (-1);
541c4516a41Schopps 	}
542c4516a41Schopps 	(void)memcpy(inap, hp->h_addr, sizeof(*inap));
543c4516a41Schopps 	return (0);
54417c454feSmycroft }
545