home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / UNZIPPED / WNWATTCP / SRC / PCDBUG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-09  |  5.6 KB  |  187 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. #ifdef WINDOWS
  8. #include <windows.h>
  9. #endif
  10.  
  11. extern void (*_dbugxmit)();
  12. extern void (*_dbugrecv)();
  13. #define DEBUGNAME "WATTCP.DBG"
  14.  
  15. char debugname[ 128 ];
  16. int debugheaders, debugdump, debugudp, debugtcp;
  17.  
  18. static char localbuf[ 128 ];
  19. static int localhandle = 0;
  20.  
  21. db_write( char *msg )
  22. {
  23.     write( localhandle, msg, strlen(msg));
  24. }
  25. static char *tcpflag[] =
  26.     /*  7  ,   6 ,  5  ,   4 ,   3 ,   2 ,   1 ,   0 */
  27.       {"??7","??6","URG","ACK","PSH","RST","SYN","FIN" };
  28. static char *tcpmode[] = {
  29.     "LISTEN","SYNSENT","SYNREC","ESTAB","ESTCLOSE","FINWT1","FINWT2",
  30.     "CLOSWT","CLOSING","LASTACK","TIMEWT","CLOSEMSL","CLOSED" };
  31.  
  32. static db_msg( char *msg, tcp_Socket *sock, in_Header *ip, tcp_Header *tp )
  33. {
  34.     int i,j,datalen, protocol;
  35.     char ch, *data;
  36.     udp_Header *up;
  37.  
  38.     switch ( protocol = ip->proto ) {
  39.     case UDP_PROTO :if (!debugudp) return;
  40.             up = (udp_Header*)(tp);
  41.             datalen = intel16(up->length);
  42.             data = (char *)(up) + sizeof( udp_Header );
  43.             break;
  44.     case TCP_PROTO :if (!debugtcp) return;
  45.             i = (tcp_GetDataOffset(tp) << 2); /* qwords to bytes */
  46.             j = in_GetHdrlenBytes(ip);
  47.             data = (char*)(tp) + i;
  48.             datalen = intel16(ip->length) - j - i;
  49.             break;
  50.     default           : return;
  51.     }
  52.     if (!localhandle) {
  53.     localhandle = _creat( debugname, 0 );
  54.     if (localhandle < 0 ) {
  55. #ifdef WINDOWS
  56.         MessageBox(GetFocus(), (LPSTR)"Unable to open debug file!",
  57.                 (LPSTR)"ERROR", MB_OK);
  58.         return;
  59. #else
  60.         outs("ERROR:unable to open debug file!\n");
  61.         exit(3);
  62. #endif
  63.     }
  64.     }
  65.     /* skip packet if no data and that was all we were looking for */
  66.     if (!debugheaders && !datalen) return;
  67.     db_write( msg );
  68.     if (!sock) {
  69.     db_write( "NO SOCKET\n\n");
  70. /*
  71.     return;
  72. */
  73.     }
  74.     db_write( inet_ntoa( localbuf, sock->hisaddr ));
  75.     db_write( ":" );
  76.     db_write( itoa( sock->hisport, localbuf, 10));
  77.     db_write( "   0.0.0.0:");
  78.     db_write( itoa( sock->myport,  localbuf, 10));
  79.     db_write("\n");
  80.     if (debugheaders) {
  81.     switch (protocol) {
  82.         case UDP_PROTO : db_write("UDP PACKET");
  83.                  break;
  84.         case TCP_PROTO :
  85.                  db_write("    TCP : ");
  86.                  db_write( tcpmode[ sock->state ] );
  87.                  db_write("  (LSEQ: 0x");
  88.                  db_write(ltoa(sock->seqnum,localbuf,16));
  89.                  db_write("  LACK: 0x");
  90.                  db_write(ltoa(sock->acknum,localbuf,16));
  91.                              db_write(") NOW: ");
  92.                              db_write( ltoa( set_timeout(0), localbuf,10));
  93.                  db_write("\n    TCP FLAGS : ");
  94.                  for ( i = 0; i < 8 ; ++i ) {
  95.                  if ( intel16(tp->flags) & ( 0x80 >> i )) {
  96.                      db_write( tcpflag[i] );
  97.                      db_write(" ");
  98.                  }
  99.                  }
  100.                  db_write("  SEQ : 0x");
  101.                  db_write(ltoa(intel(tp->seqnum),localbuf,16));
  102.                  db_write("  ACK : 0x");
  103.                  db_write(ltoa(intel(tp->acknum),localbuf,16));
  104.                  db_write("  WINDOW : ");
  105.                  db_write(itoa(intel16(tp->window),localbuf,10));
  106.                  db_write("\n K_C : ");
  107.                  db_write(itoa(sock->karn_count,localbuf,10 ));
  108.                  db_write("  VJ_SA : ");
  109.                  db_write(itoa(sock->vj_sa ,localbuf,10 ) );
  110.                  db_write("  VJ_SD : ");
  111.                  db_write(itoa(sock->vj_sd,localbuf,10 ) );
  112.                  db_write("  RTO : ");
  113.                  db_write(itoa(sock->rto ,localbuf,10 ));
  114.                  db_write(" RTT : ");
  115.                  db_write(ltoa(sock->rtt_time ,localbuf,10 ));
  116.                  db_write(" RTTDIFF : ");
  117.                  db_write(ltoa(sock->rtt_time - set_ttimeout(0),localbuf,10 ));
  118.  
  119.                  break;
  120.     }
  121.     db_write("\n");
  122.     }
  123.     if (debugdump) {
  124.     for (i = 0; i < datalen ; i+= 16 ) {
  125.         sprintf(localbuf,"%04x : ", i );
  126.         db_write( localbuf );
  127.         for (j = 0 ; (j < 16) && (j +i < datalen) ; ++j ) {
  128.         sprintf( localbuf, "%02x%c", data[j+i], (j==7)?'-':' ');
  129.         db_write( localbuf );
  130.         }
  131.         for ( ; j < 16 ; ++j )
  132.         db_write("   ");
  133.  
  134.         memset( localbuf, 0, 17 );
  135.         for ( j = 0; (j<16) && (j+i<datalen) ; ++j ) {
  136.         ch = data[j+i];
  137.                 if ( !isprint(ch) ) ch = '.';
  138.                 localbuf[j] = ch;
  139.             }
  140.             db_write( localbuf);
  141.             db_write("\n");
  142.         }
  143.     }
  144.     db_write("\n");
  145.     if ( (i = dup( localhandle )) != -1 )
  146.         close(i);
  147. }
  148. static _dbxmit( void *sock, void *ip, void *prot, void *data )
  149. {
  150.     db_msg("Transmitted:",sock,ip,prot);
  151. }
  152. static _dbrecv( void *sock, void *ip, void *prot, void *data )
  153. {
  154.     db_msg("Received:",sock,ip,prot);
  155. }
  156.  
  157.  
  158. static void (*otherinit)();
  159. static void ourinit(char *name, char *value )
  160. {
  161.     if (!strcmp(name,"DEBUG.FILE")) {
  162.         strncpy(debugname, value, sizeof(debugname)-2);
  163.         debugname[sizeof(debugname) -1] = 0;
  164.     } else if (!strcmp(name,"DEBUG.MODE")) {
  165.         if (!stricmp( value, "DUMP" )) debugdump = 1;
  166.     if (!stricmp( value, "HEADERS")) debugheaders =1;
  167.         if (!stricmp( value, "ALL")) debugheaders = debugdump = 1;
  168.     } else if (!strcmp(name,"DEBUG.PROTO")) {
  169.         if (!stricmp( value, "TCP")) debugtcp = 1;
  170.     if (!stricmp( value, "UDP")) debugudp =1;
  171.         if (!stricmp( value, "ALL")) debugudp = debugtcp = 1;
  172.     } else if (otherinit)
  173.     (*otherinit)(name,value);
  174. }
  175.  
  176. extern void (*usr_init)();
  177.  
  178. dbuginit()
  179. {
  180.     strcpy(debugname,DEBUGNAME );
  181.     otherinit = usr_init;
  182.     usr_init = ourinit;
  183.     _dbugxmit = _dbxmit;
  184.     _dbugrecv = _dbrecv;
  185.     debugheaders = debugdump = debugudp = debugtcp = 0;
  186. }
  187.