LinuxEthernetTap.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: 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_LINUXETHERNETTAP_HPP
  14. #define ZT_LINUXETHERNETTAP_HPP
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string>
  18. #include <vector>
  19. #include <stdexcept>
  20. #include <atomic>
  21. #include <array>
  22. #include <thread>
  23. #include <mutex>
  24. #include "../node/MulticastGroup.hpp"
  25. #include "EthernetTap.hpp"
  26. namespace ZeroTier {
  27. class LinuxEthernetTap : public EthernetTap
  28. {
  29. public:
  30. LinuxEthernetTap(
  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. virtual ~LinuxEthernetTap();
  40. virtual void setEnabled(bool en);
  41. virtual bool enabled() const;
  42. virtual bool addIp(const InetAddress &ip);
  43. virtual bool addIps(std::vector<InetAddress> ips);
  44. virtual bool removeIp(const InetAddress &ip);
  45. virtual std::vector<InetAddress> ips() const;
  46. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  47. virtual std::string deviceName() const;
  48. virtual void setFriendlyName(const char *friendlyName);
  49. virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  50. virtual void setMtu(unsigned int mtu);
  51. virtual void setDns(const char *domain, const std::vector<InetAddress> &servers) {}
  52. private:
  53. void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  54. void *_arg;
  55. uint64_t _nwid;
  56. MAC _mac;
  57. std::string _homePath;
  58. std::string _dev;
  59. std::vector<MulticastGroup> _multicastGroups;
  60. unsigned int _mtu;
  61. int _fd;
  62. int _shutdownSignalPipe[2];
  63. std::atomic_bool _enabled;
  64. std::atomic_bool _run;
  65. std::thread _tapReaderThread;
  66. };
  67. } // namespace ZeroTier
  68. #endif