NetconEthernetTap.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #ifdef ZT_ENABLE_NETCON
  28. #include <algorithm>
  29. #include <utility>
  30. #include "NetconEthernetTap.hpp"
  31. #include "../node/Utils.hpp"
  32. #include "../osdep/OSUtils.hpp"
  33. #include "../osdep/Phy.hpp"
  34. namespace ZeroTier {
  35. NetconEthernetTap::NetconEthernetTap(
  36. const char *homePath,
  37. const MAC &mac,
  38. unsigned int mtu,
  39. unsigned int metric,
  40. uint64_t nwid,
  41. const char *friendlyName,
  42. void (*handler)(void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  43. void *arg) :
  44. _phy(this,false,true),
  45. _unixListenSocket((PhySocket *)0),
  46. _handler(handler),
  47. _arg(arg),
  48. _nwid(nwid),
  49. _homePath(homePath),
  50. _mtu(mtu),
  51. _enabled(true),
  52. _run(true)
  53. {
  54. char sockPath[4096];
  55. Utils::snprintf(sockPath,sizeof(sockPath),"/tmp/.ztnc_%.16llx",(unsigned long long)nwid);
  56. _dev = sockPath;
  57. _unixListenSocket = _phy.unixListen(sockPath,(void *)this);
  58. if (!_unixListenSocket)
  59. throw std::runtime_error(std::string("unable to bind to ")+sockPath);
  60. _thread = Thread::start(this);
  61. }
  62. NetconEthernetTap::~NetconEthernetTap()
  63. {
  64. _run = false;
  65. _phy.whack();
  66. _phy.whack();
  67. Thread::join(_thread);
  68. _phy.close(_unixListenSocket,false);
  69. }
  70. void NetconEthernetTap::setEnabled(bool en)
  71. {
  72. _enabled = en;
  73. }
  74. bool NetconEthernetTap::enabled() const
  75. {
  76. return _enabled;
  77. }
  78. bool NetconEthernetTap::addIp(const InetAddress &ip)
  79. {
  80. Mutex::Lock _l(_ips_m);
  81. if (std::find(_ips.begin(),_ips.end(),ip) == _ips.end()) {
  82. _ips.push_back(ip);
  83. std::sort(_ips.begin(),_ips.end());
  84. // TODO: alloc IP in LWIP
  85. }
  86. }
  87. bool NetconEthernetTap::removeIp(const InetAddress &ip)
  88. {
  89. Mutex::Lock _l(_ips_m);
  90. std::vector<InetAddress>::iterator i(std::find(_ips.begin(),_ips.end(),ip));
  91. if (i == _ips.end())
  92. return false;
  93. _ips.erase(i);
  94. // TODO: dealloc IP from LWIP
  95. return true;
  96. }
  97. std::vector<InetAddress> NetconEthernetTap::ips() const
  98. {
  99. Mutex::Lock _l(_ips_m);
  100. return _ips;
  101. }
  102. void NetconEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  103. {
  104. if (!_enabled)
  105. return;
  106. }
  107. std::string NetconEthernetTap::deviceName() const
  108. {
  109. return _dev;
  110. }
  111. void NetconEthernetTap::setFriendlyName(const char *friendlyName)
  112. {
  113. }
  114. void NetconEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  115. {
  116. // TODO: get multicast subscriptions from LWIP
  117. }
  118. void NetconEthernetTap::threadMain()
  119. throw()
  120. {
  121. while (_run) {
  122. unsigned long pollTimeout = 500;
  123. // TODO: compute timeout from LWIP stuff
  124. _phy.poll(pollTimeout);
  125. }
  126. // TODO: cleanup -- destroy LWIP state, kill any clients, unload .so, etc.
  127. }
  128. // Unused -- no UDP or TCP from this thread/Phy<>
  129. void NetconEthernetTap::phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len) {}
  130. void NetconEthernetTap::phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) {}
  131. void NetconEthernetTap::phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) {}
  132. void NetconEthernetTap::phyOnTcpClose(PhySocket *sock,void **uptr) {}
  133. void NetconEthernetTap::phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) {}
  134. void NetconEthernetTap::phyOnTcpWritable(PhySocket *sock,void **uptr) {}
  135. void NetconEthernetTap::phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
  136. {
  137. }
  138. void NetconEthernetTap::phyOnUnixClose(PhySocket *sock,void **uptr)
  139. {
  140. }
  141. void NetconEthernetTap::phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  142. {
  143. }
  144. void NetconEthernetTap::phyOnUnixWritable(PhySocket *sock,void **uptr)
  145. {
  146. }
  147. } // namespace ZeroTier
  148. #endif // ZT_ENABLE_NETCON