home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / ICMPHDR.C < prev    next >
C/C++ Source or Header  |  1992-04-08  |  2KB  |  101 lines

  1. /* ICMP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "internet.h"
  7. #include "ip.h"
  8. #include "icmp.h"
  9.  
  10. /* Generate ICMP header in network byte order, link data, compute checksum */
  11. struct mbuf *
  12. htonicmp(icmp,bp)
  13. struct icmp *icmp;
  14. struct mbuf *bp;
  15. {
  16.     register char *cp;
  17.     int16 checksum;
  18.  
  19.     bp = pushdown(bp,ICMPLEN);
  20.     cp = bp->data;
  21.  
  22.     *cp++ = icmp->type;
  23.     *cp++ = icmp->code;
  24.     cp = put16(cp,0);        /* Clear checksum */
  25.     switch(icmp->type){
  26.     case ICMP_DEST_UNREACH:
  27.         if(icmp->code == ICMP_FRAG_NEEDED){
  28.             /* Deering/Mogul max MTU indication */
  29.             cp = put16(cp,0);
  30.             cp = put16(cp,icmp->args.mtu);
  31.         } else
  32.             cp = put32(cp,0L);
  33.         break;
  34.     case ICMP_PARAM_PROB:
  35.         *cp++ = icmp->args.pointer;
  36.         *cp++ = 0;
  37.         cp = put16(cp,0);
  38.         break;
  39.     case ICMP_REDIRECT:
  40.         cp = put32(cp,icmp->args.address);
  41.         break;
  42.     case ICMP_ECHO:
  43.     case ICMP_ECHO_REPLY:
  44.     case ICMP_TIMESTAMP:
  45.     case ICMP_TIME_REPLY:
  46.     case ICMP_INFO_RQST:
  47.     case ICMP_INFO_REPLY:
  48.         cp = put16(cp,icmp->args.echo.id);
  49.         cp = put16(cp,icmp->args.echo.seq);
  50.         break;
  51.     default:
  52.         cp = put32(cp,0L);
  53.         break;
  54.     }
  55.     /* Compute checksum, and stash result */
  56.     checksum = cksum(NULLHEADER,bp,len_p(bp));
  57.     cp = &bp->data[2];
  58.     cp = put16(cp,checksum);
  59.  
  60.     return bp;
  61. }
  62. /* Pull off ICMP header */
  63. int
  64. ntohicmp(icmp,bpp)
  65. struct icmp *icmp;
  66. struct mbuf **bpp;
  67. {
  68.     char icmpbuf[8];
  69.  
  70.     if(icmp == (struct icmp *)NULL)
  71.         return -1;
  72.     if(pullup(bpp,icmpbuf,8) != 8)
  73.         return -1;
  74.     icmp->type = icmpbuf[0];
  75.     icmp->code = icmpbuf[1];
  76.     switch(icmp->type){
  77.     case ICMP_DEST_UNREACH:
  78.         /* Retrieve Deering/Mogul MTU value */
  79.         if(icmp->code == ICMP_FRAG_NEEDED)
  80.             icmp->args.mtu = get16(&icmpbuf[6]);
  81.         break;
  82.     case ICMP_PARAM_PROB:
  83.         icmp->args.pointer = icmpbuf[4];
  84.         break;
  85.     case ICMP_REDIRECT:
  86.         icmp->args.address = get32(&icmpbuf[4]);
  87.         break;
  88.     case ICMP_ECHO:
  89.     case ICMP_ECHO_REPLY:
  90.     case ICMP_TIMESTAMP:
  91.     case ICMP_TIME_REPLY:
  92.     case ICMP_INFO_RQST:
  93.     case ICMP_INFO_REPLY:
  94.         icmp->args.echo.id = get16(&icmpbuf[4]);
  95.         icmp->args.echo.seq = get16(&icmpbuf[6]);
  96.         break;
  97.     }
  98.     return 0;
  99. }
  100.  
  101.