Locator.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <algorithm>
  16. #include <vector>
  17. #include <cstdint>
  18. #include "Constants.hpp"
  19. #include "Endpoint.hpp"
  20. #include "Identity.hpp"
  21. #include "TriviallyCopyable.hpp"
  22. #define ZT_LOCATOR_MAX_ENDPOINTS 8
  23. #define ZT_LOCATOR_MARSHAL_SIZE_MAX (1 + 8 + 2 + (ZT_ENDPOINT_MARSHAL_SIZE_MAX * ZT_LOCATOR_MAX_ENDPOINTS) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE)
  24. namespace ZeroTier {
  25. /**
  26. * Signed information about a node's location on the network
  27. *
  28. * A locator contains long-lived endpoints for a node such as IP/port pairs,
  29. * URLs, or other nodes, and is signed by the node it describes.
  30. */
  31. class Locator : public TriviallyCopyable
  32. {
  33. public:
  34. ZT_INLINE Locator() noexcept { memoryZero(this); }
  35. /**
  36. * Zero the Locator data structure
  37. */
  38. ZT_INLINE void clear() noexcept { memoryZero(this); }
  39. /**
  40. * @return Timestamp (a.k.a. revision number) set by Location signer
  41. */
  42. ZT_INLINE int64_t timestamp() const noexcept { return m_ts; }
  43. /**
  44. * @return True if locator is signed
  45. */
  46. ZT_INLINE bool isSigned() const noexcept { return m_signatureLength > 0; }
  47. /**
  48. * @return Length of signature in bytes or 0 if none
  49. */
  50. ZT_INLINE unsigned int signatureLength() const noexcept { return m_signatureLength; }
  51. /**
  52. * @return Pointer to signature bytes
  53. */
  54. ZT_INLINE const uint8_t *signature() const noexcept { return m_signature; }
  55. /**
  56. * @return Number of endpoints in this locator
  57. */
  58. ZT_INLINE unsigned int endpointCount() const noexcept { return m_endpointCount; }
  59. /**
  60. * @return Pointer to array of endpoints
  61. */
  62. ZT_INLINE const Endpoint *endpoints() const noexcept { return m_at; }
  63. /**
  64. * Add an endpoint to this locator
  65. *
  66. * This doesn't check for the presence of the endpoint, so take
  67. * care not to add duplicates.
  68. *
  69. * @param ep Endpoint to add
  70. * @return True if endpoint was added (or already present), false if locator is full
  71. */
  72. ZT_INLINE bool add(const Endpoint &ep) noexcept
  73. {
  74. if (m_endpointCount >= ZT_LOCATOR_MAX_ENDPOINTS)
  75. return false;
  76. m_at[m_endpointCount++] = ep;
  77. return true;
  78. }
  79. /**
  80. * Sign this locator
  81. *
  82. * This sets timestamp, sorts endpoints so that the same set of endpoints
  83. * will always produce the same locator, and signs.
  84. *
  85. * @param id Identity that includes private key
  86. * @return True if signature successful
  87. */
  88. bool sign(int64_t ts,const Identity &id) noexcept;
  89. /**
  90. * Verify this Locator's validity and signature
  91. *
  92. * @param id Identity corresponding to hash
  93. * @return True if valid and signature checks out
  94. */
  95. bool verify(const Identity &id) const noexcept;
  96. explicit ZT_INLINE operator bool() const noexcept { return m_ts != 0; }
  97. static constexpr int marshalSizeMax() noexcept { return ZT_LOCATOR_MARSHAL_SIZE_MAX; }
  98. int marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX],bool excludeSignature = false) const noexcept;
  99. int unmarshal(const uint8_t *restrict data,int len) noexcept;
  100. /**
  101. * Create a signed Locator and package it with the root's identity to make a root spec
  102. *
  103. * @param id Identity (must have secret)
  104. * @param ts Timestamp
  105. * @param endpoints Endpoints
  106. * @param rootSpecBuf Buffer to store identity and locator into
  107. * @param rootSpecBufSize Size of buffer
  108. * @return Bytes written to buffer or -1 on error
  109. */
  110. static int makeRootSpecification(const Identity &id,int64_t ts,const Vector<Endpoint> &endpoints,void *rootSpecBuf,unsigned int rootSpecBufSize);
  111. /**
  112. * Parse a root specification and decode the identity and locator
  113. *
  114. * @param rootSpec Root spec bytes
  115. * @param rootSpecSize Size in bytes
  116. * @return Identity and locator, with identity NULL if an error occurs
  117. */
  118. static std::pair<Identity,Locator> parseRootSpecification(const void *rootSpec,unsigned int rootSpecSize);
  119. private:
  120. int64_t m_ts;
  121. unsigned int m_endpointCount;
  122. unsigned int m_signatureLength;
  123. Endpoint m_at[ZT_LOCATOR_MAX_ENDPOINTS];
  124. uint16_t m_flags;
  125. uint8_t m_signature[ZT_SIGNATURE_BUFFER_SIZE];
  126. };
  127. } // namespace ZeroTier
  128. #endif