home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / c / mscwattc / src / pcdbug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-17  |  5.5 KB  |  182 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8.  
  9. #include <wattcp.h>
  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 void 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.         outs("ERROR:unable to open debug file!\n");
  56.         exit(3);
  57.     }
  58.     }
  59.     /* skip packet if no data and that was all we were looking for */
  60.     if (!debugheaders && !datalen) return;
  61.     db_write( msg );
  62.     if (!sock) {
  63.         db_write( "NO SOCKET\n\n");
  64. /*
  65.         return;
  66. */
  67.     }
  68.     db_write( inet_ntoa( localbuf, sock->hisaddr ));
  69.     db_write( ":" );
  70.     db_write( itoa( sock->hisport, localbuf, 10));
  71.     db_write( "   0.0.0.0:");
  72.     db_write( itoa( sock->myport,  localbuf, 10));
  73.     db_write("\n");
  74.     if (debugheaders) {
  75.     switch (protocol) {
  76.         case UDP_PROTO : db_write("UDP PACKET");
  77.                  break;
  78.         case TCP_PROTO :
  79.                  db_write("    TCP : ");
  80.                  db_write( tcpmode[ sock->state ] );
  81.                  db_write("  (LSEQ: 0x");
  82.                  db_write(ltoa(sock->seqnum,localbuf,16));
  83.                  db_write("  LACK: 0x");
  84.                  db_write(ltoa(sock->acknum,localbuf,16));
  85.                              db_write(") NOW: ");
  86.                              db_write( ltoa( set_timeout(0), localbuf,10));
  87.                              db_write("\n    TCP FLAGS : ");
  88.                  for ( i = 0; i < 8 ; ++i ) {
  89.                  if ( intel16(tp->flags) & ( 0x80 >> i )) {
  90.                      db_write( tcpflag[i] );
  91.                      db_write(" ");
  92.                  }
  93.                  }
  94.  
  95.                  db_write("  SEQ : 0x");
  96.                  db_write(ltoa(intel(tp->seqnum),localbuf,16));
  97.                  db_write("  ACK : 0x");
  98.                  db_write(ltoa(intel(tp->acknum),localbuf,16));
  99.                  db_write("  WINDOW : ");
  100.                  db_write(itoa(intel16(tp->window),localbuf,10));
  101.                  db_write("\n K_C : ");
  102.                  db_write(itoa(sock->karn_count,localbuf,10 ));
  103.                  db_write("  VJ_SA : ");
  104.                  db_write(itoa(sock->vj_sa ,localbuf,10 ) );
  105.                  db_write("  VJ_SD : ");
  106.                  db_write(itoa(sock->vj_sd,localbuf,10 ) );
  107.                  db_write("  RTO : ");
  108.                  db_write(itoa(sock->rto ,localbuf,10 ));
  109.                  db_write(" RTT : ");
  110.                  db_write(ltoa(sock->rtt_time ,localbuf,10 ));
  111.                  db_write(" RTTDIFF : ");
  112.                  db_write(ltoa(sock->rtt_time - set_ttimeout(0),localbuf,10 ));
  113.  
  114.                  break;
  115.     }
  116.     db_write("\n");
  117.     }
  118.     if (debugdump) {
  119.     for (i = 0; i < datalen ; i+= 16 ) {
  120.         sprintf(localbuf,"%04x : ", i );
  121.         db_write( localbuf );
  122.         for (j = 0 ; (j < 16) && (j +i < datalen) ; ++j ) {
  123.                 sprintf( localbuf, "%02x%c", data[j+i], (j==7)?'-':' ');
  124.         db_write( localbuf );
  125.         }
  126.         for ( ; j < 16 ; ++j )
  127.         db_write("   ");
  128.  
  129.         memset( localbuf, 0, 17 );
  130.         for ( j = 0; (j<16) && (j+i<datalen) ; ++j ) {
  131.         ch = data[j+i];
  132.                 if ( !isprint(ch) ) ch = '.';
  133.                 localbuf[j] = ch;
  134.             }
  135.             db_write( localbuf);
  136.             db_write("\n");
  137.         }
  138.     }
  139.     db_write("\n");
  140.     if ( (i = dup( localhandle )) != -1 )
  141.         close(i);
  142. }
  143. static _dbxmit( void *sock, void *ip, void *prot, void *data )
  144. {
  145.     db_msg("Transmitted:",sock,ip,prot);
  146. }
  147. static _dbrecv( void *sock, void *ip, void *prot, void *data )
  148. {
  149.     db_msg("Received:",sock,ip,prot);
  150. }
  151.  
  152.  
  153. static void (*otherinit)();
  154. static void ourinit(char *name, char *value )
  155. {
  156.     if (!strcmp(name,"DEBUG.FILE")) {
  157.         strncpy(debugname, value, sizeof(debugname)-2);
  158.         debugname[sizeof(debugname) -1] = 0;
  159.     } else if (!strcmp(name,"DEBUG.MODE")) {
  160.         if (!stricmp( value, "DUMP" )) debugdump = 1;
  161.     if (!stricmp( value, "HEADERS")) debugheaders =1;
  162.         if (!stricmp( value, "ALL")) debugheaders = debugdump = 1;
  163.     } else if (!strcmp(name,"DEBUG.PROTO")) {
  164.         if (!stricmp( value, "TCP")) debugtcp = 1;
  165.     if (!stricmp( value, "UDP")) debugudp =1;
  166.         if (!stricmp( value, "ALL")) debugudp = debugtcp = 1;
  167.     } else if (otherinit)
  168.     (*otherinit)(name,value);
  169. }
  170.  
  171. extern void (*usr_init)();
  172.  
  173. dbuginit()
  174. {
  175.     strcpy(debugname,DEBUGNAME );
  176.     otherinit = usr_init;
  177.     usr_init = ourinit;
  178.     _dbugxmit = _dbxmit;
  179.     _dbugrecv = _dbrecv;
  180.     debugheaders = debugdump = debugudp = debugtcp = 0;
  181. }
  182.