dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / arp.c
blob19ab13fd47e77fb1403b3eef7204f1596ac694ae
1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
4 */
5 /*
6 * Copyright (c) 1984 Regents of the University of California.
7 * All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Sun Microsystems, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
43 * arp - display, set, and delete arp table entries
46 #include <stdio.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <sys/ioctl.h>
51 #include <errno.h>
52 #include <netdb.h>
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <stdlib.h>
56 #include <unistd.h>
57 #include <string.h>
58 #include <arpa/inet.h>
59 #include <net/if_types.h>
60 #include <net/if_dl.h>
62 static int file(char *);
63 static int set(int, char *[]);
64 static void get(char *);
65 static void delete(char *);
66 static void usage(void);
68 int
69 main(int argc, char *argv[])
71 int c, nflags = 0, argsleft;
72 int n_flag, a_flag, d_flag, f_flag, s_flag;
74 n_flag = a_flag = d_flag = f_flag = s_flag = 0;
76 #define CASE(x, y) \
77 case x: \
78 if (nflags > 0) { \
79 usage(); \
80 exit(1); \
81 } else \
82 y##_flag = 1; \
83 nflags++; \
84 break
86 while ((c = getopt(argc, argv, "nadfs")) != EOF) {
87 switch (c) {
88 case '?':
89 usage();
90 exit(1);
91 /* NOTREACHED */
92 break;
93 case 'n':
94 n_flag = 1;
95 break;
96 CASE('a', a);
97 CASE('d', d);
98 CASE('f', f);
99 CASE('s', s);
103 #undef CASE
106 * -n only allowed with -a
108 if (n_flag && !a_flag) {
109 usage();
110 exit(1);
113 argsleft = argc - optind;
115 if (a_flag && (argsleft == 0)) {
117 * the easiest way to get the complete arp table
118 * is to let netstat, which prints it as part of
119 * the MIB statistics, do it.
121 (void) execl("/usr/bin/netstat", "netstat",
122 (n_flag ? "-np" : "-p"),
123 "-f", "inet", (char *)0);
124 (void) fprintf(stderr, "failed to exec netstat: %s\n",
125 strerror(errno));
126 exit(1);
128 } else if (s_flag && (argsleft >= 2)) {
129 if (set(argsleft, &argv[optind]) != 0)
130 exit(1);
132 } else if (d_flag && (argsleft == 1)) {
133 delete(argv[optind]);
135 } else if (f_flag && (argsleft == 1)) {
136 if (file(argv[optind]) != 0)
137 exit(1);
139 } else if ((nflags == 0) && (argsleft == 1)) {
140 get(argv[optind]);
142 } else {
143 usage();
144 exit(1);
146 return (0);
150 * Process a file to set standard arp entries
152 static int
153 file(char *name)
156 * A line of input can be:
157 * <hostname> <macaddr> ["temp"] ["pub"] ["trail"] ["permanent"]
159 #define MAX_LINE_LEN (MAXHOSTNAMELEN + \
160 sizeof (" xx:xx:xx:xx:xx:xx temp pub trail permanent\n"))
161 #define MIN_ARGS 2
162 #define MAX_ARGS 5
164 FILE *fp;
165 char line[MAX_LINE_LEN];
166 int retval;
168 if ((fp = fopen(name, "r")) == NULL) {
169 (void) fprintf(stderr, "arp: cannot open %s\n", name);
170 exit(1);
173 retval = 0;
174 while (fgets(line, MAX_LINE_LEN, fp) != NULL) {
175 char line_copy[MAX_LINE_LEN];
176 char *args[MAX_ARGS];
177 char *start;
178 int i;
181 * Keep a copy of the un-altered line for error
182 * reporting.
184 (void) strlcpy(line_copy, line, MAX_LINE_LEN);
186 start = line_copy;
187 for (i = 0; i < MAX_ARGS; i++) {
188 if ((args[i] = strtok(start, " \t\n")) == NULL)
189 break;
191 start = NULL;
194 if (i < MIN_ARGS) {
195 (void) fprintf(stderr, "arp: bad line: %s\n",
196 line);
197 retval = 1;
198 continue;
201 if (set(i, args) != 0)
202 retval = 1;
205 #undef MAX_LINE_LEN
206 #undef MIN_ARGS
207 #undef MAX_ARGS
209 (void) fclose(fp);
210 return (retval);
214 * Set an individual arp entry
216 static int
217 set(int argc, char *argv[])
219 struct xarpreq ar;
220 struct hostent *hp;
221 struct sockaddr_in *sin;
222 uchar_t *ea;
223 int s;
224 char *host = argv[0], *eaddr = argv[1];
226 argc -= 2;
227 argv += 2;
228 (void) memset(&ar, 0, sizeof (ar));
229 sin = (struct sockaddr_in *)&ar.xarp_pa;
230 sin->sin_family = AF_INET;
231 sin->sin_addr.s_addr = inet_addr(host);
232 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
233 hp = gethostbyname(host);
234 if (hp == NULL) {
235 (void) fprintf(stderr, "arp: %s: unknown host\n",
236 host);
237 return (1);
239 (void) memcpy(&sin->sin_addr, hp->h_addr,
240 sizeof (sin->sin_addr));
242 ea = _link_aton(eaddr, &s);
243 if (ea == NULL) {
244 if (s == -1) {
245 (void) fprintf(stderr,
246 "arp: invalid link layer address '%s'\n", eaddr);
247 return (1);
249 perror("arp: nomem");
250 exit(1);
252 ar.xarp_ha.sdl_alen = s;
253 (void) memcpy(LLADDR(&ar.xarp_ha), ea, ar.xarp_ha.sdl_alen);
254 free(ea);
255 ar.xarp_ha.sdl_family = AF_LINK;
256 ar.xarp_flags = ATF_PERM;
257 while (argc-- > 0) {
258 if (strncmp(argv[0], "temp", 4) == 0) {
259 ar.xarp_flags &= ~ATF_PERM;
260 } else if (strncmp(argv[0], "pub", 3) == 0) {
261 ar.xarp_flags |= ATF_PUBL;
262 } else if (strncmp(argv[0], "trail", 5) == 0) {
263 ar.xarp_flags |= ATF_USETRAILERS;
264 } else if (strcmp(argv[0], "permanent") == 0) {
265 ar.xarp_flags |= ATF_AUTHORITY;
266 } else {
267 (void) fprintf(stderr,
268 "arp: unknown keyword '%s'\n", argv[0]);
269 return (1);
271 argv++;
274 if ((ar.xarp_flags & (ATF_PERM|ATF_AUTHORITY)) == ATF_AUTHORITY) {
275 (void) fprintf(stderr, "arp: 'temp' and 'permanent' flags are "
276 "not usable together.\n");
277 return (1);
280 s = socket(AF_INET, SOCK_DGRAM, 0);
281 if (s < 0) {
282 perror("arp: socket");
283 exit(1);
285 if (ioctl(s, SIOCSXARP, (caddr_t)&ar) < 0) {
286 perror(host);
287 exit(1);
289 (void) close(s);
290 return (0);
294 * Display an individual arp entry
296 static void
297 get(char *host)
299 struct xarpreq ar;
300 struct hostent *hp;
301 struct sockaddr_in *sin;
302 uchar_t *ea;
303 int s;
304 char *str = NULL;
306 (void) memset(&ar, 0, sizeof (ar));
307 sin = (struct sockaddr_in *)&ar.xarp_pa;
308 sin->sin_family = AF_INET;
309 sin->sin_addr.s_addr = inet_addr(host);
310 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
311 hp = gethostbyname(host);
312 if (hp == NULL) {
313 (void) fprintf(stderr, "arp: %s: unknown host\n",
314 host);
315 exit(1);
317 (void) memcpy(&sin->sin_addr, hp->h_addr,
318 sizeof (sin->sin_addr));
320 s = socket(AF_INET, SOCK_DGRAM, 0);
321 if (s < 0) {
322 perror("arp: socket");
323 exit(1);
325 ar.xarp_ha.sdl_family = AF_LINK;
326 if (ioctl(s, SIOCGXARP, (caddr_t)&ar) < 0) {
327 if (errno == ENXIO)
328 (void) printf("%s (%s) -- no entry\n",
329 host, inet_ntoa(sin->sin_addr));
330 else
331 perror("SIOCGXARP");
332 exit(1);
334 (void) close(s);
335 ea = (uchar_t *)LLADDR(&ar.xarp_ha);
336 if (ar.xarp_flags & ATF_COM) {
337 str = _link_ntoa(ea, str, ar.xarp_ha.sdl_alen, IFT_OTHER);
338 if (str != NULL) {
339 (void) printf("%s (%s) at %s", host,
340 inet_ntoa(sin->sin_addr), str);
341 free(str);
342 } else {
343 perror("arp: nomem");
344 exit(1);
346 } else {
347 (void) printf("%s (%s) at (incomplete)", host,
348 inet_ntoa(sin->sin_addr));
350 if (!(ar.xarp_flags & ATF_PERM))
351 (void) printf(" temp");
352 if (ar.xarp_flags & ATF_PUBL)
353 (void) printf(" pub");
354 if (ar.xarp_flags & ATF_USETRAILERS)
355 (void) printf(" trail");
356 if (ar.xarp_flags & ATF_AUTHORITY)
357 (void) printf(" permanent");
358 (void) printf("\n");
362 * Delete an arp entry
364 static void
365 delete(char *host)
367 struct xarpreq ar;
368 struct hostent *hp;
369 struct sockaddr_in *sin;
370 int s;
372 (void) memset(&ar, 0, sizeof (ar));
373 sin = (struct sockaddr_in *)&ar.xarp_pa;
374 sin->sin_family = AF_INET;
375 sin->sin_addr.s_addr = inet_addr(host);
376 if (sin->sin_addr.s_addr == (in_addr_t)-1) {
377 hp = gethostbyname(host);
378 if (hp == NULL) {
379 (void) fprintf(stderr, "arp: %s: unknown host\n",
380 host);
381 exit(1);
383 (void) memcpy(&sin->sin_addr, hp->h_addr,
384 sizeof (sin->sin_addr));
386 s = socket(AF_INET, SOCK_DGRAM, 0);
387 if (s < 0) {
388 perror("arp: socket");
389 exit(1);
391 ar.xarp_ha.sdl_family = AF_LINK;
392 if (ioctl(s, SIOCDXARP, (caddr_t)&ar) < 0) {
393 if (errno == ENXIO)
394 (void) printf("%s (%s) -- no entry\n",
395 host, inet_ntoa(sin->sin_addr));
396 else
397 perror("SIOCDXARP");
398 exit(1);
400 (void) close(s);
401 (void) printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
404 static void
405 usage(void)
407 (void) printf("Usage: arp hostname\n");
408 (void) printf(" arp -a [-n]\n");
409 (void) printf(" arp -d hostname\n");
410 (void) printf(" arp -s hostname ether_addr "
411 "[temp] [pub] [trail] [permanent]\n");
412 (void) printf(" arp -f filename\n");