Locator.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_ALWAYS_INLINE Locator() noexcept { memoryZero(this); }
  35. /**
  36. * Zero the Locator data structure
  37. */
  38. ZT_ALWAYS_INLINE void clear() noexcept { memoryZero(this); }
  39. /**
  40. * @return Timestamp (a.k.a. revision number) set by Location signer
  41. */
  42. ZT_ALWAYS_INLINE int64_t timestamp() const noexcept { return _ts; }
  43. /**
  44. * @return True if locator is signed
  45. */
  46. ZT_ALWAYS_INLINE bool isSigned() const noexcept { return (_signatureLength > 0); }
  47. /**
  48. * @return Length of signature in bytes or 0 if none
  49. */
  50. ZT_ALWAYS_INLINE unsigned int signatureLength() const noexcept { return _signatureLength; }
  51. /**
  52. * @return Pointer to signature bytes
  53. */
  54. ZT_ALWAYS_INLINE const uint8_t *signature() const noexcept { return _signature; }
  55. /**
  56. * @return Number of endpoints in this locator
  57. */
  58. ZT_ALWAYS_INLINE unsigned int endpointCount() const noexcept { return _endpointCount; }
  59. /**
  60. * @return Pointer to array of endpoints
  61. */
  62. ZT_ALWAYS_INLINE const Endpoint *endpoints() const noexcept { return _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_ALWAYS_INLINE bool add(const Endpoint &ep) noexcept
  73. {
  74. if (_endpointCount >= ZT_LOCATOR_MAX_ENDPOINTS)
  75. return false;
  76. _at[_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_ALWAYS_INLINE operator bool() const noexcept { return (_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. private:
  101. int64_t _ts;
  102. unsigned int _endpointCount;
  103. unsigned int _signatureLength;
  104. Endpoint _at[ZT_LOCATOR_MAX_ENDPOINTS];
  105. uint16_t _flags;
  106. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  107. };
  108. } // namespace ZeroTier
  109. #endif