Locator.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_LOCATOR_HPP
  14. #define ZT_LOCATOR_HPP
  15. #include "Constants.hpp"
  16. #include "Endpoint.hpp"
  17. #include "Identity.hpp"
  18. #include "TriviallyCopyable.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "FCV.hpp"
  21. #include "Containers.hpp"
  22. #include "Dictionary.hpp"
  23. /**
  24. * Maximum size of endpoint attributes dictionary plus one byte for size.
  25. *
  26. * This cannot be (easily) changed.
  27. */
  28. #define ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE 256
  29. /**
  30. * Maximum number of endpoints, which can be increased.
  31. */
  32. #define ZT_LOCATOR_MAX_ENDPOINTS 16
  33. #define ZT_LOCATOR_MARSHAL_SIZE_MAX (8 + ZT_FINGERPRINT_MARSHAL_SIZE + 2 + (ZT_LOCATOR_MAX_ENDPOINTS * (ZT_ENDPOINT_MARSHAL_SIZE_MAX + ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE)) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE)
  34. /**
  35. * Maximum size of a string format Locator (this is way larger than needed)
  36. */
  37. #define ZT_LOCATOR_STRING_SIZE_MAX 16384
  38. namespace ZeroTier {
  39. /**
  40. * Signed information about a node's location on the network
  41. *
  42. * A locator contains long-lived endpoints for a node such as IP/port pairs,
  43. * URLs, or other nodes, and is signed by the node it describes.
  44. */
  45. class Locator
  46. {
  47. friend class SharedPtr< Locator >;
  48. friend class SharedPtr< const Locator >;
  49. public:
  50. /**
  51. * Attributes of an endpoint in this locator
  52. *
  53. * This is specified for future use, but there are currently no attributes
  54. * defined. A Dictionary is used for serialization for extensibility.
  55. */
  56. struct EndpointAttributes
  57. {
  58. /**
  59. * Raw attributes data in the form of a dictionary prefixed by its size.
  60. *
  61. * The maximum size of attributes is 256, which is more than enough for
  62. * tiny things like bandwidth and priority.
  63. */
  64. uint8_t data[ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE];
  65. ZT_INLINE EndpointAttributes() noexcept
  66. { Utils::zero< ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE >(data); }
  67. ZT_INLINE bool operator==(const EndpointAttributes &a) const noexcept
  68. { return ((data[0] == a.data[0]) && (memcmp(data, a.data, data[0]) == 0)); }
  69. ZT_INLINE bool operator<(const EndpointAttributes &a) const noexcept
  70. { return ((data[0] < a.data[0]) || ((data[0] == a.data[0]) && (memcmp(data, a.data, data[0]) < 0))); }
  71. ZT_INLINE bool operator!=(const EndpointAttributes &a) const noexcept
  72. { return !(*this == a); }
  73. ZT_INLINE bool operator>(const EndpointAttributes &a) const noexcept
  74. { return (a < *this); }
  75. ZT_INLINE bool operator<=(const EndpointAttributes &a) const noexcept
  76. { return !(a < *this); }
  77. ZT_INLINE bool operator>=(const EndpointAttributes &a) const noexcept
  78. { return !(*this < a); }
  79. };
  80. ZT_INLINE Locator() noexcept:
  81. m_ts(0)
  82. {}
  83. explicit Locator(const char *const str) noexcept;
  84. ZT_INLINE Locator(const Locator &loc) noexcept:
  85. m_ts(loc.m_ts),
  86. m_signer(loc.m_signer),
  87. m_endpoints(loc.m_endpoints),
  88. m_signature(loc.m_signature),
  89. __refCount(0)
  90. {}
  91. /**
  92. * @return Timestamp (a.k.a. revision number) set by Location signer
  93. */
  94. ZT_INLINE int64_t timestamp() const noexcept
  95. { return m_ts; }
  96. /**
  97. * @return Fingerprint of identity that signed this locator
  98. */
  99. ZT_INLINE const Fingerprint &signer() const noexcept
  100. { return m_signer; }
  101. /**
  102. * @return Endpoints specified in locator
  103. */
  104. ZT_INLINE const Vector< std::pair< Endpoint, EndpointAttributes > > &endpoints() const noexcept
  105. { return m_endpoints; }
  106. /**
  107. * @return Signature data
  108. */
  109. ZT_INLINE const FCV< uint8_t, ZT_SIGNATURE_BUFFER_SIZE > &signature() const noexcept
  110. { return m_signature; }
  111. /**
  112. * Add an endpoint to this locator
  113. *
  114. * This doesn't check for the presence of the endpoint, so take
  115. * care not to add duplicates.
  116. *
  117. * @param ep Endpoint to add
  118. * @param a Endpoint attributes
  119. * @return True if endpoint was added (or already present), false if locator is full
  120. */
  121. bool add(const Endpoint &ep, const EndpointAttributes &a);
  122. /**
  123. * Sign this locator
  124. *
  125. * This sets timestamp, sorts endpoints so that the same set of endpoints
  126. * will always produce the same locator, and signs.
  127. *
  128. * @param id Identity that includes private key
  129. * @return True if signature successful
  130. */
  131. bool sign(int64_t ts, const Identity &id) noexcept;
  132. /**
  133. * Verify this Locator's validity and signature
  134. *
  135. * @param id Identity corresponding to hash
  136. * @return True if valid and signature checks out
  137. */
  138. bool verify(const Identity &id) const noexcept;
  139. /**
  140. * Convert this locator to a string
  141. *
  142. * @param s String buffer
  143. * @return Pointer to buffer
  144. */
  145. char *toString(char s[ZT_LOCATOR_STRING_SIZE_MAX]) const noexcept;
  146. ZT_INLINE String toString() const
  147. {
  148. char tmp[ZT_LOCATOR_STRING_SIZE_MAX];
  149. return String(toString(tmp));
  150. }
  151. /**
  152. * Decode a string format locator
  153. *
  154. * @param s Locator from toString()
  155. * @return True if format was valid
  156. */
  157. bool fromString(const char *s) noexcept;
  158. explicit ZT_INLINE operator bool() const noexcept
  159. { return m_ts > 0; }
  160. static constexpr int marshalSizeMax() noexcept
  161. { return ZT_LOCATOR_MARSHAL_SIZE_MAX; }
  162. int marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX], bool excludeSignature = false) const noexcept;
  163. int unmarshal(const uint8_t *data, int len) noexcept;
  164. ZT_INLINE bool operator==(const Locator &l) const noexcept
  165. {
  166. return (
  167. (m_ts == l.m_ts) &&
  168. (m_signer == l.m_signer) &&
  169. (m_endpoints == l.m_endpoints) &&
  170. (m_signature == l.m_signature));
  171. }
  172. ZT_INLINE bool operator!=(const Locator &l) const noexcept
  173. { return !(*this == l); }
  174. private:
  175. int64_t m_ts;
  176. Fingerprint m_signer;
  177. Vector< std::pair< Endpoint, EndpointAttributes > > m_endpoints;
  178. FCV< uint8_t, ZT_SIGNATURE_BUFFER_SIZE > m_signature;
  179. std::atomic< int > __refCount;
  180. };
  181. } // namespace ZeroTier
  182. #endif