arp.c revision 1.8 1 1.1 cgd /*
2 1.5 mycroft * Copyright (c) 1984, 1993
3 1.5 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.5 mycroft static char copyright[] =
39 1.5 mycroft "@(#) Copyright (c) 1984, 1993\n\
40 1.5 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.5 mycroft /*static char sccsid[] = "from: @(#)arp.c 8.2 (Berkeley) 1/2/94";*/
45 1.8 chopps static char *rcsid = "$Id: arp.c,v 1.8 1995/02/27 22:00:14 chopps Exp $";
46 1.1 cgd #endif /* not lint */
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * arp - display, set, and delete arp table entries
50 1.1 cgd */
51 1.1 cgd
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.5 mycroft #include <sys/sysctl.h>
56 1.5 mycroft
57 1.5 mycroft #include <net/if.h>
58 1.5 mycroft #include <net/if_dl.h>
59 1.5 mycroft #include <net/if_types.h>
60 1.5 mycroft #include <net/route.h>
61 1.1 cgd #include <netinet/in.h>
62 1.1 cgd #include <netinet/if_ether.h>
63 1.2 mycroft #include <arpa/inet.h>
64 1.1 cgd
65 1.5 mycroft #include <netdb.h>
66 1.1 cgd #include <errno.h>
67 1.8 chopps #include <err.h>
68 1.1 cgd #include <nlist.h>
69 1.1 cgd #include <stdio.h>
70 1.8 chopps #include <stdlib.h>
71 1.1 cgd #include <paths.h>
72 1.1 cgd
73 1.8 chopps int delete __P((const char *, const char *));
74 1.8 chopps void dump __P((u_long));
75 1.8 chopps int ether_aton __P((const char *, u_char *));
76 1.8 chopps void ether_print __P((const u_char *));
77 1.8 chopps int file __P((char *));
78 1.8 chopps void get __P((const char *));
79 1.8 chopps int getinetaddr __P((const char *, struct in_addr *));
80 1.8 chopps void getsocket __P((void));
81 1.8 chopps int rtmsg __P((int));
82 1.8 chopps int set __P((int, char **));
83 1.8 chopps void usage __P((void));
84 1.8 chopps
85 1.5 mycroft static int pid;
86 1.5 mycroft static int nflag;
87 1.5 mycroft static int s = -1;
88 1.1 cgd
89 1.8 chopps int
90 1.1 cgd main(argc, argv)
91 1.1 cgd int argc;
92 1.1 cgd char **argv;
93 1.1 cgd {
94 1.1 cgd int ch;
95 1.1 cgd
96 1.5 mycroft pid = getpid();
97 1.6 chopps while ((ch = getopt(argc, argv, "andsf")) != EOF)
98 1.1 cgd switch((char)ch) {
99 1.5 mycroft case 'a':
100 1.5 mycroft dump(0);
101 1.8 chopps return (0);
102 1.1 cgd case 'd':
103 1.5 mycroft if (argc < 3 || argc > 4)
104 1.1 cgd usage();
105 1.8 chopps (void)delete(argv[2], argv[3]);
106 1.8 chopps return (0);
107 1.5 mycroft case 'n':
108 1.5 mycroft nflag = 1;
109 1.8 chopps break;
110 1.1 cgd case 's':
111 1.1 cgd if (argc < 4 || argc > 7)
112 1.1 cgd usage();
113 1.8 chopps return (set(argc-2, &argv[2]) ? 1 : 0);
114 1.6 chopps case 'f':
115 1.6 chopps if (argc != 3)
116 1.6 chopps usage();
117 1.8 chopps return (file(argv[2]));
118 1.1 cgd case '?':
119 1.1 cgd default:
120 1.1 cgd usage();
121 1.1 cgd }
122 1.8 chopps if (argc == 2 || (argc == 3 && nflag))
123 1.8 chopps get(argv[argc - 1]);
124 1.8 chopps else
125 1.1 cgd usage();
126 1.8 chopps return (0);
127 1.1 cgd }
128 1.1 cgd
129 1.1 cgd /*
130 1.1 cgd * Process a file to set standard arp entries
131 1.1 cgd */
132 1.8 chopps int
133 1.1 cgd file(name)
134 1.1 cgd char *name;
135 1.1 cgd {
136 1.8 chopps char line[100], arg[5][50], *args[5];
137 1.8 chopps int i, retval;
138 1.1 cgd FILE *fp;
139 1.1 cgd
140 1.8 chopps if ((fp = fopen(name, "r")) == NULL)
141 1.8 chopps err(1, "cannot open %s", name);
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.8 chopps 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.8 chopps warnx("bad line: %s", 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.8 chopps void
164 1.8 chopps getsocket()
165 1.8 chopps {
166 1.8 chopps if (s >= 0)
167 1.8 chopps return;
168 1.8 chopps s = socket(PF_ROUTE, SOCK_RAW, 0);
169 1.8 chopps if (s < 0)
170 1.8 chopps err(1, "socket");
171 1.5 mycroft }
172 1.5 mycroft
173 1.5 mycroft struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
174 1.5 mycroft struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
175 1.5 mycroft struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
176 1.5 mycroft int expire_time, flags, export_only, doing_proxy, found_entry;
177 1.5 mycroft struct {
178 1.5 mycroft struct rt_msghdr m_rtm;
179 1.5 mycroft char m_space[512];
180 1.5 mycroft } m_rtmsg;
181 1.5 mycroft
182 1.1 cgd /*
183 1.1 cgd * Set an individual arp entry
184 1.1 cgd */
185 1.8 chopps int
186 1.1 cgd set(argc, argv)
187 1.1 cgd int argc;
188 1.1 cgd char **argv;
189 1.1 cgd {
190 1.8 chopps register struct sockaddr_inarp *sin;
191 1.5 mycroft register struct sockaddr_dl *sdl;
192 1.8 chopps register struct rt_msghdr *rtm;
193 1.1 cgd u_char *ea;
194 1.8 chopps char *host = argv[0], *eaddr;
195 1.8 chopps
196 1.8 chopps sin = &sin_m;
197 1.8 chopps rtm = &(m_rtmsg.m_rtm);
198 1.8 chopps eaddr = argv[1];
199 1.1 cgd
200 1.5 mycroft getsocket();
201 1.1 cgd argc -= 2;
202 1.1 cgd argv += 2;
203 1.8 chopps sdl_m = blank_sdl; /* struct copy */
204 1.8 chopps sin_m = blank_sin; /* struct copy */
205 1.8 chopps if (getinetaddr(host, &sin->sin_addr) == -1)
206 1.8 chopps return (1);
207 1.5 mycroft ea = (u_char *)LLADDR(&sdl_m);
208 1.5 mycroft if (ether_aton(eaddr, ea) == 0)
209 1.5 mycroft sdl_m.sdl_alen = 6;
210 1.5 mycroft doing_proxy = flags = export_only = expire_time = 0;
211 1.1 cgd while (argc-- > 0) {
212 1.5 mycroft if (strncmp(argv[0], "temp", 4) == 0) {
213 1.5 mycroft struct timeval time;
214 1.8 chopps (void)gettimeofday(&time, 0);
215 1.5 mycroft expire_time = time.tv_sec + 20 * 60;
216 1.5 mycroft }
217 1.5 mycroft else if (strncmp(argv[0], "pub", 3) == 0) {
218 1.5 mycroft flags |= RTF_ANNOUNCE;
219 1.5 mycroft doing_proxy = SIN_PROXY;
220 1.5 mycroft } else if (strncmp(argv[0], "trail", 5) == 0) {
221 1.8 chopps (void)printf(
222 1.8 chopps "%s: Sending trailers is no longer supported\n",
223 1.8 chopps host);
224 1.5 mycroft }
225 1.1 cgd argv++;
226 1.1 cgd }
227 1.5 mycroft tryagain:
228 1.5 mycroft if (rtmsg(RTM_GET) < 0) {
229 1.8 chopps warn("%s", host);
230 1.5 mycroft return (1);
231 1.1 cgd }
232 1.5 mycroft sin = (struct sockaddr_inarp *)(rtm + 1);
233 1.5 mycroft sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
234 1.5 mycroft if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
235 1.5 mycroft if (sdl->sdl_family == AF_LINK &&
236 1.5 mycroft (rtm->rtm_flags & RTF_LLINFO) &&
237 1.5 mycroft !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
238 1.5 mycroft case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
239 1.5 mycroft case IFT_ISO88024: case IFT_ISO88025:
240 1.5 mycroft goto overwrite;
241 1.5 mycroft }
242 1.5 mycroft if (doing_proxy == 0) {
243 1.8 chopps (void)printf("set: can only proxy for %s\n", host);
244 1.5 mycroft return (1);
245 1.5 mycroft }
246 1.5 mycroft if (sin_m.sin_other & SIN_PROXY) {
247 1.8 chopps (void)printf(
248 1.8 chopps "set: proxy entry exists for non 802 device\n");
249 1.8 chopps return (1);
250 1.5 mycroft }
251 1.5 mycroft sin_m.sin_other = SIN_PROXY;
252 1.5 mycroft export_only = 1;
253 1.5 mycroft goto tryagain;
254 1.5 mycroft }
255 1.5 mycroft overwrite:
256 1.5 mycroft if (sdl->sdl_family != AF_LINK) {
257 1.8 chopps (void)printf("cannot intuit interface index and type for %s\n",
258 1.8 chopps host);
259 1.5 mycroft return (1);
260 1.1 cgd }
261 1.5 mycroft sdl_m.sdl_type = sdl->sdl_type;
262 1.5 mycroft sdl_m.sdl_index = sdl->sdl_index;
263 1.5 mycroft return (rtmsg(RTM_ADD));
264 1.1 cgd }
265 1.1 cgd
266 1.1 cgd /*
267 1.1 cgd * Display an individual arp entry
268 1.1 cgd */
269 1.8 chopps void
270 1.1 cgd get(host)
271 1.8 chopps const char *host;
272 1.1 cgd {
273 1.8 chopps struct sockaddr_inarp *sin;
274 1.1 cgd u_char *ea;
275 1.1 cgd
276 1.8 chopps sin = &sin_m;
277 1.8 chopps sin_m = blank_sin; /* struct copy */
278 1.8 chopps if (getinetaddr(host, &sin->sin_addr) == -1)
279 1.8 chopps exit(1);
280 1.5 mycroft dump(sin->sin_addr.s_addr);
281 1.5 mycroft if (found_entry == 0) {
282 1.8 chopps (void)printf("%s (%s) -- no entry\n", host,
283 1.8 chopps inet_ntoa(sin->sin_addr));
284 1.1 cgd exit(1);
285 1.1 cgd }
286 1.1 cgd }
287 1.1 cgd
288 1.1 cgd /*
289 1.1 cgd * Delete an arp entry
290 1.1 cgd */
291 1.8 chopps int
292 1.5 mycroft delete(host, info)
293 1.8 chopps const char *host;
294 1.8 chopps const char *info;
295 1.1 cgd {
296 1.8 chopps register struct sockaddr_inarp *sin;
297 1.8 chopps register struct rt_msghdr *rtm;
298 1.5 mycroft struct sockaddr_dl *sdl;
299 1.5 mycroft u_char *ea;
300 1.5 mycroft char *eaddr;
301 1.1 cgd
302 1.8 chopps sin = &sin_m;
303 1.8 chopps rtm = &m_rtmsg.m_rtm;
304 1.8 chopps
305 1.5 mycroft if (info && strncmp(info, "pro", 3) )
306 1.5 mycroft export_only = 1;
307 1.5 mycroft getsocket();
308 1.8 chopps sin_m = blank_sin; /* struct copy */
309 1.8 chopps if (getinetaddr(host, &sin->sin_addr) == -1)
310 1.8 chopps return (1);
311 1.5 mycroft tryagain:
312 1.5 mycroft if (rtmsg(RTM_GET) < 0) {
313 1.8 chopps warn("%s", host);
314 1.5 mycroft return (1);
315 1.5 mycroft }
316 1.5 mycroft sin = (struct sockaddr_inarp *)(rtm + 1);
317 1.5 mycroft sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
318 1.5 mycroft if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
319 1.5 mycroft if (sdl->sdl_family == AF_LINK &&
320 1.5 mycroft (rtm->rtm_flags & RTF_LLINFO) &&
321 1.5 mycroft !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
322 1.5 mycroft case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
323 1.5 mycroft case IFT_ISO88024: case IFT_ISO88025:
324 1.5 mycroft goto delete;
325 1.5 mycroft }
326 1.1 cgd }
327 1.5 mycroft if (sin_m.sin_other & SIN_PROXY) {
328 1.8 chopps warnx("delete: can't locate %s", host);
329 1.5 mycroft return (1);
330 1.5 mycroft } else {
331 1.5 mycroft sin_m.sin_other = SIN_PROXY;
332 1.5 mycroft goto tryagain;
333 1.5 mycroft }
334 1.5 mycroft delete:
335 1.5 mycroft if (sdl->sdl_family != AF_LINK) {
336 1.8 chopps (void)printf("cannot locate %s\n", host);
337 1.5 mycroft return (1);
338 1.1 cgd }
339 1.8 chopps if (rtmsg(RTM_DELETE))
340 1.8 chopps return (1);
341 1.8 chopps (void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
342 1.8 chopps return (0);
343 1.1 cgd }
344 1.1 cgd
345 1.1 cgd /*
346 1.1 cgd * Dump the entire arp table
347 1.1 cgd */
348 1.8 chopps void
349 1.5 mycroft dump(addr)
350 1.8 chopps u_long addr;
351 1.1 cgd {
352 1.5 mycroft int mib[6];
353 1.5 mycroft size_t needed;
354 1.8 chopps char *host, *lim, *buf, *next;
355 1.5 mycroft struct rt_msghdr *rtm;
356 1.5 mycroft struct sockaddr_inarp *sin;
357 1.5 mycroft struct sockaddr_dl *sdl;
358 1.1 cgd extern int h_errno;
359 1.1 cgd struct hostent *hp;
360 1.1 cgd
361 1.5 mycroft mib[0] = CTL_NET;
362 1.5 mycroft mib[1] = PF_ROUTE;
363 1.5 mycroft mib[2] = 0;
364 1.5 mycroft mib[3] = AF_INET;
365 1.5 mycroft mib[4] = NET_RT_FLAGS;
366 1.5 mycroft mib[5] = RTF_LLINFO;
367 1.5 mycroft if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
368 1.8 chopps err(1, "route-sysctl-estimate");
369 1.5 mycroft if ((buf = malloc(needed)) == NULL)
370 1.8 chopps err(1, "malloc");
371 1.5 mycroft if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
372 1.8 chopps err(1, "actual retrieval of routing table");
373 1.5 mycroft lim = buf + needed;
374 1.5 mycroft for (next = buf; next < lim; next += rtm->rtm_msglen) {
375 1.5 mycroft rtm = (struct rt_msghdr *)next;
376 1.5 mycroft sin = (struct sockaddr_inarp *)(rtm + 1);
377 1.5 mycroft sdl = (struct sockaddr_dl *)(sin + 1);
378 1.5 mycroft if (addr) {
379 1.5 mycroft if (addr != sin->sin_addr.s_addr)
380 1.5 mycroft continue;
381 1.5 mycroft found_entry = 1;
382 1.5 mycroft }
383 1.5 mycroft if (nflag == 0)
384 1.5 mycroft hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
385 1.5 mycroft sizeof sin->sin_addr, AF_INET);
386 1.1 cgd else
387 1.1 cgd hp = 0;
388 1.1 cgd if (hp)
389 1.1 cgd host = hp->h_name;
390 1.1 cgd else {
391 1.1 cgd host = "?";
392 1.1 cgd if (h_errno == TRY_AGAIN)
393 1.5 mycroft nflag = 1;
394 1.1 cgd }
395 1.8 chopps (void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
396 1.5 mycroft if (sdl->sdl_alen)
397 1.5 mycroft ether_print(LLADDR(sdl));
398 1.1 cgd else
399 1.8 chopps (void)printf("(incomplete)");
400 1.5 mycroft if (rtm->rtm_rmx.rmx_expire == 0)
401 1.8 chopps (void)printf(" permanent");
402 1.5 mycroft if (sin->sin_other & SIN_PROXY)
403 1.8 chopps (void)printf(" published (proxy only)");
404 1.5 mycroft if (rtm->rtm_addrs & RTA_NETMASK) {
405 1.5 mycroft sin = (struct sockaddr_inarp *)
406 1.5 mycroft (sdl->sdl_len + (char *)sdl);
407 1.5 mycroft if (sin->sin_addr.s_addr == 0xffffffff)
408 1.8 chopps (void)printf(" published");
409 1.5 mycroft if (sin->sin_len != 8)
410 1.8 chopps (void)printf("(wierd)");
411 1.5 mycroft }
412 1.8 chopps (void)printf("\n");
413 1.1 cgd }
414 1.1 cgd }
415 1.1 cgd
416 1.8 chopps void
417 1.1 cgd ether_print(cp)
418 1.8 chopps const u_char *cp;
419 1.1 cgd {
420 1.8 chopps (void)printf("%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4],
421 1.8 chopps cp[5]);
422 1.1 cgd }
423 1.1 cgd
424 1.8 chopps int
425 1.1 cgd ether_aton(a, n)
426 1.8 chopps const char *a;
427 1.1 cgd u_char *n;
428 1.1 cgd {
429 1.1 cgd int i, o[6];
430 1.1 cgd
431 1.8 chopps i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2], &o[3], &o[4],
432 1.8 chopps &o[5]);
433 1.1 cgd if (i != 6) {
434 1.8 chopps warnx("invalid Ethernet address '%s'", a);
435 1.1 cgd return (1);
436 1.1 cgd }
437 1.1 cgd for (i=0; i<6; i++)
438 1.1 cgd n[i] = o[i];
439 1.1 cgd return (0);
440 1.1 cgd }
441 1.1 cgd
442 1.8 chopps void
443 1.1 cgd usage()
444 1.1 cgd {
445 1.8 chopps (void)fprintf(stderr, "usage: arp [-n] hostname\n");
446 1.8 chopps (void)fprintf(stderr, "usage: arp [-n] -a\n");
447 1.8 chopps (void)fprintf(stderr, "usage: arp -d hostname\n");
448 1.8 chopps (void)fprintf(stderr,
449 1.8 chopps "usage: arp -s hostname ether_addr [temp] [pub]\n");
450 1.8 chopps (void)fprintf(stderr, "usage: arp -f filename\n");
451 1.5 mycroft exit(1);
452 1.5 mycroft }
453 1.5 mycroft
454 1.8 chopps int
455 1.5 mycroft rtmsg(cmd)
456 1.8 chopps int cmd;
457 1.5 mycroft {
458 1.5 mycroft static int seq;
459 1.5 mycroft int rlen;
460 1.8 chopps register struct rt_msghdr *rtm;
461 1.8 chopps register char *cp;
462 1.5 mycroft register int l;
463 1.5 mycroft
464 1.8 chopps rtm = &m_rtmsg.m_rtm;
465 1.8 chopps cp = m_rtmsg.m_space;
466 1.5 mycroft errno = 0;
467 1.8 chopps
468 1.5 mycroft if (cmd == RTM_DELETE)
469 1.5 mycroft goto doit;
470 1.8 chopps (void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
471 1.5 mycroft rtm->rtm_flags = flags;
472 1.5 mycroft rtm->rtm_version = RTM_VERSION;
473 1.5 mycroft
474 1.5 mycroft switch (cmd) {
475 1.5 mycroft default:
476 1.8 chopps errx(1, "internal wrong cmd");
477 1.8 chopps /*NOTREACHED*/
478 1.5 mycroft case RTM_ADD:
479 1.5 mycroft rtm->rtm_addrs |= RTA_GATEWAY;
480 1.5 mycroft rtm->rtm_rmx.rmx_expire = expire_time;
481 1.5 mycroft rtm->rtm_inits = RTV_EXPIRE;
482 1.5 mycroft rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
483 1.5 mycroft sin_m.sin_other = 0;
484 1.5 mycroft if (doing_proxy) {
485 1.5 mycroft if (export_only)
486 1.5 mycroft sin_m.sin_other = SIN_PROXY;
487 1.5 mycroft else {
488 1.5 mycroft rtm->rtm_addrs |= RTA_NETMASK;
489 1.5 mycroft rtm->rtm_flags &= ~RTF_HOST;
490 1.5 mycroft }
491 1.5 mycroft }
492 1.5 mycroft /* FALLTHROUGH */
493 1.5 mycroft case RTM_GET:
494 1.5 mycroft rtm->rtm_addrs |= RTA_DST;
495 1.5 mycroft }
496 1.5 mycroft #define NEXTADDR(w, s) \
497 1.5 mycroft if (rtm->rtm_addrs & (w)) { \
498 1.8 chopps (void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
499 1.5 mycroft
500 1.5 mycroft NEXTADDR(RTA_DST, sin_m);
501 1.5 mycroft NEXTADDR(RTA_GATEWAY, sdl_m);
502 1.5 mycroft NEXTADDR(RTA_NETMASK, so_mask);
503 1.5 mycroft
504 1.5 mycroft rtm->rtm_msglen = cp - (char *)&m_rtmsg;
505 1.5 mycroft doit:
506 1.5 mycroft l = rtm->rtm_msglen;
507 1.5 mycroft rtm->rtm_seq = ++seq;
508 1.5 mycroft rtm->rtm_type = cmd;
509 1.5 mycroft if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
510 1.5 mycroft if (errno != ESRCH || cmd != RTM_DELETE) {
511 1.8 chopps warn("writing to routing socket");
512 1.5 mycroft return (-1);
513 1.5 mycroft }
514 1.5 mycroft }
515 1.5 mycroft do {
516 1.5 mycroft l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
517 1.5 mycroft } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
518 1.5 mycroft if (l < 0)
519 1.8 chopps warn("read from routing socket");
520 1.5 mycroft return (0);
521 1.5 mycroft }
522 1.5 mycroft
523 1.8 chopps int
524 1.8 chopps getinetaddr(host, inap)
525 1.8 chopps const char *host;
526 1.8 chopps struct in_addr *inap;
527 1.5 mycroft {
528 1.8 chopps extern char *__progname; /* Program name, from crt0. */
529 1.8 chopps struct hostent *hp;
530 1.8 chopps u_long addr;
531 1.8 chopps
532 1.8 chopps if ((inap->s_addr = inet_addr(host)) != INADDR_NONE)
533 1.8 chopps return (0);
534 1.8 chopps if ((hp = gethostbyname(host)) == NULL) {
535 1.8 chopps (void)fprintf(stderr, "%s: %s: ", __progname, host);
536 1.8 chopps herror(NULL);
537 1.8 chopps return (-1);
538 1.8 chopps }
539 1.8 chopps (void)memcpy(inap, hp->h_addr, sizeof(*inap));
540 1.8 chopps return (0);
541 1.1 cgd }
542