InetAddress.hpp 20 KB

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