InetAddress.hpp 21 KB

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