]> git.saurik.com Git - apple/network_cmds.git/blame - rtadvd.tproj/dump.c
network_cmds-356.9.tar.gz
[apple/network_cmds.git] / rtadvd.tproj / dump.c
CommitLineData
fdfd5971
A
1/*
2 * Copyright (c) 2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
7ba0088d
A
29/* $KAME: dump.c,v 1.16 2001/03/21 17:41:13 jinmei Exp $ */
30
31/*
32 * Copyright (C) 2000 WIDE Project.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the project nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * $FreeBSD: src/usr.sbin/rtadvd/dump.c,v 1.1.2.2 2001/07/03 11:02:14 ume Exp $
60 */
61#include <sys/types.h>
62#include <sys/socket.h>
63#include <sys/queue.h>
64
65#include <net/if.h>
66#if defined(__FreeBSD__) && __FreeBSD__ >= 3
67#include <net/if_var.h>
68#endif /* __FreeBSD__ >= 3 */
69#include <net/if_dl.h>
70
71#include <netinet/in.h>
72
73/* XXX: the following two are non-standard include files */
74#include <netinet6/in6_var.h>
75#include <netinet6/nd6.h>
76
77#include <arpa/inet.h>
78
79#include <time.h>
80#include <stdio.h>
81#include <stdarg.h>
82#include <syslog.h>
83#include <string.h>
84#include <errno.h>
85
86#include "rtadvd.h"
87#include "timer.h"
88#include "if.h"
89#include "dump.h"
90
91static FILE *fp;
92
93extern struct rainfo *ralist;
94
95static char *ether_str __P((struct sockaddr_dl *));
96static void if_dump __P((void));
97
98#ifdef __FreeBSD__ /* XXX: see PORTABILITY */
99#define LONGLONG "%qu"
100#else
101#define LONGLONG "%llu"
102#endif
103
104static char *rtpref_str[] = {
105 "medium", /* 00 */
106 "high", /* 01 */
107 "rsv", /* 10 */
108 "low" /* 11 */
109};
110
111static char *
112ether_str(sdl)
113 struct sockaddr_dl *sdl;
114{
115 static char ebuf[32];
116 u_char *cp;
117
118 if (sdl->sdl_alen && sdl->sdl_alen > 5) {
119 cp = (u_char *)LLADDR(sdl);
fdfd5971 120 snprintf(ebuf, sizeof(ebuf), "%x:%x:%x:%x:%x:%x",
7ba0088d
A
121 cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
122 }
123 else {
fdfd5971 124 snprintf(ebuf, sizeof(ebuf), "NONE");
7ba0088d
A
125 }
126
127 return(ebuf);
128}
129
130static void
131if_dump()
132{
133 struct rainfo *rai;
134 struct prefix *pfx;
135 char prefixbuf[INET6_ADDRSTRLEN];
136 int first;
137 struct timeval now;
138
139 gettimeofday(&now, NULL); /* XXX: unused in most cases */
140 for (rai = ralist; rai; rai = rai->next) {
141 fprintf(fp, "%s:\n", rai->ifname);
142
143 fprintf(fp, " Status: %s\n",
144 (iflist[rai->ifindex]->ifm_flags & IFF_UP) ? "UP" :
145 "DOWN");
146
147 /* control information */
148 if (rai->lastsent.tv_sec) {
149 /* note that ctime() appends CR by itself */
150 fprintf(fp, " Last RA sent: %s",
151 ctime((time_t *)&rai->lastsent.tv_sec));
152 }
153 if (rai->timer) {
154 fprintf(fp, " Next RA will be sent: %s",
155 ctime((time_t *)&rai->timer->tm.tv_sec));
156 }
157 else
158 fprintf(fp, " RA timer is stopped");
159 fprintf(fp, " waits: %d, initcount: %d\n",
160 rai->waiting, rai->initcounter);
161
162 /* statistics */
163 fprintf(fp,
164 " statistics: RA(out/in/inconsistent): "
165 LONGLONG "/" LONGLONG "/" LONGLONG ", ",
166 (unsigned long long)rai->raoutput,
167 (unsigned long long)rai->rainput,
168 (unsigned long long)rai->rainconsistent);
169 fprintf(fp, "RS(input): " LONGLONG "\n",
170 (unsigned long long)rai->rsinput);
171
172 /* interface information */
173 if (rai->advlinkopt)
174 fprintf(fp, " Link-layer address: %s\n",
175 ether_str(rai->sdl));
176 fprintf(fp, " MTU: %d\n", rai->phymtu);
177
178 /* Router configuration variables */
179 fprintf(fp,
180 " DefaultLifetime: %d, MaxAdvInterval: %d, "
181 "MinAdvInterval: %d\n",
182 rai->lifetime, rai->maxinterval, rai->mininterval);
183 fprintf(fp, " Flags: %s%s%s, ",
184 rai->managedflg ? "M" : "", rai->otherflg ? "O" : "",
185#ifdef MIP6
186 rai->haflg ? "H" :
187#endif
188 "");
189 fprintf(fp, "Preference: %s, ",
190 rtpref_str[(rai->rtpref >> 3) & 0xff]);
191 fprintf(fp, "MTU: %d\n", rai->linkmtu);
192 fprintf(fp, " ReachableTime: %d, RetransTimer: %d, "
193 "CurHopLimit: %d\n", rai->reachabletime,
194 rai->retranstimer, rai->hoplimit);
195#ifdef MIP6
196 fprintf(fp, " HAPreference: %d, HALifetime: %d\n",
197 rai->hapref, rai->hatime);
198#endif
199
200 if (rai->clockskew)
201 fprintf(fp, " Clock skew: %ldsec\n",
202 rai->clockskew);
203 for (first = 1, pfx = rai->prefix.next; pfx != &rai->prefix;
204 pfx = pfx->next) {
205 if (first) {
206 fprintf(fp, " Prefixes:\n");
207 first = 0;
208 }
209 fprintf(fp, " %s/%d(",
210 inet_ntop(AF_INET6, &pfx->prefix,
211 prefixbuf, sizeof(prefixbuf)),
212 pfx->prefixlen);
213 switch(pfx->origin) {
214 case PREFIX_FROM_KERNEL:
215 fprintf(fp, "KERNEL, ");
216 break;
217 case PREFIX_FROM_CONFIG:
218 fprintf(fp, "CONFIG, ");
219 break;
220 case PREFIX_FROM_DYNAMIC:
221 fprintf(fp, "DYNAMIC, ");
222 break;
223 }
224 if (pfx->validlifetime == ND6_INFINITE_LIFETIME)
225 fprintf(fp, "vltime: infinity");
226 else
227 fprintf(fp, "vltime: %ld",
228 (long)pfx->validlifetime);
229 if (pfx->vltimeexpire != 0)
230 fprintf(fp, "(decr,expire %ld), ", (long)
231 pfx->vltimeexpire > now.tv_sec ?
232 pfx->vltimeexpire - now.tv_sec : 0);
233 else
234 fprintf(fp, ", ");
235 if (pfx->preflifetime == ND6_INFINITE_LIFETIME)
236 fprintf(fp, "pltime: infinity");
237 else
238 fprintf(fp, "pltime: %ld",
239 (long)pfx->preflifetime);
240 if (pfx->pltimeexpire != 0)
241 fprintf(fp, "(decr,expire %ld), ", (long)
242 pfx->pltimeexpire > now.tv_sec ?
243 pfx->pltimeexpire - now.tv_sec : 0);
244 else
245 fprintf(fp, ", ");
246 fprintf(fp, "flags: %s%s%s",
247 pfx->onlinkflg ? "L" : "",
248 pfx->autoconfflg ? "A" : "",
249#ifdef MIP6
250 pfx->routeraddr ? "R" :
251#endif
252 "");
253 fprintf(fp, ")\n");
254 }
255 }
256}
257
258void
259rtadvd_dump_file(dumpfile)
260 char *dumpfile;
261{
262 if ((fp = fopen(dumpfile, "w")) == NULL) {
263 syslog(LOG_WARNING, "<%s> open a dump file(%s)",
264 __FUNCTION__, dumpfile);
265 return;
266 }
267
268 if_dump();
269
270 fclose(fp);
271}