Demarc.cpp 5.8 KB

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