home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / wattcp / src / pcdbug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  6.7 KB  |  228 lines

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