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
41 static char sccsid[] = "@(#)trace.c 8.1 (Berkeley) 6/5/93";
43 static const char rcsid[] =
48 * Routing Table Management Daemon
54 #include <sys/types.h>
58 #define NRECORDS 50 /* size of circular trace buffer */
60 FILE *ftrace = stdout;
67 void dumpif(FILE *fd, struct interface *ifp);
68 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
69 static int iftraceinit(struct interface *ifp, struct ifdebug *ifd);
73 register struct interface *ifp;
75 if (iftraceinit(ifp, &ifp->int_input) &&
76 iftraceinit(ifp, &ifp->int_output))
79 syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
84 struct interface *ifp;
85 register struct ifdebug *ifd;
87 register struct iftrace *t;
90 (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
91 if (ifd->ifd_records == 0)
93 ifd->ifd_front = ifd->ifd_records;
95 for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
110 ftrace = fopen(file, "a");
113 dup2(fileno(ftrace), 1);
114 dup2(fileno(ftrace), 2);
130 trace(ifd, who, p, len, m)
131 register struct ifdebug *ifd;
132 struct sockaddr *who;
136 register struct iftrace *t;
138 if (ifd->ifd_records == 0)
140 t = ifd->ifd_front++;
141 if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
142 ifd->ifd_front = ifd->ifd_records;
143 if (ifd->ifd_count < NRECORDS)
145 if (t->ift_size > 0 && t->ift_packet)
148 t->ift_stamp = time(0);
151 t->ift_packet = malloc(len);
153 bcopy(p, t->ift_packet, len);
162 traceaction(fd, action, rt)
167 struct sockaddr_ipx *dst, *gate;
173 { RTF_GATEWAY, "GATEWAY" },
174 { RTF_HOST, "HOST" },
177 { RTS_PASSIVE, "PASSIVE" },
178 { RTS_REMOTE, "REMOTE" },
179 { RTS_INTERFACE,"INTERFACE" },
180 { RTS_CHANGED, "CHANGED" },
183 register struct bits *p;
189 fprintf(fd, "%s ", action);
190 dst = (struct sockaddr_ipx *)&rt->rt_dst;
191 gate = (struct sockaddr_ipx *)&rt->rt_router;
192 fprintf(fd, "dst %s, ", ipxdp_ntoa(&dst->sipx_addr));
193 fprintf(fd, "router %s, metric %d, ticks %d, flags",
194 ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
196 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
197 if ((rt->rt_flags & p->t_bits) == 0)
199 fprintf(fd, cp, p->t_name);
205 fprintf(fd, " state");
207 for (first = 1, p = statebits; p->t_bits > 0; p++) {
208 if ((rt->rt_state & p->t_bits) == 0)
210 fprintf(fd, cp, p->t_name);
217 if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
218 dumpif(fd, rt->rt_ifp);
223 traceactionlog(action, rt)
227 struct sockaddr_ipx *dst, *gate;
233 { RTF_GATEWAY, "GATEWAY" },
234 { RTF_HOST, "HOST" },
237 { RTS_PASSIVE, "PASSIVE" },
238 { RTS_REMOTE, "REMOTE" },
239 { RTS_INTERFACE,"INTERFACE" },
240 { RTS_CHANGED, "CHANGED" },
243 register struct bits *p;
248 dst = (struct sockaddr_ipx *)&rt->rt_dst;
249 gate = (struct sockaddr_ipx *)&rt->rt_router;
250 asprintf(&lstr, "%s dst %s,", action, ipxdp_ntoa(&dst->sipx_addr));
252 asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
253 olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
257 for (first = 1, p = flagbits; p->t_bits > 0; p++) {
258 if ((rt->rt_flags & p->t_bits) == 0)
260 asprintf(&lstr, cp, olstr, p->t_name);
268 asprintf(&lstr, "%s state", olstr);
272 for (first = 1, p = statebits; p->t_bits > 0; p++) {
273 if ((rt->rt_state & p->t_bits) == 0)
275 asprintf(&lstr, cp, olstr, p->t_name);
283 syslog(LOG_DEBUG, "%s", lstr);
288 tracesapactionlog(action, sap)
290 struct sap_entry *sap;
292 syslog(LOG_DEBUG, "%-12.12s service %04X %-20.20s "
293 "addr %s.%04X %c metric %d\n",
295 ntohs(sap->sap.ServType),
297 ipxdp_ntoa(&sap->sap.ipx),
298 ntohs(sap->sap.ipx.x_port),
299 (sap->clone ? 'C' : ' '),
300 ntohs(sap->sap.hops));
305 register struct interface *ifp;
308 if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
309 fprintf(fd, "*** Packet history for interface %s ***\n",
311 dumptrace(fd, "to", &ifp->int_output);
312 dumptrace(fd, "from", &ifp->int_input);
313 fprintf(fd, "*** end packet history ***\n");
318 dumptrace(fd, dir, ifd)
321 register struct ifdebug *ifd;
323 register struct iftrace *t;
324 char *cp = !strcmp(dir, "to") ? "Output" : "Input";
326 if (ifd->ifd_front == ifd->ifd_records &&
327 ifd->ifd_front->ift_size == 0) {
328 fprintf(fd, "%s: no packets.\n", cp);
331 fprintf(fd, "%s trace:\n", cp);
332 t = ifd->ifd_front - ifd->ifd_count;
333 if (t < ifd->ifd_records)
335 for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
336 if (t >= ifd->ifd_records + NRECORDS)
337 t = ifd->ifd_records;
338 if (t->ift_size == 0)
340 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
342 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
347 dumppacket(fd, dir, source, cp, size)
350 struct sockaddr *source;
354 register struct rip *msg = (struct rip *)cp;
355 register struct netinfo *n;
356 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
358 if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
359 fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
360 dir, ipxdp_ntoa(&who->sipx_addr),
361 ntohs(who->sipx_addr.x_port));
363 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
364 dir, ipxdp_ntoa(&who->sipx_addr),
365 ntohs(who->sipx_addr.x_port));
366 fprintf(fd, "size=%d cp=%p packet=%p\n", size,
370 switch (ntohs(msg->rip_cmd)) {
373 case RIPCMD_RESPONSE:
375 size -= sizeof (u_short);
377 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
378 if (size < sizeof (struct netinfo))
380 fprintf(fd, "\tnet %s metric %d ticks %d\n",
381 ipxdp_nettoa(n->rip_dst),
382 ntohs(n->rip_metric),
383 ntohs(n->rip_ticks));
391 dumpsappacket(fd, dir, source, cp, size)
394 struct sockaddr *source;
398 register struct sap_packet *msg = (struct sap_packet *)cp;
399 register struct sap_info *n;
400 struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
402 if (msg->sap_cmd && ntohs(msg->sap_cmd) < SAPCMD_MAX)
403 fprintf(fd, "%s %s %s#%x", sapcmds[ntohs(msg->sap_cmd)],
404 dir, ipxdp_ntoa(&who->sipx_addr),
405 ntohs(who->sipx_addr.x_port));
407 fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->sap_cmd),
408 dir, ipxdp_ntoa(&who->sipx_addr),
409 ntohs(who->sipx_addr.x_port));
410 fprintf(fd, "size=%d cp=%p packet=%p\n", size,
414 switch (ntohs(msg->sap_cmd)) {
421 size -= sizeof (u_short);
423 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
424 if (size < sizeof (struct sap_info))
426 fprintf(fd, " service %04X %-20.20s "
427 "addr %s.%04X metric %d\n",
431 ntohs(n->ipx.x_port),
444 register struct sap_entry *sap;
445 struct sap_hash *hash;
448 fprintf(fd, "------- SAP table dump. -------\n");
449 for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
450 fprintf(fd, "HASH %d\n", x);
452 for (; sap != (struct sap_entry *)hash; sap = sap->forw) {
453 fprintf(fd, " service %04X %-20.20s "
454 "addr %s.%04X %c metric %d\n",
455 ntohs(sap->sap.ServType),
457 ipxdp_ntoa(&sap->sap.ipx),
458 ntohs(sap->sap.ipx.x_port),
459 (sap->clone ? 'C' : ' '),
460 ntohs(sap->sap.hops));
470 register struct rt_entry *rip;
473 struct rthash *rh = nethash;
475 fprintf(fd, "------- RIP table dump. -------\n");
477 fprintf(fd, "Network table.\n");
479 for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
480 fprintf(fd, "HASH %d\n", x);
482 for (; rip != (struct rt_entry *)hash; rip = rip->rt_forw) {
483 fprintf(fd, " dest %s\t",
484 ipxdp_ntoa(&satoipx_addr(rip->rt_dst)));
485 fprintf(fd, "%s metric %d, ticks %d\n",
486 ipxdp_ntoa(&satoipx_addr(rip->rt_router)),
500 static char buf[100];
502 (void)sprintf(buf, "%u", ntohl(net.long_e));
509 struct ipx_addr *addr;
511 static char buf[100];
513 (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
514 ipxdp_nettoa(addr->x_net),
515 addr->x_host.c_host[0], addr->x_host.c_host[1],
516 addr->x_host.c_host[2], addr->x_host.c_host[3],
517 addr->x_host.c_host[4], addr->x_host.c_host[5]);