InetAddress.hpp 19 KB

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