home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 3 / hamradioversion3.0examsandprograms1992.iso / misc / 9q920411 / ipdump.c < prev    next >
C/C++ Source or Header  |  1991-04-20  |  2KB  |  90 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(ip.flags.congest){
  59.         fprintf(fp," CE");
  60.     }
  61.     if(csum != 0)
  62.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  63.  
  64.     if(ip.offset != 0){
  65.         putc('\n',fp);
  66.         return;
  67.     }
  68.     switch(uchar(ip.protocol)){
  69.     case IP_PTCL:
  70.         fprintf(fp," prot IP\n");
  71.         ip_dump(fp,bpp,check);
  72.         break;
  73.     case TCP_PTCL:
  74.         fprintf(fp," prot TCP\n");
  75.         tcp_dump(fp,bpp,ip.source,ip.dest,check);
  76.         break;
  77.     case UDP_PTCL:
  78.         fprintf(fp," prot UDP\n");
  79.         udp_dump(fp,bpp,ip.source,ip.dest,check);
  80.         break;
  81.     case ICMP_PTCL:
  82.         fprintf(fp," prot ICMP\n");
  83.         icmp_dump(fp,bpp,ip.source,ip.dest,check);
  84.         break;
  85.     default:
  86.         fprintf(fp," prot %u\n",uchar(ip.protocol));
  87.         break;
  88.     }
  89. }
  90.