LinuxEthernetTap.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include "BlockingQueue.hpp"
  27. namespace ZeroTier {
  28. class LinuxEthernetTap : public EthernetTap
  29. {
  30. public:
  31. LinuxEthernetTap(
  32. const char *homePath,
  33. const MAC &mac,
  34. unsigned int mtu,
  35. unsigned int metric,
  36. uint64_t nwid,
  37. const char *friendlyName,
  38. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  39. void *arg);
  40. virtual ~LinuxEthernetTap();
  41. virtual void setEnabled(bool en);
  42. virtual bool enabled() const;
  43. virtual bool addIp(const InetAddress &ip);
  44. virtual bool addIps(std::vector<InetAddress> ips);
  45. virtual bool removeIp(const InetAddress &ip);
  46. virtual std::vector<InetAddress> ips() const;
  47. virtual void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  48. virtual std::string deviceName() const;
  49. virtual void setFriendlyName(const char *friendlyName);
  50. virtual void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  51. virtual void setMtu(unsigned int mtu);
  52. virtual void setDns(const char *domain, const std::vector<InetAddress> &servers) {}
  53. private:
  54. void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  55. void *_arg;
  56. uint64_t _nwid;
  57. MAC _mac;
  58. std::string _homePath;
  59. std::string _dev;
  60. std::vector<MulticastGroup> _multicastGroups;
  61. unsigned int _mtu;
  62. int _fd;
  63. int _shutdownSignalPipe[2];
  64. std::atomic_bool _enabled;
  65. std::atomic_bool _run;
  66. mutable std::vector<InetAddress> _ifaddrs;
  67. mutable uint64_t _lastIfAddrsUpdate;
  68. std::vector<std::thread> _rxThreads;
  69. };
  70. } // namespace ZeroTier
  71. #endif