dump.c revision 124524
1124524Sume/*	$KAME: dump.c,v 1.13 2003/10/05 00:09:36 itojun Exp $	*/
266776Skris
355163Sshin/*
455163Sshin * Copyright (C) 1999 WIDE Project.
555163Sshin * All rights reserved.
662632Skris *
755163Sshin * Redistribution and use in source and binary forms, with or without
855163Sshin * modification, are permitted provided that the following conditions
955163Sshin * are met:
1055163Sshin * 1. Redistributions of source code must retain the above copyright
1155163Sshin *    notice, this list of conditions and the following disclaimer.
1255163Sshin * 2. Redistributions in binary form must reproduce the above copyright
1355163Sshin *    notice, this list of conditions and the following disclaimer in the
1455163Sshin *    documentation and/or other materials provided with the distribution.
1555163Sshin * 3. Neither the name of the project nor the names of its contributors
1655163Sshin *    may be used to endorse or promote products derived from this software
1755163Sshin *    without specific prior written permission.
1862632Skris *
1955163Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2055163Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2155163Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2255163Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2355163Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2455163Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2555163Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2655163Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2755163Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2855163Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2955163Sshin * SUCH DAMAGE.
3055163Sshin *
3155163Sshin * $FreeBSD: head/usr.sbin/rtsold/dump.c 124524 2004-01-14 17:16:19Z ume $
3255163Sshin */
3355163Sshin
3455163Sshin#include <sys/types.h>
3555163Sshin#include <sys/time.h>
3662632Skris#include <sys/socket.h>
3755163Sshin
3862632Skris#include <net/if.h>
3955163Sshin#include <netinet/in.h>
4055163Sshin#include <netinet/icmp6.h>
4155163Sshin
4255163Sshin#include <syslog.h>
4355163Sshin#include <time.h>
4455163Sshin#include <stdio.h>
4555163Sshin#include <string.h>
4655163Sshin#include <errno.h>
4755163Sshin
4855163Sshin#include "rtsold.h"
4955163Sshin
5055163Sshinstatic FILE *fp;
5155163Sshin
5255163Sshinextern struct ifinfo *iflist;
5355163Sshin
5462632Skrisstatic void dump_interface_status __P((void));
5555163Sshinstatic char *sec2str __P((time_t));
5655163Sshinchar *ifstatstr[] = {"IDLE", "DELAY", "PROBE", "DOWN", "TENTATIVE"};
5755163Sshin
5855163Sshinstatic void
59124524Sumedump_interface_status(void)
6055163Sshin{
6155163Sshin	struct ifinfo *ifinfo;
6255163Sshin	struct timeval now;
6355163Sshin
6455163Sshin	gettimeofday(&now, NULL);
6555163Sshin
6655163Sshin	for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) {
6755163Sshin		fprintf(fp, "Interface %s\n", ifinfo->ifname);
6855163Sshin		fprintf(fp, "  probe interval: ");
6955163Sshin		if (ifinfo->probeinterval) {
7055163Sshin			fprintf(fp, "%d\n", ifinfo->probeinterval);
7155163Sshin			fprintf(fp, "  probe timer: %d\n", ifinfo->probetimer);
72118664Sume		} else {
7355163Sshin			fprintf(fp, "infinity\n");
7455163Sshin			fprintf(fp, "  no probe timer\n");
7555163Sshin		}
7655163Sshin		fprintf(fp, "  interface status: %s\n",
77118664Sume		    ifinfo->active > 0 ? "active" : "inactive");
78118661Sume		fprintf(fp, "  other config: %s\n",
79118661Sume		    ifinfo->otherconfig ? "on" : "off");
8055163Sshin		fprintf(fp, "  rtsold status: %s\n", ifstatstr[ifinfo->state]);
8155163Sshin		fprintf(fp, "  carrier detection: %s\n",
82118664Sume		    ifinfo->mediareqok ? "available" : "unavailable");
8355163Sshin		fprintf(fp, "  probes: %d, dadcount = %d\n",
84118664Sume		    ifinfo->probes, ifinfo->dadcount);
8555163Sshin		if (ifinfo->timer.tv_sec == tm_max.tv_sec &&
8655163Sshin		    ifinfo->timer.tv_usec == tm_max.tv_usec)
8755163Sshin			fprintf(fp, "  no timer\n");
8855163Sshin		else {
8955163Sshin			fprintf(fp, "  timer: interval=%d:%d, expire=%s\n",
90118664Sume			    (int)ifinfo->timer.tv_sec,
91118664Sume			    (int)ifinfo->timer.tv_usec,
92118664Sume			    (ifinfo->expire.tv_sec < now.tv_sec) ? "expired"
93118664Sume			    : sec2str(ifinfo->expire.tv_sec - now.tv_sec));
9455163Sshin		}
9555163Sshin		fprintf(fp, "  number of valid RAs: %d\n", ifinfo->racnt);
9655163Sshin	}
9755163Sshin}
9855163Sshin
9955163Sshinvoid
100124524Sumertsold_dump_file(char *dumpfile)
10155163Sshin{
10255163Sshin	if ((fp = fopen(dumpfile, "w")) == NULL) {
103118660Sume		warnmsg(LOG_WARNING, __func__, "open a dump file(%s): %s",
104118664Sume		    dumpfile, strerror(errno));
10555163Sshin		return;
10655163Sshin	}
10755163Sshin	dump_interface_status();
10855163Sshin	fclose(fp);
10955163Sshin}
11055163Sshin
11155163Sshinstatic char *
112124524Sumesec2str(time_t total)
11355163Sshin{
11455163Sshin	static char result[256];
11555163Sshin	int days, hours, mins, secs;
11655163Sshin	int first = 1;
11755163Sshin	char *p = result;
118118786Sume	char *ep = &result[sizeof(result)];
119118786Sume	int n;
12055163Sshin
12155163Sshin	days = total / 3600 / 24;
12255163Sshin	hours = (total / 3600) % 24;
12355163Sshin	mins = (total / 60) % 60;
12455163Sshin	secs = total % 60;
12555163Sshin
12655163Sshin	if (days) {
12755163Sshin		first = 0;
128118786Sume		n = snprintf(p, ep - p, "%dd", days);
129118786Sume		if (n < 0 || n >= ep - p)
130118786Sume			return "?";
131118786Sume		p += n;
13255163Sshin	}
13355163Sshin	if (!first || hours) {
13455163Sshin		first = 0;
135118786Sume		n = snprintf(p, ep - p, "%dh", hours);
136118786Sume		if (n < 0 || n >= ep - p)
137118786Sume			return "?";
138118786Sume		p += n;
13955163Sshin	}
14055163Sshin	if (!first || mins) {
14155163Sshin		first = 0;
142118786Sume		n = snprintf(p, ep - p, "%dm", mins);
143118786Sume		if (n < 0 || n >= ep - p)
144118786Sume			return "?";
145118786Sume		p += n;
14655163Sshin	}
147118786Sume	snprintf(p, ep - p, "%ds", secs);
14855163Sshin	return(result);
14955163Sshin}
150