SelfAwareness.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  4. *
  5. * (c) ZeroTier, Inc.
  6. * https://www.zerotier.com/
  7. */
  8. #ifndef ZT_SELFAWARENESS_HPP
  9. #define ZT_SELFAWARENESS_HPP
  10. #include "Address.hpp"
  11. #include "Constants.hpp"
  12. #include "Hashtable.hpp"
  13. #include "InetAddress.hpp"
  14. #include "Mutex.hpp"
  15. namespace ZeroTier {
  16. class RuntimeEnvironment;
  17. /**
  18. * Tracks changes to this peer's real world addresses
  19. */
  20. class SelfAwareness {
  21. public:
  22. SelfAwareness(const RuntimeEnvironment* renv);
  23. /**
  24. * Called when a trusted remote peer informs us of our external network address
  25. *
  26. * @param reporter ZeroTier address of reporting peer
  27. * @param receivedOnLocalAddress Local address on which report was received
  28. * @param reporterPhysicalAddress Physical address that reporting peer seems to have
  29. * @param myPhysicalAddress Physical address that peer says we have
  30. * @param trusted True if this peer is trusted as an authority to inform us of external address changes
  31. * @param now Current time
  32. */
  33. void iam(void* tPtr, const Address& reporter, const int64_t receivedOnLocalSocket, const InetAddress& reporterPhysicalAddress, const InetAddress& myPhysicalAddress, bool trusted, int64_t now);
  34. /**
  35. * Return all known external surface addresses reported by peers
  36. *
  37. * @return A vector of InetAddress(es)
  38. */
  39. std::vector<InetAddress> whoami();
  40. /**
  41. * Clean up database periodically
  42. *
  43. * @param now Current time
  44. */
  45. void clean(int64_t now);
  46. private:
  47. struct PhySurfaceKey {
  48. Address reporter;
  49. int64_t receivedOnLocalSocket;
  50. InetAddress reporterPhysicalAddress;
  51. InetAddress::IpScope scope;
  52. PhySurfaceKey() : reporter(), scope(InetAddress::IP_SCOPE_NONE)
  53. {
  54. }
  55. PhySurfaceKey(const Address& r, const int64_t rol, const InetAddress& ra, InetAddress::IpScope s) : reporter(r), receivedOnLocalSocket(rol), reporterPhysicalAddress(ra), scope(s)
  56. {
  57. }
  58. inline unsigned long hashCode() const
  59. {
  60. return ((unsigned long)reporter.toInt() + (unsigned long)scope);
  61. }
  62. inline bool operator==(const PhySurfaceKey& k) const
  63. {
  64. return ((reporter == k.reporter) && (receivedOnLocalSocket == k.receivedOnLocalSocket) && (reporterPhysicalAddress == k.reporterPhysicalAddress) && (scope == k.scope));
  65. }
  66. };
  67. struct PhySurfaceEntry {
  68. InetAddress mySurface;
  69. uint64_t ts;
  70. bool trusted;
  71. PhySurfaceEntry() : mySurface(), ts(0), trusted(false)
  72. {
  73. }
  74. PhySurfaceEntry(const InetAddress& a, const uint64_t t) : mySurface(a), ts(t), trusted(false)
  75. {
  76. }
  77. };
  78. const RuntimeEnvironment* RR;
  79. Hashtable<PhySurfaceKey, PhySurfaceEntry> _phy;
  80. Mutex _phy_m;
  81. };
  82. } // namespace ZeroTier
  83. #endif