home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / tcpipsrc / tcpdump.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  2KB  |  77 lines

  1. /* TCP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "netuser.h"
  8. #include "internet.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. };
  22.  
  23. /* Dump a TCP segment header. Assumed to be in network byte order */
  24. void
  25. tcp_dump(fp,bpp,source,dest,check)
  26. FILE *fp;
  27. struct mbuf **bpp;
  28. int32 source,dest;    /* IP source and dest addresses */
  29. int check;        /* 0 if checksum test is to be bypassed */
  30. {
  31.     struct tcp seg;
  32.     struct pseudo_header ph;
  33.     int16 csum;
  34.     int16 dlen;
  35.  
  36.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  37.         return;
  38.  
  39.     /* Verify checksum */
  40.     ph.source = source;
  41.     ph.dest = dest;
  42.     ph.protocol = TCP_PTCL;
  43.     ph.length = len_p(*bpp);
  44.     csum = cksum(&ph,*bpp,ph.length);
  45.  
  46.     ntohtcp(&seg,bpp);
  47.  
  48.     fprintf(fp,"TCP: %u->%u Seq x%lx",seg.source,seg.dest,seg.seq,seg.ack);
  49.     if(seg.flags.ack)
  50.         fprintf(fp," Ack x%lx",seg.ack);
  51.     if(seg.flags.urg)
  52.         fprintf(fp," %s",Tcpflags[5]);
  53.     if(seg.flags.ack)
  54.         fprintf(fp," %s",Tcpflags[4]);
  55.     if(seg.flags.psh)
  56.         fprintf(fp," %s",Tcpflags[3]);
  57.     if(seg.flags.rst)
  58.         fprintf(fp," %s",Tcpflags[2]);
  59.     if(seg.flags.syn)
  60.         fprintf(fp," %s",Tcpflags[1]);
  61.     if(seg.flags.fin)
  62.         fprintf(fp," %s",Tcpflags[0]);
  63.  
  64.     fprintf(fp," Wnd %u",seg.wnd);
  65.     if(seg.flags.urg)
  66.         fprintf(fp," UP x%x",seg.up);
  67.     /* Print options, if any */
  68.     if(seg.mss != 0)
  69.         fprintf(fp," MSS %u",seg.mss);
  70.     if((dlen = len_p(*bpp)) != 0)
  71.         fprintf(fp," Data %u",dlen);
  72.     if(check && csum != 0)
  73.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  74.     putc('\n',fp);
  75. }
  76.  
  77.