• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 /* This header file is explicitly allowed to be shared with
29    ngtcp2_crypto library. */
30 
31 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif /* HAVE_CONFIG_H */
34 
35 #ifdef HAVE_ARPA_INET_H
36 #  include <arpa/inet.h>
37 #endif /* HAVE_ARPA_INET_H */
38 
39 #ifdef HAVE_NETINET_IN_H
40 #  include <netinet/in.h>
41 #endif /* HAVE_NETINET_IN_H */
42 
43 #ifdef HAVE_BYTESWAP_H
44 #  include <byteswap.h>
45 #endif /* HAVE_BYTESWAP_H */
46 
47 #ifdef HAVE_ENDIAN_H
48 #  include <endian.h>
49 #endif /* HAVE_ENDIAN_H */
50 
51 #ifdef HAVE_SYS_ENDIAN_H
52 #  include <sys/endian.h>
53 #endif /* HAVE_SYS_ENDIAN_H */
54 
55 #include <ngtcp2/ngtcp2.h>
56 
57 #if defined(HAVE_BSWAP_64) ||                                                  \
58     (defined(HAVE_DECL_BSWAP_64) && HAVE_DECL_BSWAP_64 > 0)
59 #  define ngtcp2_bswap64 bswap_64
60 #else /* !HAVE_BSWAP_64 */
61 #  define ngtcp2_bswap64(N)                                                    \
62     ((uint64_t)(ngtcp2_ntohl((uint32_t)(N))) << 32 |                           \
63      ngtcp2_ntohl((uint32_t)((N) >> 32)))
64 #endif /* !HAVE_BSWAP_64 */
65 
66 #if defined(HAVE_BE64TOH) ||                                                   \
67     (defined(HAVE_DECL_BE64TOH) && HAVE_DECL_BE64TOH > 0)
68 #  define ngtcp2_ntohl64(N) be64toh(N)
69 #  define ngtcp2_htonl64(N) htobe64(N)
70 #else /* !HAVE_BE64TOH */
71 #  if defined(WORDS_BIGENDIAN)
72 #    define ngtcp2_ntohl64(N) (N)
73 #    define ngtcp2_htonl64(N) (N)
74 #  else /* !WORDS_BIGENDIAN */
75 #    define ngtcp2_ntohl64(N) ngtcp2_bswap64(N)
76 #    define ngtcp2_htonl64(N) ngtcp2_bswap64(N)
77 #  endif /* !WORDS_BIGENDIAN */
78 #endif   /* !HAVE_BE64TOH */
79 
80 #if defined(WIN32)
81 /* Windows requires ws2_32 library for ntonl family functions.  We
82    define inline functions for those function so that we don't have
83    dependeny on that lib. */
84 
85 #  ifdef _MSC_VER
86 #    define STIN static __inline
87 #  else
88 #    define STIN static inline
89 #  endif
90 
ngtcp2_htonl(uint32_t hostlong)91 STIN uint32_t ngtcp2_htonl(uint32_t hostlong) {
92   uint32_t res;
93   unsigned char *p = (unsigned char *)&res;
94   *p++ = (unsigned char)(hostlong >> 24);
95   *p++ = (hostlong >> 16) & 0xffu;
96   *p++ = (hostlong >> 8) & 0xffu;
97   *p = hostlong & 0xffu;
98   return res;
99 }
100 
ngtcp2_htons(uint16_t hostshort)101 STIN uint16_t ngtcp2_htons(uint16_t hostshort) {
102   uint16_t res;
103   unsigned char *p = (unsigned char *)&res;
104   *p++ = (unsigned char)(hostshort >> 8);
105   *p = hostshort & 0xffu;
106   return res;
107 }
108 
ngtcp2_ntohl(uint32_t netlong)109 STIN uint32_t ngtcp2_ntohl(uint32_t netlong) {
110   uint32_t res;
111   unsigned char *p = (unsigned char *)&netlong;
112   res = *p++ << 24;
113   res += *p++ << 16;
114   res += *p++ << 8;
115   res += *p;
116   return res;
117 }
118 
ngtcp2_ntohs(uint16_t netshort)119 STIN uint16_t ngtcp2_ntohs(uint16_t netshort) {
120   uint16_t res;
121   unsigned char *p = (unsigned char *)&netshort;
122   res = *p++ << 8;
123   res += *p;
124   return res;
125 }
126 
127 #else /* !WIN32 */
128 
129 #  define ngtcp2_htonl htonl
130 #  define ngtcp2_htons htons
131 #  define ngtcp2_ntohl ntohl
132 #  define ngtcp2_ntohs ntohs
133 
134 #endif /* !WIN32 */
135 
136 #endif /* NGTCP2_NET_H */
137