UPNPClient.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_USE_MINIUPNPC
  28. // Uncomment to dump debug messages
  29. //#define ZT_UPNP_TRACE 1
  30. // Uncomment to build a main() for ad-hoc testing
  31. //#define ZT_UPNP_TEST 1
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "../node/Utils.hpp"
  36. #include "UPNPClient.hpp"
  37. #include <miniupnpc/miniupnpc.h>
  38. #include <miniupnpc/upnpcommands.h>
  39. namespace ZeroTier {
  40. class UPNPClientImpl
  41. {
  42. public:
  43. UPNPClientImpl(int localUdpPortToMap) :
  44. run(true),
  45. localPort(localUdpPortToMap)
  46. {
  47. }
  48. void threadMain()
  49. throw()
  50. {
  51. char lanaddr[4096];
  52. char externalip[4096]; // no range checking? so make these buffers larger than any UDP packet a uPnP server could send us as a precaution :P
  53. char inport[16];
  54. char outport[16];
  55. struct UPNPUrls urls;
  56. struct IGDdatas data;
  57. #ifdef ZT_UPNP_TRACE
  58. fprintf(stderr,"UPNPClient: started for UDP port %d"ZT_EOL_S,localPort);
  59. #endif
  60. unsigned int tryPortStart = 0;
  61. Utils::getSecureRandom(&tryPortStart,sizeof(tryPortStart));
  62. tryPortStart = (tryPortStart % (65535 - 1025)) + 1025;
  63. while (run) {
  64. {
  65. int upnpError = 0;
  66. UPNPDev *devlist = upnpDiscover(2000,(const char *)0,(const char *)0,0,0,&upnpError);
  67. if (devlist) {
  68. #ifdef ZT_UPNP_TRACE
  69. {
  70. UPNPDev *dev = devlist;
  71. while (dev) {
  72. fprintf(stderr,"UPNPClient: found device at URL '%s': %s"ZT_EOL_S,dev->descURL,dev->st);
  73. dev = dev->pNext;
  74. }
  75. }
  76. #endif
  77. memset(lanaddr,0,sizeof(lanaddr));
  78. memset(externalip,0,sizeof(externalip));
  79. memset(&urls,0,sizeof(urls));
  80. memset(&data,0,sizeof(data));
  81. Utils::snprintf(inport,sizeof(inport),"%d",localPort);
  82. if ((UPNP_GetValidIGD(devlist,&urls,&data,lanaddr,sizeof(lanaddr)))&&(lanaddr[0])) {
  83. #ifdef ZT_UPNP_TRACE
  84. fprintf(stderr,"UPNPClient: my LAN IP address: %s"ZT_EOL_S,lanaddr);
  85. #endif
  86. if ((UPNP_GetExternalIPAddress(urls.controlURL,data.first.servicetype,externalip) == UPNPCOMMAND_SUCCESS)&&(externalip[0])) {
  87. #ifdef ZT_UPNP_TRACE
  88. fprintf(stderr,"UPNPClient: my external IP address: %s"ZT_EOL_S,externalip);
  89. #endif
  90. for(int tries=0;tries<64;++tries) {
  91. int tryPort = (int)tryPortStart + tries;
  92. if (tryPort >= 65535)
  93. tryPort = (tryPort - 65535) + 1025;
  94. Utils::snprintf(outport,sizeof(outport),"%u",tryPort);
  95. int mapResult = 0;
  96. if ((mapResult = UPNP_AddPortMapping(urls.controlURL,data.first.servicetype,outport,inport,lanaddr,"ZeroTier","UDP",(const char *)0,ZT_UPNP_LEASE_DURATION)) == UPNPCOMMAND_SUCCESS) {
  97. #ifdef ZT_UPNP_TRACE
  98. fprintf(stderr,"UPNPClient: reserved external port: %s"ZT_EOL_S,outport);
  99. #endif
  100. {
  101. Mutex::Lock sl(surface_l);
  102. surface.clear();
  103. InetAddress tmp(externalip);
  104. tmp.setPort(tryPort);
  105. surface.push_back(tmp);
  106. }
  107. break;
  108. } else {
  109. #ifdef ZT_UPNP_TRACE
  110. fprintf(stderr,"UPNPClient: UPNP_AddAnyPortMapping(%s) failed: %d"ZT_EOL_S,outport,mapResult);
  111. #endif
  112. Thread::sleep(1000);
  113. }
  114. }
  115. } else {
  116. #ifdef ZT_UPNP_TRACE
  117. fprintf(stderr,"UPNPClient: UPNP_GetExternalIPAddress failed"ZT_EOL_S);
  118. #endif
  119. }
  120. } else {
  121. #ifdef ZT_UPNP_TRACE
  122. fprintf(stderr,"UPNPClient: UPNP_GetValidIGD failed"ZT_EOL_S);
  123. #endif
  124. }
  125. freeUPNPDevlist(devlist);
  126. } else {
  127. #ifdef ZT_UPNP_TRACE
  128. fprintf(stderr,"UPNPClient: upnpDiscover error code: %d"ZT_EOL_S,upnpError);
  129. #endif
  130. }
  131. }
  132. #ifdef ZT_UPNP_TRACE
  133. fprintf(stderr,"UPNPClient: rescanning in %d ms"ZT_EOL_S,ZT_UPNP_CLIENT_REFRESH_DELAY);
  134. #endif
  135. Thread::sleep(ZT_UPNP_CLIENT_REFRESH_DELAY);
  136. }
  137. delete this;
  138. }
  139. volatile bool run;
  140. int localPort;
  141. Mutex surface_l;
  142. std::vector<InetAddress> surface;
  143. };
  144. UPNPClient::UPNPClient(int localUdpPortToMap)
  145. {
  146. _impl = new UPNPClientImpl(localUdpPortToMap);
  147. Thread::start(_impl);
  148. }
  149. UPNPClient::~UPNPClient()
  150. {
  151. _impl->run = false;
  152. }
  153. std::vector<InetAddress> UPNPClient::get() const
  154. {
  155. Mutex::Lock _l(_impl->surface_l);
  156. return _impl->surface;
  157. }
  158. } // namespace ZeroTier
  159. #ifdef ZT_UPNP_TEST
  160. int main(int argc,char **argv)
  161. {
  162. ZeroTier::UPNPClient *client = new ZeroTier::UPNPClient(12345);
  163. ZeroTier::Thread::sleep(0xffffffff); // wait forever
  164. return 0;
  165. }
  166. #endif
  167. #endif // ZT_USE_MINIUPNPC