InetAddress.hpp 20 KB

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