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