Merge from vendor branch LIBSTDC++:
[dragonfly.git] / usr.sbin / IPXrouted / trace.c
1 /*
2  * Copyright (c) 1985, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Copyright (c) 1995 John Hay.  All rights reserved.
6  *
7  * This file includes significant work done at Cornell University by
8  * Bill Nesheim.  That work included by permission.
9  *
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.
25  *
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.
37  *
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 $
40  *
41  * @(#)trace.c  8.1 (Berkeley) 6/5/93
42  */
43
44 /*
45  * Routing Table Management Daemon
46  */
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"
54
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
63
64 void dumpif(FILE *fd, struct interface *ifp);
65 void dumptrace(FILE *fd, char *dir, struct ifdebug *ifd);
66
67 void
68 traceinit(ifp)
69         register struct interface *ifp;
70 {
71         static int iftraceinit();
72
73         if (iftraceinit(ifp, &ifp->int_input) &&
74             iftraceinit(ifp, &ifp->int_output))
75                 return;
76         tracing = 0;
77         syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
78 }
79
80 static int
81 iftraceinit(ifp, ifd)
82         struct interface *ifp;
83         register struct ifdebug *ifd;
84 {
85         register struct iftrace *t;
86
87         ifd->ifd_records =
88           (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
89         if (ifd->ifd_records == 0)
90                 return (0);
91         ifd->ifd_front = ifd->ifd_records;
92         ifd->ifd_count = 0;
93         for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
94                 t->ift_size = 0;
95                 t->ift_packet = 0;
96         }
97         ifd->ifd_if = ifp;
98         return (1);
99 }
100
101 void
102 traceon(file)
103         char *file;
104 {
105
106         if (ftrace != NULL)
107                 return;
108         ftrace = fopen(file, "a");
109         if (ftrace == NULL)
110                 return;
111         dup2(fileno(ftrace), 1);
112         dup2(fileno(ftrace), 2);
113         tracing = 1;
114 }
115
116 void
117 traceoff(void)
118 {
119         if (!tracing)
120                 return;
121         if (ftrace != NULL)
122                 fclose(ftrace);
123         ftrace = NULL;
124         tracing = 0;
125 }
126
127 void
128 trace(ifd, who, p, len, m)
129         register struct ifdebug *ifd;
130         struct sockaddr *who;
131         char *p;
132         int len, m;
133 {
134         register struct iftrace *t;
135
136         if (ifd->ifd_records == 0)
137                 return;
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)
142                 ifd->ifd_count++;
143         if (t->ift_size > 0 && t->ift_packet)
144                 free(t->ift_packet);
145         t->ift_packet = 0;
146         t->ift_stamp = time(0);
147         t->ift_who = *who;
148         if (len > 0) {
149                 t->ift_packet = malloc(len);
150                 if (t->ift_packet)
151                         bcopy(p, t->ift_packet, len);
152                 else
153                         len = 0;
154         }
155         t->ift_size = len;
156         t->ift_metric = m;
157 }
158
159 void
160 traceaction(fd, action, rt)
161         FILE *fd;
162         char *action;
163         struct rt_entry *rt;
164 {
165         struct sockaddr_ipx *dst, *gate;
166         static struct bits {
167                 int     t_bits;
168                 char    *t_name;
169         } flagbits[] = {
170                 { RTF_UP,       "UP" },
171                 { RTF_GATEWAY,  "GATEWAY" },
172                 { RTF_HOST,     "HOST" },
173                 { 0 }
174         }, statebits[] = {
175                 { RTS_PASSIVE,  "PASSIVE" },
176                 { RTS_REMOTE,   "REMOTE" },
177                 { RTS_INTERFACE,"INTERFACE" },
178                 { RTS_CHANGED,  "CHANGED" },
179                 { 0 }
180         };
181         register struct bits *p;
182         register int first;
183         char *cp;
184
185         if (fd == NULL)
186                 return;
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);
193         cp = " %s";
194         for (first = 1, p = flagbits; p->t_bits > 0; p++) {
195                 if ((rt->rt_flags & p->t_bits) == 0)
196                         continue;
197                 fprintf(fd, cp, p->t_name);
198                 if (first) {
199                         cp = "|%s";
200                         first = 0;
201                 }
202         }
203         fprintf(fd, " state");
204         cp = " %s";
205         for (first = 1, p = statebits; p->t_bits > 0; p++) {
206                 if ((rt->rt_state & p->t_bits) == 0)
207                         continue;
208                 fprintf(fd, cp, p->t_name);
209                 if (first) {
210                         cp = "|%s";
211                         first = 0;
212                 }
213         }
214         putc('\n', fd);
215         if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
216                 dumpif(fd, rt->rt_ifp);
217         fflush(fd);
218 }
219
220 void
221 traceactionlog(action, rt)
222         char *action;
223         struct rt_entry *rt;
224 {
225         struct sockaddr_ipx *dst, *gate;
226         static struct bits {
227                 int     t_bits;
228                 char    *t_name;
229         } flagbits[] = {
230                 { RTF_UP,       "UP" },
231                 { RTF_GATEWAY,  "GATEWAY" },
232                 { RTF_HOST,     "HOST" },
233                 { 0 }
234         }, statebits[] = {
235                 { RTS_PASSIVE,  "PASSIVE" },
236                 { RTS_REMOTE,   "REMOTE" },
237                 { RTS_INTERFACE,"INTERFACE" },
238                 { RTS_CHANGED,  "CHANGED" },
239                 { 0 }
240         };
241         register struct bits *p;
242         register int first;
243         char *cp;
244         char *lstr, *olstr;
245
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));
249         olstr = lstr;
250         asprintf(&lstr, "%s router %s, metric %d, ticks %d, flags",
251              olstr, ipxdp_ntoa(&gate->sipx_addr), rt->rt_metric, rt->rt_ticks);
252         free(olstr);
253         olstr = lstr;
254         cp = "%s %s";
255         for (first = 1, p = flagbits; p->t_bits > 0; p++) {
256                 if ((rt->rt_flags & p->t_bits) == 0)
257                         continue;
258                 asprintf(&lstr, cp, olstr, p->t_name);
259                 free(olstr);
260                 olstr = lstr;
261                 if (first) {
262                         cp = "%s|%s";
263                         first = 0;
264                 }
265         }
266         asprintf(&lstr, "%s state", olstr);
267         free(olstr);
268         olstr = lstr;
269         cp = "%s %s";
270         for (first = 1, p = statebits; p->t_bits > 0; p++) {
271                 if ((rt->rt_state & p->t_bits) == 0)
272                         continue;
273                 asprintf(&lstr, cp, olstr, p->t_name);
274                 free(olstr);
275                 olstr = lstr;
276                 if (first) {
277                         cp = "%s|%s";
278                         first = 0;
279                 }
280         }
281         syslog(LOG_DEBUG, "%s", lstr);
282         free(lstr);
283 }
284
285 void
286 tracesapactionlog(action, sap)
287         char *action;
288         struct sap_entry *sap;
289 {
290         syslog(LOG_DEBUG, "%-12.12s  service %04X %-20.20s "
291                     "addr %s.%04X %c metric %d\n",
292                      action,
293                      ntohs(sap->sap.ServType),
294                      sap->sap.ServName,
295                      ipxdp_ntoa(&sap->sap.ipx),
296                      ntohs(sap->sap.ipx.x_port),
297                      (sap->clone ? 'C' : ' '),
298                      ntohs(sap->sap.hops));
299 }
300
301 void
302 dumpif(fd, ifp)
303         register struct interface *ifp;
304         FILE *fd;
305 {
306         if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
307                 fprintf(fd, "*** Packet history for interface %s ***\n",
308                         ifp->int_name);
309                 dumptrace(fd, "to", &ifp->int_output);
310                 dumptrace(fd, "from", &ifp->int_input);
311                 fprintf(fd, "*** end packet history ***\n");
312         }
313 }
314
315 void
316 dumptrace(fd, dir, ifd)
317         FILE *fd;
318         char *dir;
319         register struct ifdebug *ifd;
320 {
321         register struct iftrace *t;
322         char *cp = !strcmp(dir, "to") ? "Output" : "Input";
323
324         if (ifd->ifd_front == ifd->ifd_records &&
325             ifd->ifd_front->ift_size == 0) {
326                 fprintf(fd, "%s: no packets.\n", cp);
327                 return;
328         }
329         fprintf(fd, "%s trace:\n", cp);
330         t = ifd->ifd_front - ifd->ifd_count;
331         if (t < ifd->ifd_records)
332                 t += NRECORDS;
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)
337                         continue;
338                 fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
339                         t->ift_metric);
340                 dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
341         }
342 }
343
344 void
345 dumppacket(fd, dir, source, cp, size)
346         FILE *fd;
347         char *dir;
348         struct sockaddr *source;
349         char *cp;
350         register int size;
351 {
352         register struct rip *msg = (struct rip *)cp;
353         register struct netinfo *n;
354         struct sockaddr_ipx *who = (struct sockaddr_ipx *)source;
355
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));
360         else {
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);
366                 return;
367         }
368         switch (ntohs(msg->rip_cmd)) {
369
370         case RIPCMD_REQUEST:
371         case RIPCMD_RESPONSE:
372                 fprintf(fd, ":\n");
373                 size -= sizeof (u_short);
374                 n = msg->rip_nets;
375                 for (; size > 0; n++, size -= sizeof (struct netinfo)) {
376                         if (size < sizeof (struct netinfo))
377                                 break;
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));
382                 }
383                 break;
384
385         }
386 }
387
388 void
389 dumpsappacket(fd, dir, source, cp, size)
390         FILE *fd;
391         char *dir;
392         struct sockaddr *source;
393         char *cp;
394         register int size;
395 {
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;
399
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));
404         else {
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);
410                 return;
411         }
412         switch (ntohs(msg->sap_cmd)) {
413
414         case SAP_REQ:
415         case SAP_RESP:
416         case SAP_REQ_NEAR:
417         case SAP_RESP_NEAR:
418                 fprintf(fd, ":\n");
419                 size -= sizeof (u_short);
420                 n = msg->sap;
421                 for (; size > 0; n++, size -= sizeof (struct sap_info)) {
422                         if (size < sizeof (struct sap_info))
423                                 break;
424                         fprintf(fd, "  service %04X %-20.20s "
425                                     "addr %s.%04X metric %d\n",
426                              ntohs(n->ServType),
427                              n->ServName,
428                              ipxdp_ntoa(&n->ipx),
429                              ntohs(n->ipx.x_port),
430                              ntohs(n->hops));
431                 }
432                 break;
433
434         }
435 }
436
437 void
438 dumpsaptable(fd, sh)
439         FILE *fd;
440         struct sap_hash *sh;
441 {
442         register struct sap_entry *sap;
443         struct sap_hash *hash;
444         int x = 0;
445
446         fprintf(fd, "------- SAP table dump. -------\n");
447         for (hash = sh; hash < &sh[SAPHASHSIZ]; hash++, x++) {
448                 fprintf(fd, "HASH %d\n", x);
449                 sap = hash->forw;
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),
454                                      sap->sap.ServName,
455                                      ipxdp_ntoa(&sap->sap.ipx),
456                                      ntohs(sap->sap.ipx.x_port),
457                                      (sap->clone ? 'C' : ' '),
458                                      ntohs(sap->sap.hops));
459                 }
460         }
461         fprintf(fd, "\n");
462 }
463
464 void
465 dumpriptable(fd)
466         FILE *fd;
467 {
468         register struct rt_entry *rip;
469         struct rthash *hash;
470         int x;
471         struct rthash *rh = nethash;
472
473         fprintf(fd, "------- RIP table dump. -------\n");
474         x = 0;
475         fprintf(fd, "Network table.\n");
476
477         for (hash = rh; hash < &rh[ROUTEHASHSIZ]; hash++, x++) {
478                 fprintf(fd, "HASH %d\n", x);
479                 rip = hash->rt_forw;
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)),
485                                 rip->rt_metric,
486                                 rip->rt_ticks);
487                 }
488         }
489         fprintf(fd, "\n");
490 }
491
492 union ipx_net_u net;
493
494 char *
495 ipxdp_nettoa(val)
496 union ipx_net val;
497 {
498         static char buf[100];
499         net.net_e = val;
500         (void)sprintf(buf, "%lx", ntohl(net.long_e));
501         return (buf);
502 }
503
504
505 char *
506 ipxdp_ntoa(addr)
507 struct ipx_addr *addr;
508 {
509     static char buf[100];
510
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]);
516         
517     return(buf);
518 }