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