NeighborDiscovery.hpp 1.6 KB

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