BSDEthernetTap.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_BSDETHERNETTAP_HPP
  27. #define ZT_BSDETHERNETTAP_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string>
  31. #include <vector>
  32. #include <stdexcept>
  33. #include "../node/Constants.hpp"
  34. #include "../node/MulticastGroup.hpp"
  35. #include "../node/MAC.hpp"
  36. #include "Thread.hpp"
  37. namespace ZeroTier {
  38. class BSDEthernetTap
  39. {
  40. public:
  41. BSDEthernetTap(
  42. const char *homePath,
  43. const MAC &mac,
  44. unsigned int mtu,
  45. unsigned int metric,
  46. uint64_t nwid,
  47. const char *friendlyName,
  48. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  49. void *arg);
  50. ~BSDEthernetTap();
  51. void setEnabled(bool en);
  52. bool enabled() const;
  53. bool addIp(const InetAddress &ip);
  54. bool removeIp(const InetAddress &ip);
  55. std::vector<InetAddress> ips() const;
  56. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  57. std::string deviceName() const;
  58. void setFriendlyName(const char *friendlyName);
  59. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  60. void threadMain()
  61. throw();
  62. private:
  63. void (*_handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  64. void *_arg;
  65. uint64_t _nwid;
  66. Thread _thread;
  67. std::string _dev;
  68. std::vector<MulticastGroup> _multicastGroups;
  69. unsigned int _mtu;
  70. unsigned int _metric;
  71. int _fd;
  72. int _shutdownSignalPipe[2];
  73. volatile bool _enabled;
  74. };
  75. } // namespace ZeroTier
  76. #endif