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