LinuxEthernetTap.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifdef __SYNOLOGY__
  48. bool addIpSyn(std::vector<InetAddress> ips);
  49. #endif
  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. int _fd;
  68. int _shutdownSignalPipe[2];
  69. volatile bool _enabled;
  70. };
  71. } // namespace ZeroTier
  72. #endif