nlookup.9 - document nlookup_init_root
[dragonfly.git] / usr.sbin / IPXrouted / trace.c
blobbca2ba8d760f940c009d637390361bc9259f09fc
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1995 John Hay. All rights reserved.
7 * This file includes significant work done at Cornell University by
8 * Bill Nesheim. That work included by permission.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
38 * $FreeBSD: src/usr.sbin/IPXrouted/trace.c,v 1.6.2.1 2000/07/20 10:35:22 kris Exp $
39 * $DragonFly: src/usr.sbin/IPXrouted/trace.c,v 1.6 2006/01/22 03:43:38 swildner Exp $
41 * @(#)trace.c 8.1 (Berkeley) 6/5/93
45 * Routing Table Management Daemon
47 #define RIPCMDS
48 #define SAPCMDS
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <sys/types.h>
52 #include <time.h>
53 #include "defs.h"
55 #define NRECORDS 50 /* size of circular trace buffer */
56 #ifdef DEBUG
57 FILE *ftrace = stdout;
58 int tracing = 1;
59 #else /* DEBUG */
60 FILE *ftrace = NULL;
61 int tracing = 0;
62 #endif
64 void dumpif(FILE *fd, struct interface *ifp);
65 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
67 static int iftraceinit(struct interface *, struct ifdebug *);
69 void
70 traceinit(struct interface *ifp)
72 if (iftraceinit(ifp, &ifp->int_input) &&
73 iftraceinit(ifp, &ifp->int_output))
74 return;
75 tracing = 0;
76 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
79 static int
80 iftraceinit(struct interface *ifp, struct ifdebug *ifd)
82 struct iftrace *t;
84 ifd->ifd_records =
85 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
86 if (ifd->ifd_records == 0)
87 return (0);
88 ifd->ifd_front = ifd->ifd_records;
89 ifd->ifd_count = 0;
90 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
91 t->ift_size = 0;
92 t->ift_packet = 0;
94 ifd->ifd_if = ifp;
95 return (1);
98 void
99 traceon(char *file)
102 if (ftrace != NULL)
103 return;
104 ftrace = fopen(file, "a");
105 if (ftrace == NULL)
106 return;
107 dup2(fileno(ftrace), 1);
108 dup2(fileno(ftrace), 2);
109 tracing = 1;
112 void
113 traceoff(void)
115 if (!tracing)
116 return;
117 if (ftrace != NULL)
118 fclose(ftrace);
119 ftrace = NULL;
120 tracing = 0;
123 void
124 trace(struct ifdebug *ifd, struct sockaddr *who, char *p, int len, int m)
126 struct iftrace *t;
128 if (ifd->ifd_records == 0)
129 return;
130 t = ifd->ifd_front++;
131 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
132 ifd->ifd_front = ifd->ifd_records;
133 if (ifd->ifd_count < NRECORDS)
134 ifd->ifd_count++;
135 if (t->ift_size > 0 && t->ift_packet)
136 free(t->ift_packet);
137 t->ift_packet = 0;
138 t->ift_stamp = time(0);
139 t->ift_who = *who;
140 if (len > 0) {
141 t->ift_packet = malloc(len);
142 if (t->ift_packet)
143 bcopy(p, t->ift_packet, len);
144 else
145 len = 0;
147 t->ift_size = len;
148 t->ift_metric = m;
151 void
152 traceaction(FILE *fd, char *action, struct rt_entry *rt)
154 struct sockaddr_ipx *dst, *gate;
155 static struct bits {
156 int t_bits;
157 char *t_name;
158 } flagbits[] = {
159 { RTF_UP, "UP" },
160 { RTF_GATEWAY, "GATEWAY" },
161 { RTF_HOST, "HOST" },
162 { 0 }
163 }, statebits[] = {
164 { RTS_PASSIVE, "PASSIVE" },
165 { RTS_REMOTE, "REMOTE" },
166 { RTS_INTERFACE,"INTERFACE" },
167 { RTS_CHANGED, "CHANGED" },
168 { 0 }
170 struct bits *p;
171 int first;
172 char *cp;
174 if (fd == NULL)
175 return;
176 fprintf(fd, "%s ", action);
177 dst = (struct sockaddr_ipx *)&rt->rt_dst;
178 gate = (struct sockaddr_ipx *)&rt->rt_router;
179 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
180 fprintf(fd, "router %s, metric %d, ticks %d, flags",
181 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
182 cp = " %s";
183 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
184 if ((rt->rt_flags & p->t_bits) == 0)
185 continue;
186 fprintf(fd, cp, p->t_name);
187 if (first) {
188 cp = "|%s";
189 first = 0;
192 fprintf(fd, " state");
193 cp = " %s";
194 for (first = 1, p = statebits; p->t_bits > 0; p++) {
195 if ((rt->rt_state & p->t_bits) == 0)
196 continue;
197 fprintf(fd, cp, p->t_name);
198 if (first) {
199 cp = "|%s";
200 first = 0;
203 putc('\n', fd);
204 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
205 dumpif(fd, rt->rt_ifp);
206 fflush(fd);
209 void
210 traceactionlog(char *action, struct rt_entry *rt)
212 struct sockaddr_ipx *dst, *gate;
213 static struct bits {
214 int t_bits;
215 char *t_name;
216 } flagbits[] = {
217 { RTF_UP, "UP" },
218 { RTF_GATEWAY, "GATEWAY" },
219 { RTF_HOST, "HOST" },
220 { 0 }
221 }, statebits[] = {
222 { RTS_PASSIVE, "PASSIVE" },
223 { RTS_REMOTE, "REMOTE" },
224 { RTS_INTERFACE,"INTERFACE" },
225 { RTS_CHANGED, "CHANGED" },
226 { 0 }
228 struct bits *p;
229 int first;
230 char *cp;
231 char *lstr, *olstr;
233 dst = (struct sockaddr_ipx *)&rt->rt_dst;
234 gate = (struct sockaddr_ipx *)&rt->rt_router;
235 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
236 olstr = lstr;
237 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
238 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
239 free(olstr);
240 olstr = lstr;
241 cp = "%s %s";
242 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
243 if ((rt->rt_flags & p->t_bits) == 0)
244 continue;
245 asprintf(&lstr, cp, olstr, p->t_name);
246 free(olstr);
247 olstr = lstr;
248 if (first) {
249 cp = "%s|%s";
250 first = 0;
253 asprintf(&lstr, "%s state", olstr);
254 free(olstr);
255 olstr = lstr;
256 cp = "%s %s";
257 for (first = 1, p = statebits; p->t_bits > 0; p++) {
258 if ((rt->rt_state & p->t_bits) == 0)
259 continue;
260 asprintf(&lstr, cp, olstr, p->t_name);
261 free(olstr);
262 olstr = lstr;
263 if (first) {
264 cp = "%s|%s";
265 first = 0;
268 syslog(LOG_DEBUG, "%s", lstr);
269 free(lstr);
272 void
273 tracesapactionlog(char *action, struct sap_entry *sap)
275 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
276 "addr %s.%04X %c metric %d\n",
277 action,
278 ntohs(sap->sap.ServType),
279 sap->sap.ServName,
280 ipxdp_ntoa(&sap->sap.ipx),
281 ntohs(sap->sap.ipx.x_port),
282 (sap->clone ? 'C' : ' '),
283 ntohs(sap->sap.hops));
286 void
287 dumpif(FILE *fd, struct interface *ifp)
289 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
290 fprintf(fd, "*** Packet history for interface %s ***\n",
291 ifp->int_name);
292 dumptrace(fd, "to", &ifp->int_output);
293 dumptrace(fd, "from", &ifp->int_input);
294 fprintf(fd, "*** end packet history ***\n");
298 void
299 dumptrace(FILE *fd, char *dir, struct ifdebug *ifd)
301 struct iftrace *t;
302 char *cp;
304 cp = !strcmp(dir, "to") ? "Output" : "Input";
305 if (ifd->ifd_front == ifd->ifd_records &&
306 ifd->ifd_front->ift_size == 0) {
307 fprintf(fd, "%s: no packets.\n", cp);
308 return;
310 fprintf(fd, "%s trace:\n", cp);
311 t = ifd->ifd_front - ifd->ifd_count;
312 if (t < ifd->ifd_records)
313 t += NRECORDS;
314 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
315 if (t >= ifd->ifd_records + NRECORDS)
316 t = ifd->ifd_records;
317 if (t->ift_size == 0)
318 continue;
319 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
320 t->ift_metric);
321 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
325 void
326 dumppacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
328 struct rip *msg = (struct rip *)cp;
329 struct netinfo *n;
330 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
332 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
333 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
334 dir, ipxdp_ntoa(&who->sipx_addr),
335 ntohs(who->sipx_addr.x_port));
336 else {
337 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
338 dir, ipxdp_ntoa(&who->sipx_addr),
339 ntohs(who->sipx_addr.x_port));
340 fprintf(fd, "size=%d cp=%p packet=%p\n", size, cp, packet);
341 return;
343 switch (ntohs(msg->rip_cmd)) {
345 case RIPCMD_REQUEST:
346 case RIPCMD_RESPONSE:
347 fprintf(fd, ":\n");
348 size -= sizeof (u_short);
349 n = msg->rip_nets;
350 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
351 if (size < sizeof (struct netinfo))
352 break;
353 fprintf(fd, "\tnet %s metric %d ticks %d\n",
354 ipxdp_nettoa(n->rip_dst),
355 ntohs(n->rip_metric),
356 ntohs(n->rip_ticks));
358 break;
363 void
364 dumpsappacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
366 struct sap_packet *msg = (struct sap_packet *)cp;
367 struct sap_info *n;
368 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
370 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
371 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
372 dir, ipxdp_ntoa(&who->sipx_addr),
373 ntohs(who->sipx_addr.x_port));
374 else {
375 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
376 dir, ipxdp_ntoa(&who->sipx_addr),
377 ntohs(who->sipx_addr.x_port));
378 fprintf(fd, "size=%d cp=%p packet=%p\n", size, cp, packet);
379 return;
381 switch (ntohs(msg->sap_cmd)) {
383 case SAP_REQ:
384 case SAP_RESP:
385 case SAP_REQ_NEAR:
386 case SAP_RESP_NEAR:
387 fprintf(fd, ":\n");
388 size -= sizeof (u_short);
389 n = msg->sap;
390 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
391 if (size < sizeof (struct sap_info))
392 break;
393 fprintf(fd, " service %04X %-20.20s "
394 "addr %s.%04X metric %d\n",
395 ntohs(n->ServType),
396 n->ServName,
397 ipxdp_ntoa(&n->ipx),
398 ntohs(n->ipx.x_port),
399 ntohs(n->hops));
401 break;
406 void
407 dumpsaptable(FILE *fd, struct sap_hash *sh)
409 struct sap_entry *sap;
410 struct sap_hash *hash;
411 int x = 0;
413 fprintf(fd, "------- SAP table dump. -------\n");
414 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
415 fprintf(fd, "HASH %d\n", x);
416 sap = hash->forw;
417 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
418 fprintf(fd, " service %04X %-20.20s "
419 "addr %s.%04X %c metric %d\n",
420 ntohs(sap->sap.ServType),
421 sap->sap.ServName,
422 ipxdp_ntoa(&sap->sap.ipx),
423 ntohs(sap->sap.ipx.x_port),
424 (sap->clone ? 'C' : ' '),
425 ntohs(sap->sap.hops));
428 fprintf(fd, "\n");
431 void
432 dumpriptable(FILE *fd)
434 struct rt_entry *rip;
435 struct rthash *hash;
436 int x;
437 struct rthash *rh = nethash;
439 fprintf(fd, "------- RIP table dump. -------\n");
440 x = 0;
441 fprintf(fd, "Network table.\n");
443 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
444 fprintf(fd, "HASH %d\n", x);
445 rip = hash->rt_forw;
446 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
447 fprintf(fd, " dest %s\t",
448 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
449 fprintf(fd, "%s metric %d, ticks %d\n",
450 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
451 rip->rt_metric,
452 rip->rt_ticks);
455 fprintf(fd, "\n");
458 union ipx_net_u net;
460 char *
461 ipxdp_nettoa(union ipx_net val)
463 static char buf[100];
465 net.net_e = val;
466 sprintf(buf, "%u", ntohl(net.long_e));
467 return (buf);
471 char *
472 ipxdp_ntoa(struct ipx_addr *addr)
474 static char buf[100];
476 sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
477 ipxdp_nettoa(addr->x_net),
478 addr->x_host.c_host[0], addr->x_host.c_host[1],
479 addr->x_host.c_host[2], addr->x_host.c_host[3],
480 addr->x_host.c_host[4], addr->x_host.c_host[5]);
482 return(buf);