- E76F8111F2C74A992113F6B9691407F6F14E2D37920516727EDF7F78FDAF7A4E4D42C3378B46C0B5EAB7F45DE7E375ACDEB6AFB9C33725E7081760D15338E281+ A1B78A422161C6F5F3CA51A585D8C3F68E9490AF3F1BA48B7772CBE4D5A9711619707984376F9A51487AEC676F437FBF6DF38309D535DACCB966873F9608E9F1bitcoin/src/net.cpp(14 . 12)(14 . 6)
152 #include <string.h>
153 #endif
154
155 #ifdef USE_UPNP
156 #include <miniupnpc/miniwget.h>
157 #include <miniupnpc/miniupnpc.h>
158 #include <miniupnpc/upnpcommands.h>
159 #include <miniupnpc/upnperrors.h>
160 #endif
161
162 using namespace std;
163 using namespace boost;
(29 . 9)(23 . 6)
165 void ThreadMessageHandler2(void* parg);
166 void ThreadSocketHandler2(void* parg);
167 void ThreadOpenConnections2(void* parg);
168 #ifdef USE_UPNP
169 void ThreadMapPort2(void* parg);
170 #endif
171 void ThreadDNSAddressSeed2(void* parg);
172 bool OpenNetworkConnection(const CAddress& addrConnect);
173
(1110 . 153)(1101 . 6)
175
176
177
178 #ifdef USE_UPNP
179 void ThreadMapPort(void* parg)
180 {
181 IMPLEMENT_RANDOMIZE_STACK(ThreadMapPort(parg));
182 try
183 {
184 vnThreadsRunning[5]++;
185 ThreadMapPort2(parg);
186 vnThreadsRunning[5]--;
187 }
188 catch (std::exception& e) {
189 vnThreadsRunning[5]--;
190 PrintException(&e, "ThreadMapPort()");
191 } catch (...) {
192 vnThreadsRunning[5]--;
193 PrintException(NULL, "ThreadMapPort()");
194 }
195 printf("ThreadMapPort exiting\n");
196 }
197
198 void ThreadMapPort2(void* parg)
199 {
200 printf("ThreadMapPort started\n");
201
202 char port[6];
203 sprintf(port, "%d", GetListenPort());
204
205 const char * multicastif = 0;
206 const char * minissdpdpath = 0;
207 struct UPNPDev * devlist = 0;
208 char lanaddr[64];
209
210 #ifndef UPNPDISCOVER_SUCCESS
211 /* miniupnpc 1.5 */
212 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0);
213 #else
214 /* miniupnpc 1.6 */
215 int error = 0;
216 devlist = upnpDiscover(2000, multicastif, minissdpdpath, 0, 0, &error);
217 #endif
218
219 struct UPNPUrls urls;
220 struct IGDdatas data;
221 int r;
222
223 r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
224 if (r == 1)
225 {
226 if (!addrLocalHost.IsRoutable())
227 {
228 char externalIPAddress[40];
229 r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
230 if(r != UPNPCOMMAND_SUCCESS)
231 printf("UPnP: GetExternalIPAddress() returned %d\n", r);
232 else
233 {
234 if(externalIPAddress[0])
235 {
236 printf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
237 CAddress addrExternalFromUPnP(externalIPAddress, 0, false, nLocalServices);
238 if (addrExternalFromUPnP.IsRoutable())
239 addrLocalHost = addrExternalFromUPnP;
240 }
241 else
242 printf("UPnP: GetExternalIPAddress failed.\n");
243 }
244 }
245
246 string strDesc = "Bitcoin " + FormatFullVersion();
247 #ifndef UPNPDISCOVER_SUCCESS
248 /* miniupnpc 1.5 */
249 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
250 port, port, lanaddr, strDesc.c_str(), "TCP", 0);
251 #else
252 /* miniupnpc 1.6 */
253 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
254 port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
255 #endif
256
257 if(r!=UPNPCOMMAND_SUCCESS)
258 printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
259 port, port, lanaddr, r, strupnperror(r));
260 else
261 printf("UPnP Port Mapping successful.\n");
262 int i = 1;
263 loop {
264 if (fShutdown || !fUseUPnP)
265 {
266 r = UPNP_DeletePortMapping(urls.controlURL, data.first.servicetype, port, "TCP", 0);
267 printf("UPNP_DeletePortMapping() returned : %d\n", r);
268 freeUPNPDevlist(devlist); devlist = 0;
269 FreeUPNPUrls(&urls);
270 return;
271 }
272 if (i % 600 == 0) // Refresh every 20 minutes
273 {
274 #ifndef UPNPDISCOVER_SUCCESS
275 /* miniupnpc 1.5 */
276 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
277 port, port, lanaddr, strDesc.c_str(), "TCP", 0);
278 #else
279 /* miniupnpc 1.6 */
280 r = UPNP_AddPortMapping(urls.controlURL, data.first.servicetype,
281 port, port, lanaddr, strDesc.c_str(), "TCP", 0, "0");
282 #endif
283
284 if(r!=UPNPCOMMAND_SUCCESS)
285 printf("AddPortMapping(%s, %s, %s) failed with code %d (%s)\n",
286 port, port, lanaddr, r, strupnperror(r));
287 else
288 printf("UPnP Port Mapping successful.\n");;
289 }
290 Sleep(2000);
291 i++;
292 }
293 } else {
294 printf("No valid UPnP IGDs found\n");
295 freeUPNPDevlist(devlist); devlist = 0;
296 if (r != 0)
297 FreeUPNPUrls(&urls);
298 loop {
299 if (fShutdown || !fUseUPnP)
300 return;
301 Sleep(2000);
302 }
303 }
304 }
305
306 void MapPort(bool fMapPort)
307 {
308 if (fUseUPnP != fMapPort)
309 {
310 fUseUPnP = fMapPort;
311 WriteSetting("fUseUPnP", fUseUPnP);
312 }
313 if (fUseUPnP && vnThreadsRunning[5] < 1)
314 {
315 if (!CreateThread(ThreadMapPort, NULL))
316 printf("Error: ThreadMapPort(ThreadMapPort) failed\n");
317 }
318 }
319 #else
320 void MapPort(bool /* unused fMapPort */)
321 {
322 // Intentionally left blank.
323 }
324 #endif
325
326
327
(1872 . 10)(1716 . 6)
329 if (!CreateThread(ThreadDNSAddressSeed, NULL))
330 printf("Error: CreateThread(ThreadDNSAddressSeed) failed\n");
331
332 // Map ports with UPnP
333 if (fHaveUPnP)
334 MapPort(fUseUPnP);
335
336 // Get addresses from IRC and advertise ours
337 if (!CreateThread(ThreadIRCSeed, NULL))
338 printf("Error: CreateThread(ThreadIRCSeed) failed\n");
(1903 . 9)(1743 . 6)
340 nTransactionsUpdated++;
341 int64 nStart = GetTime();
342 while (vnThreadsRunning[0] > 0 || vnThreadsRunning[1] > 0 || vnThreadsRunning[2] > 0 || vnThreadsRunning[3] > 0 || vnThreadsRunning[4] > 0
343 #ifdef USE_UPNP
344 || vnThreadsRunning[5] > 0
345 #endif
346 )
347 {
348 if (GetTime() - nStart > 20)
(1917 . 7)(1754 . 6)
350 if (vnThreadsRunning[2] > 0) printf("ThreadMessageHandler still running\n");
351 if (vnThreadsRunning[3] > 0) printf("ThreadBitcoinMiner still running\n");
352 if (vnThreadsRunning[4] > 0) printf("ThreadRPCServer still running\n");
353 if (fHaveUPnP && vnThreadsRunning[5] > 0) printf("ThreadMapPort still running\n");
354 if (vnThreadsRunning[6] > 0) printf("ThreadDNSAddressSeed still running\n");
355 while (vnThreadsRunning[2] > 0 || vnThreadsRunning[4] > 0)
356 Sleep(20);