Demarc.cpp 5.7 KB

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