udphdr.c
Upload User: hepax88
Upload Date: 2007-01-03
Package Size: 1101k
Code Size: 2k
Category:

TCP/IP Stack

Development Platform:

Visual C++

  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. /* Convert UDP header in internal format to an mbuf in external format */
  10. void
  11. htonudp(
  12. struct udp *udp,
  13. struct mbuf **bpp,
  14. struct pseudo_header *ph
  15. ){
  16. register uint8 *cp;
  17. uint16 checksum;
  18. /* Allocate UDP protocol header and fill it in */
  19. pushdown(bpp,NULL,UDPHDR);
  20. cp = (*bpp)->data;
  21. cp = put16(cp,udp->source); /* Source port */
  22. cp = put16(cp,udp->dest); /* Destination port */
  23. cp = put16(cp,udp->length); /* Length */
  24. *cp++ = 0; /* Clear checksum */
  25. *cp-- = 0;
  26. /* All zeros and all ones is equivalent in one's complement arithmetic;
  27.  * the spec requires us to change zeros into ones to distinguish an
  28.    * all-zero checksum from no checksum at all
  29.  */
  30. if((checksum = cksum(ph,*bpp,ph->length)) == 0)
  31. checksum = 0xffff;
  32. put16(cp,checksum);
  33. }
  34. /* Convert UDP header in mbuf to internal structure */
  35. int
  36. ntohudp(
  37. struct udp *udp,
  38. struct mbuf **bpp
  39. ){
  40. uint8 udpbuf[UDPHDR];
  41. if(pullup(bpp,udpbuf,UDPHDR) != UDPHDR)
  42. return -1;
  43. udp->source = get16(&udpbuf[0]);
  44. udp->dest = get16(&udpbuf[2]);
  45. udp->length = get16(&udpbuf[4]);
  46. udp->checksum = get16(&udpbuf[6]);
  47. return 0;
  48. }
  49. /* Extract UDP checksum value from a network-format header without
  50.  * disturbing the header
  51.  */
  52. uint16
  53. udpcksum(
  54. struct mbuf *bp
  55. ){
  56. struct mbuf *dup;
  57. if(dup_p(&dup,bp,6,2) != 2)
  58. return 0;
  59. return pull16(&dup);
  60. }