2
0

LinuxEthernetTap.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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: 2026-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 "../node/MulticastGroup.hpp"
  16. #include "BlockingQueue.hpp"
  17. #include "EthernetTap.hpp"
  18. #include <array>
  19. #include <atomic>
  20. #include <mutex>
  21. #include <stdexcept>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string>
  25. #include <thread>
  26. #include <vector>
  27. namespace ZeroTier {
  28. class LinuxEthernetTap : public EthernetTap {
  29. public:
  30. LinuxEthernetTap(
  31. const char* homePath,
  32. unsigned int concurrency,
  33. bool pinning,
  34. const MAC& mac,
  35. unsigned int mtu,
  36. unsigned int metric,
  37. uint64_t nwid,
  38. const char* friendlyName,
  39. void (*handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int),
  40. void* arg);
  41. virtual ~LinuxEthernetTap();
  42. virtual void setEnabled(bool en);
  43. virtual bool enabled() const;
  44. virtual bool addIp(const InetAddress& ip);
  45. virtual bool addIps(std::vector<InetAddress> ips);
  46. virtual bool removeIp(const InetAddress& ip);
  47. virtual std::vector<InetAddress> ips() const;
  48. virtual void put(const MAC& from, const MAC& to, unsigned int etherType, const void* data, unsigned int len);
  49. virtual std::string deviceName() const;
  50. virtual void setFriendlyName(const char* friendlyName);
  51. virtual void scanMulticastGroups(std::vector<MulticastGroup>& added, std::vector<MulticastGroup>& removed);
  52. virtual void setMtu(unsigned int mtu);
  53. virtual void setDns(const char* domain, const std::vector<InetAddress>& servers)
  54. {
  55. fprintf(stderr, "WARNING: ignoring call to LinuxEthernetTap::setDns on Linux. This is not implemented yet. See https://github.com/zerotier/ZeroTierOne/issues/2492 for details" ZT_EOL_S);
  56. }
  57. private:
  58. void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
  59. void* _arg;
  60. uint64_t _nwid;
  61. MAC _mac;
  62. std::string _homePath;
  63. std::string _dev;
  64. std::vector<MulticastGroup> _multicastGroups;
  65. unsigned int _mtu;
  66. int _fd;
  67. int _shutdownSignalPipe[2];
  68. std::atomic_bool _enabled;
  69. std::atomic_bool _run;
  70. mutable std::vector<InetAddress> _ifaddrs;
  71. mutable uint64_t _lastIfAddrsUpdate;
  72. std::vector<std::thread> _rxThreads;
  73. };
  74. } // namespace ZeroTier
  75. #endif