ndp.c revision 1.2 1 1.2 itojun /* $NetBSD: ndp.c,v 1.2 1999/07/06 13:20:04 itojun Exp $ */
2 1.2 itojun
3 1.1 itojun /*
4 1.1 itojun * Copyright (C) 1995, 1996, 1997, 1998, and 1999 WIDE Project.
5 1.1 itojun * All rights reserved.
6 1.1 itojun *
7 1.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.1 itojun * modification, are permitted provided that the following conditions
9 1.1 itojun * are met:
10 1.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.1 itojun * documentation and/or other materials provided with the distribution.
15 1.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.1 itojun * may be used to endorse or promote products derived from this software
17 1.1 itojun * without specific prior written permission.
18 1.1 itojun *
19 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 itojun * SUCH DAMAGE.
30 1.1 itojun */
31 1.1 itojun /*
32 1.1 itojun * Copyright (c) 1984, 1993
33 1.1 itojun * The Regents of the University of California. All rights reserved.
34 1.1 itojun *
35 1.1 itojun * This code is derived from software contributed to Berkeley by
36 1.1 itojun * Sun Microsystems, Inc.
37 1.1 itojun *
38 1.1 itojun * Redistribution and use in source and binary forms, with or without
39 1.1 itojun * modification, are permitted provided that the following conditions
40 1.1 itojun * are met:
41 1.1 itojun * 1. Redistributions of source code must retain the above copyright
42 1.1 itojun * notice, this list of conditions and the following disclaimer.
43 1.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
44 1.1 itojun * notice, this list of conditions and the following disclaimer in the
45 1.1 itojun * documentation and/or other materials provided with the distribution.
46 1.1 itojun * 3. All advertising materials mentioning features or use of this software
47 1.1 itojun * must display the following acknowledgement:
48 1.1 itojun * This product includes software developed by the University of
49 1.1 itojun * California, Berkeley and its contributors.
50 1.1 itojun * 4. Neither the name of the University nor the names of its contributors
51 1.1 itojun * may be used to endorse or promote products derived from this software
52 1.1 itojun * without specific prior written permission.
53 1.1 itojun *
54 1.1 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 1.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 1.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 1.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 1.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 1.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 1.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 1.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 1.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 1.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 1.1 itojun * SUCH DAMAGE.
65 1.1 itojun */
66 1.1 itojun
67 1.1 itojun /*
68 1.1 itojun * Based on:
69 1.1 itojun * "@(#) Copyright (c) 1984, 1993\n\
70 1.1 itojun * The Regents of the University of California. All rights reserved.\n";
71 1.1 itojun *
72 1.1 itojun * "@(#)arp.c 8.2 (Berkeley) 1/2/94";
73 1.1 itojun */
74 1.1 itojun
75 1.1 itojun /*
76 1.1 itojun * ndp - display, set, delete and flush neighbor cache
77 1.1 itojun */
78 1.1 itojun
79 1.1 itojun
80 1.1 itojun #include <sys/param.h>
81 1.1 itojun #include <sys/file.h>
82 1.1 itojun #include <sys/ioctl.h>
83 1.1 itojun #include <sys/socket.h>
84 1.1 itojun #include <sys/sysctl.h>
85 1.1 itojun #include <sys/time.h>
86 1.1 itojun
87 1.1 itojun #include <net/if.h>
88 1.1 itojun #if defined(__FreeBSD__) && __FreeBSD__ >= 3
89 1.1 itojun #include <net/if_var.h>
90 1.1 itojun #endif /* __FreeBSD__ >= 3 */
91 1.1 itojun #include <net/if_dl.h>
92 1.1 itojun #include <net/if_types.h>
93 1.1 itojun #include <net/route.h>
94 1.1 itojun
95 1.1 itojun #include <netinet/in.h>
96 1.1 itojun #ifndef __NetBSD__
97 1.1 itojun #include <netinet/if_ether.h>
98 1.1 itojun #endif
99 1.1 itojun
100 1.1 itojun #include <netinet/icmp6.h>
101 1.1 itojun #include <netinet6/in6_var.h>
102 1.1 itojun #include <netinet6/nd6.h>
103 1.1 itojun
104 1.1 itojun #include <arpa/inet.h>
105 1.1 itojun
106 1.1 itojun #include <netdb.h>
107 1.1 itojun #include <errno.h>
108 1.1 itojun #include <nlist.h>
109 1.1 itojun #include <stdio.h>
110 1.1 itojun #include <string.h>
111 1.1 itojun #include <paths.h>
112 1.1 itojun #include <err.h>
113 1.1 itojun #include <stdlib.h>
114 1.1 itojun #include <fcntl.h>
115 1.1 itojun #include <unistd.h>
116 1.1 itojun #include "gmt2local.h"
117 1.1 itojun
118 1.1 itojun extern int errno;
119 1.1 itojun static int pid;
120 1.1 itojun static int fflag;
121 1.1 itojun static int nflag;
122 1.1 itojun static int tflag;
123 1.1 itojun static int32_t thiszone; /* time difference with gmt */
124 1.1 itojun static int s = -1;
125 1.1 itojun static int repeat = 0;
126 1.1 itojun
127 1.1 itojun char ntop_buf[INET6_ADDRSTRLEN]; /* inet_ntop() */
128 1.1 itojun char ifix_buf[IFNAMSIZ]; /* if_indextoname() */
129 1.1 itojun
130 1.1 itojun int main __P((int, char **));
131 1.1 itojun int file __P((char *));
132 1.1 itojun void getsocket __P((void));
133 1.1 itojun int set __P((int, char **));
134 1.1 itojun void get __P((char *));
135 1.1 itojun int delete __P((char *));
136 1.1 itojun void dump __P((struct in6_addr *));
137 1.1 itojun static struct in6_nbrinfo *getnbrinfo __P((struct in6_addr *addr, int ifindex));
138 1.1 itojun static char *ether_str __P((struct sockaddr_dl *));
139 1.1 itojun int ndp_ether_aton __P((char *, u_char *));
140 1.1 itojun void usage __P((void));
141 1.1 itojun int rtmsg __P((int));
142 1.1 itojun void ifinfo __P((char *));
143 1.1 itojun void rtrlist __P((void));
144 1.1 itojun void plist __P((void));
145 1.1 itojun void pfx_flush __P((void));
146 1.1 itojun void rtr_flush __P((void));
147 1.1 itojun void harmonize_rtr __P((void));
148 1.1 itojun void quit __P((char *));
149 1.1 itojun static char *sec2str __P((time_t t));
150 1.1 itojun static char *ether_str __P((struct sockaddr_dl *sdl));
151 1.1 itojun static void ts_print __P((const struct timeval *));
152 1.1 itojun
153 1.1 itojun int
154 1.1 itojun main(argc, argv)
155 1.1 itojun int argc;
156 1.1 itojun char **argv;
157 1.1 itojun {
158 1.1 itojun int ch;
159 1.1 itojun int aflag = 0, cflag = 0, dflag = 0, sflag = 0, Hflag = 0,
160 1.1 itojun pflag = 0, rflag = 0, Pflag = 0, Rflag = 0;
161 1.1 itojun extern char *optarg;
162 1.1 itojun extern int optind;
163 1.1 itojun
164 1.1 itojun pid = getpid();
165 1.1 itojun thiszone = gmt2local(0);
166 1.1 itojun while ((ch = getopt(argc, argv, "acndfiprstA:HPR")) != EOF)
167 1.1 itojun switch ((char)ch) {
168 1.1 itojun case 'a':
169 1.1 itojun aflag = 1;
170 1.1 itojun break;
171 1.1 itojun case 'c':
172 1.1 itojun fflag = 1;
173 1.1 itojun cflag = 1;
174 1.1 itojun break;
175 1.1 itojun case 'd':
176 1.1 itojun dflag = 1;
177 1.1 itojun break;
178 1.1 itojun case 'i' :
179 1.1 itojun if (argc != 3)
180 1.1 itojun usage();
181 1.1 itojun ifinfo(argv[2]);
182 1.1 itojun exit(0);
183 1.1 itojun case 'n':
184 1.1 itojun nflag = 1;
185 1.1 itojun continue;
186 1.1 itojun case 'p':
187 1.1 itojun pflag = 1;
188 1.1 itojun break;
189 1.1 itojun case 'f' :
190 1.1 itojun if (argc != 3)
191 1.1 itojun usage();
192 1.1 itojun file(argv[2]);
193 1.1 itojun exit(0);
194 1.1 itojun case 'r' :
195 1.1 itojun rflag = 1;
196 1.1 itojun break;
197 1.1 itojun case 's':
198 1.1 itojun sflag = 1;
199 1.1 itojun break;
200 1.1 itojun case 't':
201 1.1 itojun tflag = 1;
202 1.1 itojun break;
203 1.1 itojun case 'A':
204 1.1 itojun aflag = 1;
205 1.1 itojun repeat = atoi(optarg);
206 1.1 itojun if (repeat < 0)
207 1.1 itojun usage();
208 1.1 itojun break;
209 1.1 itojun case 'H' :
210 1.1 itojun Hflag = 1;
211 1.1 itojun break;
212 1.1 itojun case 'P':
213 1.1 itojun Pflag = 1;
214 1.1 itojun break;
215 1.1 itojun case 'R':
216 1.1 itojun Rflag = 1;
217 1.1 itojun break;
218 1.1 itojun default:
219 1.1 itojun usage();
220 1.1 itojun }
221 1.1 itojun
222 1.1 itojun argc -= optind;
223 1.1 itojun argv += optind;
224 1.1 itojun
225 1.1 itojun if (aflag || cflag) {
226 1.1 itojun dump(0);
227 1.1 itojun exit(0);
228 1.1 itojun }
229 1.1 itojun if (dflag) {
230 1.1 itojun if (argc != 1)
231 1.1 itojun usage();
232 1.1 itojun delete(argv[0]);
233 1.1 itojun }
234 1.1 itojun if (pflag) {
235 1.1 itojun plist();
236 1.1 itojun exit(0);
237 1.1 itojun }
238 1.1 itojun if (rflag) {
239 1.1 itojun rtrlist();
240 1.1 itojun exit(0);
241 1.1 itojun }
242 1.1 itojun if (sflag) {
243 1.1 itojun if (argc < 2 || argc > 4)
244 1.1 itojun usage();
245 1.1 itojun exit(set(argc, argv) ? 1 : 0);
246 1.1 itojun }
247 1.1 itojun if (Hflag) {
248 1.1 itojun harmonize_rtr();
249 1.1 itojun exit(0);
250 1.1 itojun }
251 1.1 itojun if (Pflag) {
252 1.1 itojun pfx_flush();
253 1.1 itojun exit(0);
254 1.1 itojun }
255 1.1 itojun if (Rflag) {
256 1.1 itojun rtr_flush();
257 1.1 itojun exit(0);
258 1.1 itojun }
259 1.1 itojun
260 1.1 itojun if (argc != 1)
261 1.1 itojun usage();
262 1.1 itojun get(argv[0]);
263 1.1 itojun exit(0);
264 1.1 itojun }
265 1.1 itojun
266 1.1 itojun /*
267 1.1 itojun * Process a file to set standard ndp entries
268 1.1 itojun */
269 1.1 itojun int
270 1.1 itojun file(name)
271 1.1 itojun char *name;
272 1.1 itojun {
273 1.1 itojun FILE *fp;
274 1.1 itojun int i, retval;
275 1.1 itojun char line[100], arg[5][50], *args[5];
276 1.1 itojun
277 1.1 itojun if ((fp = fopen(name, "r")) == NULL) {
278 1.1 itojun fprintf(stderr, "ndp: cannot open %s\n", name);
279 1.1 itojun exit(1);
280 1.1 itojun }
281 1.1 itojun args[0] = &arg[0][0];
282 1.1 itojun args[1] = &arg[1][0];
283 1.1 itojun args[2] = &arg[2][0];
284 1.1 itojun args[3] = &arg[3][0];
285 1.1 itojun args[4] = &arg[4][0];
286 1.1 itojun retval = 0;
287 1.1 itojun while(fgets(line, 100, fp) != NULL) {
288 1.1 itojun i = sscanf(line, "%s %s %s %s %s", arg[0], arg[1], arg[2],
289 1.1 itojun arg[3], arg[4]);
290 1.1 itojun if (i < 2) {
291 1.1 itojun fprintf(stderr, "ndp: bad line: %s\n", line);
292 1.1 itojun retval = 1;
293 1.1 itojun continue;
294 1.1 itojun }
295 1.1 itojun if (set(i, args))
296 1.1 itojun retval = 1;
297 1.1 itojun }
298 1.1 itojun fclose(fp);
299 1.1 itojun return (retval);
300 1.1 itojun }
301 1.1 itojun
302 1.1 itojun void
303 1.1 itojun getsocket()
304 1.1 itojun {
305 1.1 itojun if (s < 0) {
306 1.1 itojun s = socket(PF_ROUTE, SOCK_RAW, 0);
307 1.1 itojun if (s < 0) {
308 1.1 itojun perror("ndp: socket");
309 1.1 itojun exit(1);
310 1.1 itojun }
311 1.1 itojun }
312 1.1 itojun }
313 1.1 itojun
314 1.1 itojun struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
315 1.1 itojun struct sockaddr_in6 blank_sin = {sizeof(blank_sin), AF_INET6 }, sin_m;
316 1.1 itojun struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
317 1.1 itojun int expire_time, flags, found_entry;
318 1.1 itojun struct {
319 1.1 itojun struct rt_msghdr m_rtm;
320 1.1 itojun char m_space[512];
321 1.1 itojun } m_rtmsg;
322 1.1 itojun
323 1.1 itojun /*
324 1.1 itojun * Set an individual neighbor cache entry
325 1.1 itojun */
326 1.1 itojun int
327 1.1 itojun set(argc, argv)
328 1.1 itojun int argc;
329 1.1 itojun char **argv;
330 1.1 itojun {
331 1.1 itojun register struct sockaddr_in6 *sin = &sin_m;
332 1.1 itojun register struct sockaddr_dl *sdl;
333 1.1 itojun register struct rt_msghdr *rtm = &(m_rtmsg.m_rtm);
334 1.1 itojun struct addrinfo hints, *res;
335 1.1 itojun int gai_error;
336 1.1 itojun u_char *ea;
337 1.1 itojun char *host = argv[0], *eaddr = argv[1];
338 1.1 itojun
339 1.1 itojun getsocket();
340 1.1 itojun argc -= 2;
341 1.1 itojun argv += 2;
342 1.1 itojun sdl_m = blank_sdl;
343 1.1 itojun sin_m = blank_sin;
344 1.1 itojun
345 1.1 itojun bzero(&hints, sizeof(hints));
346 1.1 itojun hints.ai_family = AF_INET6;
347 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
348 1.1 itojun if (gai_error) {
349 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
350 1.1 itojun gai_strerror(gai_error));
351 1.1 itojun return 1;
352 1.1 itojun }
353 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
354 1.1 itojun ea = (u_char *)LLADDR(&sdl_m);
355 1.1 itojun if (ndp_ether_aton(eaddr, ea) == 0)
356 1.1 itojun sdl_m.sdl_alen = 6;
357 1.1 itojun flags = expire_time = 0;
358 1.1 itojun while (argc-- > 0) {
359 1.1 itojun if (strncmp(argv[0], "temp", 4) == 0) {
360 1.1 itojun struct timeval time;
361 1.1 itojun gettimeofday(&time, 0);
362 1.1 itojun expire_time = time.tv_sec + 20 * 60;
363 1.1 itojun }
364 1.1 itojun argv++;
365 1.1 itojun }
366 1.1 itojun tryagain:
367 1.1 itojun if (rtmsg(RTM_GET) < 0) {
368 1.1 itojun perror(host);
369 1.1 itojun return (1);
370 1.1 itojun }
371 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
372 1.1 itojun sdl = (struct sockaddr_dl *)(sin->sin6_len + (char *)sin);
373 1.1 itojun if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
374 1.1 itojun if (sdl->sdl_family == AF_LINK &&
375 1.1 itojun (rtm->rtm_flags & RTF_LLINFO) &&
376 1.1 itojun !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
377 1.1 itojun case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
378 1.1 itojun case IFT_ISO88024: case IFT_ISO88025:
379 1.1 itojun goto overwrite;
380 1.1 itojun }
381 1.1 itojun goto tryagain;
382 1.1 itojun }
383 1.1 itojun overwrite:
384 1.1 itojun if (sdl->sdl_family != AF_LINK) {
385 1.1 itojun printf("cannot intuit interface index and type for %s\n", host);
386 1.1 itojun return (1);
387 1.1 itojun }
388 1.1 itojun sdl_m.sdl_type = sdl->sdl_type;
389 1.1 itojun sdl_m.sdl_index = sdl->sdl_index;
390 1.1 itojun return (rtmsg(RTM_ADD));
391 1.1 itojun }
392 1.1 itojun
393 1.1 itojun /*
394 1.1 itojun * Display an individual neighbor cache entry
395 1.1 itojun */
396 1.1 itojun void
397 1.1 itojun get(host)
398 1.1 itojun char *host;
399 1.1 itojun {
400 1.1 itojun struct sockaddr_in6 *sin = &sin_m;
401 1.1 itojun struct addrinfo hints, *res;
402 1.1 itojun int gai_error;
403 1.1 itojun
404 1.1 itojun sin_m = blank_sin;
405 1.1 itojun bzero(&hints, sizeof(hints));
406 1.1 itojun hints.ai_family = AF_INET6;
407 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
408 1.1 itojun if (gai_error) {
409 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
410 1.1 itojun gai_strerror(gai_error));
411 1.1 itojun return;
412 1.1 itojun }
413 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
414 1.1 itojun dump(&sin->sin6_addr);
415 1.1 itojun if (found_entry == 0) {
416 1.1 itojun printf("%s (%s) -- no entry\n",
417 1.1 itojun host, inet_ntop(AF_INET6, &sin->sin6_addr, ntop_buf,
418 1.1 itojun sizeof(ntop_buf)));
419 1.1 itojun exit(1);
420 1.1 itojun }
421 1.1 itojun }
422 1.1 itojun
423 1.1 itojun /*
424 1.1 itojun * Delete a neighbor cache entry
425 1.1 itojun */
426 1.1 itojun int
427 1.1 itojun delete(host)
428 1.1 itojun char *host;
429 1.1 itojun {
430 1.1 itojun register struct sockaddr_in6 *sin = &sin_m;
431 1.1 itojun register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
432 1.1 itojun struct sockaddr_dl *sdl;
433 1.1 itojun struct addrinfo hints, *res;
434 1.1 itojun int gai_error;
435 1.1 itojun
436 1.1 itojun getsocket();
437 1.1 itojun sin_m = blank_sin;
438 1.1 itojun
439 1.1 itojun bzero(&hints, sizeof(hints));
440 1.1 itojun hints.ai_family = AF_INET6;
441 1.1 itojun gai_error = getaddrinfo(host, NULL, &hints, &res);
442 1.1 itojun if (gai_error) {
443 1.1 itojun fprintf(stderr, "ndp: %s: %s\n", host,
444 1.1 itojun gai_strerror(gai_error));
445 1.1 itojun return 1;
446 1.1 itojun }
447 1.1 itojun sin->sin6_addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
448 1.1 itojun /*tryagain:*/
449 1.1 itojun if (rtmsg(RTM_GET) < 0) {
450 1.1 itojun perror(host);
451 1.1 itojun return (1);
452 1.1 itojun }
453 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
454 1.1 itojun sdl = (struct sockaddr_dl *)(sin->sin6_len + (char *)sin);
455 1.1 itojun if (IN6_ARE_ADDR_EQUAL(&sin->sin6_addr, &sin_m.sin6_addr)) {
456 1.1 itojun if (sdl->sdl_family == AF_LINK &&
457 1.1 itojun (rtm->rtm_flags & RTF_LLINFO) &&
458 1.1 itojun !(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
459 1.1 itojun case IFT_ETHER: case IFT_FDDI: case IFT_ISO88023:
460 1.1 itojun case IFT_ISO88024: case IFT_ISO88025:
461 1.1 itojun goto delete;
462 1.1 itojun }
463 1.1 itojun }
464 1.1 itojun delete:
465 1.1 itojun if (sdl->sdl_family != AF_LINK) {
466 1.1 itojun printf("cannot locate %s\n", host);
467 1.1 itojun return (1);
468 1.1 itojun }
469 1.1 itojun if (rtmsg(RTM_DELETE) == 0)
470 1.1 itojun printf("%s (%s) deleted\n", host,
471 1.1 itojun inet_ntop(AF_INET6, &sin->sin6_addr, ntop_buf,
472 1.1 itojun sizeof(ntop_buf)));
473 1.1 itojun return 0;
474 1.1 itojun }
475 1.1 itojun
476 1.1 itojun /*
477 1.1 itojun * Dump the entire neighbor cache
478 1.1 itojun */
479 1.1 itojun void
480 1.1 itojun dump(addr)
481 1.1 itojun struct in6_addr *addr;
482 1.1 itojun {
483 1.1 itojun int mib[6];
484 1.1 itojun size_t needed;
485 1.1 itojun char *host, *lim, *buf, *next;
486 1.1 itojun struct rt_msghdr *rtm;
487 1.1 itojun struct sockaddr_in6 *sin;
488 1.1 itojun struct sockaddr_dl *sdl;
489 1.1 itojun extern int h_errno;
490 1.1 itojun struct hostent *hp;
491 1.1 itojun struct in6_nbrinfo *nbi;
492 1.1 itojun struct timeval time;
493 1.1 itojun
494 1.1 itojun /* Print header */
495 1.1 itojun if (!tflag)
496 1.1 itojun printf("%-29.29s %-18.18s %6.6s %-9.9s %2s %4s %4s\n",
497 1.1 itojun "Neighbor", "Linklayer Address", "Netif", "Expire",
498 1.1 itojun "St", "Flgs", "Prbs");
499 1.1 itojun
500 1.1 itojun again:;
501 1.1 itojun mib[0] = CTL_NET;
502 1.1 itojun mib[1] = PF_ROUTE;
503 1.1 itojun mib[2] = 0;
504 1.1 itojun mib[3] = AF_INET6;
505 1.1 itojun mib[4] = NET_RT_FLAGS;
506 1.1 itojun mib[5] = RTF_LLINFO;
507 1.1 itojun if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
508 1.1 itojun quit("route-sysctl-estimate");
509 1.1 itojun if ((buf = malloc(needed)) == NULL)
510 1.1 itojun quit("malloc");
511 1.1 itojun if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
512 1.1 itojun quit("actual retrieval of routing table");
513 1.1 itojun lim = buf + needed;
514 1.1 itojun
515 1.1 itojun for (next = buf; next < lim; next += rtm->rtm_msglen) {
516 1.1 itojun int isrouter = 0, prbs = 0;
517 1.1 itojun
518 1.1 itojun rtm = (struct rt_msghdr *)next;
519 1.1 itojun sin = (struct sockaddr_in6 *)(rtm + 1);
520 1.1 itojun sdl = (struct sockaddr_dl *)(sin + 1);
521 1.1 itojun if (addr) {
522 1.1 itojun if (!IN6_ARE_ADDR_EQUAL(addr, &sin->sin6_addr))
523 1.1 itojun continue;
524 1.1 itojun found_entry = 1;
525 1.1 itojun } else if (IN6_IS_ADDR_MULTICAST(&sin->sin6_addr))
526 1.1 itojun continue;
527 1.1 itojun if (fflag == 1) {
528 1.1 itojun delete((char *)inet_ntop(AF_INET6, &sin->sin6_addr,
529 1.1 itojun ntop_buf, sizeof(ntop_buf)));
530 1.1 itojun continue;
531 1.1 itojun }
532 1.1 itojun host = NULL;
533 1.1 itojun if (nflag == 0) {
534 1.1 itojun hp = gethostbyaddr((char *)&sin->sin6_addr,
535 1.1 itojun sizeof(struct in6_addr), AF_INET6);
536 1.1 itojun if (hp)
537 1.1 itojun host = hp->h_name;
538 1.1 itojun }
539 1.1 itojun if (host == NULL) {
540 1.1 itojun inet_ntop(AF_INET6, &sin->sin6_addr,
541 1.1 itojun ntop_buf, sizeof(ntop_buf));
542 1.1 itojun host = ntop_buf;
543 1.1 itojun }
544 1.1 itojun
545 1.1 itojun gettimeofday(&time, 0);
546 1.1 itojun if (tflag)
547 1.1 itojun ts_print(&time);
548 1.1 itojun
549 1.1 itojun printf("%-29.29s %-18.18s %6.6s", host,
550 1.1 itojun ether_str(sdl),
551 1.1 itojun if_indextoname(sdl->sdl_index, ifix_buf));
552 1.1 itojun
553 1.1 itojun /* Print neighbor discovery specific informations */
554 1.1 itojun putchar(' ');
555 1.1 itojun nbi = getnbrinfo(&sin->sin6_addr, sdl->sdl_index);
556 1.1 itojun if (nbi) {
557 1.1 itojun if (nbi->expire > time.tv_sec) {
558 1.1 itojun printf(" %-9.9s",
559 1.1 itojun sec2str(nbi->expire - time.tv_sec));
560 1.1 itojun }
561 1.1 itojun else if (nbi->expire == 0)
562 1.1 itojun printf(" %-9.9s", "permanent");
563 1.1 itojun else
564 1.1 itojun printf(" %-9.9s", "expired");
565 1.1 itojun
566 1.1 itojun switch(nbi->state) {
567 1.1 itojun case ND6_LLINFO_NOSTATE:
568 1.1 itojun printf(" N");
569 1.1 itojun break;
570 1.1 itojun case ND6_LLINFO_WAITDELETE:
571 1.1 itojun printf(" W");
572 1.1 itojun break;
573 1.1 itojun case ND6_LLINFO_INCOMPLETE:
574 1.1 itojun printf(" I");
575 1.1 itojun break;
576 1.1 itojun case ND6_LLINFO_REACHABLE:
577 1.1 itojun printf(" R");
578 1.1 itojun break;
579 1.1 itojun case ND6_LLINFO_STALE:
580 1.1 itojun printf(" S");
581 1.1 itojun break;
582 1.1 itojun case ND6_LLINFO_DELAY:
583 1.1 itojun printf(" D");
584 1.1 itojun break;
585 1.1 itojun case ND6_LLINFO_PROBE:
586 1.1 itojun printf(" P");
587 1.1 itojun break;
588 1.1 itojun default:
589 1.1 itojun printf(" ?");
590 1.1 itojun break;
591 1.1 itojun }
592 1.1 itojun
593 1.1 itojun isrouter = nbi->isrouter;
594 1.1 itojun prbs = nbi->asked;
595 1.1 itojun }
596 1.1 itojun else {
597 1.1 itojun warnx("failed to get neighbor information");
598 1.1 itojun printf(" ");
599 1.1 itojun }
600 1.1 itojun
601 1.1 itojun /* other flags */
602 1.1 itojun putchar(' ');
603 1.1 itojun {
604 1.1 itojun u_char flgbuf[8], *p = flgbuf;
605 1.1 itojun
606 1.1 itojun flgbuf[0] = '\0';
607 1.1 itojun if (isrouter)
608 1.1 itojun p += sprintf((char *)p, "R");
609 1.1 itojun #ifndef RADISH
610 1.1 itojun if (rtm->rtm_addrs & RTA_NETMASK) {
611 1.1 itojun sin = (struct sockaddr_in6 *)
612 1.1 itojun (sdl->sdl_len + (char *)sdl);
613 1.1 itojun if (!IN6_IS_ADDR_ANY(&sin->sin6_addr))
614 1.1 itojun p += sprintf((char *)p, "P");
615 1.1 itojun if (sin->sin6_len != sizeof(struct sockaddr_in6))
616 1.1 itojun p += sprintf((char *)p, "W");
617 1.1 itojun }
618 1.1 itojun #endif /*RADISH*/
619 1.1 itojun printf("%4s", flgbuf);
620 1.1 itojun }
621 1.1 itojun
622 1.1 itojun putchar(' ');
623 1.1 itojun if (prbs)
624 1.1 itojun printf("% 4d", prbs);
625 1.1 itojun
626 1.1 itojun printf("\n");
627 1.1 itojun }
628 1.1 itojun
629 1.1 itojun if (repeat) {
630 1.1 itojun printf("\n");
631 1.1 itojun sleep(repeat);
632 1.1 itojun goto again;
633 1.1 itojun }
634 1.1 itojun }
635 1.1 itojun
636 1.1 itojun static struct in6_nbrinfo *
637 1.1 itojun getnbrinfo(addr, ifindex)
638 1.1 itojun struct in6_addr *addr;
639 1.1 itojun int ifindex;
640 1.1 itojun {
641 1.1 itojun static struct in6_nbrinfo nbi;
642 1.1 itojun int s;
643 1.1 itojun
644 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
645 1.1 itojun err(1, "socket");
646 1.1 itojun
647 1.1 itojun bzero(&nbi, sizeof(nbi));
648 1.1 itojun if_indextoname(ifindex, nbi.ifname);
649 1.1 itojun nbi.addr = *addr;
650 1.1 itojun if (ioctl(s, SIOCGNBRINFO_IN6, (caddr_t)&nbi) < 0) {
651 1.1 itojun warn("ioctl");
652 1.1 itojun close(s);
653 1.1 itojun return(NULL);
654 1.1 itojun }
655 1.1 itojun
656 1.1 itojun close(s);
657 1.1 itojun return(&nbi);
658 1.1 itojun }
659 1.1 itojun
660 1.1 itojun static char *
661 1.1 itojun ether_str(sdl)
662 1.1 itojun struct sockaddr_dl *sdl;
663 1.1 itojun {
664 1.1 itojun static char ebuf[32];
665 1.1 itojun u_char *cp;
666 1.1 itojun
667 1.1 itojun if (sdl->sdl_alen) {
668 1.1 itojun cp = (u_char *)LLADDR(sdl);
669 1.1 itojun sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
670 1.1 itojun cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
671 1.1 itojun }
672 1.1 itojun else {
673 1.1 itojun sprintf(ebuf, "(incomplete)");
674 1.1 itojun }
675 1.1 itojun
676 1.1 itojun return(ebuf);
677 1.1 itojun }
678 1.1 itojun
679 1.1 itojun int
680 1.1 itojun ndp_ether_aton(a, n)
681 1.1 itojun char *a;
682 1.1 itojun u_char *n;
683 1.1 itojun {
684 1.1 itojun int i, o[6];
685 1.1 itojun
686 1.1 itojun i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o[0], &o[1], &o[2],
687 1.1 itojun &o[3], &o[4], &o[5]);
688 1.1 itojun if (i != 6) {
689 1.1 itojun fprintf(stderr, "ndp: invalid Ethernet address '%s'\n", a);
690 1.1 itojun return (1);
691 1.1 itojun }
692 1.1 itojun for (i=0; i<6; i++)
693 1.1 itojun n[i] = o[i];
694 1.1 itojun return (0);
695 1.1 itojun }
696 1.1 itojun
697 1.1 itojun void
698 1.1 itojun usage()
699 1.1 itojun {
700 1.1 itojun printf("usage: ndp hostname\n");
701 1.1 itojun printf(" ndp -a[nt]\n");
702 1.1 itojun printf(" ndp [-nt] -A wait\n");
703 1.1 itojun printf(" ndp -c[nt]\n");
704 1.1 itojun printf(" ndp -d[nt] hostname\n");
705 1.1 itojun printf(" ndp -f[nt] filename\n");
706 1.1 itojun printf(" ndp -i interface\n");
707 1.1 itojun printf(" ndp -p\n");
708 1.1 itojun printf(" ndp -r\n");
709 1.1 itojun printf(" ndp -s hostname ether_addr [temp]\n");
710 1.1 itojun printf(" ndp -H\n");
711 1.1 itojun printf(" ndp -P\n");
712 1.1 itojun printf(" ndp -R\n");
713 1.1 itojun exit(1);
714 1.1 itojun }
715 1.1 itojun
716 1.1 itojun int
717 1.1 itojun rtmsg(cmd)
718 1.1 itojun int cmd;
719 1.1 itojun {
720 1.1 itojun static int seq;
721 1.1 itojun int rlen;
722 1.1 itojun register struct rt_msghdr *rtm = &m_rtmsg.m_rtm;
723 1.1 itojun register char *cp = m_rtmsg.m_space;
724 1.1 itojun register int l;
725 1.1 itojun
726 1.1 itojun errno = 0;
727 1.1 itojun if (cmd == RTM_DELETE)
728 1.1 itojun goto doit;
729 1.1 itojun bzero((char *)&m_rtmsg, sizeof(m_rtmsg));
730 1.1 itojun rtm->rtm_flags = flags;
731 1.1 itojun rtm->rtm_version = RTM_VERSION;
732 1.1 itojun
733 1.1 itojun switch (cmd) {
734 1.1 itojun default:
735 1.1 itojun fprintf(stderr, "ndp: internal wrong cmd\n");
736 1.1 itojun exit(1);
737 1.1 itojun case RTM_ADD:
738 1.1 itojun rtm->rtm_addrs |= RTA_GATEWAY;
739 1.1 itojun rtm->rtm_rmx.rmx_expire = expire_time;
740 1.1 itojun rtm->rtm_inits = RTV_EXPIRE;
741 1.1 itojun rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
742 1.1 itojun /* FALLTHROUGH */
743 1.1 itojun case RTM_GET:
744 1.1 itojun rtm->rtm_addrs |= RTA_DST;
745 1.1 itojun }
746 1.1 itojun #define NEXTADDR(w, s) \
747 1.1 itojun if (rtm->rtm_addrs & (w)) { \
748 1.1 itojun bcopy((char *)&s, cp, sizeof(s)); cp += sizeof(s);}
749 1.1 itojun
750 1.1 itojun NEXTADDR(RTA_DST, sin_m);
751 1.1 itojun NEXTADDR(RTA_GATEWAY, sdl_m);
752 1.1 itojun NEXTADDR(RTA_NETMASK, so_mask);
753 1.1 itojun
754 1.1 itojun rtm->rtm_msglen = cp - (char *)&m_rtmsg;
755 1.1 itojun doit:
756 1.1 itojun l = rtm->rtm_msglen;
757 1.1 itojun rtm->rtm_seq = ++seq;
758 1.1 itojun rtm->rtm_type = cmd;
759 1.1 itojun if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
760 1.1 itojun if (errno != ESRCH || cmd != RTM_DELETE) {
761 1.1 itojun perror("writing to routing socket");
762 1.1 itojun return (-1);
763 1.1 itojun }
764 1.1 itojun }
765 1.1 itojun do {
766 1.1 itojun l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
767 1.1 itojun } while (l > 0 && (rtm->rtm_seq != seq || rtm->rtm_pid != pid));
768 1.1 itojun if (l < 0)
769 1.1 itojun (void) fprintf(stderr, "ndp: read from routing socket: %s\n",
770 1.1 itojun strerror(errno));
771 1.1 itojun return (0);
772 1.1 itojun }
773 1.1 itojun
774 1.1 itojun void
775 1.1 itojun ifinfo(ifname)
776 1.1 itojun char *ifname;
777 1.1 itojun {
778 1.1 itojun struct in6_ndireq nd;
779 1.1 itojun int s;
780 1.1 itojun
781 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
782 1.1 itojun perror("ndp: socket");
783 1.1 itojun exit(1);
784 1.1 itojun }
785 1.1 itojun bzero(&nd, sizeof(nd));
786 1.1 itojun strcpy(nd.ifname, ifname);
787 1.1 itojun if (ioctl(s, SIOCGIFINFO_IN6, (caddr_t)&nd) < 0) {
788 1.1 itojun perror("ioctl (SIOCGIFINFO_IN6)");
789 1.1 itojun exit(1);
790 1.1 itojun }
791 1.1 itojun #define ND nd.ndi
792 1.1 itojun printf("linkmtu=%d", ND.linkmtu);
793 1.1 itojun printf(", curhlim=%d", ND.chlim);
794 1.1 itojun printf(", basereachable=%ds%dms",
795 1.1 itojun ND.basereachable / 1000, ND.basereachable % 1000);
796 1.1 itojun printf(", reachable=%ds", ND.reachable);
797 1.1 itojun printf(", retrans=%ds%dms\n", ND.retrans / 1000, ND.retrans % 1000);
798 1.1 itojun #undef ND
799 1.1 itojun close(s);
800 1.1 itojun }
801 1.1 itojun
802 1.1 itojun void
803 1.1 itojun rtrlist()
804 1.1 itojun {
805 1.1 itojun struct in6_drlist dr;
806 1.1 itojun int s, i;
807 1.1 itojun struct timeval time;
808 1.1 itojun
809 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
810 1.1 itojun perror("ndp: socket");
811 1.1 itojun exit(1);
812 1.1 itojun }
813 1.1 itojun bzero(&dr, sizeof(dr));
814 1.1 itojun strcpy(dr.ifname, "lo0"); /* dummy */
815 1.1 itojun if (ioctl(s, SIOCGDRLST_IN6, (caddr_t)&dr) < 0) {
816 1.1 itojun perror("ioctl (SIOCGDRLST_IN6)");
817 1.1 itojun exit(1);
818 1.1 itojun }
819 1.1 itojun #define DR dr.defrouter[i]
820 1.1 itojun for (i = 0 ; DR.if_index && i < PRLSTSIZ ; i++) {
821 1.1 itojun printf("%s if=%s", inet_ntop(AF_INET6, &DR.rtaddr,
822 1.1 itojun ntop_buf, sizeof(ntop_buf)),
823 1.1 itojun if_indextoname(DR.if_index, ifix_buf));
824 1.1 itojun printf(", flags=%s%s",
825 1.1 itojun DR.flags & ND_RA_FLAG_MANAGED ? "M" : "",
826 1.1 itojun DR.flags & ND_RA_FLAG_OTHER ? "O" : "");
827 1.1 itojun gettimeofday(&time, 0);
828 1.1 itojun if (DR.expire == 0)
829 1.1 itojun printf(", expire=Never\n");
830 1.1 itojun else
831 1.1 itojun printf(", expire=%s\n",
832 1.1 itojun sec2str(DR.expire - time.tv_sec));
833 1.1 itojun }
834 1.1 itojun #undef DR
835 1.1 itojun close(s);
836 1.1 itojun }
837 1.1 itojun
838 1.1 itojun void
839 1.1 itojun plist()
840 1.1 itojun {
841 1.1 itojun struct in6_prlist pr;
842 1.1 itojun int s, i;
843 1.1 itojun struct timeval time;
844 1.1 itojun
845 1.1 itojun gettimeofday(&time, 0);
846 1.1 itojun
847 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
848 1.1 itojun perror("ndp: socket");
849 1.1 itojun exit(1);
850 1.1 itojun }
851 1.1 itojun bzero(&pr, sizeof(pr));
852 1.1 itojun strcpy(pr.ifname, "lo0"); /* dummy */
853 1.1 itojun if (ioctl(s, SIOCGPRLST_IN6, (caddr_t)&pr) < 0) {
854 1.1 itojun perror("ioctl (SIOCGPRLST_IN6)");
855 1.1 itojun exit(1);
856 1.1 itojun }
857 1.1 itojun #define PR pr.prefix[i]
858 1.1 itojun for (i = 0; PR.if_index && i < PRLSTSIZ ; i++) {
859 1.1 itojun printf("%s/%d if=%s\n",
860 1.1 itojun inet_ntop(AF_INET6, &PR.prefix, ntop_buf,
861 1.1 itojun sizeof(ntop_buf)), PR.prefixlen,
862 1.1 itojun if_indextoname(PR.if_index, ifix_buf));
863 1.1 itojun gettimeofday(&time, 0);
864 1.1 itojun printf(" flags=%s%s",
865 1.1 itojun PR.raflags.onlink ? "L" : "",
866 1.1 itojun PR.raflags.autonomous ? "A" : "");
867 1.1 itojun if (PR.vltime == ND6_INFINITE_LIFETIME)
868 1.1 itojun printf(" vltime=infinity");
869 1.1 itojun else
870 1.1 itojun printf(" vltime=%ld", (long)PR.vltime);
871 1.1 itojun if (PR.pltime == ND6_INFINITE_LIFETIME)
872 1.1 itojun printf(", pltime=infinity");
873 1.1 itojun else
874 1.1 itojun printf(", pltime=%ld", (long)PR.pltime);
875 1.1 itojun if (PR.expire == 0)
876 1.1 itojun printf(", expire=Never\n");
877 1.1 itojun else if (PR.expire >= time.tv_sec)
878 1.1 itojun printf(", expire=%s\n",
879 1.1 itojun sec2str(PR.expire - time.tv_sec));
880 1.1 itojun else
881 1.1 itojun printf(", expired\n");
882 1.1 itojun if (PR.advrtrs) {
883 1.1 itojun int j;
884 1.1 itojun printf(" advertised by\n");
885 1.1 itojun for (j = 0; j < PR.advrtrs; j++) {
886 1.1 itojun printf(" %s\n",
887 1.1 itojun inet_ntop(AF_INET6, &PR.advrtr[j],
888 1.1 itojun ntop_buf,
889 1.1 itojun sizeof(ntop_buf)));
890 1.1 itojun }
891 1.1 itojun if (PR.advrtrs > DRLSTSIZ)
892 1.1 itojun printf(" and %d routers\n",
893 1.1 itojun PR.advrtrs - DRLSTSIZ);
894 1.1 itojun }
895 1.1 itojun else
896 1.1 itojun printf(" No advertising router\n");
897 1.1 itojun }
898 1.1 itojun #undef PR
899 1.1 itojun close(s);
900 1.1 itojun }
901 1.1 itojun
902 1.1 itojun void
903 1.1 itojun pfx_flush()
904 1.1 itojun {
905 1.1 itojun char dummyif[IFNAMSIZ+8];
906 1.1 itojun int s;
907 1.1 itojun
908 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
909 1.1 itojun err(1, "socket");
910 1.1 itojun strcpy(dummyif, "lo0"); /* dummy */
911 1.1 itojun if (ioctl(s, SIOCSPFXFLUSH_IN6, (caddr_t)&dummyif) < 0)
912 1.1 itojun err(1, "ioctl(SIOCSPFXFLUSH_IN6)");
913 1.1 itojun }
914 1.1 itojun
915 1.1 itojun void
916 1.1 itojun rtr_flush()
917 1.1 itojun {
918 1.1 itojun char dummyif[IFNAMSIZ+8];
919 1.1 itojun int s;
920 1.1 itojun
921 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
922 1.1 itojun err(1, "socket");
923 1.1 itojun strcpy(dummyif, "lo0"); /* dummy */
924 1.1 itojun if (ioctl(s, SIOCSRTRFLUSH_IN6, (caddr_t)&dummyif) < 0)
925 1.1 itojun err(1, "ioctl(SIOCSRTRFLUSH_IN6)");
926 1.1 itojun }
927 1.1 itojun
928 1.1 itojun void
929 1.1 itojun harmonize_rtr()
930 1.1 itojun {
931 1.1 itojun char dummyif[IFNAMSIZ+8];
932 1.1 itojun int s;
933 1.1 itojun
934 1.1 itojun if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
935 1.1 itojun perror("ndp: socket");
936 1.1 itojun exit(1);
937 1.1 itojun }
938 1.1 itojun strcpy(dummyif, "lo0"); /* dummy */
939 1.1 itojun if (ioctl(s, SIOCSNDFLUSH_IN6, (caddr_t)&dummyif) < 0) {
940 1.1 itojun perror("ioctl (SIOCSNDFLUSH_IN6)");
941 1.1 itojun exit(1);
942 1.1 itojun }
943 1.1 itojun }
944 1.1 itojun
945 1.1 itojun void
946 1.1 itojun quit(msg)
947 1.1 itojun char *msg;
948 1.1 itojun {
949 1.1 itojun fprintf(stderr, "%s\n", msg);
950 1.1 itojun exit(1);
951 1.1 itojun }
952 1.1 itojun
953 1.1 itojun static char *
954 1.1 itojun sec2str(total)
955 1.1 itojun time_t total;
956 1.1 itojun {
957 1.1 itojun static char result[256];
958 1.1 itojun int days, hours, mins, secs;
959 1.1 itojun int first = 1;
960 1.1 itojun char *p = result;
961 1.1 itojun
962 1.1 itojun days = total / 3600 / 24;
963 1.1 itojun hours = (total / 3600) % 24;
964 1.1 itojun mins = (total / 60) % 60;
965 1.1 itojun secs = total % 60;
966 1.1 itojun
967 1.1 itojun if (days) {
968 1.1 itojun first = 0;
969 1.1 itojun p += sprintf(p, "%dd", days);
970 1.1 itojun }
971 1.1 itojun if (!first || hours) {
972 1.1 itojun first = 0;
973 1.1 itojun p += sprintf(p, "%dh", hours);
974 1.1 itojun }
975 1.1 itojun if (!first || mins) {
976 1.1 itojun first = 0;
977 1.1 itojun p += sprintf(p, "%dm", mins);
978 1.1 itojun }
979 1.1 itojun sprintf(p, "%ds", secs);
980 1.1 itojun
981 1.1 itojun return(result);
982 1.1 itojun }
983 1.1 itojun
984 1.1 itojun /*
985 1.1 itojun * Print the timestamp
986 1.1 itojun * from tcpdump/util.c
987 1.1 itojun */
988 1.1 itojun static void
989 1.1 itojun ts_print(tvp)
990 1.1 itojun const struct timeval *tvp;
991 1.1 itojun {
992 1.1 itojun int s;
993 1.1 itojun
994 1.1 itojun /* Default */
995 1.1 itojun s = (tvp->tv_sec + thiszone) % 86400;
996 1.1 itojun (void)printf("%02d:%02d:%02d.%06u ",
997 1.1 itojun s / 3600, (s % 3600) / 60, s % 60, (u_int32_t)tvp->tv_usec);
998 1.1 itojun }
999