InetAddress.hpp 21 KB

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