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