dump.c revision 118786
1/*	$KAME: dump.c,v 1.12 2003/04/11 10:14:55 jinmei 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 118786 2003-08-11 15:49:47Z ume $
32 */
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/socket.h>
37
38#include <net/if.h>
39#include <netinet/in.h>
40#include <netinet/icmp6.h>
41
42#include <syslog.h>
43#include <time.h>
44#include <stdio.h>
45#include <string.h>
46#include <errno.h>
47
48#include "rtsold.h"
49
50static FILE *fp;
51
52extern struct ifinfo *iflist;
53
54static void dump_interface_status __P((void));
55static char *sec2str __P((time_t));
56char *ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
57
58static void
59dump_interface_status()
60{
61	struct ifinfo *ifinfo;
62	struct timeval now;
63
64	gettimeofday(&now, NULL);
65
66	for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
67		fprintf(fp, "Interface %s\n", ifinfo->ifname);
68		fprintf(fp, "  probe interval: ");
69		if (ifinfo->probeinterval) {
70			fprintf(fp, "%d\n", ifinfo->probeinterval);
71			fprintf(fp, "  probe timer: %d\n", ifinfo->probetimer);
72		} else {
73			fprintf(fp, "infinity\n");
74			fprintf(fp, "  no probe timer\n");
75		}
76		fprintf(fp, "  interface status: %s\n",
77		    ifinfo->active > 0 ? "active" : "inactive");
78		fprintf(fp, "  other config: %s\n",
79		    ifinfo->otherconfig ? "on" : "off");
80		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifinfo->state]);
81		fprintf(fp, "  carrier detection: %s\n",
82		    ifinfo->mediareqok ? "available" : "unavailable");
83		fprintf(fp, "  probes: %d, dadcount = %d\n",
84		    ifinfo->probes, ifinfo->dadcount);
85		if (ifinfo->timer.tv_sec == tm_max.tv_sec &&
86		    ifinfo->timer.tv_usec == tm_max.tv_usec)
87			fprintf(fp, "  no timer\n");
88		else {
89			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
90			    (int)ifinfo->timer.tv_sec,
91			    (int)ifinfo->timer.tv_usec,
92			    (ifinfo->expire.tv_sec < now.tv_sec) ? "expired"
93			    : sec2str(ifinfo->expire.tv_sec - now.tv_sec));
94		}
95		fprintf(fp, "  number of valid RAs: %d\n", ifinfo->racnt);
96	}
97}
98
99void
100rtsold_dump_file(dumpfile)
101	char *dumpfile;
102{
103	if ((fp = fopen(dumpfile, "w")) == NULL) {
104		warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
105		    dumpfile, strerror(errno));
106		return;
107	}
108	dump_interface_status();
109	fclose(fp);
110}
111
112static char *
113sec2str(total)
114	time_t total;
115{
116	static char result[256];
117	int days, hours, mins, secs;
118	int first = 1;
119	char *p = result;
120	char *ep = &result[sizeof(result)];
121	int n;
122
123	days = total / 3600 / 24;
124	hours = (total / 3600) % 24;
125	mins = (total / 60) % 60;
126	secs = total % 60;
127
128	if (days) {
129		first = 0;
130		n = snprintf(p, ep - p, "%dd", days);
131		if (n < 0 || n >= ep - p)
132			return "?";
133		p += n;
134	}
135	if (!first || hours) {
136		first = 0;
137		n = snprintf(p, ep - p, "%dh", hours);
138		if (n < 0 || n >= ep - p)
139			return "?";
140		p += n;
141	}
142	if (!first || mins) {
143		first = 0;
144		n = snprintf(p, ep - p, "%dm", mins);
145		if (n < 0 || n >= ep - p)
146			return "?";
147		p += n;
148	}
149	snprintf(p, ep - p, "%ds", secs);
150	return(result);
151}
152