NeighborDiscovery.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_NEIGHBORDISCOVERY_HPP
  19. #define ZT_NEIGHBORDISCOVERY_HPP
  20. #include "../node/Hashtable.hpp"
  21. #include "../node/MAC.hpp"
  22. #include "../node/InetAddress.hpp"
  23. #define ZT_ND_QUERY_INTERVAL 2000
  24. #define ZT_ND_QUERY_MAX_TTL 5000
  25. #define ZT_ND_EXPIRE 600000
  26. namespace ZeroTier {
  27. class NeighborDiscovery
  28. {
  29. public:
  30. NeighborDiscovery();
  31. /**
  32. * Set a local IP entry that we should respond to Neighbor Requests withPrefix64k
  33. *
  34. * @param mac Our local MAC address
  35. * @param ip Our IPv6 address
  36. */
  37. void addLocal(const sockaddr_storage &address, const MAC &mac);
  38. /**
  39. * Delete a local IP entry or cached Neighbor entry
  40. *
  41. * @param address IPv6 address to remove
  42. */
  43. void remove(const sockaddr_storage &address);
  44. sockaddr_storage processIncomingND(const uint8_t *nd, unsigned int len, const sockaddr_storage &localIp, uint8_t *response, unsigned int &responseLen, MAC &responseDest);
  45. MAC query(const MAC &localMac, const sockaddr_storage &localIp, const sockaddr_storage &targetIp, uint8_t *query, unsigned int &queryLen, MAC &queryDest);
  46. private:
  47. struct _NDEntry
  48. {
  49. _NDEntry() : lastQuerySent(0), lastResponseReceived(0), mac(), local(false) {}
  50. uint64_t lastQuerySent;
  51. uint64_t lastResponseReceived;
  52. MAC mac;
  53. bool local;
  54. };
  55. Hashtable<InetAddress, _NDEntry> _cache;
  56. uint64_t _lastCleaned;
  57. };
  58. } // namespace ZeroTier
  59. #endif