home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / telecomm / ka9q_src / trace.c < prev    next >
C/C++ Source or Header  |  1988-11-29  |  4KB  |  215 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "config.h"
  5. #include "mbuf.h"
  6. #include "iface.h"
  7. #include "trace.h"
  8.  
  9. extern FILE *stdprt;
  10. extern int printtrace;        /* DG2KK: (see st.c kbread()) for details */
  11.  
  12. /* Redefined here so that programs calling dump in the library won't pull
  13.  * in the rest of the package
  14.  */
  15. static char nospace[] = "No space!!\n";
  16.  
  17. dump(interface,direction,type,bp)
  18. register struct interface *interface;
  19. int direction;
  20. unsigned type;
  21. struct mbuf *bp;
  22. {
  23.     struct mbuf *tbp;
  24.     void ascii_dump(),hex_dump();
  25.     int ax25_dump(),ether_dump(),ip_dump(),at_dump(),slfp_dump();
  26.     int (*func)();
  27.     /* added (timestamp) */
  28.     char *cp;
  29.     long t;
  30.     int16 size;
  31.  
  32.     if((interface->trace & direction) == 0)
  33.         return;    /* Nothing to trace */
  34.  
  35.     if(bp == NULLBUF || (size = len_mbuf(bp)) == 0)
  36.         return;
  37.  
  38. #ifdef SCREEN
  39.     (void)outscreen(1);        /* DG2KK: switch to trace screen */
  40. #endif
  41.     time(&t);            /* DG2KK: get time */
  42.     cp = ctime(&t);
  43.     rip(cp);
  44.  
  45.     switch(direction){
  46.     case IF_TRACE_IN:
  47.         printf("%s recv: [%s]\n",interface->name,cp);
  48.         if (printtrace != 0)
  49.             fprintf(stdprt,"%s recv: [%s]\n",interface->name,cp);
  50.         break;
  51.     case IF_TRACE_OUT:
  52.         printf("%s sent: [%s]\n",interface->name,cp);
  53.         if (printtrace != 0)
  54.             fprintf(stdprt,"%s sent: [%s]\n",interface->name,cp);
  55.         break;
  56.     }
  57.     if(type < NTRACE)
  58.         func = tracef[type];
  59.     else
  60.         func = NULLFP;
  61.  
  62.     dup_p(&tbp,bp,0,size);
  63.     if(tbp == NULLBUF){
  64.         printf(nospace);
  65.         if (printtrace != 0) {
  66.             fprintf(stdprt,nospace);
  67.             fflush(stdprt);
  68.         }
  69.         fflush(stdout);
  70.         return;
  71.     }
  72.     if(func != NULLFP)
  73.         (*func)(&tbp,1);
  74.     if(interface->trace & IF_TRACE_ASCII){
  75.         /* Dump only data portion of packet in ascii */
  76.         ascii_dump(&tbp);
  77.     } else if(interface->trace & IF_TRACE_HEX){
  78.         /* Dump entire packet in hex/ascii */
  79.         free_p(tbp);
  80.         dup_p(&tbp,bp,0,len_mbuf(bp));
  81.         if(tbp != NULLBUF)
  82.             hex_dump(&tbp);
  83.         else
  84.             printf(nospace);
  85.     }
  86.     free_p(tbp);
  87.     printf("\n");            /* added */
  88.     if (printtrace != 0)
  89.         fprintf(stdprt,"\n");
  90.     fflush(stdout);
  91.     if (printtrace != 0)
  92.         fflush(stdprt);
  93.  
  94. #ifdef SCREEN
  95.     (void)outscreen(0);        /* switch back to main screen */
  96. #endif
  97. }
  98.  
  99. /* Dump an mbuf in hex */
  100. void
  101. hex_dump(bpp)
  102. register struct mbuf **bpp;
  103. {
  104.     int16 n;
  105.     int16 address;
  106.     void fmtline();
  107.     char buf[16];
  108.  
  109.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  110.         return;
  111.  
  112.     address = 0;
  113.     while((n = pullup(bpp,buf,16)) != 0){
  114.         fmtline(address,buf,n);
  115.         address += n;
  116.     }
  117. }
  118. /* Dump an mbuf in ascii */
  119. void
  120. ascii_dump(bpp)
  121. register struct mbuf **bpp;
  122. {
  123.     char c;
  124.     register int16 tot;
  125.  
  126.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  127.         return;
  128.  
  129.     tot = 0;
  130.     while(pullup(bpp,&c,1) == 1){
  131.         if((tot % 64) == 0) {
  132.             printf("%04x  ",tot);
  133.             if (printtrace != 0) {
  134.                 fprintf(stdprt,"%04x  ",tot);
  135.             }
  136.         }
  137.         if(isprint(c)) {
  138.             putchar(c);
  139.             if (printtrace != 0)
  140.                 putc(c,stdprt);
  141.         } else {
  142.             putchar('.');
  143.             if (printtrace != 0)
  144.                 putc('.',stdprt);
  145.         }
  146.         tot++;
  147.         if((tot % 64) == 0) {
  148.             printf("\n");
  149.             if (printtrace != 0) {
  150.                 fprintf(stdprt,"\n");
  151.             }
  152.         }
  153.     }
  154.     if((tot % 64) != 0) {
  155.         printf("\n");
  156.         if (printtrace != 0) {
  157.             fprintf(stdprt,"\n");
  158.         }
  159.     }
  160. }
  161.  
  162. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  163.  * translation, e.g.,
  164.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  165.  */
  166. void
  167. fmtline(addr,buf,len)
  168. int16 addr;
  169. char *buf;
  170. int16 len;
  171. {
  172.     char line[80];
  173.     register char *aptr,*cptr;
  174.     register int16 c;
  175.     void ctohex();
  176.  
  177.     for(cptr = line;cptr < &line[80];cptr++)
  178.         *cptr = ' ';
  179.     ctohex(line,(int16)hibyte(addr));
  180.     ctohex(line+2,(int16)lobyte(addr));
  181.     aptr = &line[6];
  182.     cptr = &line[55];
  183.     while(len-- != 0){
  184.         c = *buf++ & 0xff;
  185.         ctohex(aptr,c);
  186.         aptr += 3;
  187.         c &= 0x7f;
  188.         if(isprint(c)){
  189.             *cptr++ = c;
  190.         } else {
  191.             *cptr++ = '.';
  192.         }
  193.     }
  194.     *cptr++ = '\r';
  195.     *cptr++ = '\n';
  196.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  197.     /* added */
  198.     if (printtrace == 1) {
  199.         fwrite(line,1,(unsigned)(cptr-line),stdprt);
  200.     }
  201. }
  202. /* Convert byte to two ascii-hex characters */
  203. static
  204. void
  205. ctohex(buf,c)
  206. register char *buf;
  207. register int16 c;
  208. {
  209.     static char hex[] = "0123456789abcdef";
  210.  
  211.     buf[0] = hex[hinibble(c)];
  212.     buf[1] = hex[lonibble(c)];
  213. }
  214.  
  215.