OSXEthernetTap.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef ZT_OSXETHERNETTAP_HPP
  19. #define ZT_OSXETHERNETTAP_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdexcept>
  23. #include <string>
  24. #include <vector>
  25. #include "../node/Constants.hpp"
  26. #include "../node/MAC.hpp"
  27. #include "../node/InetAddress.hpp"
  28. #include "../node/MulticastGroup.hpp"
  29. #include "Thread.hpp"
  30. namespace ZeroTier {
  31. /**
  32. * OSX Ethernet tap using ZeroTier kernel extension zt# devices
  33. */
  34. class OSXEthernetTap
  35. {
  36. public:
  37. OSXEthernetTap(
  38. const char *homePath,
  39. const MAC &mac,
  40. unsigned int mtu,
  41. unsigned int metric,
  42. uint64_t nwid,
  43. const char *friendlyName,
  44. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  45. void *arg);
  46. ~OSXEthernetTap();
  47. void setEnabled(bool en);
  48. bool enabled() const;
  49. bool addIp(const InetAddress &ip);
  50. bool removeIp(const InetAddress &ip);
  51. std::vector<InetAddress> ips() const;
  52. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  53. std::string deviceName() const;
  54. void setFriendlyName(const char *friendlyName);
  55. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  56. void threadMain()
  57. throw();
  58. private:
  59. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  60. void *_arg;
  61. uint64_t _nwid;
  62. Thread _thread;
  63. std::string _homePath;
  64. std::string _dev;
  65. std::vector<MulticastGroup> _multicastGroups;
  66. unsigned int _mtu;
  67. unsigned int _metric;
  68. int _fd;
  69. int _shutdownSignalPipe[2];
  70. volatile bool _enabled;
  71. };
  72. } // namespace ZeroTier
  73. #endif