Home | History | Annotate | Line # | Download | only in rtadvd
dump.c revision 1.1
      1 /*	$NetBSD: dump.c,v 1.1 2000/05/23 11:37:58 itojun Exp $	*/
      2 /*	$KAME: dump.c,v 1.10 2000/05/23 11:31:25 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 2000 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 #include <sys/types.h>
     33 #include <sys/socket.h>
     34 
     35 #include <net/if.h>
     36 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
     37 #include <net/if_var.h>
     38 #endif /* __FreeBSD__ >= 3 */
     39 #include <net/if_dl.h>
     40 
     41 #include <netinet/in.h>
     42 
     43 /* XXX: the following two are non-standard include files */
     44 #include <netinet6/in6_var.h>
     45 #include <netinet6/nd6.h>
     46 
     47 #include <arpa/inet.h>
     48 
     49 #include <time.h>
     50 #include <stdio.h>
     51 #include <stdarg.h>
     52 #include <syslog.h>
     53 #include <string.h>
     54 #include <errno.h>
     55 
     56 #include "rtadvd.h"
     57 #include "timer.h"
     58 #include "dump.h"
     59 
     60 static FILE *fp;
     61 
     62 extern struct rainfo *ralist;
     63 
     64 static char *ether_str __P((struct sockaddr_dl *));
     65 static void if_dump __P((void));
     66 
     67 #ifdef __FreeBSD__		/* XXX: see PORTABILITY */
     68 #define LONGLONG "%qu"
     69 #else
     70 #define LONGLONG "%llu"
     71 #endif
     72 
     73 static char *
     74 ether_str(sdl)
     75 	struct sockaddr_dl *sdl;
     76 {
     77 	static char ebuf[32];
     78 	u_char *cp;
     79 
     80 	if (sdl->sdl_alen && sdl->sdl_alen > 5) {
     81 		cp = (u_char *)LLADDR(sdl);
     82 		sprintf(ebuf, "%x:%x:%x:%x:%x:%x",
     83 			cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
     84 	}
     85 	else {
     86 		sprintf(ebuf, "NONE");
     87 	}
     88 
     89 	return(ebuf);
     90 }
     91 
     92 static void
     93 if_dump()
     94 {
     95 	struct rainfo *rai;
     96 	struct prefix *pfx;
     97 	char prefixbuf[INET6_ADDRSTRLEN];
     98 	int first;
     99 
    100 	for (rai = ralist; rai; rai = rai->next) {
    101 		fprintf(fp, "%s:\n", rai->ifname);
    102 
    103 		/* control information */
    104 		if (rai->lastsent.tv_sec) {
    105 			/* note that ctime() appends CR by itself */
    106 			fprintf(fp, "  Last RA sent: %s",
    107 				ctime((time_t *)&rai->lastsent.tv_sec));
    108 		}
    109 		if (rai->timer) {
    110 			fprintf(fp, "  Next RA will be sent: %s",
    111 				ctime((time_t *)&rai->timer->tm.tv_sec));
    112 		}
    113 		fprintf(fp, "  waits: %d, initcount: %d\n",
    114 			rai->waiting, rai->initcounter);
    115 
    116 		/* statistics */
    117 		fprintf(fp,
    118 			"  statistics: RA(out/in/inconsistent): "
    119 			LONGLONG "/" LONGLONG "/" LONGLONG ", ",
    120 			(unsigned long long)rai->raoutput,
    121 			(unsigned long long)rai->rainput,
    122 			(unsigned long long)rai->rainconsistent);
    123 		fprintf(fp, "RS(input): " LONGLONG "\n",
    124 			(unsigned long long)rai->rsinput);
    125 
    126 		/* interface information */
    127 		if (rai->advlinkopt)
    128 			fprintf(fp, "  Link-layer address: %s\n",
    129 				ether_str(rai->sdl));
    130 		fprintf(fp, "  MTU: %d\n", rai->phymtu);
    131 
    132 		/* Router configuration variables */
    133 		fprintf(fp,
    134 			"  DefaultLifetime: %d, MaxAdvInterval: %d, "
    135 			"MinAdvInterval: %d\n",
    136 			rai->lifetime, rai->maxinterval, rai->mininterval);
    137 		fprintf(fp, "  Flags: %s%s%s MTU: %d\n",
    138 			rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
    139 #ifdef MIP6
    140 			rai->haflg ? "H" :
    141 #endif
    142 			"", rai->linkmtu);
    143 		fprintf(fp, "  ReachableTime: %d, RetransTimer: %d, "
    144 			"CurHopLimit: %d\n", rai->reachabletime,
    145 			rai->retranstimer, rai->hoplimit);
    146 #ifdef MIP6
    147 		fprintf(fp, "  HAPreference: %d, HALifetime: %d\n",
    148 			rai->hapref, rai->hatime);
    149 #endif
    150 
    151 		for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
    152 		     pfx = pfx->next) {
    153 			if (first) {
    154 				fprintf(fp, "  Prefixes:\n");
    155 				first = 0;
    156 			}
    157 			fprintf(fp, "    %s/%d(",
    158 				inet_ntop(AF_INET6, &pfx->prefix,
    159 					  prefixbuf, sizeof(prefixbuf)),
    160 				pfx->prefixlen);
    161 			switch(pfx->origin) {
    162 			case PREFIX_FROM_KERNEL:
    163 				fprintf(fp, "KERNEL, ");
    164 				break;
    165 			case PREFIX_FROM_CONFIG:
    166 				fprintf(fp, "CONFIG, ");
    167 				break;
    168 			case PREFIX_FROM_DYNAMIC:
    169 				fprintf(fp, "DYNAMIC, ");
    170 				break;
    171 			}
    172 			if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
    173 				fprintf(fp, "vltime: infinity, ");
    174 			else
    175 				fprintf(fp, "vltime: %ld, ",
    176 					(long)pfx->validlifetime);
    177 			if (pfx->preflifetime ==  ND6_INFINITE_LIFETIME)
    178 				fprintf(fp, "pltime: infinity, ");
    179 			else
    180 				fprintf(fp, "pltime: %ld, ",
    181 					(long)pfx->preflifetime);
    182 			fprintf(fp, "flags: %s%s%s",
    183 				pfx->onlinkflg ? "L" : "",
    184 				pfx->autoconfflg ? "A" : "",
    185 #ifdef MIP6
    186 				pfx->routeraddr ? "R" :
    187 #endif
    188 				"");
    189 			fprintf(fp, ")\n");
    190 		}
    191 	}
    192 }
    193 
    194 void
    195 rtadvd_dump_file(dumpfile)
    196 	char *dumpfile;
    197 {
    198 	if ((fp = fopen(dumpfile, "w")) == NULL) {
    199 		syslog(LOG_WARNING, "<%s> open a dump file(%s)",
    200 		       __FUNCTION__, dumpfile);
    201 		return;
    202 	}
    203 
    204 	if_dump();
    205 
    206 	fclose(fp);
    207 }
    208