home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / WATTCP.ZIP / SRC / PCDBUG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-13  |  6.6 KB  |  225 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)();
  13. extern void (*_dbugrecv)();
  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. int db_open()
  27. {
  28.     if (!localhandle) {
  29.         localhandle = _creat( debugname, 0 );
  30.         if (localhandle < 0 ) {
  31.             outs("ERROR:unable to open debug file!\n");
  32.             exit(3);
  33.         }
  34.     }
  35. }
  36. void db_close()
  37. {
  38.     int i;
  39.     if ( (i = dup( localhandle )) != -1 )
  40.         close(i);
  41. }
  42.  
  43. void dbug_printf( char *format, ... )
  44. {
  45.     va_list argptr;
  46.     static char localspace[ 256 ];
  47.     if ( localhandle ) {
  48.         db_write( "\n > ");
  49.         va_start( argptr, format );
  50.         vsprintf( localspace, format, argptr );
  51.         va_end( argptr );
  52.         db_write( localspace );
  53.         db_write( "\n" );
  54.         db_close();
  55.     }
  56. }
  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 db_msg( char *msg, tcp_Socket *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->hisaddr ));
  104.         db_write( ":" );
  105.         db_write( itoa( sock->hisport, localbuf, 10));
  106.         db_write( "   0.0.0.0:");
  107.         db_write( itoa( sock->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->state ] );
  117.                  db_write("  (LSEQ: 0x");
  118.                  db_write(ltoa(sock->seqnum,localbuf,16));
  119.                  db_write("  LACK: 0x");
  120.                  db_write(ltoa(sock->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->karn_count,localbuf,10 ));
  139.                  db_write("  VJ_SA : ");
  140.                  db_write(itoa(sock->vj_sa ,localbuf,10 ) );
  141.                  db_write("  VJ_SD : ");
  142.                  db_write(itoa(sock->vj_sd,localbuf,10 ) );
  143.                  db_write("  RTO : ");
  144.                  db_write(itoa(sock->rto ,localbuf,10 ));
  145.                  db_write(" RTT : ");
  146.                  db_write(ltoa(sock->rtt_time ,localbuf,10 ));
  147.                  db_write(" RTTDIFF : ");
  148.                  db_write(ltoa(sock->rtt_time - set_ttimeout(0),localbuf,10 ));
  149.                  db_write(" UNHAPPY : ");
  150.                  db_write(itoa(sock->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. static void _dbxmit( void *sock, void *ip, void *prot, int line )
  184. {
  185.     db_msg("Transmitted:",sock,ip,prot,line);
  186. }
  187. static void _dbrecv( void *sock, void *ip, void *prot, int line )
  188. {
  189.     db_msg("Received:",sock,ip,prot, line);
  190. }
  191.  
  192.  
  193. static void (*otherinit)();
  194. static void ourinit(char *name, char *value )
  195. {
  196.     if (!strcmp(name,"DEBUG.FILE")) {
  197.         strncpy(debugname, value, sizeof(debugname)-2);
  198.         debugname[sizeof(debugname) -1] = 0;
  199.         db_open();
  200.     } else if (!strcmp(name,"DEBUG.MODE")) {
  201.         if (!stricmp( value, "DUMP" )) debugdump = 1;
  202.     if (!stricmp( value, "HEADERS")) debugheaders =1;
  203.         if (!stricmp( value, "ALL")) debugheaders = debugdump = 1;
  204.     } else if (!strcmp(name,"DEBUG.PROTO")) {
  205.         if (!stricmp( value, "TCP")) debugtcp = 1;
  206.     if (!stricmp( value, "UDP")) debugudp =1;
  207.         if (!stricmp( value, "ALL")) debugudp = debugtcp = 1;
  208.     } else if (otherinit)
  209.     (*otherinit)(name,value);
  210. }
  211.  
  212. extern void (*usr_init)();
  213.  
  214. dbug_init()
  215. {
  216.     strcpy(debugname,DEBUGNAME );
  217.     otherinit = usr_init;
  218.     usr_init = ourinit;
  219.     _dbugxmit = _dbxmit;
  220.     _dbugrecv = _dbrecv;
  221.     debugheaders = debugdump = debugudp = debugtcp = 0;
  222. }
  223.  
  224.  
  225.