home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / TCPDUMP.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  2KB  |  97 lines

  1. /* TCP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "internet.h"
  8. #include "iface.h"
  9. #include "tcp.h"
  10. #include "ip.h"
  11. #include "trace.h"
  12.   
  13. /* TCP segment header flags */
  14. static char *Tcpflags[] = {
  15.     "FIN",  /* 0x01 */
  16.     "SYN",  /* 0x02 */
  17.     "RST",  /* 0x04 */
  18.     "PSH",  /* 0x08 */
  19.     "ACK",  /* 0x10 */
  20.     "URG",  /* 0x20 */
  21.     "CE"    /* 0x40 */
  22. };
  23.   
  24. /* Dump a TCP segment header. Assumed to be in network byte order */
  25. void
  26. tcp_dump(fp,bpp,source,dest,check)
  27. FILE *fp;
  28. struct mbuf **bpp;
  29. int32 source,dest;  /* IP source and dest addresses */
  30. int check;      /* 0 if checksum test is to be bypassed */
  31. {
  32.     struct tcp seg;
  33.     struct pseudo_header ph;
  34.     int16 csum;
  35.     int16 dlen;
  36.   
  37.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  38.         return;
  39.   
  40.     /* Verify checksum */
  41.     ph.source = source;
  42.     ph.dest = dest;
  43.     ph.protocol = TCP_PTCL;
  44.     ph.length = len_p(*bpp);
  45.     csum = cksum(&ph,*bpp,ph.length);
  46.   
  47.     ntohtcp(&seg,bpp);
  48.   
  49. #ifdef MONITOR
  50.     if (Trace_compact_header)
  51.         fprintf(fp, "%u->%u", seg.source, seg.dest);
  52.     else
  53. #endif
  54.         fprintf(fp,"TCP: %u->%u Seq x%lx",seg.source,seg.dest,seg.seq,seg.ack);
  55. #ifdef MONITOR
  56.     if (!Trace_compact_header)
  57. #endif
  58.         if(seg.flags.ack)
  59.             fprintf(fp," Ack x%lx",seg.ack);
  60.     if(seg.flags.congest)
  61.         fprintf(fp," %s",Tcpflags[6]);
  62.     if(seg.flags.urg)
  63.         fprintf(fp," %s",Tcpflags[5]);
  64.     if(seg.flags.ack)
  65.         fprintf(fp," %s",Tcpflags[4]);
  66.     if(seg.flags.psh)
  67.         fprintf(fp," %s",Tcpflags[3]);
  68.     if(seg.flags.rst)
  69.         fprintf(fp," %s",Tcpflags[2]);
  70.     if(seg.flags.syn)
  71.         fprintf(fp," %s",Tcpflags[1]);
  72.     if(seg.flags.fin)
  73.         fprintf(fp," %s",Tcpflags[0]);
  74.   
  75. #ifdef MONITOR
  76.     if (!Trace_compact_header)
  77. #endif
  78.         fprintf(fp," Wnd %u",seg.wnd);
  79.     if(seg.flags.urg)
  80.         fprintf(fp," UP x%x",seg.up);
  81.     /* Print options, if any */
  82. #ifdef MONITOR
  83.     if (!Trace_compact_header)
  84. #endif
  85.         if(seg.mss != 0)
  86.             fprintf(fp," MSS %u",seg.mss);
  87. #ifdef MONITOR
  88.     if (!Trace_compact_header)
  89. #endif
  90.         if((dlen = len_p(*bpp)) != 0)
  91.             fprintf(fp," Data %u",dlen);
  92.     if(check && csum != 0)
  93.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  94.     fprintf(fp,"\n");
  95. }
  96.   
  97.