NeighborDiscovery.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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: 2025-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 "../core/Containers.hpp"
  16. #include "../core/MAC.hpp"
  17. #include "../core/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. {}
  46. uint64_t lastQuerySent;
  47. uint64_t lastResponseReceived;
  48. MAC mac;
  49. bool local;
  50. };
  51. Map< InetAddress, _NDEntry > _cache;
  52. uint64_t _lastCleaned;
  53. };
  54. } // namespace ZeroTier
  55. #endif