InetAddress.hpp 20 KB

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