NetconEthernetTap.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_NETCONETHERNETTAP_HPP
  28. #define ZT_NETCONETHERNETTAP_HPP
  29. #ifdef ZT_ENABLE_NETCON
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string>
  33. #include <vector>
  34. #include <stdexcept>
  35. #include "../node/Constants.hpp"
  36. #include "../node/MulticastGroup.hpp"
  37. #include "../node/Mutex.hpp"
  38. #include "../node/InetAddress.hpp"
  39. #include "../osdep/Thread.hpp"
  40. #include "../osdep/Phy.hpp"
  41. namespace ZeroTier {
  42. class NetconEthernetTap;
  43. /**
  44. * Network Containers instance -- emulates an Ethernet tap device as far as OneService knows
  45. */
  46. class NetconEthernetTap
  47. {
  48. friend class Phy<NetconEthernetTap *>;
  49. public:
  50. NetconEthernetTap(
  51. const char *homePath,
  52. const MAC &mac,
  53. unsigned int mtu,
  54. unsigned int metric,
  55. uint64_t nwid,
  56. const char *friendlyName,
  57. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  58. void *arg);
  59. ~NetconEthernetTap();
  60. void setEnabled(bool en);
  61. bool enabled() const;
  62. bool addIp(const InetAddress &ip);
  63. bool removeIp(const InetAddress &ip);
  64. std::vector<InetAddress> ips() const;
  65. void put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len);
  66. std::string deviceName() const;
  67. void setFriendlyName(const char *friendlyName);
  68. void scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed);
  69. void threadMain()
  70. throw();
  71. private:
  72. void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len);
  73. void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success);
  74. void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from);
  75. void phyOnTcpClose(PhySocket *sock,void **uptr);
  76. void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  77. void phyOnTcpWritable(PhySocket *sock,void **uptr);
  78. void phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN);
  79. void phyOnUnixClose(PhySocket *sock,void **uptr);
  80. void phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len);
  81. void phyOnUnixWritable(PhySocket *sock,void **uptr);
  82. void (*_handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int);
  83. void *_arg;
  84. Phy<NetconEthernetTap *> _phy;
  85. PhySocket *_unixListenSocket;
  86. uint64_t _nwid;
  87. Thread _thread;
  88. std::string _homePath;
  89. std::string _dev; // path to Unix domain socket
  90. std::vector<MulticastGroup> _lastMulticastGroupList;
  91. Mutex _lastMulticastGroupList_m;
  92. std::vector<InetAddress> _ips;
  93. Mutex _ips_m;
  94. unsigned int _mtu;
  95. volatile bool _enabled;
  96. volatile bool _run;
  97. };
  98. } // namespace ZeroTier
  99. #endif // ZT_ENABLE_NETCON
  100. #endif