NeighborDiscovery.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_NEIGHBORDISCOVERY_HPP
  27. #define ZT_NEIGHBORDISCOVERY_HPP
  28. #include "../node/Hashtable.hpp"
  29. #include "../node/MAC.hpp"
  30. #include "../node/InetAddress.hpp"
  31. #define ZT_ND_QUERY_INTERVAL 2000
  32. #define ZT_ND_QUERY_MAX_TTL 5000
  33. #define ZT_ND_EXPIRE 600000
  34. namespace ZeroTier {
  35. class NeighborDiscovery
  36. {
  37. public:
  38. NeighborDiscovery();
  39. /**
  40. * Set a local IP entry that we should respond to Neighbor Requests withPrefix64k
  41. *
  42. * @param mac Our local MAC address
  43. * @param ip Our IPv6 address
  44. */
  45. void addLocal(const sockaddr_storage &address, const MAC &mac);
  46. /**
  47. * Delete a local IP entry or cached Neighbor entry
  48. *
  49. * @param address IPv6 address to remove
  50. */
  51. void remove(const sockaddr_storage &address);
  52. sockaddr_storage processIncomingND(const uint8_t *nd, unsigned int len, const sockaddr_storage &localIp, uint8_t *response, unsigned int &responseLen, MAC &responseDest);
  53. MAC query(const MAC &localMac, const sockaddr_storage &localIp, const sockaddr_storage &targetIp, uint8_t *query, unsigned int &queryLen, MAC &queryDest);
  54. private:
  55. struct _NDEntry
  56. {
  57. _NDEntry() : lastQuerySent(0), lastResponseReceived(0), mac(), local(false) {}
  58. uint64_t lastQuerySent;
  59. uint64_t lastResponseReceived;
  60. MAC mac;
  61. bool local;
  62. };
  63. Hashtable<InetAddress, _NDEntry> _cache;
  64. uint64_t _lastCleaned;
  65. };
  66. } // namespace ZeroTier
  67. #endif