EthernetTap.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: 2023-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_ETHERNETTAP_HPP
  14. #define ZT_ETHERNETTAP_HPP
  15. #include "../node/Constants.hpp"
  16. #include "../node/MAC.hpp"
  17. #include "../node/InetAddress.hpp"
  18. #include "../node/MulticastGroup.hpp"
  19. #include "ManagedRoute.hpp"
  20. #include <string>
  21. #include <memory>
  22. #include <vector>
  23. #include <mutex>
  24. #include <map>
  25. namespace ZeroTier {
  26. class EthernetTap
  27. {
  28. public:
  29. static std::shared_ptr<EthernetTap> newInstance(
  30. const char *tapDeviceType, // OS-specific, NULL for default
  31. const char *homePath,
  32. const MAC &mac,
  33. unsigned int mtu,
  34. unsigned int metric,
  35. uint64_t nwid,
  36. const char *friendlyName,
  37. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  38. void *arg);
  39. EthernetTap();
  40. virtual ~EthernetTap();
  41. virtual void setEnabled(bool en) = 0;
  42. virtual bool enabled() const = 0;
  43. virtual bool addIp(const InetAddress &ip) = 0;
  44. virtual bool addIps(std::vector<InetAddress> ips); // uses addIp() unless overridden
  45. virtual bool removeIp(const InetAddress &ip) = 0;
  46. virtual std::vector<InetAddress> ips() const = 0;
  47. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len) = 0;
  48. virtual std::string deviceName() const = 0;
  49. virtual std::string routingDeviceName() const;
  50. virtual void setFriendlyName(const char *friendlyName) = 0;
  51. virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed) = 0;
  52. virtual void setMtu(unsigned int mtu) = 0;
  53. ZT_ALWAYS_INLINE int addRoute(const InetAddress &target,const InetAddress &via,const unsigned int metric)
  54. {
  55. const std::string dn(this->routingDeviceName());
  56. const char *const dnp = (dn.length() > 0) ? dn.c_str() : (const char *)0;
  57. std::lock_guard<std::mutex> l(_managedRoutes_l);
  58. _managedRoutes[std::pair<InetAddress,unsigned int>(target,metric)] = std::shared_ptr<ManagedRoute>(new ManagedRoute(target,via,dnp));
  59. return 0;
  60. }
  61. ZT_ALWAYS_INLINE int removeRoute(const InetAddress &target,const InetAddress &via,const unsigned int metric)
  62. {
  63. std::lock_guard<std::mutex> l(_managedRoutes_l);
  64. _managedRoutes.erase(std::pair<InetAddress,unsigned int>(target,metric));
  65. return 0;
  66. }
  67. ZT_ALWAYS_INLINE int syncRoutes()
  68. {
  69. std::lock_guard<std::mutex> l(_managedRoutes_l);
  70. for(auto r=_managedRoutes.begin();r!=_managedRoutes.end();++r) {
  71. r->second->sync();
  72. }
  73. return 0;
  74. }
  75. private:
  76. std::map< std::pair<InetAddress,unsigned int>,std::shared_ptr<ManagedRoute> > _managedRoutes;
  77. std::mutex _managedRoutes_l;
  78. };
  79. } // namespace ZeroTier
  80. #endif