home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / UDPHDR.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  2KB  |  77 lines

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