def.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @file
  3. * Common functions used throughout the stack.
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Simon Goldschmidt
  35. *
  36. */
  37. #include "lwip/opt.h"
  38. #include "lwip/def.h"
  39. /**
  40. * These are reference implementations of the byte swapping functions.
  41. * Again with the aim of being simple, correct and fully portable.
  42. * Byte swapping is the second thing you would want to optimize. You will
  43. * need to port it to your architecture and in your cc.h:
  44. *
  45. * #define LWIP_PLATFORM_BYTESWAP 1
  46. * #define LWIP_PLATFORM_HTONS(x) <your_htons>
  47. * #define LWIP_PLATFORM_HTONL(x) <your_htonl>
  48. *
  49. * Note ntohs() and ntohl() are merely references to the htonx counterparts.
  50. */
  51. #if (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN)
  52. /**
  53. * Convert an u16_t from host- to network byte order.
  54. *
  55. * @param n u16_t in host byte order
  56. * @return n in network byte order
  57. */
  58. u16_t
  59. lwip_htons(u16_t n)
  60. {
  61. return ((n & 0xff) << 8) | ((n & 0xff00) >> 8);
  62. }
  63. /**
  64. * Convert an u16_t from network- to host byte order.
  65. *
  66. * @param n u16_t in network byte order
  67. * @return n in host byte order
  68. */
  69. u16_t
  70. lwip_ntohs(u16_t n)
  71. {
  72. return lwip_htons(n);
  73. }
  74. /**
  75. * Convert an u32_t from host- to network byte order.
  76. *
  77. * @param n u32_t in host byte order
  78. * @return n in network byte order
  79. */
  80. u32_t
  81. lwip_htonl(u32_t n)
  82. {
  83. return ((n & 0xff) << 24) |
  84. ((n & 0xff00) << 8) |
  85. ((n & 0xff0000UL) >> 8) |
  86. ((n & 0xff000000UL) >> 24);
  87. }
  88. /**
  89. * Convert an u32_t from network- to host byte order.
  90. *
  91. * @param n u32_t in network byte order
  92. * @return n in host byte order
  93. */
  94. u32_t
  95. lwip_ntohl(u32_t n)
  96. {
  97. return lwip_htonl(n);
  98. }
  99. #endif /* (LWIP_PLATFORM_BYTESWAP == 0) && (BYTE_ORDER == LITTLE_ENDIAN) */