No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / rtsold / dump.c
blobcd63bca2157d43966f21c5c5c59e6c402cab6d9c
1 /* $NetBSD: dump.c,v 1.8 2004/01/03 01:40:31 itojun Exp $ */
2 /* $KAME: dump.c,v 1.10 2002/05/31 10:10:03 itojun Exp $ */
4 /*
5 * Copyright (C) 1999 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <netinet/icmp6.h>
41 #include <syslog.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <errno.h>
47 #include "rtsold.h"
49 static FILE *fp;
51 extern struct ifinfo *iflist;
53 static void dump_interface_status __P((void));
54 static const char *sec2str __P((time_t));
55 const char *ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
57 static void
58 dump_interface_status(void)
60 struct ifinfo *ifinfo;
61 struct timeval now;
63 gettimeofday(&now, NULL);
65 for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
66 fprintf(fp, "Interface %s\n", ifinfo->ifname);
67 fprintf(fp, " probe interval: ");
68 if (ifinfo->probeinterval) {
69 fprintf(fp, "%d\n", ifinfo->probeinterval);
70 fprintf(fp, " probe timer: %d\n", ifinfo->probetimer);
71 } else {
72 fprintf(fp, "infinity\n");
73 fprintf(fp, " no probe timer\n");
75 fprintf(fp, " interface status: %s\n",
76 ifinfo->active > 0 ? "active" : "inactive");
77 fprintf(fp, " rtsold status: %s\n", ifstatstr[ifinfo->state]);
78 fprintf(fp, " carrier detection: %s\n",
79 ifinfo->mediareqok ? "available" : "unavailable");
80 fprintf(fp, " probes: %d, dadcount = %d\n",
81 ifinfo->probes, ifinfo->dadcount);
82 if (ifinfo->timer.tv_sec == tm_max.tv_sec &&
83 ifinfo->timer.tv_usec == tm_max.tv_usec)
84 fprintf(fp, " no timer\n");
85 else {
86 fprintf(fp, " timer: interval=%d:%d, expire=%s\n",
87 (int)ifinfo->timer.tv_sec,
88 (int)ifinfo->timer.tv_usec,
89 (ifinfo->expire.tv_sec < now.tv_sec) ? "expired"
90 : sec2str(ifinfo->expire.tv_sec - now.tv_sec));
92 fprintf(fp, " number of valid RAs: %d\n", ifinfo->racnt);
96 void
97 rtsold_dump_file(const char *dumpfile)
99 if ((fp = fopen(dumpfile, "w")) == NULL) {
100 warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
101 dumpfile, strerror(errno));
102 return;
104 dump_interface_status();
105 fclose(fp);
108 static const char *
109 sec2str(time_t total)
111 static char result[256];
112 int days, hours, mins, secs;
113 int first = 1;
114 char *p = result;
115 char *ep = &result[sizeof(result)];
116 int n;
118 days = total / 3600 / 24;
119 hours = (total / 3600) % 24;
120 mins = (total / 60) % 60;
121 secs = total % 60;
123 if (days) {
124 first = 0;
125 n = snprintf(p, ep - p, "%dd", days);
126 if (n < 0 || n >= ep - p)
127 return "?";
128 p += n;
130 if (!first || hours) {
131 first = 0;
132 n = snprintf(p, ep - p, "%dh", hours);
133 if (n < 0 || n >= ep - p)
134 return "?";
135 p += n;
137 if (!first || mins) {
138 first = 0;
139 n = snprintf(p, ep - p, "%dm", mins);
140 if (n < 0 || n >= ep - p)
141 return "?";
142 p += n;
144 snprintf(p, ep - p, "%ds", secs);
145 return(result);