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/time.h>
36#include <sys/socket.h>
37#include <sys/queue.h>
38
39#include <net/if.h>
40#include <netinet/in.h>
41#include <netinet/icmp6.h>
42#include <arpa/inet.h>
43
44#include <syslog.h>
45#include <time.h>
46#include <stdio.h>
47#include <string.h>
48#include <errno.h>
49
50#include "rtsold.h"
51
52static FILE *fp;
53
54extern struct ifinfo *iflist;
55
56static void dump_interface_status(void);
57static const char * const ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
58
59static void
60dump_interface_status(void)
61{
62	struct ifinfo *ifi;
63	struct rainfo *rai;
64	struct ra_opt *rao;
65	struct timeval now;
66	char ntopbuf[INET6_ADDRSTRLEN];
67
68	gettimeofday(&now, NULL);
69
70	TAILQ_FOREACH(ifi, &ifinfo_head, ifi_next) {
71		fprintf(fp, "Interface %s\n", ifi->ifname);
72		fprintf(fp, "  probe interval: ");
73		if (ifi->probeinterval) {
74			fprintf(fp, "%d\n", ifi->probeinterval);
75			fprintf(fp, "  probe timer: %d\n", ifi->probetimer);
76		} else {
77			fprintf(fp, "infinity\n");
78			fprintf(fp, "  no probe timer\n");
79		}
80		fprintf(fp, "  interface status: %s\n",
81		    ifi->active > 0 ? "active" : "inactive");
82		fprintf(fp, "  other config: %s\n",
83		    ifi->otherconfig ? "on" : "off");
84		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifi->state]);
85		fprintf(fp, "  carrier detection: %s\n",
86		    ifi->mediareqok ? "available" : "unavailable");
87		fprintf(fp, "  probes: %d, dadcount = %d\n",
88		    ifi->probes, ifi->dadcount);
89		if (ifi->timer.tv_sec == tm_max.tv_sec &&
90		    ifi->timer.tv_usec == tm_max.tv_usec)
91			fprintf(fp, "  no timer\n");
92		else {
93			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
94			    (int)ifi->timer.tv_sec,
95			    (int)ifi->timer.tv_usec,
96			    (ifi->expire.tv_sec < now.tv_sec) ? "expired"
97			    : sec2str(&ifi->expire));
98		}
99		fprintf(fp, "  number of valid RAs: %d\n", ifi->racnt);
100
101		TAILQ_FOREACH(rai, &ifi->ifi_rainfo, rai_next) {
102			fprintf(fp, "   RA from %s\n",
103			    inet_ntop(AF_INET6, &rai->rai_saddr.sin6_addr,
104				ntopbuf, sizeof(ntopbuf)));
105			TAILQ_FOREACH(rao, &rai->rai_ra_opt, rao_next) {
106				fprintf(fp, "    option: ");
107				switch (rao->rao_type) {
108				case ND_OPT_RDNSS:
109					fprintf(fp, "RDNSS: %s (expire: %s)\n",
110					    (char *)rao->rao_msg,
111					    sec2str(&rao->rao_expire));
112					break;
113				case ND_OPT_DNSSL:
114					fprintf(fp, "DNSSL: %s (expire: %s)\n",
115					    (char *)rao->rao_msg,
116					    sec2str(&rao->rao_expire));
117					break;
118				default:
119					break;
120				}
121			}
122			fprintf(fp, "\n");
123		}
124	}
125}
126
127void
128rtsold_dump_file(const char *dumpfile)
129{
130	if ((fp = fopen(dumpfile, "w")) == NULL) {
131		warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
132		    dumpfile, strerror(errno));
133		return;
134	}
135	dump_interface_status();
136	fclose(fp);
137}
138
139const char *
140sec2str(const struct timeval *total)
141{
142	static char result[256];
143	int days, hours, mins, secs;
144	int first = 1;
145	char *p = result;
146	char *ep = &result[sizeof(result)];
147	int n;
148	struct timeval now;
149	time_t tsec;
150
151	gettimeofday(&now, NULL);
152	tsec  = total->tv_sec;
153	tsec += total->tv_usec / 1000000;
154	tsec -= now.tv_sec;
155	tsec -= now.tv_usec / 1000000;
156
157	days = tsec / 3600 / 24;
158	hours = (tsec / 3600) % 24;
159	mins = (tsec / 60) % 60;
160	secs = tsec % 60;
161
162	if (days) {
163		first = 0;
164		n = snprintf(p, ep - p, "%dd", days);
165		if (n < 0 || n >= ep - p)
166			return "?";
167		p += n;
168	}
169	if (!first || hours) {
170		first = 0;
171		n = snprintf(p, ep - p, "%dh", hours);
172		if (n < 0 || n >= ep - p)
173			return "?";
174		p += n;
175	}
176	if (!first || mins) {
177		first = 0;
178		n = snprintf(p, ep - p, "%dm", mins);
179		if (n < 0 || n >= ep - p)
180			return "?";
181		p += n;
182	}
183	snprintf(p, ep - p, "%ds", secs);
184
185	return (result);
186}
187