| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#ifndef NGTCP2_NET_H |
| 26 |
#define NGTCP2_NET_H |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#ifdef HAVE_CONFIG_H |
| 32 |
# include <config.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifdef HAVE_ARPA_INET_H |
| 36 |
# include <arpa/inet.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef HAVE_NETINET_IN_H |
| 40 |
# include <netinet/in.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef HAVE_BYTESWAP_H |
| 44 |
# include <byteswap.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#ifdef HAVE_ENDIAN_H |
| 48 |
# include <endian.h> |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifdef HAVE_SYS_ENDIAN_H |
| 52 |
# include <sys/endian.h> |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifdef __APPLE__ |
| 56 |
# include <libkern/OSByteOrder.h> |
| 57 |
#endif |
| 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 |
| 65 |
# ifdef WORDS_BIGENDIAN |
| 66 |
# define ngtcp2_ntohl64(N) (N) |
| 67 |
# define ngtcp2_htonl64(N) (N) |
| 68 |
# else |
| 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 |
| 76 |
# define ngtcp2_bswap64(N) \ |
| 77 |
((uint64_t)(ngtcp2_ntohl((uint32_t)(N))) << 32 | \ |
| 78 |
ngtcp2_ntohl((uint32_t)((N) >> 32))) |
| 79 |
# endif |
| 80 |
# define ngtcp2_ntohl64(N) ngtcp2_bswap64(N) |
| 81 |
# define ngtcp2_htonl64(N) ngtcp2_bswap64(N) |
| 82 |
# endif |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef WIN32 |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
# ifdef _MSC_VER |
| 91 |
# define STIN static __inline |
| 92 |
# else |
| 93 |
# define STIN static inline |
| 94 |
# endif |
| 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 |
| 133 |
|
| 134 |
# define ngtcp2_htonl htonl |
| 135 |
# define ngtcp2_htons htons |
| 136 |
# define ngtcp2_ntohl ntohl |
| 137 |
# define ngtcp2_ntohs ntohs |
| 138 |
|
| 139 |
#endif |
| 140 |
|
| 141 |
#endif |