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