NeighborDiscovery.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/MAC.hpp"
  17. #include "../node/InetAddress.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. {
  24. public:
  25. NeighborDiscovery();
  26. /**
  27. * Set a local IP entry that we should respond to Neighbor Requests withPrefix64k
  28. *
  29. * @param mac Our local MAC address
  30. * @param ip Our IPv6 address
  31. */
  32. void addLocal(const sockaddr_storage &address, const MAC &mac);
  33. /**
  34. * Delete a local IP entry or cached Neighbor entry
  35. *
  36. * @param address IPv6 address to remove
  37. */
  38. void remove(const sockaddr_storage &address);
  39. sockaddr_storage processIncomingND(const uint8_t *nd, unsigned int len, const sockaddr_storage &localIp, uint8_t *response, unsigned int &responseLen, MAC &responseDest);
  40. MAC query(const MAC &localMac, const sockaddr_storage &localIp, const sockaddr_storage &targetIp, uint8_t *query, unsigned int &queryLen, MAC &queryDest);
  41. private:
  42. struct _NDEntry
  43. {
  44. _NDEntry() : lastQuerySent(0), lastResponseReceived(0), mac(), local(false) {}
  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