xref: /freebsd/usr.sbin/rtsold/dump.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
1 /*	$KAME: dump.c,v 1.13 2003/10/05 00:09:36 itojun Exp $	*/
2 
3 /*
4  * Copyright (C) 1999 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/queue.h>
37 
38 #include <net/if.h>
39 #include <netinet/in.h>
40 #include <netinet/icmp6.h>
41 #include <arpa/inet.h>
42 
43 #include <syslog.h>
44 #include <time.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <errno.h>
48 
49 #include "rtsold.h"
50 
51 static FILE *fp;
52 
53 static void dump_interface_status(void);
54 static const char * const ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
55 
56 static void
57 dump_interface_status(void)
58 {
59 	struct ifinfo *ifi;
60 	struct rainfo *rai;
61 	struct ra_opt *rao;
62 	struct timespec now;
63 	char ntopbuf[INET6_ADDRSTRLEN];
64 
65 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
66 
67 	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
68 		fprintf(fp, "Interface %s\n", ifi->ifname);
69 		fprintf(fp, "  probe interval: ");
70 		if (ifi->probeinterval) {
71 			fprintf(fp, "%d\n", ifi->probeinterval);
72 			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
73 		} else {
74 			fprintf(fp, "infinity\n");
75 			fprintf(fp, "  no probe timer\n");
76 		}
77 		fprintf(fp, "  interface status: %s\n",
78 		    ifi->active > 0 ? "active" : "inactive");
79 		fprintf(fp, "  other config: %s\n",
80 		    ifi->otherconfig ? "on" : "off");
81 		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifi->state]);
82 		fprintf(fp, "  carrier detection: %s\n",
83 		    ifi->mediareqok ? "available" : "unavailable");
84 		fprintf(fp, "  probes: %d, dadcount = %d\n",
85 		    ifi->probes, ifi->dadcount);
86 		if (ifi->timer.tv_sec == tm_max.tv_sec &&
87 		    ifi->timer.tv_nsec == tm_max.tv_nsec)
88 			fprintf(fp, "  no timer\n");
89 		else {
90 			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
91 			    (int)ifi->timer.tv_sec,
92 			    (int)ifi->timer.tv_nsec / 1000,
93 			    (ifi->expire.tv_sec < now.tv_sec) ? "expired"
94 			    : sec2str(&ifi->expire));
95 		}
96 		fprintf(fp, "  number of valid RAs: %d\n", ifi->racnt);
97 
98 		TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
99 			fprintf(fp, "   RA from %s\n",
100 			    inet_ntop(AF_INET6, &rai->rai_saddr.sin6_addr,
101 				ntopbuf, sizeof(ntopbuf)));
102 			TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
103 				fprintf(fp, "    option: ");
104 				switch (rao->rao_type) {
105 				case ND_OPT_RDNSS:
106 					fprintf(fp, "RDNSS: %s (expire: %s)\n",
107 					    (char *)rao->rao_msg,
108 					    sec2str(&rao->rao_expire));
109 					break;
110 				case ND_OPT_DNSSL:
111 					fprintf(fp, "DNSSL: %s (expire: %s)\n",
112 					    (char *)rao->rao_msg,
113 					    sec2str(&rao->rao_expire));
114 					break;
115 				default:
116 					break;
117 				}
118 			}
119 			fprintf(fp, "\n");
120 		}
121 	}
122 }
123 
124 void
125 rtsold_dump_file(const char *dumpfile)
126 {
127 	if ((fp = fopen(dumpfile, "w")) == NULL) {
128 		warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
129 		    dumpfile, strerror(errno));
130 		return;
131 	}
132 	dump_interface_status();
133 	fclose(fp);
134 }
135 
136 const char *
137 sec2str(const struct timespec *total)
138 {
139 	static char result[256];
140 	int days, hours, mins, secs;
141 	int first = 1;
142 	char *p = result;
143 	char *ep = &result[sizeof(result)];
144 	int n;
145 	struct timespec now;
146 	time_t tsec;
147 
148 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
149 	tsec  = total->tv_sec;
150 	tsec += total->tv_nsec / 1000 / 1000000;
151 	tsec -= now.tv_sec;
152 	tsec -= now.tv_nsec / 1000 / 1000000;
153 
154 	days = tsec / 3600 / 24;
155 	hours = (tsec / 3600) % 24;
156 	mins = (tsec / 60) % 60;
157 	secs = tsec % 60;
158 
159 	if (days) {
160 		first = 0;
161 		n = snprintf(p, ep - p, "%dd", days);
162 		if (n < 0 || n >= ep - p)
163 			return "?";
164 		p += n;
165 	}
166 	if (!first || hours) {
167 		first = 0;
168 		n = snprintf(p, ep - p, "%dh", hours);
169 		if (n < 0 || n >= ep - p)
170 			return "?";
171 		p += n;
172 	}
173 	if (!first || mins) {
174 		first = 0;
175 		n = snprintf(p, ep - p, "%dm", mins);
176 		if (n < 0 || n >= ep - p)
177 			return "?";
178 		p += n;
179 	}
180 	snprintf(p, ep - p, "%ds", secs);
181 
182 	return (result);
183 }
184