NeighborDiscovery.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c)2019 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: 2026-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_NEIGHBORDISCOVERY_HPP
  14. #define ZT_NEIGHBORDISCOVERY_HPP
  15. #include "../node/Hashtable.hpp"
  16. #include "../node/InetAddress.hpp"
  17. #include "../node/MAC.hpp"
  18. #define ZT_ND_QUERY_INTERVAL 2000
  19. #define ZT_ND_QUERY_MAX_TTL 5000
  20. #define ZT_ND_EXPIRE 600000
  21. namespace ZeroTier {
  22. class NeighborDiscovery {
  23. public:
  24. NeighborDiscovery();
  25. /**
  26. * Set a local IP entry that we should respond to Neighbor Requests withPrefix64k
  27. *
  28. * @param mac Our local MAC address
  29. * @param ip Our IPv6 address
  30. */
  31. void addLocal(const sockaddr_storage& address, const MAC& mac);
  32. /**
  33. * Delete a local IP entry or cached Neighbor entry
  34. *
  35. * @param address IPv6 address to remove
  36. */
  37. void remove(const sockaddr_storage& address);
  38. sockaddr_storage processIncomingND(const uint8_t* nd, unsigned int len, const sockaddr_storage& localIp, uint8_t* response, unsigned int& responseLen, MAC& responseDest);
  39. MAC query(const MAC& localMac, const sockaddr_storage& localIp, const sockaddr_storage& targetIp, uint8_t* query, unsigned int& queryLen, MAC& queryDest);
  40. private:
  41. struct _NDEntry {
  42. _NDEntry() : lastQuerySent(0), lastResponseReceived(0), mac(), local(false)
  43. {
  44. }
  45. uint64_t lastQuerySent;
  46. uint64_t lastResponseReceived;
  47. MAC mac;
  48. bool local;
  49. };
  50. Hashtable<InetAddress, _NDEntry> _cache;
  51. uint64_t _lastCleaned;
  52. };
  53. } // namespace ZeroTier
  54. #endif