UPNPClient.cpp 5.4 KB

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