BSDEthernetTap.hpp 2.3 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_BSDETHERNETTAP_HPP
  14. #define ZT_BSDETHERNETTAP_HPP
  15. #include "../node/Constants.hpp"
  16. #include "../node/MAC.hpp"
  17. #include "../node/MulticastGroup.hpp"
  18. #include "EthernetTap.hpp"
  19. #include "Thread.hpp"
  20. #include <stdexcept>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string>
  24. #include <thread>
  25. #include <vector>
  26. namespace ZeroTier {
  27. class BSDEthernetTap : public EthernetTap {
  28. public:
  29. BSDEthernetTap(
  30. const char* homePath,
  31. unsigned int concurrency,
  32. bool pinning,
  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 ~BSDEthernetTap();
  41. virtual void setEnabled(bool en);
  42. virtual bool enabled() const;
  43. virtual bool addIp(const InetAddress& ip);
  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. {
  53. }
  54. void threadMain() throw();
  55. private:
  56. void (*_handler)(void*, void*, uint64_t, const MAC&, const MAC&, unsigned int, unsigned int, const void*, unsigned int);
  57. void* _arg;
  58. unsigned int _concurrency;
  59. bool _pinning;
  60. uint64_t _nwid;
  61. Thread _thread;
  62. std::string _dev;
  63. std::vector<MulticastGroup> _multicastGroups;
  64. unsigned int _mtu;
  65. unsigned int _metric;
  66. int _fd;
  67. int _shutdownSignalPipe[2];
  68. volatile bool _enabled;
  69. mutable std::vector<InetAddress> _ifaddrs;
  70. mutable uint64_t _lastIfAddrsUpdate;
  71. std::vector<std::thread> _rxThreads;
  72. };
  73. } // namespace ZeroTier
  74. #endif