InetAddress.hpp 23 KB

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