Annotation of 43BSDReno/sbin/XNSrouted/trace.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1985 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This file includes significant work done at Cornell University by
                      6:  * Bill Nesheim.  That work included by permission.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted
                      9:  * provided that: (1) source distributions retain this entire copyright
                     10:  * notice and comment, and (2) distributions including binaries display
                     11:  * the following acknowledgement:  ``This product includes software
                     12:  * developed by the University of California, Berkeley and its contributors''
                     13:  * in the documentation or other materials provided with the distribution
                     14:  * and in all advertising materials mentioning features or use of this
                     15:  * software. Neither the name of the University nor the names of its
                     16:  * contributors may be used to endorse or promote products derived
                     17:  * from this software without specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     19:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: static char sccsid[] = "@(#)trace.c    5.9 (Berkeley) 6/1/90";
                     25: #endif /* not lint */
                     26: 
                     27: /*
                     28:  * Routing Table Management Daemon
                     29:  */
                     30: #define        RIPCMDS
                     31: #include "defs.h"
                     32: 
                     33: #define        NRECORDS        50              /* size of circular trace buffer */
                     34: #ifdef DEBUG
                     35: FILE   *ftrace = stdout;
                     36: int    tracing = 1;
                     37: #else DEBUG
                     38: FILE   *ftrace = NULL;
                     39: int    tracing = 0;
                     40: #endif
                     41: 
                     42: char *xns_ntoa();
                     43: 
                     44: traceinit(ifp)
                     45:        register struct interface *ifp;
                     46: {
                     47: 
                     48:        if (iftraceinit(ifp, &ifp->int_input) &&
                     49:            iftraceinit(ifp, &ifp->int_output))
                     50:                return;
                     51:        tracing = 0;
                     52:        syslog(LOG_ERR, "traceinit: can't init %s\n", ifp->int_name);
                     53: }
                     54: 
                     55: static
                     56: iftraceinit(ifp, ifd)
                     57:        struct interface *ifp;
                     58:        register struct ifdebug *ifd;
                     59: {
                     60:        register struct iftrace *t;
                     61: 
                     62:        ifd->ifd_records =
                     63:          (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace));
                     64:        if (ifd->ifd_records == 0)
                     65:                return (0);
                     66:        ifd->ifd_front = ifd->ifd_records;
                     67:        ifd->ifd_count = 0;
                     68:        for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) {
                     69:                t->ift_size = 0;
                     70:                t->ift_packet = 0;
                     71:        }
                     72:        ifd->ifd_if = ifp;
                     73:        return (1);
                     74: }
                     75: 
                     76: traceon(file)
                     77:        char *file;
                     78: {
                     79: 
                     80:        if (ftrace != NULL)
                     81:                return;
                     82:        ftrace = fopen(file, "a");
                     83:        if (ftrace == NULL)
                     84:                return;
                     85:        dup2(fileno(ftrace), 1);
                     86:        dup2(fileno(ftrace), 2);
                     87:        tracing = 1;
                     88: }
                     89: 
                     90: traceoff()
                     91: {
                     92:        if (!tracing)
                     93:                return;
                     94:        if (ftrace != NULL)
                     95:                fclose(ftrace);
                     96:        ftrace = NULL;
                     97:        tracing = 0;
                     98: }
                     99: 
                    100: trace(ifd, who, p, len, m)
                    101:        register struct ifdebug *ifd;
                    102:        struct sockaddr *who;
                    103:        char *p;
                    104:        int len, m;
                    105: {
                    106:        register struct iftrace *t;
                    107: 
                    108:        if (ifd->ifd_records == 0)
                    109:                return;
                    110:        t = ifd->ifd_front++;
                    111:        if (ifd->ifd_front >= ifd->ifd_records + NRECORDS)
                    112:                ifd->ifd_front = ifd->ifd_records;
                    113:        if (ifd->ifd_count < NRECORDS)
                    114:                ifd->ifd_count++;
                    115:        if (t->ift_size > 0 && t->ift_packet)
                    116:                free(t->ift_packet);
                    117:        t->ift_packet = 0;
                    118:        t->ift_stamp = time(0);
                    119:        t->ift_who = *who;
                    120:        if (len > 0) {
                    121:                t->ift_packet = malloc(len);
                    122:                if (t->ift_packet)
                    123:                        bcopy(p, t->ift_packet, len);
                    124:                else
                    125:                        len = 0;
                    126:        }
                    127:        t->ift_size = len;
                    128:        t->ift_metric = m;
                    129: }
                    130: 
                    131: traceaction(fd, action, rt)
                    132:        FILE *fd;
                    133:        char *action;
                    134:        struct rt_entry *rt;
                    135: {
                    136:        struct sockaddr_ns *dst, *gate;
                    137:        static struct bits {
                    138:                int     t_bits;
                    139:                char    *t_name;
                    140:        } flagbits[] = {
                    141:                { RTF_UP,       "UP" },
                    142:                { RTF_GATEWAY,  "GATEWAY" },
                    143:                { RTF_HOST,     "HOST" },
                    144:                { 0 }
                    145:        }, statebits[] = {
                    146:                { RTS_PASSIVE,  "PASSIVE" },
                    147:                { RTS_REMOTE,   "REMOTE" },
                    148:                { RTS_INTERFACE,"INTERFACE" },
                    149:                { RTS_CHANGED,  "CHANGED" },
                    150:                { 0 }
                    151:        };
                    152:        register struct bits *p;
                    153:        register int first;
                    154:        char *cp;
                    155:        struct interface *ifp;
                    156: 
                    157:        if (fd == NULL)
                    158:                return;
                    159:        fprintf(fd, "%s ", action);
                    160:        dst = (struct sockaddr_ns *)&rt->rt_dst;
                    161:        gate = (struct sockaddr_ns *)&rt->rt_router;
                    162:        fprintf(fd, "dst %s, ", xns_ntoa(&dst->sns_addr));
                    163:        fprintf(fd, "router %s, metric %d, flags",
                    164:             xns_ntoa(&gate->sns_addr), rt->rt_metric);
                    165:        cp = " %s";
                    166:        for (first = 1, p = flagbits; p->t_bits > 0; p++) {
                    167:                if ((rt->rt_flags & p->t_bits) == 0)
                    168:                        continue;
                    169:                fprintf(fd, cp, p->t_name);
                    170:                if (first) {
                    171:                        cp = "|%s";
                    172:                        first = 0;
                    173:                }
                    174:        }
                    175:        fprintf(fd, " state");
                    176:        cp = " %s";
                    177:        for (first = 1, p = statebits; p->t_bits > 0; p++) {
                    178:                if ((rt->rt_state & p->t_bits) == 0)
                    179:                        continue;
                    180:                fprintf(fd, cp, p->t_name);
                    181:                if (first) {
                    182:                        cp = "|%s";
                    183:                        first = 0;
                    184:                }
                    185:        }
                    186:        putc('\n', fd);
                    187:        if (!tracepackets && (rt->rt_state & RTS_PASSIVE) == 0 && rt->rt_ifp)
                    188:                dumpif(fd, rt->rt_ifp);
                    189:        fflush(fd);
                    190: }
                    191: 
                    192: dumpif(fd, ifp)
                    193:        register struct interface *ifp;
                    194:        FILE *fd;
                    195: {
                    196:        if (ifp->int_input.ifd_count || ifp->int_output.ifd_count) {
                    197:                fprintf(fd, "*** Packet history for interface %s ***\n",
                    198:                        ifp->int_name);
                    199:                dumptrace(fd, "to", &ifp->int_output);
                    200:                dumptrace(fd, "from", &ifp->int_input);
                    201:                fprintf(fd, "*** end packet history ***\n");
                    202:        }
                    203: }
                    204: 
                    205: dumptrace(fd, dir, ifd)
                    206:        FILE *fd;
                    207:        char *dir;
                    208:        register struct ifdebug *ifd;
                    209: {
                    210:        register struct iftrace *t;
                    211:        char *cp = !strcmp(dir, "to") ? "Output" : "Input";
                    212: 
                    213:        if (ifd->ifd_front == ifd->ifd_records &&
                    214:            ifd->ifd_front->ift_size == 0) {
                    215:                fprintf(fd, "%s: no packets.\n", cp);
                    216:                return;
                    217:        }
                    218:        fprintf(fd, "%s trace:\n", cp);
                    219:        t = ifd->ifd_front - ifd->ifd_count;
                    220:        if (t < ifd->ifd_records)
                    221:                t += NRECORDS;
                    222:        for ( ; ifd->ifd_count; ifd->ifd_count--, t++) {
                    223:                if (t >= ifd->ifd_records + NRECORDS)
                    224:                        t = ifd->ifd_records;
                    225:                if (t->ift_size == 0)
                    226:                        continue;
                    227:                fprintf(fd, "%.24s: metric=%d\n", ctime(&t->ift_stamp),
                    228:                        t->ift_metric);
                    229:                dumppacket(fd, dir, &t->ift_who, t->ift_packet, t->ift_size);
                    230:        }
                    231: }
                    232: 
                    233: dumppacket(fd, dir, who, cp, size)
                    234:        FILE *fd;
                    235:        struct sockaddr_ns *who;                /* should be sockaddr */
                    236:        char *dir, *cp;
                    237:        register int size;
                    238: {
                    239:        register struct rip *msg = (struct rip *)cp;
                    240:        register struct netinfo *n;
                    241:        char *xns_nettoa();
                    242: 
                    243:        if (msg->rip_cmd && ntohs(msg->rip_cmd) < RIPCMD_MAX)
                    244:                fprintf(fd, "%s %s %s#%x", ripcmds[ntohs(msg->rip_cmd)],
                    245:                    dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port));
                    246:        else {
                    247:                fprintf(fd, "Bad cmd 0x%x %s %s#%x\n", ntohs(msg->rip_cmd),
                    248:                    dir, xns_ntoa(&who->sns_addr), ntohs(who->sns_addr.x_port));
                    249:                fprintf(fd, "size=%d cp=%x packet=%x\n", size, cp, packet);
                    250:                return;
                    251:        }
                    252:        switch (ntohs(msg->rip_cmd)) {
                    253: 
                    254:        case RIPCMD_REQUEST:
                    255:        case RIPCMD_RESPONSE:
                    256:                fprintf(fd, ":\n");
                    257:                size -= sizeof (u_short);
                    258:                n = msg->rip_nets;
                    259:                for (; size > 0; n++, size -= sizeof (struct netinfo)) {
                    260:                        if (size < sizeof (struct netinfo))
                    261:                                break;
                    262:                        fprintf(fd, "\tnet %s metric %d\n",
                    263:                             xns_nettoa(n->rip_dst),
                    264:                             ntohs(n->rip_metric));
                    265:                }
                    266:                break;
                    267: 
                    268:        }
                    269: }
                    270: 
                    271: union ns_net_u net;
                    272: 
                    273: char *
                    274: xns_nettoa(val)
                    275: union ns_net val;
                    276: {
                    277:        static char buf[100];
                    278:        net.net_e = val;
                    279:        (void)sprintf(buf, "%lx", ntohl(net.long_e));
                    280:        return (buf);
                    281: }
                    282: 
                    283: 
                    284: char *
                    285: xns_ntoa(addr)
                    286: struct ns_addr *addr;
                    287: {
                    288:     static char buf[100];
                    289: 
                    290:     (void)sprintf(buf, "%s#%x:%x:%x:%x:%x:%x",
                    291:        xns_nettoa(addr->x_net),
                    292:        addr->x_host.c_host[0], addr->x_host.c_host[1], 
                    293:        addr->x_host.c_host[2], addr->x_host.c_host[3], 
                    294:        addr->x_host.c_host[4], addr->x_host.c_host[5]);
                    295:        
                    296:     return(buf);
                    297: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.