InetAddress.hpp 20 KB

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