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