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