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

  1. /* IP header tracing routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  /* Mods by PA0GRI */
  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. #ifdef MONITOR
  28.     if (!Trace_compact_header)
  29. #endif
  30.         fprintf(fp,"IP: ");
  31.     /* Sneak peek at IP header and find length */
  32.     ip_len = ((*bpp)->data[0] & 0xf) << 2;
  33.     if(ip_len < IPLEN){
  34.         fprintf(fp,"bad header\n");
  35.         return;
  36.     }
  37.     if(check)
  38.         csum = cksum(NULLHEADER,*bpp,ip_len);
  39.     else
  40.         csum = 0;
  41.   
  42.     ntohip(&ip,bpp);    /* Can't fail, we've already checked ihl */
  43.   
  44.     /* Trim data segment if necessary. */
  45.     length = ip.length - ip_len;    /* Length of data portion */
  46.     trim_mbuf(bpp,length);
  47. #ifdef MONITOR
  48.     if (!Trace_compact_header)
  49. #endif
  50.         fprintf(fp,"len %u",ip.length);
  51.     fprintf(fp," %s",inet_ntoa(ip.source));
  52. #ifdef MONITOR
  53.     if (Trace_compact_header)
  54.         fprintf(fp, "->%s", inet_ntoa(ip.dest));
  55.     else
  56. #endif
  57.         fprintf(fp,"->%s ihl %u ttl %u",
  58.         inet_ntoa(ip.dest),ip_len,uchar(ip.ttl));
  59. #ifdef MONITOR
  60.     if (!Trace_compact_header)
  61. #endif
  62.         if(ip.tos != 0)
  63.             fprintf(fp," tos %u",uchar(ip.tos));
  64.     if(ip.offset != 0 || ip.flags.mf)
  65.         fprintf(fp," id %u offs %u",ip.id,ip.offset);
  66.     if(ip.flags.congest)
  67.         fprintf(fp," CE");
  68.     if(ip.flags.df)
  69.         fprintf(fp," DF");
  70.     if(ip.flags.mf){
  71.         fprintf(fp," MF");
  72.         check = 0;  /* Bypass host-level checksum verify */
  73.     }
  74.     if(csum != 0)
  75.         fprintf(fp," CHECKSUM ERROR (%u)",csum);
  76.   
  77.     if(ip.offset != 0){
  78.         fprintf(fp,"\n");
  79.         return;
  80.     }
  81.     switch(uchar(ip.protocol)){
  82.         case TCP_PTCL:
  83. #ifdef MONITOR
  84.             if (Trace_compact_header)
  85.                 fprintf(fp, " TCP ");
  86.             else
  87. #endif
  88.                 fprintf(fp," prot TCP\n");
  89.             tcp_dump(fp,bpp,ip.source,ip.dest,check);
  90.             break;
  91.         case UDP_PTCL:
  92. #ifdef MONITOR
  93.             if (Trace_compact_header)
  94.                 fprintf(fp, " UDP ");
  95.             else
  96. #endif
  97.                 fprintf(fp," prot UDP\n");
  98.             udp_dump(fp,bpp,ip.source,ip.dest,check);
  99.             break;
  100.         case ICMP_PTCL:
  101. #ifdef MONITOR
  102.             if (Trace_compact_header)
  103.                 fprintf(fp, " ICMP ");
  104.             else
  105. #endif
  106.                 fprintf(fp," prot ICMP\n");
  107.             icmp_dump(fp,bpp,ip.source,ip.dest,check);
  108.             break;
  109.         case IP_PTCL:
  110. #ifdef MONITOR
  111.             if (Trace_compact_header)
  112.                 fprintf(fp, " <= ");
  113.             else
  114. #endif
  115.                 fprintf(fp," prot IP\n");
  116.             ip_dump(fp,bpp,check);
  117.             break;
  118. #ifdef AX25
  119.         case AX25_PTCL:
  120. #ifdef MONITOR
  121.             if (Trace_compact_header)
  122.                 fprintf(fp, " <= ");
  123.             else
  124. #endif
  125.                 fprintf(fp," prot AX25\n");
  126.             ax25_dump(fp,bpp,check);
  127.             break;
  128. #endif
  129. #ifdef  RSPF
  130.         case RSPF_PTCL:
  131. #ifdef MONITOR
  132.             if (Trace_compact_header)
  133.                 fprintf(fp, " RSPF\n");
  134.             else
  135. #endif
  136.                 fprintf(fp," prot RSPF\n");
  137.             rspf_dump(fp,bpp,ip.source,ip.dest,check);
  138.             break;
  139. #endif
  140.         default:
  141.             fprintf(fp," prot %u\n",uchar(ip.protocol));
  142.             break;
  143.     }
  144. }
  145.