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
42 static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
46 * Routing Table Management Daemon
52 #include <sys/types.h>
56 #define NRECORDS 50 /* size of circular trace buffer */
58 FILE *ftrace = stdout;
65 void dumpif(FILE *fd, struct interface *ifp);
66 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
70 register struct interface *ifp;
72 static int iftraceinit();
74 if (iftraceinit(ifp, &ifp->int_input) &&
75 iftraceinit(ifp, &ifp->int_output))
78 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
83 struct interface *ifp;
84 register struct ifdebug *ifd;
86 register struct iftrace *t;
89 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
90 if (ifd->ifd_records == 0)
92 ifd->ifd_front = ifd->ifd_records;
94 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
109 ftrace = fopen(file, "a");
112 dup2(fileno(ftrace), 1);
113 dup2(fileno(ftrace), 2);
129 trace(ifd, who, p, len, m)
130 register struct ifdebug *ifd;
131 struct sockaddr *who;
135 register struct iftrace *t;
137 if (ifd->ifd_records == 0)
139 t = ifd->ifd_front++;
140 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
141 ifd->ifd_front = ifd->ifd_records;
142 if (ifd->ifd_count < NRECORDS)
144 if (t->ift_size > 0 && t->ift_packet)
147 t->ift_stamp = time(0);
150 t->ift_packet = malloc(len);
152 bcopy(p, t->ift_packet, len);
161 traceaction(fd, action, rt)
166 struct sockaddr_ipx *dst, *gate;
172 { RTF_GATEWAY, "GATEWAY" },
173 { RTF_HOST, "HOST" },
176 { RTS_PASSIVE, "PASSIVE" },
177 { RTS_REMOTE, "REMOTE" },
178 { RTS_INTERFACE,"INTERFACE" },
179 { RTS_CHANGED, "CHANGED" },
182 register struct bits *p;
188 fprintf(fd, "%s ", action);
189 dst = (struct sockaddr_ipx *)&rt->rt_dst;
190 gate = (struct sockaddr_ipx *)&rt->rt_router;
191 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
192 fprintf(fd, "router %s, metric %d, ticks %d, flags",
193 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
195 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
196 if ((rt->rt_flags & p->t_bits) == 0)
198 fprintf(fd, cp, p->t_name);
204 fprintf(fd, " state");
206 for (first = 1, p = statebits; p->t_bits > 0; p++) {
207 if ((rt->rt_state & p->t_bits) == 0)
209 fprintf(fd, cp, p->t_name);
216 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
217 dumpif(fd, rt->rt_ifp);
222 traceactionlog(action, rt)
226 struct sockaddr_ipx *dst, *gate;
232 { RTF_GATEWAY, "GATEWAY" },
233 { RTF_HOST, "HOST" },
236 { RTS_PASSIVE, "PASSIVE" },
237 { RTS_REMOTE, "REMOTE" },
238 { RTS_INTERFACE,"INTERFACE" },
239 { RTS_CHANGED, "CHANGED" },
242 register struct bits *p;
247 dst = (struct sockaddr_ipx *)&rt->rt_dst;
248 gate = (struct sockaddr_ipx *)&rt->rt_router;
249 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
251 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
252 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
256 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
257 if ((rt->rt_flags & p->t_bits) == 0)
259 asprintf(&lstr, cp, olstr, p->t_name);
267 asprintf(&lstr, "%s state", olstr);
271 for (first = 1, p = statebits; p->t_bits > 0; p++) {
272 if ((rt->rt_state & p->t_bits) == 0)
274 asprintf(&lstr, cp, olstr, p->t_name);
282 syslog(LOG_DEBUG, "%s", lstr);
287 tracesapactionlog(action, sap)
289 struct sap_entry *sap;
291 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
292 "addr %s.%04X %c metric %d\n",
294 ntohs(sap->sap.ServType),
296 ipxdp_ntoa(&sap->sap.ipx),
297 ntohs(sap->sap.ipx.x_port),
298 (sap->clone ? 'C' : ' '),
299 ntohs(sap->sap.hops));
304 register struct interface *ifp;
307 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
308 fprintf(fd, "*** Packet history for interface %s ***\n",
310 dumptrace(fd, "to", &ifp->int_output);
311 dumptrace(fd, "from", &ifp->int_input);
312 fprintf(fd, "*** end packet history ***\n");
317 dumptrace(fd, dir, ifd)
320 register struct ifdebug *ifd;
322 register struct iftrace *t;
323 char *cp = !strcmp(dir, "to") ? "Output" : "Input";
325 if (ifd->ifd_front == ifd->ifd_records &&
326 ifd->ifd_front->ift_size == 0) {
327 fprintf(fd, "%s: no packets.\n", cp);
330 fprintf(fd, "%s trace:\n", cp);
331 t = ifd->ifd_front - ifd->ifd_count;
332 if (t < ifd->ifd_records)
334 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
335 if (t >= ifd->ifd_records + NRECORDS)
336 t = ifd->ifd_records;
337 if (t->ift_size == 0)
339 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
341 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
346 dumppacket(fd, dir, source, cp, size)
349 struct sockaddr *source;
353 register struct rip *msg = (struct rip *)cp;
354 register struct netinfo *n;
355 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
357 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
358 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
359 dir, ipxdp_ntoa(&who->sipx_addr),
360 ntohs(who->sipx_addr.x_port));
362 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
363 dir, ipxdp_ntoa(&who->sipx_addr),
364 ntohs(who->sipx_addr.x_port));
365 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
366 (u_int)cp, (u_int)packet);
369 switch (ntohs(msg->rip_cmd)) {
372 case RIPCMD_RESPONSE:
374 size -= sizeof (u_short);
376 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
377 if (size < sizeof (struct netinfo))
379 fprintf(fd, "\tnet %s metric %d ticks %d\n",
380 ipxdp_nettoa(n->rip_dst),
381 ntohs(n->rip_metric),
382 ntohs(n->rip_ticks));
390 dumpsappacket(fd, dir, source, cp, size)
393 struct sockaddr *source;
397 register struct sap_packet *msg = (struct sap_packet *)cp;
398 register struct sap_info *n;
399 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
401 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
402 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
403 dir, ipxdp_ntoa(&who->sipx_addr),
404 ntohs(who->sipx_addr.x_port));
406 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
407 dir, ipxdp_ntoa(&who->sipx_addr),
408 ntohs(who->sipx_addr.x_port));
409 fprintf(fd, "size=%d cp=%x packet=%x\n", size,
410 (u_int)cp, (u_int)packet);
413 switch (ntohs(msg->sap_cmd)) {
420 size -= sizeof (u_short);
422 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
423 if (size < sizeof (struct sap_info))
425 fprintf(fd, " service %04X %-20.20s "
426 "addr %s.%04X metric %d\n",
430 ntohs(n->ipx.x_port),
443 register struct sap_entry *sap;
444 struct sap_hash *hash;
447 fprintf(fd, "------- SAP table dump. -------\n");
448 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
449 fprintf(fd, "HASH %d\n", x);
451 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
452 fprintf(fd, " service %04X %-20.20s "
453 "addr %s.%04X %c metric %d\n",
454 ntohs(sap->sap.ServType),
456 ipxdp_ntoa(&sap->sap.ipx),
457 ntohs(sap->sap.ipx.x_port),
458 (sap->clone ? 'C' : ' '),
459 ntohs(sap->sap.hops));
469 register struct rt_entry *rip;
472 struct rthash *rh = nethash;
474 fprintf(fd, "------- RIP table dump. -------\n");
476 fprintf(fd, "Network table.\n");
478 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
479 fprintf(fd, "HASH %d\n", x);
481 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
482 fprintf(fd, " dest %s\t",
483 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
484 fprintf(fd, "%s metric %d, ticks %d\n",
485 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
499 static char buf[100];
501 (void)sprintf(buf, "%lx", ntohl(net.long_e));
508 struct ipx_addr *addr;
510 static char buf[100];
512 (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
513 ipxdp_nettoa(addr->x_net),
514 addr->x_host.c_host[0], addr->x_host.c_host[1],
515 addr->x_host.c_host[2], addr->x_host.c_host[3],
516 addr->x_host.c_host[4], addr->x_host.c_host[5]);