InetAddress.hpp 19 KB

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