InetAddress.hpp 21 KB

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