home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / MSWATTCP.ZIP / SRC / PCDBUG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-16  |  5.3 KB  |  194 lines

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