Locator.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #define ZT_LOCATOR_MAX_ENDPOINTS 8
  22. #define ZT_LOCATOR_MARSHAL_SIZE_MAX (8 + 2 + (ZT_ENDPOINT_MARSHAL_SIZE_MAX * ZT_LOCATOR_MAX_ENDPOINTS) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE)
  23. namespace ZeroTier {
  24. /**
  25. * Signed information about a node's location on the network
  26. *
  27. * A locator contains long-lived endpoints for a node such as IP/port pairs,
  28. * URLs, or other nodes, and is signed by the node it describes.
  29. */
  30. class Locator
  31. {
  32. public:
  33. ZT_ALWAYS_INLINE Locator() { this->clear(); }
  34. /**
  35. * Zero the Locator data structure
  36. */
  37. ZT_ALWAYS_INLINE void clear() { memset(reinterpret_cast<void *>(this),0,sizeof(Locator)); }
  38. /**
  39. * @return Timestamp (a.k.a. revision number) set by Location signer
  40. */
  41. ZT_ALWAYS_INLINE int64_t timestamp() const { return _ts; }
  42. /**
  43. * @return True if locator is signed
  44. */
  45. ZT_ALWAYS_INLINE bool isSigned() const { return (_signatureLength > 0); }
  46. /**
  47. * @return Length of signature in bytes or 0 if none
  48. */
  49. ZT_ALWAYS_INLINE unsigned int signatureLength() const { return _signatureLength; }
  50. /**
  51. * @return Pointer to signature bytes
  52. */
  53. ZT_ALWAYS_INLINE const uint8_t *signature() const { return _signature; }
  54. /**
  55. * @return Number of endpoints in this locator
  56. */
  57. ZT_ALWAYS_INLINE unsigned int endpointCount() const { return _endpointCount; }
  58. /**
  59. * @return Pointer to array of endpoints
  60. */
  61. ZT_ALWAYS_INLINE const Endpoint *endpoints() const { return _at; }
  62. /**
  63. * Add an endpoint to this locator
  64. *
  65. * This doesn't check for the presence of the endpoint, so take
  66. * care not to add duplicates.
  67. *
  68. * @param ep Endpoint to add
  69. * @return True if endpoint was added (or already present), false if locator is full
  70. */
  71. ZT_ALWAYS_INLINE bool add(const Endpoint &ep)
  72. {
  73. if (_endpointCount >= ZT_LOCATOR_MAX_ENDPOINTS)
  74. return false;
  75. _at[_endpointCount++] = ep;
  76. return true;
  77. }
  78. /**
  79. * Sign this locator
  80. *
  81. * This sets timestamp, sorts endpoints so that the same set of endpoints
  82. * will always produce the same locator, and signs.
  83. *
  84. * @param id Identity that includes private key
  85. * @return True if signature successful
  86. */
  87. bool sign(int64_t ts,const Identity &id);
  88. /**
  89. * Verify this Locator's validity and signature
  90. *
  91. * @param id Identity corresponding to hash
  92. * @return True if valid and signature checks out
  93. */
  94. bool verify(const Identity &id) const;
  95. explicit ZT_ALWAYS_INLINE operator bool() const { return (_ts != 0); }
  96. static ZT_ALWAYS_INLINE int marshalSizeMax() { return ZT_LOCATOR_MARSHAL_SIZE_MAX; }
  97. int marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX],bool excludeSignature = false) const;
  98. int unmarshal(const uint8_t *restrict data,int len);
  99. private:
  100. int64_t _ts;
  101. unsigned int _endpointCount;
  102. unsigned int _signatureLength;
  103. Endpoint _at[ZT_LOCATOR_MAX_ENDPOINTS];
  104. uint16_t _flags;
  105. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  106. };
  107. } // namespace ZeroTier
  108. #endif