InetAddress.hpp 19 KB

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