home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / CEREBRUM / WAT9609.ZIP / SRC / PCDBUG.C < prev    next >
Text File  |  1994-11-28  |  7KB  |  226 lines

  1. #include <stdio.h>
  2. #include <wattcp.h>
  3. #include <fcntl.h>
  4. #include <sys/stat.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <stdarg.h>
  8. #include <io.h>
  9. #include <string.h>
  10. #include <mem.h>
  11.  
  12. extern void (*_dbugxmit)( sock_type *s, in_Header *inp, void *phdr, unsigned line );
  13. extern void (*_dbugrecv)( sock_type *s, in_Header *inp, void *phdr, unsigned line );
  14. #define DEBUGNAME "WATTCP.DBG"
  15.  
  16. char debugname[ 128 ];
  17. int debugheaders, debugdump, debugudp, debugtcp;
  18.  
  19. static char localbuf[ 128 ];
  20. static int localhandle = 0;
  21.  
  22. void db_write( char *msg )
  23. {
  24.     write( localhandle, msg, strlen(msg));
  25. }
  26.  
  27. void db_open( void )
  28. {
  29.     if (!localhandle) {
  30.         localhandle = _creat( debugname, 0 );
  31.         if (localhandle < 0 ) {
  32.             outs("ERROR:unable to open debug file!\n");
  33.             exit(3);
  34.         }
  35.     }
  36. }
  37.  
  38. void db_close( void )
  39. {
  40.     int i;
  41.     if ( (i = dup( localhandle )) != -1 )
  42.         close(i);
  43. }
  44.  
  45. void dbug_printf( char *format, ... )
  46. {
  47.     va_list argptr;
  48.     static char localspace[ 256 ];
  49.     if ( localhandle ) {
  50.         db_write( "\n > ");
  51.         va_start( argptr, format );
  52.         vsprintf( localspace, format, argptr );
  53.         va_end( argptr );
  54.         db_write( localspace );
  55.         db_write( "\n" );
  56.         db_close();
  57.     }
  58. }
  59.  
  60. static char *tcpflag[] =
  61.     /*  7  ,   6 ,  5  ,   4 ,   3 ,   2 ,   1 ,   0 */
  62.       {"??7","??6","URG","ACK","PSH","RST","SYN","FIN" };
  63. static char *tcpmode[] = {
  64.     "LISTEN","SYNSENT","SYNREC","ESTAB","ESTCLOSE","FINWT1","FINWT2",
  65.     "CLOSWT","CLOSING","LASTACK","TIMEWT","CLOSEMSL","CLOSED" };
  66.  
  67. static void db_msg( char *msg, sock_type *sock, in_Header *ip, tcp_Header *tp, int line )
  68. {
  69.     int i,j,datalen, protocol;
  70.     byte ch, *data;
  71.     udp_Header *up;
  72.  
  73.     switch ( protocol = ip->proto ) {
  74.     case UDP_PROTO :if (!debugudp) return;
  75.             up = (udp_Header*)(tp);
  76.             datalen = intel16(up->length);
  77.             data = (char *)(up) + sizeof( udp_Header );
  78.             break;
  79.     case TCP_PROTO :if (!debugtcp) return;
  80.             i = (tcp_GetDataOffset(tp) << 2); /* qwords to bytes */
  81.             j = in_GetHdrlenBytes(ip);
  82.             data = (char*)(tp) + i;
  83.             datalen = intel16(ip->length) - j - i;
  84.             break;
  85.     default           : return;
  86.     }
  87.     db_open();
  88.     /* skip packet if no data and that was all we were looking for */
  89.     if (!debugheaders && !datalen) return;
  90.     db_write( msg );
  91.     if (!sock) {
  92.         db_write( "NO SOCKET : ");
  93.         db_write( inet_ntoa( localbuf, intel( ip->source) ));
  94.         db_write( ":" );
  95.         db_write( itoa( intel16(tp->srcPort), localbuf, 10));
  96.         db_write( "   0.0.0.0:");
  97.         db_write( inet_ntoa( localbuf, intel( ip->destination ) ));
  98.         db_write( itoa( intel16(tp->dstPort),  localbuf, 10));
  99. /*
  100.         return;
  101. */
  102.     } else {
  103.         db_write( inet_ntoa( localbuf, sock->tcp.hisaddr ));
  104.         db_write( ":" );
  105.         db_write( itoa( sock->tcp.hisport, localbuf, 10));
  106.         db_write( "   0.0.0.0:");
  107.         db_write( itoa( sock->tcp.myport,  localbuf, 10));
  108.     }
  109.     db_write("\n");
  110.     if (debugheaders) {
  111.     switch (protocol) {
  112.         case UDP_PROTO : db_write("UDP PACKET");
  113.                  break;
  114.         case TCP_PROTO :
  115.                  db_write("    TCP : ");
  116.                  db_write( tcpmode[ sock->tcp.state ] );
  117.                  db_write("  (LSEQ: 0x");
  118.                  db_write(ltoa(sock->tcp.seqnum,localbuf,16));
  119.                  db_write("  LACK: 0x");
  120.                  db_write(ltoa(sock->tcp.acknum,localbuf,16));
  121.                              db_write(") NOW: ");
  122.                              db_write( ltoa( set_timeout(0), localbuf,10));
  123.                              db_write("\n    TCP FLAGS : ");
  124.                  for ( i = 0; i < 8 ; ++i ) {
  125.                  if ( intel16(tp->flags) & ( 0x80 >> i )) {
  126.                      db_write( tcpflag[i] );
  127.                      db_write(" ");
  128.                  }
  129.                  }
  130.  
  131.                  db_write("  SEQ : 0x");
  132.                  db_write(ltoa(intel(tp->seqnum),localbuf,16));
  133.                  db_write("  ACK : 0x");
  134.                  db_write(ltoa(intel(tp->acknum),localbuf,16));
  135.                  db_write("  WINDOW : ");
  136.                  db_write(itoa(intel16(tp->window),localbuf,10));
  137.                  db_write("\n K_C : ");
  138.                  db_write(itoa(sock->tcp.karn_count,localbuf,10 ));
  139.                  db_write("  VJ_SA : ");
  140.                  db_write(itoa(sock->tcp.vj_sa ,localbuf,10 ) );
  141.                  db_write("  VJ_SD : ");
  142.                  db_write(itoa(sock->tcp.vj_sd,localbuf,10 ) );
  143.                  db_write("  RTO : ");
  144.                  db_write(itoa(sock->tcp.rto ,localbuf,10 ));
  145.                  db_write(" RTT : ");
  146.                  db_write(ltoa(sock->tcp.rtt_time ,localbuf,10 ));
  147.                  db_write(" RTTDIFF : ");
  148.                  db_write(ltoa(sock->tcp.rtt_time - set_ttimeout(0),localbuf,10 ));
  149.                  db_write(" UNHAPPY : ");
  150.                  db_write(itoa(sock->tcp.unhappy,localbuf,10 ));
  151.                  if (line) {
  152.                     db_write(" LINE : ");
  153.                     db_write(itoa(line, localbuf, 10 ));
  154.                 }
  155.                 break;
  156.     }
  157.     db_write("\n");
  158.     }
  159.     if (debugdump) {
  160.     for (i = 0; i < datalen ; i+= 16 ) {
  161.         sprintf(localbuf,"%04x : ", i );
  162.         db_write( localbuf );
  163.         for (j = 0 ; (j < 16) && (j +i < datalen) ; ++j ) {
  164.                 sprintf( localbuf, "%02x%c", (unsigned) data[j+i], (j==7)?'-':' ');
  165.         db_write( localbuf );
  166.         }
  167.         for ( ; j < 16 ; ++j )
  168.         db_write("   ");
  169.  
  170.         memset( localbuf, 0, 17 );
  171.         for ( j = 0; (j<16) && (j+i<datalen) ; ++j ) {
  172.         ch = data[j+i];
  173.                 if ( !isprint(ch) ) ch = '.';
  174.                 localbuf[j] = ch;
  175.             }
  176.             db_write( localbuf);
  177.             db_write("\n");
  178.         }
  179.     }
  180.     db_write("\n");
  181.     db_close();
  182. }
  183.  
  184. static void _dbxmit( sock_type *sock, in_Header *ip, void *prot, unsigned line )
  185. {
  186.     db_msg("Transmitted:",sock,ip,prot,line);
  187. }
  188.  
  189. static void _dbrecv( sock_type *sock, in_Header *ip, void *prot, unsigned line )
  190. {
  191.     db_msg("Received:",sock,ip,prot, line);
  192. }
  193.  
  194.  
  195. static void (*otherinit)( char *name, char *value );
  196.  
  197. static void ourinit( char *name, char *value )
  198. {
  199.     if (!strcmp(name,"DEBUG.FILE")) {
  200.         strncpy(debugname, value, sizeof(debugname)-2);
  201.         debugname[sizeof(debugname) -1] = 0;
  202.         db_open();
  203.     } else if (!strcmp(name,"DEBUG.MODE")) {
  204.         if (!stricmp( value, "DUMP" )) debugdump = 1;
  205.     if (!stricmp( value, "HEADERS")) debugheaders =1;
  206.         if (!stricmp( value, "ALL")) debugheaders = debugdump = 1;
  207.     } else if (!strcmp(name,"DEBUG.PROTO")) {
  208.         if (!stricmp( value, "TCP")) debugtcp = 1;
  209.     if (!stricmp( value, "UDP")) debugudp =1;
  210.         if (!stricmp( value, "ALL")) debugudp = debugtcp = 1;
  211.     } else if (otherinit)
  212.     (*otherinit)(name,value);
  213. }
  214.  
  215. extern void (*usr_init)( char *name, char *value );
  216.  
  217. void dbug_init( void )
  218. {
  219.     strcpy(debugname,DEBUGNAME );
  220.     otherinit = usr_init;
  221.     usr_init = ourinit;
  222.     _dbugxmit = _dbxmit;
  223.     _dbugrecv = _dbrecv;
  224.     debugheaders = debugdump = debugudp = debugtcp = 0;
  225. }
  226.