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