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

  1. /* UDP header conversion routines
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "ip.h"
  7. #include "internet.h"
  8. #include "udp.h"
  9.  
  10. /* Convert UDP header in internal format to an mbuf in external format */
  11. struct mbuf *
  12. htonudp(udp,bp,ph)
  13. struct udp *udp;
  14. struct mbuf *bp;
  15. struct pseudo_header *ph;
  16. {
  17.     register char *cp;
  18.     int16 checksum;
  19.  
  20.     /* Allocate UDP protocol header and fill it in */
  21.     bp = pushdown(bp,UDPHDR);
  22.     cp = bp->data;
  23.     cp = put16(cp,udp->source);    /* Source port */
  24.     cp = put16(cp,udp->dest);    /* Destination port */
  25.     cp = put16(cp,udp->length);    /* Length */
  26.     *cp++ = 0;            /* Clear checksum */
  27.     *cp-- = 0;
  28.  
  29.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  30.      * the spec requires us to change zeros into ones to distinguish an
  31.       * all-zero checksum from no checksum at all
  32.      */
  33.     if((checksum = cksum(ph,bp,ph->length)) == 0)
  34.         checksum = 0xffffffff;
  35.     put16(cp,checksum);
  36.     return bp;
  37. }
  38. /* Convert UDP header in mbuf to internal structure */
  39. int
  40. ntohudp(udp,bpp)
  41. struct udp *udp;
  42. struct mbuf **bpp;
  43. {
  44.     char udpbuf[UDPHDR];
  45.  
  46.     if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR)
  47.         return -1;
  48.     udp->source = get16(&udpbuf[0]);
  49.     udp->dest = get16(&udpbuf[2]);
  50.     udp->length = get16(&udpbuf[4]);
  51.     udp->checksum = get16(&udpbuf[6]);
  52.     return 0;
  53. }
  54. /* Extract UDP checksum value from a network-format header without
  55.  * disturbing the header
  56.  */
  57. int16
  58. udpcksum(bp)
  59. struct mbuf *bp;
  60. {
  61.     struct mbuf *dup;
  62.  
  63.     if(dup_p(&dup,bp,6,2) != 2)
  64.         return 0;
  65.     return pull16(&dup);
  66. }
  67.  
  68.