ngtcp2_net.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2022 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_NET_H
  26. #define NGTCP2_NET_H
  27. /* This header file is explicitly allowed to be shared with
  28. ngtcp2_crypto library. */
  29. #ifdef HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif /* defined(HAVE_CONFIG_H) */
  32. #ifdef HAVE_ARPA_INET_H
  33. # include <arpa/inet.h>
  34. #endif /* defined(HAVE_ARPA_INET_H) */
  35. #ifdef HAVE_NETINET_IN_H
  36. # include <netinet/in.h>
  37. #endif /* defined(HAVE_NETINET_IN_H) */
  38. #ifdef HAVE_BYTESWAP_H
  39. # include <byteswap.h>
  40. #endif /* defined(HAVE_BYTESWAP_H) */
  41. #ifdef HAVE_ENDIAN_H
  42. # include <endian.h>
  43. #endif /* defined(HAVE_ENDIAN_H) */
  44. #ifdef HAVE_SYS_ENDIAN_H
  45. # include <sys/endian.h>
  46. #endif /* defined(HAVE_SYS_ENDIAN_H) */
  47. #ifdef __APPLE__
  48. # include <libkern/OSByteOrder.h>
  49. #endif /* defined(__APPLE__) */
  50. #include <ngtcp2/ngtcp2.h>
  51. #if HAVE_DECL_BE64TOH
  52. # define ngtcp2_ntohl64(N) be64toh(N)
  53. # define ngtcp2_htonl64(N) htobe64(N)
  54. #else /* !HAVE_DECL_BE64TOH */
  55. # ifdef WORDS_BIGENDIAN
  56. # define ngtcp2_ntohl64(N) (N)
  57. # define ngtcp2_htonl64(N) (N)
  58. # else /* !defined(WORDS_BIGENDIAN) */
  59. # if HAVE_DECL_BSWAP_64
  60. # define ngtcp2_bswap64 bswap_64
  61. # elif defined(WIN32)
  62. # define ngtcp2_bswap64 _byteswap_uint64
  63. # elif defined(__APPLE__)
  64. # define ngtcp2_bswap64 OSSwapInt64
  65. # else /* !(HAVE_DECL_BSWAP_64 || defined(WIN32) || defined(__APPLE__)) */
  66. # define ngtcp2_bswap64(N) \
  67. ((uint64_t)(ngtcp2_ntohl((uint32_t)(N))) << 32 | \
  68. ngtcp2_ntohl((uint32_t)((N) >> 32)))
  69. # endif /* !(HAVE_DECL_BSWAP_64 || defined(WIN32) || defined(__APPLE__)) */
  70. # define ngtcp2_ntohl64(N) ngtcp2_bswap64(N)
  71. # define ngtcp2_htonl64(N) ngtcp2_bswap64(N)
  72. # endif /* !defined(WORDS_BIGENDIAN) */
  73. #endif /* !HAVE_DECL_BE64TOH */
  74. #ifdef WIN32
  75. /* Windows requires ws2_32 library for ntonl family functions. We
  76. define inline functions for those function so that we don't have
  77. dependency on that lib. */
  78. # ifdef _MSC_VER
  79. # define STIN static __inline
  80. # else /* !defined(_MSC_VER) */
  81. # define STIN static inline
  82. # endif /* !defined(_MSC_VER) */
  83. STIN uint32_t ngtcp2_htonl(uint32_t hostlong) {
  84. uint32_t res;
  85. unsigned char *p = (unsigned char *)&res;
  86. *p++ = (unsigned char)(hostlong >> 24);
  87. *p++ = (hostlong >> 16) & 0xffu;
  88. *p++ = (hostlong >> 8) & 0xffu;
  89. *p = hostlong & 0xffu;
  90. return res;
  91. }
  92. STIN uint16_t ngtcp2_htons(uint16_t hostshort) {
  93. uint16_t res;
  94. unsigned char *p = (unsigned char *)&res;
  95. *p++ = (unsigned char)(hostshort >> 8);
  96. *p = hostshort & 0xffu;
  97. return res;
  98. }
  99. STIN uint32_t ngtcp2_ntohl(uint32_t netlong) {
  100. uint32_t res;
  101. unsigned char *p = (unsigned char *)&netlong;
  102. res = (uint32_t)(*p++ << 24);
  103. res += (uint32_t)(*p++ << 16);
  104. res += (uint32_t)(*p++ << 8);
  105. res += *p;
  106. return res;
  107. }
  108. STIN uint16_t ngtcp2_ntohs(uint16_t netshort) {
  109. uint16_t res;
  110. unsigned char *p = (unsigned char *)&netshort;
  111. res = (uint16_t)(*p++ << 8);
  112. res += *p;
  113. return res;
  114. }
  115. #else /* !defined(WIN32) */
  116. # define ngtcp2_htonl htonl
  117. # define ngtcp2_htons htons
  118. # define ngtcp2_ntohl ntohl
  119. # define ngtcp2_ntohs ntohs
  120. #endif /* !defined(WIN32) */
  121. #endif /* !defined(NGTCP2_NET_H) */