home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / UDPHDR.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  2KB  |  71 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,data,ph)
  13. struct udp *udp;
  14. struct mbuf *data;
  15. struct pseudo_header *ph;
  16. {
  17.     struct mbuf *bp;
  18.     register char *cp;
  19.     int16 checksum;
  20.   
  21.     /* Allocate UDP protocol header and fill it in */
  22.     if((bp = pushdown(data,UDPHDR)) == NULLBUF)
  23.         return NULLBUF;
  24.   
  25.     cp = bp->data;
  26.     cp = put16(cp,udp->source); /* Source port */
  27.     cp = put16(cp,udp->dest);   /* Destination port */
  28.     cp = put16(cp,udp->length); /* Length */
  29.     *cp++ = 0;          /* Clear checksum */
  30.     *cp-- = 0;
  31.   
  32.     /* All zeros and all ones is equivalent in one's complement arithmetic;
  33.      * the spec requires us to change zeros into ones to distinguish an
  34.      * all-zero checksum from no checksum at all
  35.      */
  36.     if((checksum = cksum(ph,bp,ph->length)) == 0)
  37.         checksum = 0xffffU;
  38.     put16(cp,checksum);
  39.     return bp;
  40. }
  41. /* Convert UDP header in mbuf to internal structure */
  42. int
  43. ntohudp(udp,bpp)
  44. struct udp *udp;
  45. struct mbuf **bpp;
  46. {
  47.     char udpbuf[UDPHDR];
  48.   
  49.     if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR)
  50.         return -1;
  51.     udp->source = get16(&udpbuf[0]);
  52.     udp->dest = get16(&udpbuf[2]);
  53.     udp->length = get16(&udpbuf[4]);
  54.     udp->checksum = get16(&udpbuf[6]);
  55.     return 0;
  56. }
  57. /* Extract UDP checksum value from a network-format header without
  58.  * disturbing the header
  59.  */
  60. int16
  61. udpcksum(bp)
  62. struct mbuf *bp;
  63. {
  64.     struct mbuf *dup;
  65.   
  66.     if(dup_p(&dup,bp,6,2) != 2)
  67.         return 0;
  68.     return pull16(&dup);
  69. }
  70.   
  71.