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
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
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.2 2003/06/17 04:29:52 dillon Exp $
41 * @(#)trace.c 8.1 (Berkeley) 6/5/93
45 * Routing Table Management Daemon
51 #include <sys/types.h>
55 #define NRECORDS 50 /* size of circular trace buffer */
57 FILE *ftrace = stdout;
64 void dumpif(FILE *fd, struct interface *ifp);
65 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
69 register struct interface *ifp;
71 static int iftraceinit();
73 if (iftraceinit(ifp, &ifp->int_input) &&
74 iftraceinit(ifp, &ifp->int_output))
77 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
82 struct interface *ifp;
83 register struct ifdebug *ifd;
85 register struct iftrace *t;
88 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
89 if (ifd->ifd_records == 0)
91 ifd->ifd_front = ifd->ifd_records;
93 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
108 ftrace = fopen(file, "a");
111 dup2(fileno(ftrace), 1);
112 dup2(fileno(ftrace), 2);
128 trace(ifd, who, p, len, m)
129 register struct ifdebug *ifd;
130 struct sockaddr *who;
134 register struct iftrace *t;
136 if (ifd->ifd_records == 0)
138 t = ifd->ifd_front++;
139 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
140 ifd->ifd_front = ifd->ifd_records;
141 if (ifd->ifd_count < NRECORDS)
143 if (t->ift_size > 0 && t->ift_packet)
146 t->ift_stamp = time(0);
149 t->ift_packet = malloc(len);
151 bcopy(p, t->ift_packet, len);
160 traceaction(fd, action, rt)
165 struct sockaddr_ipx *dst, *gate;
171 { RTF_GATEWAY, "GATEWAY" },
172 { RTF_HOST, "HOST" },
175 { RTS_PASSIVE, "PASSIVE" },
176 { RTS_REMOTE, "REMOTE" },
177 { RTS_INTERFACE,"INTERFACE" },
178 { RTS_CHANGED, "CHANGED" },
181 register struct bits *p;
187 fprintf(fd, "%s ", action);
188 dst = (struct sockaddr_ipx *)&rt->rt_dst;
189 gate = (struct sockaddr_ipx *)&rt->rt_router;
190 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
191 fprintf(fd, "router %s, metric %d, ticks %d, flags",
192 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
194 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
195 if ((rt->rt_flags & p->t_bits) == 0)
197 fprintf(fd, cp, p->t_name);
203 fprintf(fd, " state");
205 for (first = 1, p = statebits; p->t_bits > 0; p++) {
206 if ((rt->rt_state & p->t_bits) == 0)
208 fprintf(fd, cp, p->t_name);
215 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
216 dumpif(fd, rt->rt_ifp);
221 traceactionlog(action, rt)
225 struct sockaddr_ipx *dst, *gate;
231 { RTF_GATEWAY, "GATEWAY" },
232 { RTF_HOST, "HOST" },
235 { RTS_PASSIVE, "PASSIVE" },
236 { RTS_REMOTE, "REMOTE" },
237 { RTS_INTERFACE,"INTERFACE" },
238 { RTS_CHANGED, "CHANGED" },
241 register struct bits *p;
246 dst = (struct sockaddr_ipx *)&rt->rt_dst;
247 gate = (struct sockaddr_ipx *)&rt->rt_router;
248 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
250 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
251 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
255 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
256 if ((rt->rt_flags & p->t_bits) == 0)
258 asprintf(&lstr, cp, olstr, p->t_name);
266 asprintf(&lstr, "%s state", olstr);
270 for (first = 1, p = statebits; p->t_bits > 0; p++) {
271 if ((rt->rt_state & p->t_bits) == 0)
273 asprintf(&lstr, cp, olstr, p->t_name);
281 syslog(LOG_DEBUG, "%s", lstr);
286 tracesapactionlog(action, sap)
288 struct sap_entry *sap;
290 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
291 "addr %s.%04X %c metric %d\n",
293 ntohs(sap->sap.ServType),
295 ipxdp_ntoa(&sap->sap.ipx),
296 ntohs(sap->sap.ipx.x_port),
297 (sap->clone ? 'C' : ' '),
298 ntohs(sap->sap.hops));
303 register struct interface *ifp;
306 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
307 fprintf(fd, "*** Packet history for interface %s ***\n",
309 dumptrace(fd, "to", &ifp->int_output);
310 dumptrace(fd, "from", &ifp->int_input);
311 fprintf(fd, "*** end packet history ***\n");
316 dumptrace(fd, dir, ifd)
319 register struct ifdebug *ifd;
321 register struct iftrace *t;
322 char *cp = !strcmp(dir, "to") ? "Output" : "Input";
324 if (ifd->ifd_front == ifd->ifd_records &&
325 ifd->ifd_front->ift_size == 0) {
326 fprintf(fd, "%s: no packets.\n", cp);
329 fprintf(fd, "%s trace:\n", cp);
330 t = ifd->ifd_front - ifd->ifd_count;
331 if (t < ifd->ifd_records)
333 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
334 if (t >= ifd->ifd_records + NRECORDS)
335 t = ifd->ifd_records;
336 if (t->ift_size == 0)
338 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
340 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
345 dumppacket(fd, dir, source, cp, size)
348 struct sockaddr *source;
352 register struct rip *msg = (struct rip *)cp;
353 register struct netinfo *n;
354 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
356 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
357 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
358 dir, ipxdp_ntoa(&who->sipx_addr),
359 ntohs(who->sipx_addr.x_port));
361 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
362 dir, ipxdp_ntoa(&who->sipx_addr),
363 ntohs(who->sipx_addr.x_port));
364 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
365 (u_int)cp, (u_int)packet);
368 switch (ntohs(msg->rip_cmd)) {
371 case RIPCMD_RESPONSE:
373 size -= sizeof (u_short);
375 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
376 if (size < sizeof (struct netinfo))
378 fprintf(fd, "\tnet %s metric %d ticks %d\n",
379 ipxdp_nettoa(n->rip_dst),
380 ntohs(n->rip_metric),
381 ntohs(n->rip_ticks));
389 dumpsappacket(fd, dir, source, cp, size)
392 struct sockaddr *source;
396 register struct sap_packet *msg = (struct sap_packet *)cp;
397 register struct sap_info *n;
398 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
400 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
401 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
402 dir, ipxdp_ntoa(&who->sipx_addr),
403 ntohs(who->sipx_addr.x_port));
405 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
406 dir, ipxdp_ntoa(&who->sipx_addr),
407 ntohs(who->sipx_addr.x_port));
408 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
409 (u_int)cp, (u_int)packet);
412 switch (ntohs(msg->sap_cmd)) {
419 size -= sizeof (u_short);
421 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
422 if (size < sizeof (struct sap_info))
424 fprintf(fd, " service %04X %-20.20s "
425 "addr %s.%04X metric %d\n",
429 ntohs(n->ipx.x_port),
442 register struct sap_entry *sap;
443 struct sap_hash *hash;
446 fprintf(fd, "------- SAP table dump. -------\n");
447 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
448 fprintf(fd, "HASH %d\n", x);
450 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
451 fprintf(fd, " service %04X %-20.20s "
452 "addr %s.%04X %c metric %d\n",
453 ntohs(sap->sap.ServType),
455 ipxdp_ntoa(&sap->sap.ipx),
456 ntohs(sap->sap.ipx.x_port),
457 (sap->clone ? 'C' : ' '),
458 ntohs(sap->sap.hops));
468 register struct rt_entry *rip;
471 struct rthash *rh = nethash;
473 fprintf(fd, "------- RIP table dump. -------\n");
475 fprintf(fd, "Network table.\n");
477 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
478 fprintf(fd, "HASH %d\n", x);
480 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
481 fprintf(fd, " dest %s\t",
482 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
483 fprintf(fd, "%s metric %d, ticks %d\n",
484 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
498 static char buf[100];
500 (void)sprintf(buf, "%lx", ntohl(net.long_e));
507 struct ipx_addr *addr;
509 static char buf[100];
511 (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
512 ipxdp_nettoa(addr->x_net),
513 addr->x_host.c_host[0], addr->x_host.c_host[1],
514 addr->x_host.c_host[2], addr->x_host.c_host[3],
515 addr->x_host.c_host[4], addr->x_host.c_host[5]);