InetAddress.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_INETADDRESS_HPP
  14. #define ZT_INETADDRESS_HPP
  15. #include <cstdlib>
  16. #include <cstring>
  17. #include <cstdint>
  18. #include "Constants.hpp"
  19. #include "Utils.hpp"
  20. #include "MAC.hpp"
  21. #include "TriviallyCopyable.hpp"
  22. namespace ZeroTier {
  23. #define ZT_INETADDRESS_MARSHAL_SIZE_MAX 19
  24. #define ZT_INETADDRESS_STRING_SIZE_MAX 64
  25. /**
  26. * C++ class that overlaps in size with sockaddr_storage and adds convenience methods
  27. *
  28. * This is basically a "mixin" for sockaddr_storage. It adds methods and
  29. * operators, but does not modify the structure. This can be cast to/from
  30. * sockaddr_storage and used interchangeably. DO NOT change this by e.g.
  31. * adding non-static fields, since much code depends on this identity.
  32. */
  33. struct InetAddress : public TriviallyCopyable
  34. {
  35. private:
  36. // Internal function to copy any sockaddr_X structure to this one even if it's smaller and unpadded.
  37. template<typename SA>
  38. ZT_ALWAYS_INLINE void copySockaddrToThis(const SA *sa) noexcept
  39. {
  40. memcpy(reinterpret_cast<void *>(this),sa,sizeof(SA));
  41. if (sizeof(SA) < sizeof(InetAddress))
  42. memset(reinterpret_cast<uint8_t *>(this) + sizeof(SA),0,sizeof(InetAddress) - sizeof(SA));
  43. }
  44. public:
  45. /**
  46. * Loopback IPv4 address (no port)
  47. */
  48. static const InetAddress LO4;
  49. /**
  50. * Loopback IPV6 address (no port)
  51. */
  52. static const InetAddress LO6;
  53. /**
  54. * Null address
  55. */
  56. static const InetAddress NIL;
  57. /**
  58. * IP address scope
  59. *
  60. * Note that these values are in ascending order of path preference and
  61. * MUST remain that way or Path must be changed to reflect. Also be sure
  62. * to change ZT_INETADDRESS_MAX_SCOPE if the max changes.
  63. */
  64. enum IpScope
  65. {
  66. IP_SCOPE_NONE = 0, // NULL or not an IP address
  67. IP_SCOPE_MULTICAST = 1, // 224.0.0.0 and other V4/V6 multicast IPs
  68. IP_SCOPE_LOOPBACK = 2, // 127.0.0.1, ::1, etc.
  69. IP_SCOPE_PSEUDOPRIVATE = 3, // 28.x.x.x, etc. -- unofficially unrouted IPv4 blocks often "bogarted"
  70. IP_SCOPE_GLOBAL = 4, // globally routable IP address (all others)
  71. IP_SCOPE_LINK_LOCAL = 5, // 169.254.x.x, IPv6 LL
  72. IP_SCOPE_SHARED = 6, // currently unused, formerly used for carrier-grade NAT ranges
  73. IP_SCOPE_PRIVATE = 7 // 10.x.x.x, 192.168.x.x, etc.
  74. };
  75. // Hasher for unordered sets and maps in C++11
  76. struct Hasher { ZT_ALWAYS_INLINE std::size_t operator()(const InetAddress &a) const noexcept { return (std::size_t)a.hashCode(); } };
  77. ZT_ALWAYS_INLINE InetAddress() noexcept { memoryZero(this); }
  78. ZT_ALWAYS_INLINE InetAddress(const InetAddress &a) noexcept { memoryCopy(this,&a); }
  79. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_storage &ss) noexcept { *this = ss; }
  80. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_storage *ss) noexcept { *this = ss; }
  81. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr &sa) noexcept { *this = sa; }
  82. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr *sa) noexcept { *this = sa; }
  83. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_in &sa) noexcept { *this = sa; }
  84. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_in *sa) noexcept { *this = sa; }
  85. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_in6 &sa) noexcept { *this = sa; }
  86. explicit ZT_ALWAYS_INLINE InetAddress(const sockaddr_in6 *sa) noexcept { *this = sa; }
  87. ZT_ALWAYS_INLINE InetAddress(const void *ipBytes,unsigned int ipLen,unsigned int port) noexcept { this->set(ipBytes,ipLen,port); }
  88. ZT_ALWAYS_INLINE InetAddress(const uint32_t ipv4,unsigned int port) noexcept { this->set(&ipv4,4,port); }
  89. explicit ZT_ALWAYS_INLINE InetAddress(const char *ipSlashPort) noexcept { this->fromString(ipSlashPort); }
  90. ZT_ALWAYS_INLINE void clear() noexcept { memoryZero(this); }
  91. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_storage &ss) noexcept
  92. {
  93. memoryCopyUnsafe(this,&ss);
  94. return *this;
  95. }
  96. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_storage *ss) noexcept
  97. {
  98. if (ss)
  99. memoryCopyUnsafe(this,ss);
  100. else memoryZero(this);
  101. return *this;
  102. }
  103. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_in &sa) noexcept
  104. {
  105. copySockaddrToThis(&sa);
  106. return *this;
  107. }
  108. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_in *sa) noexcept
  109. {
  110. if (sa)
  111. copySockaddrToThis(sa);
  112. else memset(reinterpret_cast<void *>(this),0,sizeof(InetAddress));
  113. return *this;
  114. }
  115. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_in6 &sa) noexcept
  116. {
  117. copySockaddrToThis(&sa);
  118. return *this;
  119. }
  120. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr_in6 *sa) noexcept
  121. {
  122. if (sa)
  123. copySockaddrToThis(sa);
  124. else memset(reinterpret_cast<void *>(this),0,sizeof(InetAddress));
  125. return *this;
  126. }
  127. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr &sa) noexcept
  128. {
  129. if (sa.sa_family == AF_INET)
  130. copySockaddrToThis(reinterpret_cast<const sockaddr_in *>(&sa));
  131. else if (sa.sa_family == AF_INET6)
  132. copySockaddrToThis(reinterpret_cast<const sockaddr_in6 *>(&sa));
  133. else memset(reinterpret_cast<void *>(this),0,sizeof(InetAddress));
  134. return *this;
  135. }
  136. ZT_ALWAYS_INLINE InetAddress &operator=(const sockaddr *sa) noexcept
  137. {
  138. if (sa) {
  139. if (sa->sa_family == AF_INET)
  140. copySockaddrToThis(reinterpret_cast<const sockaddr_in *>(sa));
  141. else if (sa->sa_family == AF_INET6)
  142. copySockaddrToThis(reinterpret_cast<const sockaddr_in6 *>(sa));
  143. else memoryZero(this);
  144. } else {
  145. memoryZero(this);
  146. }
  147. return *this;
  148. }
  149. /**
  150. * @return Address family (ss_family in sockaddr_storage)
  151. */
  152. ZT_ALWAYS_INLINE uint8_t family() const noexcept { return _data.ss_family; }
  153. /**
  154. * @return IP scope classification (e.g. loopback, link-local, private, global)
  155. */
  156. IpScope ipScope() const noexcept;
  157. /**
  158. * Set from a raw IP and port number
  159. *
  160. * @param ipBytes Bytes of IP address in network byte order
  161. * @param ipLen Length of IP address: 4 or 16
  162. * @param port Port number or 0 for none
  163. */
  164. void set(const void *ipBytes,unsigned int ipLen,unsigned int port) noexcept;
  165. /**
  166. * Set the port component
  167. *
  168. * @param port Port, 0 to 65535
  169. */
  170. ZT_ALWAYS_INLINE void setPort(unsigned int port) noexcept
  171. {
  172. switch(_data.ss_family) {
  173. case AF_INET:
  174. reinterpret_cast<struct sockaddr_in *>(this)->sin_port = Utils::hton((uint16_t)port);
  175. break;
  176. case AF_INET6:
  177. reinterpret_cast<struct sockaddr_in6 *>(this)->sin6_port = Utils::hton((uint16_t)port);
  178. break;
  179. }
  180. }
  181. /**
  182. * @return True if this network/netmask route describes a default route (e.g. 0.0.0.0/0)
  183. */
  184. bool isDefaultRoute() const noexcept;
  185. /**
  186. * @return ASCII IP/port format representation
  187. */
  188. char *toString(char buf[ZT_INETADDRESS_STRING_SIZE_MAX]) const noexcept;
  189. /**
  190. * @return IP portion only, in ASCII string format
  191. */
  192. char *toIpString(char buf[ZT_INETADDRESS_STRING_SIZE_MAX]) const noexcept;
  193. /**
  194. * @param ipSlashPort IP/port (port is optional, will be 0 if not included)
  195. * @return True if address appeared to be valid
  196. */
  197. bool fromString(const char *ipSlashPort) noexcept;
  198. /**
  199. * @return Port or 0 if no port component defined
  200. */
  201. ZT_ALWAYS_INLINE unsigned int port() const noexcept
  202. {
  203. switch(_data.ss_family) {
  204. case AF_INET: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in *>(this)->sin_port));
  205. case AF_INET6: return Utils::ntoh((uint16_t)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_port));
  206. default: return 0;
  207. }
  208. }
  209. /**
  210. * Alias for port()
  211. *
  212. * This just aliases port() to make code more readable when netmask bits
  213. * are stuffed there, as they are in Network, EthernetTap, and a few other
  214. * spots.
  215. *
  216. * @return Netmask bits
  217. */
  218. ZT_ALWAYS_INLINE unsigned int netmaskBits() const noexcept { return port(); }
  219. /**
  220. * @return True if netmask bits is valid for the address type
  221. */
  222. ZT_ALWAYS_INLINE bool netmaskBitsValid() const noexcept
  223. {
  224. const unsigned int n = port();
  225. switch(_data.ss_family) {
  226. case AF_INET: return (n <= 32);
  227. case AF_INET6: return (n <= 128);
  228. }
  229. return false;
  230. }
  231. /**
  232. * Alias for port()
  233. *
  234. * This just aliases port() because for gateways we use this field to
  235. * store the gateway metric.
  236. *
  237. * @return Gateway metric
  238. */
  239. ZT_ALWAYS_INLINE unsigned int metric() const noexcept { return port(); }
  240. /**
  241. * Construct a full netmask as an InetAddress
  242. *
  243. * @return Netmask such as 255.255.255.0 if this address is /24 (port field will be unchanged)
  244. */
  245. InetAddress netmask() const noexcept;
  246. /**
  247. * Constructs a broadcast address from a network/netmask address
  248. *
  249. * This is only valid for IPv4 and will return a NULL InetAddress for other
  250. * address families.
  251. *
  252. * @return Broadcast address (only IP portion is meaningful)
  253. */
  254. InetAddress broadcast() const noexcept;
  255. /**
  256. * Return the network -- a.k.a. the IP ANDed with the netmask
  257. *
  258. * @return Network e.g. 10.0.1.0/24 from 10.0.1.200/24
  259. */
  260. InetAddress network() const noexcept;
  261. /**
  262. * Test whether this IPv6 prefix matches the prefix of a given IPv6 address
  263. *
  264. * @param addr Address to check
  265. * @return True if this IPv6 prefix matches the prefix of a given IPv6 address
  266. */
  267. bool isEqualPrefix(const InetAddress &addr) const noexcept;
  268. /**
  269. * Test whether this IP/netmask contains this address
  270. *
  271. * @param addr Address to check
  272. * @return True if this IP/netmask (route) contains this address
  273. */
  274. bool containsAddress(const InetAddress &addr) const noexcept;
  275. /**
  276. * @return True if this is an IPv4 address
  277. */
  278. ZT_ALWAYS_INLINE bool isV4() const noexcept { return (family() == AF_INET); }
  279. /**
  280. * @return True if this is an IPv6 address
  281. */
  282. ZT_ALWAYS_INLINE bool isV6() const noexcept { return (family() == AF_INET6); }
  283. /**
  284. * @return pointer to raw address bytes or NULL if not available
  285. */
  286. ZT_ALWAYS_INLINE const void *rawIpData() const noexcept
  287. {
  288. switch(_data.ss_family) {
  289. case AF_INET: return (const void *)&(reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr);
  290. case AF_INET6: return (const void *)(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr);
  291. default: return nullptr;
  292. }
  293. }
  294. /**
  295. * @return InetAddress containing only the IP portion of this address and a zero port, or NULL if not IPv4 or IPv6
  296. */
  297. ZT_ALWAYS_INLINE InetAddress ipOnly() const noexcept
  298. {
  299. InetAddress r;
  300. switch(_data.ss_family) {
  301. case AF_INET:
  302. reinterpret_cast<struct sockaddr_in *>(&r)->sin_family = AF_INET;
  303. reinterpret_cast<struct sockaddr_in *>(&r)->sin_addr.s_addr = reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr;
  304. break;
  305. case AF_INET6:
  306. reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_family = AF_INET;
  307. memcpy(reinterpret_cast<struct sockaddr_in6 *>(&r)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,16);
  308. break;
  309. }
  310. return r;
  311. }
  312. /**
  313. * Performs an IP-only comparison or, if that is impossible, a memcmp()
  314. *
  315. * @param a InetAddress to compare again
  316. * @return True if only IP portions are equal (false for non-IP or null addresses)
  317. */
  318. ZT_ALWAYS_INLINE bool ipsEqual(const InetAddress &a) const noexcept
  319. {
  320. const uint8_t f = _data.ss_family;
  321. if (f == a._data.ss_family) {
  322. if (f == AF_INET)
  323. return (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr);
  324. if (f == AF_INET6)
  325. return (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,16) == 0);
  326. return (memcmp(this,&a,sizeof(InetAddress)) == 0);
  327. }
  328. return false;
  329. }
  330. /**
  331. * Performs an IP-only comparison or, if that is impossible, a memcmp()
  332. *
  333. * This version compares only the first 64 bits of IPv6 addresses.
  334. *
  335. * @param a InetAddress to compare again
  336. * @return True if only IP portions are equal (false for non-IP or null addresses)
  337. */
  338. ZT_ALWAYS_INLINE bool ipsEqual2(const InetAddress &a) const noexcept
  339. {
  340. const uint8_t f = _data.ss_family;
  341. if (f == a._data.ss_family) {
  342. if (f == AF_INET)
  343. return (reinterpret_cast<const struct sockaddr_in *>(this)->sin_addr.s_addr == reinterpret_cast<const struct sockaddr_in *>(&a)->sin_addr.s_addr);
  344. if (f == AF_INET6)
  345. return (memcmp(reinterpret_cast<const struct sockaddr_in6 *>(this)->sin6_addr.s6_addr,reinterpret_cast<const struct sockaddr_in6 *>(&a)->sin6_addr.s6_addr,8) == 0);
  346. return (memcmp(this,&a,sizeof(InetAddress)) == 0);
  347. }
  348. return false;
  349. }
  350. unsigned long hashCode() const noexcept;
  351. /**
  352. * Fill out a ZT_TraceEventPathAddress from this InetAddress
  353. *
  354. * @param ta ZT_TraceEventPathAddress to fill
  355. */
  356. void forTrace(ZT_TraceEventPathAddress &ta) const noexcept;
  357. /**
  358. * Check whether this is a network/route rather than an IP assignment
  359. *
  360. * A network is an IP/netmask where everything after the netmask is
  361. * zero e.g. 10.0.0.0/8.
  362. *
  363. * @return True if everything after netmask bits is zero
  364. */
  365. bool isNetwork() const noexcept;
  366. /**
  367. * @return True if address family is non-zero
  368. */
  369. explicit ZT_ALWAYS_INLINE operator bool() const noexcept { return (family() != 0); }
  370. static constexpr int marshalSizeMax() noexcept { return ZT_INETADDRESS_MARSHAL_SIZE_MAX; }
  371. int marshal(uint8_t data[ZT_INETADDRESS_MARSHAL_SIZE_MAX]) const noexcept;
  372. int unmarshal(const uint8_t *restrict data,int len) noexcept;
  373. bool operator==(const InetAddress &a) const noexcept;
  374. bool operator<(const InetAddress &a) const noexcept;
  375. ZT_ALWAYS_INLINE bool operator!=(const InetAddress &a) const noexcept { return !(*this == a); }
  376. ZT_ALWAYS_INLINE bool operator>(const InetAddress &a) const noexcept { return (a < *this); }
  377. ZT_ALWAYS_INLINE bool operator<=(const InetAddress &a) const noexcept { return !(a < *this); }
  378. ZT_ALWAYS_INLINE bool operator>=(const InetAddress &a) const noexcept { return !(*this < a); }
  379. /**
  380. * Compute an IPv6 link-local address
  381. *
  382. * @param mac MAC address seed
  383. * @return IPv6 link-local address
  384. */
  385. static InetAddress makeIpv6LinkLocal(const MAC &mac) noexcept;
  386. /**
  387. * Compute private IPv6 unicast address from network ID and ZeroTier address
  388. *
  389. * This generates a private unicast IPv6 address that is mostly compliant
  390. * with the letter of RFC4193 and certainly compliant in spirit.
  391. *
  392. * RFC4193 specifies a format of:
  393. *
  394. * | 7 bits |1| 40 bits | 16 bits | 64 bits |
  395. * | Prefix |L| Global ID | Subnet ID | Interface ID |
  396. *
  397. * The 'L' bit is set to 1, yielding an address beginning with 0xfd. Then
  398. * the network ID is filled into the global ID, subnet ID, and first byte
  399. * of the "interface ID" field. Since the first 40 bits of the network ID
  400. * is the unique ZeroTier address of its controller, this makes a very
  401. * good random global ID. Since network IDs have 24 more bits, we let it
  402. * overflow into the interface ID.
  403. *
  404. * After that we pad with two bytes: 0x99, 0x93, namely the default ZeroTier
  405. * port in hex.
  406. *
  407. * Finally we fill the remaining 40 bits of the interface ID field with
  408. * the 40-bit unique ZeroTier device ID of the network member.
  409. *
  410. * This yields a valid RFC4193 address with a random global ID, a
  411. * meaningful subnet ID, and a unique interface ID, all mappable back onto
  412. * ZeroTier space.
  413. *
  414. * This in turn could allow us, on networks numbered this way, to emulate
  415. * IPv6 NDP and eliminate all multicast. This could be beneficial for
  416. * small devices and huge networks, e.g. IoT applications.
  417. *
  418. * The returned address is given an odd prefix length of /88, since within
  419. * a given network only the last 40 bits (device ID) are variable. This
  420. * is a bit unusual but as far as we know should not cause any problems with
  421. * any non-braindead IPv6 stack.
  422. *
  423. * @param nwid 64-bit network ID
  424. * @param zeroTierAddress 40-bit device address (in least significant 40 bits, highest 24 bits ignored)
  425. * @return IPv6 private unicast address with /88 netmask
  426. */
  427. static InetAddress makeIpv6rfc4193(uint64_t nwid,uint64_t zeroTierAddress) noexcept;
  428. /**
  429. * Compute a private IPv6 "6plane" unicast address from network ID and ZeroTier address
  430. */
  431. static InetAddress makeIpv66plane(uint64_t nwid,uint64_t zeroTierAddress) noexcept;
  432. private:
  433. sockaddr_storage _data;
  434. };
  435. static ZT_ALWAYS_INLINE InetAddress *asInetAddress(sockaddr_in *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
  436. static ZT_ALWAYS_INLINE InetAddress *asInetAddress(sockaddr_in6 *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
  437. static ZT_ALWAYS_INLINE InetAddress *asInetAddress(sockaddr *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
  438. static ZT_ALWAYS_INLINE InetAddress *asInetAddress(sockaddr_storage *p) noexcept { return reinterpret_cast<InetAddress *>(p); }
  439. static ZT_ALWAYS_INLINE const InetAddress *asInetAddress(const sockaddr_in *p) noexcept { return reinterpret_cast<const InetAddress *>(p); }
  440. static ZT_ALWAYS_INLINE const InetAddress *asInetAddress(const sockaddr_in6 *p) noexcept { return reinterpret_cast<const InetAddress *>(p); }
  441. static ZT_ALWAYS_INLINE const InetAddress *asInetAddress(const sockaddr *p) noexcept { return reinterpret_cast<const InetAddress *>(p); }
  442. static ZT_ALWAYS_INLINE const InetAddress *asInetAddress(const sockaddr_storage *p) noexcept { return reinterpret_cast<const InetAddress *>(p); }
  443. static ZT_ALWAYS_INLINE InetAddress &asInetAddress(sockaddr_in &p) noexcept { return *reinterpret_cast<InetAddress *>(&p); }
  444. static ZT_ALWAYS_INLINE InetAddress &asInetAddress(sockaddr_in6 &p) noexcept { return *reinterpret_cast<InetAddress *>(&p); }
  445. static ZT_ALWAYS_INLINE InetAddress &asInetAddress(sockaddr &p) noexcept { return *reinterpret_cast<InetAddress *>(&p); }
  446. static ZT_ALWAYS_INLINE InetAddress &asInetAddress(sockaddr_storage &p) noexcept { return *reinterpret_cast<InetAddress *>(&p); }
  447. static ZT_ALWAYS_INLINE const InetAddress &asInetAddress(const sockaddr_in &p) noexcept { return *reinterpret_cast<const InetAddress *>(&p); }
  448. static ZT_ALWAYS_INLINE const InetAddress &asInetAddress(const sockaddr_in6 &p) noexcept { return *reinterpret_cast<const InetAddress *>(&p); }
  449. static ZT_ALWAYS_INLINE const InetAddress &asInetAddress(const sockaddr &p) noexcept { return *reinterpret_cast<const InetAddress *>(&p); }
  450. static ZT_ALWAYS_INLINE const InetAddress &asInetAddress(const sockaddr_storage &p) noexcept { return *reinterpret_cast<const InetAddress *>(&p); }
  451. } // namespace ZeroTier
  452. #endif