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