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

  1. /* IP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include <stdio.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "internet.h"
  8. #include "iface.h"
  9. #include "ip.h"
  10. #include "trace.h"
  11. #include "netuser.h"
  12.  
  13. void
  14. ip_dump(fp,bpp,check)
  15. FILE *fp;
  16. struct mbuf **bpp;
  17. int check;
  18. {
  19.     struct ip ip;
  20.     int16 ip_len;
  21.     int16 length;
  22.     int16 csum;
  23.  
  24.     if(bpp == NULLBUFP || *bpp == NULLBUF)
  25.         return;    
  26.  
  27.     fprintf(fp,"IP:");
  28.     /* Sneak peek at IP header and find length */
  29.     ip_len = ((*bpp)->data[0] & 0xf) << 2;
  30.     if(ip_len < IPLEN){
  31.         fprintf(fp," bad header\n");
  32.         return;
  33.     }
  34.     if(check)
  35.         csum = cksum(NULLHEADER,*bpp,ip_len);
  36.     else
  37.         csum = 0;
  38.  
  39.     ntohip(&ip,bpp);    /* Can't fail, we've already checked ihl */
  40.  
  41.     /* Trim data segment if necessary. */
  42.     length = ip.length - ip_len;    /* Length of data portion */
  43.     trim_mbuf(bpp,length);    
  44.     fprintf(fp," len %u",ip.length);
  45.     fprintf(fp," %s",inet_ntoa(ip.source));
  46.     fprintf(fp,"->%s ihl %u ttl %u",
  47.         inet_ntoa(ip.dest),ip_len,uchar(ip.ttl));
  48.     if(ip.tos != 0)
  49.         fprintf(fp," tos %u",uchar(ip.tos));
  50.     if(ip.offset != 0 || ip.flags.mf)
  51.         fprintf(fp," id %u offs %u",ip.id,ip.offset);
  52.     if(ip.flags.df)
  53.         fprintf(fp," DF");
  54.     if(ip.flags.mf){
  55.         fprintf(fp," MF");
  56.         check = 0;    /* Bypass host-level checksum verify */
  57.     }
  58.     if(csum != 0)
  59.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  60.  
  61.     if(ip.offset != 0){
  62.         putc('\n',fp);
  63.         return;
  64.     }
  65.     switch(uchar(ip.protocol)){
  66.     case TCP_PTCL:
  67.         fprintf(fp," prot TCP\n");
  68.         tcp_dump(fp,bpp,ip.source,ip.dest,check);
  69.         break;
  70.     case UDP_PTCL:
  71.         fprintf(fp," prot UDP\n");
  72.         udp_dump(fp,bpp,ip.source,ip.dest,check);
  73.         break;
  74.     case ICMP_PTCL:
  75.         fprintf(fp," prot ICMP\n");
  76.         icmp_dump(fp,bpp,ip.source,ip.dest,check);
  77.         break;
  78.     default:
  79.         fprintf(fp," prot %u\n",uchar(ip.protocol));
  80.         break;
  81.     }
  82. }
  83.