NetconEthernetTap.cpp 4.2 KB

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