2
0

InetAddress.hpp 21 KB

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