SelfAwareness.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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_SELFAWARENESS_HPP
  14. #define ZT_SELFAWARENESS_HPP
  15. #include "Constants.hpp"
  16. #include "InetAddress.hpp"
  17. #include "Hashtable.hpp"
  18. #include "Address.hpp"
  19. #include "Mutex.hpp"
  20. #include <map>
  21. namespace ZeroTier {
  22. class Identity;
  23. class RuntimeEnvironment;
  24. /**
  25. * SelfAwareness manages awareness of this peer's external address(es) and NAT situation.
  26. *
  27. * This code should not be capable of achieving sentience and triggering the Terminator wars.
  28. */
  29. class SelfAwareness
  30. {
  31. public:
  32. explicit SelfAwareness(const RuntimeEnvironment *renv);
  33. /**
  34. * Called when a remote peer informs us of our external network address
  35. *
  36. * @param reporter Identity of reporting peer
  37. * @param receivedOnLocalAddress Local address on which report was received
  38. * @param reporterPhysicalAddress Physical address that reporting peer seems to have
  39. * @param myPhysicalAddress Physical address that peer says we have
  40. * @param trusted True if this peer is trusted as an authority to inform us of external address changes
  41. * @param now Current time
  42. */
  43. void iam(void *tPtr,const Identity &reporter,int64_t receivedOnLocalSocket,const InetAddress &reporterPhysicalAddress,const InetAddress &myPhysicalAddress,bool trusted,int64_t now);
  44. /**
  45. * Clean up database periodically
  46. *
  47. * @param now Current time
  48. */
  49. void clean(int64_t now);
  50. /**
  51. * Get external address consensus, which is the statistical "mode" of external addresses.
  52. *
  53. * @param now Current time
  54. * @return Map of count to IP/port representing how many endpoints reported each address
  55. */
  56. std::multimap<unsigned long,InetAddress> externalAddresses(int64_t now) const;
  57. private:
  58. struct PhySurfaceKey
  59. {
  60. Address reporter;
  61. int64_t receivedOnLocalSocket;
  62. InetAddress reporterPhysicalAddress;
  63. InetAddress::IpScope scope;
  64. ZT_ALWAYS_INLINE PhySurfaceKey() {}
  65. ZT_ALWAYS_INLINE PhySurfaceKey(const Address &r,const int64_t rol,const InetAddress &ra,InetAddress::IpScope s) : reporter(r),receivedOnLocalSocket(rol),reporterPhysicalAddress(ra),scope(s) {}
  66. ZT_ALWAYS_INLINE unsigned long hashCode() const { return ((unsigned long)reporter.toInt() + (unsigned long)receivedOnLocalSocket + (unsigned long)scope); }
  67. ZT_ALWAYS_INLINE bool operator==(const PhySurfaceKey &k) const { return ((reporter == k.reporter)&&(receivedOnLocalSocket == k.receivedOnLocalSocket)&&(reporterPhysicalAddress == k.reporterPhysicalAddress)&&(scope == k.scope)); }
  68. ZT_ALWAYS_INLINE bool operator!=(const PhySurfaceKey &k) const { return (!(*this == k)); }
  69. };
  70. struct PhySurfaceEntry
  71. {
  72. InetAddress mySurface;
  73. uint64_t ts;
  74. bool trusted;
  75. ZT_ALWAYS_INLINE PhySurfaceEntry() : mySurface(),ts(0),trusted(false) {}
  76. ZT_ALWAYS_INLINE PhySurfaceEntry(const InetAddress &a,const uint64_t t) : mySurface(a),ts(t),trusted(false) {}
  77. };
  78. const RuntimeEnvironment *RR;
  79. Hashtable< PhySurfaceKey,PhySurfaceEntry > _phy;
  80. Mutex _phy_l;
  81. };
  82. } // namespace ZeroTier
  83. #endif