Demarc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. #include <vector>
  28. #include "Demarc.hpp"
  29. #include "RuntimeEnvironment.hpp"
  30. #include "Logger.hpp"
  31. #include "UdpSocket.hpp"
  32. #include "InetAddress.hpp"
  33. #include "Switch.hpp"
  34. #include "Buffer.hpp"
  35. namespace ZeroTier {
  36. const Demarc::Port Demarc::ANY_PORT;
  37. const Demarc::Port Demarc::NULL_PORT;
  38. Demarc::Demarc(const RuntimeEnvironment *renv) :
  39. _r(renv)
  40. {
  41. }
  42. Demarc::~Demarc()
  43. {
  44. for(std::map< Port,DemarcPortObj >::iterator pe(_ports.begin());pe!=_ports.end();++pe) {
  45. switch (pe->second.type) {
  46. case PORT_TYPE_UDP_SOCKET_V4:
  47. case PORT_TYPE_UDP_SOCKET_V6:
  48. delete ((UdpSocket *)pe->second.obj);
  49. break;
  50. case PORT_TYPE_LOCAL_ETHERNET:
  51. case PORT_TYPE_RELAY_TUNNEL:
  52. break;
  53. }
  54. }
  55. }
  56. std::string Demarc::describe(Demarc::Port p)
  57. throw()
  58. {
  59. char buf[64];
  60. switch ((DemarcPortType)(((uint64_t)p) >> 60)) {
  61. case PORT_TYPE_UDP_SOCKET_V4:
  62. sprintf(buf,"udp/4/%d",(int)((uint64_t)p & 0xffff));
  63. return std::string(buf);
  64. case PORT_TYPE_UDP_SOCKET_V6:
  65. sprintf(buf,"udp/6/%d",(int)((uint64_t)p & 0xffff));
  66. return std::string(buf);
  67. case PORT_TYPE_LOCAL_ETHERNET:
  68. return std::string("ethernet");
  69. case PORT_TYPE_RELAY_TUNNEL:
  70. return std::string("relay");
  71. }
  72. return std::string("(null)");
  73. }
  74. bool Demarc::has(Port p) const
  75. throw()
  76. {
  77. Mutex::Lock _l(_ports_m);
  78. return (_ports.count(p));
  79. }
  80. bool Demarc::bindLocalUdp(unsigned int localPort)
  81. throw()
  82. {
  83. Mutex::Lock _l(_ports_m);
  84. uint64_t v4p = ((uint64_t)PORT_TYPE_UDP_SOCKET_V4 << 60) | (uint64_t)localPort;
  85. uint64_t v6p = ((uint64_t)PORT_TYPE_UDP_SOCKET_V6 << 60) | (uint64_t)localPort;
  86. if ((_ports.count((Port)v4p))||(_ports.count((Port)v6p)))
  87. return true;
  88. UdpSocket *v4;
  89. try {
  90. DemarcPortObj *v4r = &(_ports[(Port)v4p]);
  91. v4r->port = (Port)v4p;
  92. v4r->parent = this;
  93. v4r->obj = v4 = new UdpSocket(localPort,false,&Demarc::_CBudpSocketPacketHandler,v4r);
  94. v4r->type = PORT_TYPE_UDP_SOCKET_V4;
  95. } catch ( ... ) {
  96. _ports.erase((Port)v4p);
  97. return false;
  98. }
  99. UdpSocket *v6;
  100. try {
  101. DemarcPortObj *v6r = &(_ports[(Port)v6p]);
  102. v6r->port = (Port)v6p;
  103. v6r->parent = this;
  104. v6r->obj = v6 = new UdpSocket(localPort,true,&Demarc::_CBudpSocketPacketHandler,v6r);
  105. v6r->type = PORT_TYPE_UDP_SOCKET_V6;
  106. } catch ( ... ) {
  107. delete v4;
  108. _ports.erase((Port)v4p);
  109. _ports.erase((Port)v6p);
  110. return false;
  111. }
  112. return true;
  113. }
  114. Demarc::Port Demarc::pick(const InetAddress &to) const
  115. throw()
  116. {
  117. Mutex::Lock _l(_ports_m);
  118. try {
  119. std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
  120. for(std::map< Port,DemarcPortObj >::const_iterator pe(_ports.begin());pe!=_ports.end();++pe) {
  121. switch (pe->second.type) {
  122. case PORT_TYPE_UDP_SOCKET_V4:
  123. if (to.isV4())
  124. possibilities.push_back(pe);
  125. break;
  126. case PORT_TYPE_UDP_SOCKET_V6:
  127. if (to.isV6())
  128. possibilities.push_back(pe);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. if (possibilities.size())
  135. return possibilities[Utils::randomInt<unsigned int>() % possibilities.size()]->first;
  136. else return NULL_PORT;
  137. } catch ( ... ) {
  138. return NULL_PORT;
  139. }
  140. }
  141. Demarc::Port Demarc::send(Demarc::Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
  142. throw()
  143. {
  144. _ports_m.lock();
  145. std::map< Port,DemarcPortObj >::const_iterator pe(_ports.find(fromPort));
  146. if (pe == _ports.end()) {
  147. try {
  148. std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
  149. for(pe=_ports.begin();pe!=_ports.end();++pe) {
  150. switch (pe->second.type) {
  151. case PORT_TYPE_UDP_SOCKET_V4:
  152. if (to.isV4())
  153. possibilities.push_back(pe);
  154. break;
  155. case PORT_TYPE_UDP_SOCKET_V6:
  156. if (to.isV6())
  157. possibilities.push_back(pe);
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. if (possibilities.size())
  164. pe = possibilities[Utils::randomInt<unsigned int>() % possibilities.size()];
  165. else {
  166. _ports_m.unlock();
  167. return NULL_PORT;
  168. }
  169. } catch ( ... ) {
  170. _ports_m.unlock();
  171. return NULL_PORT;
  172. }
  173. }
  174. switch (pe->second.type) {
  175. case PORT_TYPE_UDP_SOCKET_V4:
  176. case PORT_TYPE_UDP_SOCKET_V6:
  177. _ports_m.unlock();
  178. if (((UdpSocket *)pe->second.obj)->send(to,data,len,hopLimit))
  179. return pe->first;
  180. return NULL_PORT;
  181. default:
  182. break;
  183. }
  184. _ports_m.unlock();
  185. return NULL_PORT;
  186. }
  187. void Demarc::_CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len)
  188. {
  189. ((DemarcPortObj *)arg)->parent->_r->sw->onRemotePacket(((DemarcPortObj *)arg)->port,from,Buffer<4096>(data,len));
  190. }
  191. } // namespace ZeroTier