dump.c revision 124524
134689Sbde/*	$KAME: dump.c,v 1.13 2003/10/05 00:09:36 itojun Exp $	*/
250476Speter
31573Srgrimes/*
4156813Sru * Copyright (C) 1999 WIDE Project.
5156813Sru * All rights reserved.
6270187Sian *
7270187Sian * Redistribution and use in source and binary forms, with or without
8270187Sian * modification, are permitted provided that the following conditions
9270187Sian * are met:
1034689Sbde * 1. Redistributions of source code must retain the above copyright
11292339Semaste *    notice, this list of conditions and the following disclaimer.
12270187Sian * 2. Redistributions in binary form must reproduce the above copyright
13205113Simp *    notice, this list of conditions and the following disclaimer in the
14258398Speter *    documentation and/or other materials provided with the distribution.
15215127Sed * 3. Neither the name of the project nor the names of its contributors
16270187Sian *    may be used to endorse or promote products derived from this software
17270187Sian *    without specific prior written permission.
18205113Simp *
19270187Sian * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20205113Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21270187Sian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22233337Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23205113Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24270187Sian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25205113Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26205113Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27205113Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28270187Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29205113Simp * SUCH DAMAGE.
30215127Sed *
31205113Simp * $FreeBSD: head/usr.sbin/rtsold/dump.c 124524 2004-01-14 17:16:19Z ume $
32205113Simp */
33270187Sian
34205113Simp#include <sys/types.h>
35205113Simp#include <sys/time.h>
36205113Simp#include <sys/socket.h>
37270187Sian
38205113Simp#include <net/if.h>
39270187Sian#include <netinet/in.h>
40295131Sjhb#include <netinet/icmp6.h>
41205113Simp
42205113Simp#include <syslog.h>
43322094Smarius#include <time.h>
44205113Simp#include <stdio.h>
45205113Simp#include <string.h>
46205113Simp#include <errno.h>
47255180Semaste
48205113Simp#include "rtsold.h"
49205113Simp
50275040Sdteskestatic FILE *fp;
51205113Simp
52205113Simpextern struct ifinfo *iflist;
53205113Simp
54205113Simpstatic void dump_interface_status __P((void));
55270187Sianstatic char *sec2str __P((time_t));
56205113Simpchar *ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
57205113Simp
58205113Simpstatic void
59205113Simpdump_interface_status(void)
60270187Sian{
61270187Sian	struct ifinfo *ifinfo;
62207842Smm	struct timeval now;
63279506Sngie
64241774Suqs	gettimeofday(&now, NULL);
65205113Simp
66270187Sian	for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
67205113Simp		fprintf(fp, "Interface %s\n", ifinfo->ifname);
68205113Simp		fprintf(fp, "  probe interval: ");
69280438Sken		if (ifinfo->probeinterval) {
70235537Sgber			fprintf(fp, "%d\n", ifinfo->probeinterval);
71244557Sbrooks			fprintf(fp, "  probe timer: %d\n", ifinfo->probetimer);
72270187Sian		} else {
73205113Simp			fprintf(fp, "infinity\n");
74292973Sngie			fprintf(fp, "  no probe timer\n");
75205113Simp		}
76205113Simp		fprintf(fp, "  interface status: %s\n",
77205113Simp		    ifinfo->active > 0 ? "active" : "inactive");
78205113Simp		fprintf(fp, "  other config: %s\n",
79210682Srpaulo		    ifinfo->otherconfig ? "on" : "off");
80221807Sstas		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifinfo->state]);
81278569Sngie		fprintf(fp, "  carrier detection: %s\n",
82270187Sian		    ifinfo->mediareqok ? "available" : "unavailable");
83205113Simp		fprintf(fp, "  probes: %d, dadcount = %d\n",
84210690Srpaulo		    ifinfo->probes, ifinfo->dadcount);
85270187Sian		if (ifinfo->timer.tv_sec == tm_max.tv_sec &&
86205113Simp		    ifinfo->timer.tv_usec == tm_max.tv_usec)
87205113Simp			fprintf(fp, "  no timer\n");
88294364Sian		else {
89205113Simp			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
90205113Simp			    (int)ifinfo->timer.tv_sec,
91205113Simp			    (int)ifinfo->timer.tv_usec,
92234772Sjlh			    (ifinfo->expire.tv_sec < now.tv_sec) ? "expired"
93228904Sed			    : sec2str(ifinfo->expire.tv_sec - now.tv_sec));
94270187Sian		}
95205113Simp		fprintf(fp, "  number of valid RAs: %d\n", ifinfo->racnt);
96205113Simp	}
97205113Simp}
98263019Sbapt
99205113Simpvoid
100205113Simprtsold_dump_file(char *dumpfile)
101205113Simp{
102255597Sdes	if ((fp = fopen(dumpfile, "w")) == NULL) {
103205113Simp		warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
104205113Simp		    dumpfile, strerror(errno));
105270187Sian		return;
106205113Simp	}
107245652Sneel	dump_interface_status();
108205113Simp	fclose(fp);
109205113Simp}
110270187Sian
111247779Sbaptstatic char *
112205113Simpsec2str(time_t total)
113270187Sian{
114241823Smarcel	static char result[256];
115260013Sjmmv	int days, hours, mins, secs;
116260013Sjmmv	int first = 1;
11734689Sbde	char *p = result;
118270187Sian	char *ep = &result[sizeof(result)];
119270187Sian	int n;
120270187Sian
121270187Sian	days = total / 3600 / 24;
122270187Sian	hours = (total / 3600) % 24;
123270187Sian	mins = (total / 60) % 60;
124270187Sian	secs = total % 60;
125270187Sian
126270187Sian	if (days) {
127270187Sian		first = 0;
128270187Sian		n = snprintf(p, ep - p, "%dd", days);
129275040Sdteske		if (n < 0 || n >= ep - p)
130270187Sian			return "?";
131270187Sian		p += n;
132270187Sian	}
133313199Sngie	if (!first || hours) {
134270187Sian		first = 0;
135270187Sian		n = snprintf(p, ep - p, "%dh", hours);
136270187Sian		if (n < 0 || n >= ep - p)
137278569Sngie			return "?";
138270187Sian		p += n;
139270187Sian	}
140270187Sian	if (!first || mins) {
141270187Sian		first = 0;
142270187Sian		n = snprintf(p, ep - p, "%dm", mins);
143270187Sian		if (n < 0 || n >= ep - p)
144270187Sian			return "?";
145270187Sian		p += n;
146292588Sdelphij	}
147270187Sian	snprintf(p, ep - p, "%ds", secs);
148183242Ssam	return(result);
149183242Ssam}
150156813Sru