Debian Codegraph @

contrib/libngtcp2/ngtcp2/lib/ngtcp2_net.h

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