nghttp2_net.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  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 NGHTTP2_NET_H
  26. #define NGHTTP2_NET_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #ifdef HAVE_ARPA_INET_H
  31. # include <arpa/inet.h>
  32. #endif /* HAVE_ARPA_INET_H */
  33. #ifdef HAVE_NETINET_IN_H
  34. # include <netinet/in.h>
  35. #endif /* HAVE_NETINET_IN_H */
  36. #include <nghttp2/nghttp2.h>
  37. #if defined(WIN32)
  38. /* Windows requires ws2_32 library for ntonl family functions. We
  39. define inline functions for those function so that we don't have
  40. dependency on that lib. */
  41. # ifdef _MSC_VER
  42. # define STIN static __inline
  43. # else
  44. # define STIN static inline
  45. # endif
  46. STIN uint32_t htonl(uint32_t hostlong) {
  47. uint32_t res;
  48. unsigned char *p = (unsigned char *)&res;
  49. *p++ = (unsigned char)(hostlong >> 24);
  50. *p++ = (hostlong >> 16) & 0xffu;
  51. *p++ = (hostlong >> 8) & 0xffu;
  52. *p = hostlong & 0xffu;
  53. return res;
  54. }
  55. STIN uint16_t htons(uint16_t hostshort) {
  56. uint16_t res;
  57. unsigned char *p = (unsigned char *)&res;
  58. *p++ = (unsigned char)(hostshort >> 8);
  59. *p = hostshort & 0xffu;
  60. return res;
  61. }
  62. STIN uint32_t ntohl(uint32_t netlong) {
  63. uint32_t res;
  64. unsigned char *p = (unsigned char *)&netlong;
  65. res = (uint32_t)(*p++ << 24);
  66. res += (uint32_t)(*p++ << 16);
  67. res += (uint32_t)(*p++ << 8);
  68. res += *p;
  69. return res;
  70. }
  71. STIN uint16_t ntohs(uint16_t netshort) {
  72. uint16_t res;
  73. unsigned char *p = (unsigned char *)&netshort;
  74. res = (uint16_t)(*p++ << 8);
  75. res += *p;
  76. return res;
  77. }
  78. #endif /* WIN32 */
  79. #endif /* NGHTTP2_NET_H */