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