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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/usr.sbin/IPXrouted/trace.c,v 1.6.2.1 2000/07/20 10:35:22 kris Exp $
35 * $DragonFly: src/usr.sbin/IPXrouted/trace.c,v 1.6 2006/01/22 03:43:38 swildner Exp $
37 * @(#)trace.c 8.1 (Berkeley) 6/5/93
41 * Routing Table Management Daemon
47 #include <sys/types.h>
51 #define NRECORDS 50 /* size of circular trace buffer */
53 FILE *ftrace = stdout;
60 void dumpif(FILE *fd, struct interface *ifp);
61 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
63 static int iftraceinit(struct interface *, struct ifdebug *);
66 traceinit(struct interface *ifp)
68 if (iftraceinit(ifp, &ifp->int_input) &&
69 iftraceinit(ifp, &ifp->int_output))
72 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
76 iftraceinit(struct interface *ifp, struct ifdebug *ifd)
81 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
82 if (ifd->ifd_records == 0)
84 ifd->ifd_front = ifd->ifd_records;
86 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
100 ftrace = fopen(file, "a");
103 dup2(fileno(ftrace), 1);
104 dup2(fileno(ftrace), 2);
120 trace(struct ifdebug *ifd, struct sockaddr *who, char *p, int len, int m)
124 if (ifd->ifd_records == 0)
126 t = ifd->ifd_front++;
127 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
128 ifd->ifd_front = ifd->ifd_records;
129 if (ifd->ifd_count < NRECORDS)
131 if (t->ift_size > 0 && t->ift_packet)
134 t->ift_stamp = time(0);
137 t->ift_packet = malloc(len);
139 bcopy(p, t->ift_packet, len);
148 traceaction(FILE *fd, char *action, struct rt_entry *rt)
150 struct sockaddr_ipx *dst, *gate;
156 { RTF_GATEWAY, "GATEWAY" },
157 { RTF_HOST, "HOST" },
160 { RTS_PASSIVE, "PASSIVE" },
161 { RTS_REMOTE, "REMOTE" },
162 { RTS_INTERFACE,"INTERFACE" },
163 { RTS_CHANGED, "CHANGED" },
172 fprintf(fd, "%s ", action);
173 dst = (struct sockaddr_ipx *)&rt->rt_dst;
174 gate = (struct sockaddr_ipx *)&rt->rt_router;
175 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
176 fprintf(fd, "router %s, metric %d, ticks %d, flags",
177 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
179 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
180 if ((rt->rt_flags & p->t_bits) == 0)
182 fprintf(fd, cp, p->t_name);
188 fprintf(fd, " state");
190 for (first = 1, p = statebits; p->t_bits > 0; p++) {
191 if ((rt->rt_state & p->t_bits) == 0)
193 fprintf(fd, cp, p->t_name);
200 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
201 dumpif(fd, rt->rt_ifp);
206 traceactionlog(char *action, struct rt_entry *rt)
208 struct sockaddr_ipx *dst, *gate;
214 { RTF_GATEWAY, "GATEWAY" },
215 { RTF_HOST, "HOST" },
218 { RTS_PASSIVE, "PASSIVE" },
219 { RTS_REMOTE, "REMOTE" },
220 { RTS_INTERFACE,"INTERFACE" },
221 { RTS_CHANGED, "CHANGED" },
229 dst = (struct sockaddr_ipx *)&rt->rt_dst;
230 gate = (struct sockaddr_ipx *)&rt->rt_router;
231 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
233 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
234 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
238 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
239 if ((rt->rt_flags & p->t_bits) == 0)
241 asprintf(&lstr, cp, olstr, p->t_name);
249 asprintf(&lstr, "%s state", olstr);
253 for (first = 1, p = statebits; p->t_bits > 0; p++) {
254 if ((rt->rt_state & p->t_bits) == 0)
256 asprintf(&lstr, cp, olstr, p->t_name);
264 syslog(LOG_DEBUG, "%s", lstr);
269 tracesapactionlog(char *action, struct sap_entry *sap)
271 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
272 "addr %s.%04X %c metric %d\n",
274 ntohs(sap->sap.ServType),
276 ipxdp_ntoa(&sap->sap.ipx),
277 ntohs(sap->sap.ipx.x_port),
278 (sap->clone ? 'C' : ' '),
279 ntohs(sap->sap.hops));
283 dumpif(FILE *fd, struct interface *ifp)
285 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
286 fprintf(fd, "*** Packet history for interface %s ***\n",
288 dumptrace(fd, "to", &ifp->int_output);
289 dumptrace(fd, "from", &ifp->int_input);
290 fprintf(fd, "*** end packet history ***\n");
295 dumptrace(FILE *fd, char *dir, struct ifdebug *ifd)
300 cp = !strcmp(dir, "to") ? "Output" : "Input";
301 if (ifd->ifd_front == ifd->ifd_records &&
302 ifd->ifd_front->ift_size == 0) {
303 fprintf(fd, "%s: no packets.\n", cp);
306 fprintf(fd, "%s trace:\n", cp);
307 t = ifd->ifd_front - ifd->ifd_count;
308 if (t < ifd->ifd_records)
310 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
311 if (t >= ifd->ifd_records + NRECORDS)
312 t = ifd->ifd_records;
313 if (t->ift_size == 0)
315 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
317 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
322 dumppacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
324 struct rip *msg = (struct rip *)cp;
326 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
328 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
329 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
330 dir, ipxdp_ntoa(&who->sipx_addr),
331 ntohs(who->sipx_addr.x_port));
333 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
334 dir, ipxdp_ntoa(&who->sipx_addr),
335 ntohs(who->sipx_addr.x_port));
336 fprintf(fd, "size=%d cp=%p packet=%p\n", size, cp, packet);
339 switch (ntohs(msg->rip_cmd)) {
342 case RIPCMD_RESPONSE:
344 size -= sizeof (u_short);
346 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
347 if (size < sizeof (struct netinfo))
349 fprintf(fd, "\tnet %s metric %d ticks %d\n",
350 ipxdp_nettoa(n->rip_dst),
351 ntohs(n->rip_metric),
352 ntohs(n->rip_ticks));
360 dumpsappacket(FILE *fd, char *dir, struct sockaddr *source, char *cp, int size)
362 struct sap_packet *msg = (struct sap_packet *)cp;
364 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
366 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
367 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
368 dir, ipxdp_ntoa(&who->sipx_addr),
369 ntohs(who->sipx_addr.x_port));
371 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
372 dir, ipxdp_ntoa(&who->sipx_addr),
373 ntohs(who->sipx_addr.x_port));
374 fprintf(fd, "size=%d cp=%p packet=%p\n", size, cp, packet);
377 switch (ntohs(msg->sap_cmd)) {
384 size -= sizeof (u_short);
386 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
387 if (size < sizeof (struct sap_info))
389 fprintf(fd, " service %04X %-20.20s "
390 "addr %s.%04X metric %d\n",
394 ntohs(n->ipx.x_port),
403 dumpsaptable(FILE *fd, struct sap_hash *sh)
405 struct sap_entry *sap;
406 struct sap_hash *hash;
409 fprintf(fd, "------- SAP table dump. -------\n");
410 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
411 fprintf(fd, "HASH %d\n", x);
413 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
414 fprintf(fd, " service %04X %-20.20s "
415 "addr %s.%04X %c metric %d\n",
416 ntohs(sap->sap.ServType),
418 ipxdp_ntoa(&sap->sap.ipx),
419 ntohs(sap->sap.ipx.x_port),
420 (sap->clone ? 'C' : ' '),
421 ntohs(sap->sap.hops));
428 dumpriptable(FILE *fd)
430 struct rt_entry *rip;
433 struct rthash *rh = nethash;
435 fprintf(fd, "------- RIP table dump. -------\n");
437 fprintf(fd, "Network table.\n");
439 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
440 fprintf(fd, "HASH %d\n", x);
442 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
443 fprintf(fd, " dest %s\t",
444 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
445 fprintf(fd, "%s metric %d, ticks %d\n",
446 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
457 ipxdp_nettoa(union ipx_net val)
459 static char buf[100];
462 sprintf(buf, "%u", ntohl(net.long_e));
468 ipxdp_ntoa(struct ipx_addr *addr)
470 static char buf[100];
472 sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
473 ipxdp_nettoa(addr->x_net),
474 addr->x_host.c_host[0], addr->x_host.c_host[1],
475 addr->x_host.c_host[2], addr->x_host.c_host[3],
476 addr->x_host.c_host[4], addr->x_host.c_host[5]);