LinuxEthernetTap.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_LINUXETHERNETTAP_HPP
  19. #define ZT_LINUXETHERNETTAP_HPP
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string>
  23. #include <vector>
  24. #include <stdexcept>
  25. #include "../node/MulticastGroup.hpp"
  26. #include "Thread.hpp"
  27. namespace ZeroTier {
  28. /**
  29. * Linux Ethernet tap using kernel tun/tap driver
  30. */
  31. class LinuxEthernetTap
  32. {
  33. public:
  34. LinuxEthernetTap(
  35. const char *homePath,
  36. const MAC &mac,
  37. unsigned int mtu,
  38. unsigned int metric,
  39. uint64_t nwid,
  40. const char *friendlyName,
  41. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  42. void *arg);
  43. ~LinuxEthernetTap();
  44. void setEnabled(bool en);
  45. bool enabled() const;
  46. bool addIp(const InetAddress &ip);
  47. bool removeIp(const InetAddress &ip);
  48. std::vector<InetAddress> ips() const;
  49. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  50. std::string deviceName() const;
  51. void setFriendlyName(const char *friendlyName);
  52. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  53. void threadMain()
  54. throw();
  55. private:
  56. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  57. void *_arg;
  58. uint64_t _nwid;
  59. Thread _thread;
  60. std::string _homePath;
  61. std::string _dev;
  62. std::vector<MulticastGroup> _multicastGroups;
  63. unsigned int _mtu;
  64. int _fd;
  65. int _shutdownSignalPipe[2];
  66. volatile bool _enabled;
  67. };
  68. } // namespace ZeroTier
  69. #endif