InetAddress.hpp 21 KB

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