Demarc.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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; // port already bound
  88. UdpSocket *v4 = (UdpSocket *)0;
  89. try {
  90. DemarcPortObj *v4r = &(_ports[(Port)v4p]);
  91. v4r->port = (Port)v4p;
  92. v4r->parent = this;
  93. v4r->obj = v4 = new UdpSocket(false,localPort,false,&Demarc::_CBudpSocketPacketHandler,v4r);
  94. v4r->type = PORT_TYPE_UDP_SOCKET_V4;
  95. } catch ( ... ) {
  96. _ports.erase((Port)v4p);
  97. v4 = (UdpSocket *)0;
  98. }
  99. UdpSocket *v6 = (UdpSocket *)0;
  100. try {
  101. DemarcPortObj *v6r = &(_ports[(Port)v6p]);
  102. v6r->port = (Port)v6p;
  103. v6r->parent = this;
  104. v6r->obj = v6 = new UdpSocket(false,localPort,true,&Demarc::_CBudpSocketPacketHandler,v6r);
  105. v6r->type = PORT_TYPE_UDP_SOCKET_V6;
  106. } catch ( ... ) {
  107. _ports.erase((Port)v6p);
  108. v6 = (UdpSocket *)0;
  109. }
  110. return ((v4)||(v6));
  111. }
  112. Demarc::Port Demarc::pick(const InetAddress &to) const
  113. throw()
  114. {
  115. Mutex::Lock _l(_ports_m);
  116. try {
  117. std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
  118. for(std::map< Port,DemarcPortObj >::const_iterator pe(_ports.begin());pe!=_ports.end();++pe) {
  119. switch (pe->second.type) {
  120. case PORT_TYPE_UDP_SOCKET_V4:
  121. if (to.isV4())
  122. possibilities.push_back(pe);
  123. break;
  124. case PORT_TYPE_UDP_SOCKET_V6:
  125. if (to.isV6())
  126. possibilities.push_back(pe);
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. if (possibilities.size())
  133. return possibilities[_r->prng->next32() % possibilities.size()]->first;
  134. else return NULL_PORT;
  135. } catch ( ... ) {
  136. return NULL_PORT;
  137. }
  138. }
  139. Demarc::Port Demarc::send(Demarc::Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
  140. throw()
  141. {
  142. _ports_m.lock();
  143. std::map< Port,DemarcPortObj >::const_iterator pe(_ports.find(fromPort));
  144. if (pe == _ports.end()) {
  145. try {
  146. std::vector< std::map< Port,DemarcPortObj >::const_iterator > possibilities;
  147. for(pe=_ports.begin();pe!=_ports.end();++pe) {
  148. switch (pe->second.type) {
  149. case PORT_TYPE_UDP_SOCKET_V4:
  150. if (to.isV4())
  151. possibilities.push_back(pe);
  152. break;
  153. case PORT_TYPE_UDP_SOCKET_V6:
  154. if (to.isV6())
  155. possibilities.push_back(pe);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. if (possibilities.size())
  162. pe = possibilities[_r->prng->next32() % possibilities.size()];
  163. else {
  164. _ports_m.unlock();
  165. return NULL_PORT;
  166. }
  167. } catch ( ... ) {
  168. _ports_m.unlock();
  169. return NULL_PORT;
  170. }
  171. }
  172. switch (pe->second.type) {
  173. case PORT_TYPE_UDP_SOCKET_V4:
  174. case PORT_TYPE_UDP_SOCKET_V6:
  175. _ports_m.unlock();
  176. if (((UdpSocket *)pe->second.obj)->send(to,data,len,hopLimit))
  177. return pe->first;
  178. return NULL_PORT;
  179. default:
  180. break;
  181. }
  182. _ports_m.unlock();
  183. return NULL_PORT;
  184. }
  185. void Demarc::_CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len)
  186. {
  187. ((DemarcPortObj *)arg)->parent->_r->sw->onRemotePacket(((DemarcPortObj *)arg)->port,from,Buffer<4096>(data,len));
  188. }
  189. } // namespace ZeroTier