Endpoint.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_ENDPOINT_HPP
  14. #define ZT_ENDPOINT_HPP
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstdint>
  18. #include <cstring>
  19. #include "Constants.hpp"
  20. #include "InetAddress.hpp"
  21. #include "Address.hpp"
  22. #include "Utils.hpp"
  23. // max name size + type byte + port (for DNS name/port) + 3x 16-bit coordinate for location
  24. #define ZT_ENDPOINT_MARSHAL_SIZE_MAX (ZT_ENDPOINT_MAX_NAME_SIZE+1+2+2+2+2)
  25. namespace ZeroTier {
  26. /**
  27. * Endpoint variant specifying some form of network endpoint
  28. *
  29. * This data structure supports a number of types that are not yet actually used:
  30. * DNSNAME, URL, and ETHERNET. These are present to reserve them for future use.
  31. */
  32. class Endpoint
  33. {
  34. public:
  35. enum Type
  36. {
  37. NIL = 0, // NIL value
  38. INETADDR_V4 = 1, // IPv4
  39. INETADDR_V6 = 2, // IPv6
  40. DNSNAME = 3, // DNS name and port that resolves to InetAddress
  41. ZEROTIER = 4, // ZeroTier Address (for relaying and meshy behavior)
  42. URL = 5, // URL for http/https/ws/etc. (not implemented yet)
  43. ETHERNET = 6, // 48-bit LAN-local Ethernet address
  44. UNRECOGNIZED = 255 // Unrecognized endpoint type encountered in stream
  45. };
  46. ZT_ALWAYS_INLINE Endpoint()
  47. {
  48. memset(reinterpret_cast<void *>(this),0,sizeof(Endpoint));
  49. }
  50. ZT_ALWAYS_INLINE Endpoint(const Endpoint &ep)
  51. {
  52. memcpy(reinterpret_cast<void *>(this),reinterpret_cast<const void *>(&ep),sizeof(Endpoint));
  53. }
  54. explicit ZT_ALWAYS_INLINE Endpoint(const InetAddress &sa)
  55. {
  56. *this = sa;
  57. }
  58. ZT_ALWAYS_INLINE Endpoint(const Address &zt,const uint8_t identityHash[ZT_IDENTITY_HASH_SIZE]) :
  59. _t(ZEROTIER)
  60. {
  61. _v.zt.a = zt.toInt();
  62. memcpy(_v.zt.idh,identityHash,ZT_IDENTITY_HASH_SIZE);
  63. }
  64. ZT_ALWAYS_INLINE Endpoint(const char *name,const int port) :
  65. _t(DNSNAME)
  66. {
  67. _v.dns.port = port;
  68. Utils::scopy(_v.dns.name,sizeof(_v.dns.name),name);
  69. }
  70. explicit ZT_ALWAYS_INLINE Endpoint(const char *url) :
  71. _t(URL)
  72. {
  73. Utils::scopy(_v.url,sizeof(_v.url),url);
  74. }
  75. ZT_ALWAYS_INLINE Endpoint &operator=(const Endpoint &ep)
  76. {
  77. memcpy(reinterpret_cast<void *>(this),&ep,sizeof(Endpoint));
  78. return *this;
  79. }
  80. ZT_ALWAYS_INLINE Endpoint &operator=(const InetAddress &sa)
  81. {
  82. switch(sa.ss_family) {
  83. case AF_INET:
  84. _t = INETADDR_V4;
  85. break;
  86. case AF_INET6:
  87. _t = INETADDR_V6;
  88. break;
  89. default:
  90. _t = NIL;
  91. return *this;
  92. }
  93. _v.sa = sa;
  94. return *this;
  95. }
  96. /**
  97. * @return InetAddress or NIL if not of this type
  98. */
  99. ZT_ALWAYS_INLINE const InetAddress &inetAddr() const { return ((_t == INETADDR_V4)||(_t == INETADDR_V6)) ? *reinterpret_cast<const InetAddress *>(&_v.sa) : InetAddress::NIL; }
  100. /**
  101. * @return DNS name or empty string if not of this type
  102. */
  103. ZT_ALWAYS_INLINE const char *dnsName() const { return (_t == DNSNAME) ? _v.dns.name : ""; }
  104. /**
  105. * @return Port associated with DNS name or -1 if not of this type
  106. */
  107. ZT_ALWAYS_INLINE int dnsPort() const { return (_t == DNSNAME) ? _v.dns.port : -1; }
  108. /**
  109. * @return ZeroTier address or NIL if not of this type
  110. */
  111. ZT_ALWAYS_INLINE Address ztAddress() const { return Address((_t == ZEROTIER) ? _v.zt.a : (uint64_t)0); }
  112. /**
  113. * @return 384-bit hash of identity keys or NULL if not of this type
  114. */
  115. ZT_ALWAYS_INLINE const uint8_t *ztIdentityHash() const { return (_t == ZEROTIER) ? _v.zt.idh : nullptr; }
  116. /**
  117. * @return URL or empty string if not of this type
  118. */
  119. ZT_ALWAYS_INLINE const char *url() const { return (_t == URL) ? _v.url : ""; }
  120. /**
  121. * @return Ethernet address or NIL if not of this type
  122. */
  123. ZT_ALWAYS_INLINE MAC ethernet() const { return (_t == ETHERNET) ? MAC(_v.eth) : MAC(); }
  124. /**
  125. * @return Endpoint type or NIL if unset/empty
  126. */
  127. ZT_ALWAYS_INLINE Type type() const { return _t; }
  128. explicit ZT_ALWAYS_INLINE operator bool() const { return _t != NIL; }
  129. bool operator==(const Endpoint &ep) const;
  130. ZT_ALWAYS_INLINE bool operator!=(const Endpoint &ep) const { return (!(*this == ep)); }
  131. bool operator<(const Endpoint &ep) const;
  132. ZT_ALWAYS_INLINE bool operator>(const Endpoint &ep) const { return (ep < *this); }
  133. ZT_ALWAYS_INLINE bool operator<=(const Endpoint &ep) const { return !(ep < *this); }
  134. ZT_ALWAYS_INLINE bool operator>=(const Endpoint &ep) const { return !(*this < ep); }
  135. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_ENDPOINT_MARSHAL_SIZE_MAX; }
  136. int marshal(uint8_t data[ZT_ENDPOINT_MARSHAL_SIZE_MAX]) const;
  137. int unmarshal(const uint8_t *restrict data,const int len);
  138. private:
  139. Type _t;
  140. int _l[3]; // X,Y,Z location in kilometers from the nearest gravitational center of mass
  141. union {
  142. struct sockaddr_storage sa;
  143. struct {
  144. uint16_t port;
  145. char name[ZT_ENDPOINT_MAX_NAME_SIZE];
  146. } dns;
  147. struct {
  148. uint64_t a;
  149. uint8_t idh[ZT_IDENTITY_HASH_SIZE];
  150. } zt;
  151. char url[ZT_ENDPOINT_MAX_NAME_SIZE];
  152. uint64_t eth;
  153. } _v;
  154. };
  155. } // namespace ZeroTier
  156. #endif