- 50F2BF26C02D6EDBD0AEC5A3E5B3DDCEAFFAB27C16D51A4DD972E42E01871D8E34314F525E9394211801C9EA7D1BD8F8266AE214B30A8E645CE207BF5F3BBD92
+ C67FDD55E9D9D6B4973122B76729D7E83A456A8DC410F1C130CFFBFD9F626C47CA7E8006BDE912D9E0BD0A4B8457E895270D4A0EFD22C4A199CD52FFD95B10DD
bitcoin/src/net.cpp
(10 . 10)(10 . 6)
303 #include "init.h"
304 #include "strlcpy.h"
305
306 #ifdef WIN32
307 #include <string.h>
308 #endif
309
310
311 using namespace std;
312 using namespace boost;
(94 . 13)(90 . 9)
314 bool fProxy = (fUseProxy && addrConnect.IsRoutable());
315 struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr());
316
317 #ifdef WIN32
318 u_long fNonblock = 1;
319 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
320 #else
321 int fFlags = fcntl(hSocket, F_GETFL, 0);
322 if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
323 #endif
324
325 {
326 closesocket(hSocket);
327 return false;
(133 . 11)(125 . 7)
329 return false;
330 }
331 socklen_t nRetSize = sizeof(nRet);
332 #ifdef WIN32
333 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
334 #else
335 if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
336 #endif
337 {
338 printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
339 closesocket(hSocket);
(150 . 11)(138 . 7)
341 return false;
342 }
343 }
344 #ifdef WIN32
345 else if (WSAGetLastError() != WSAEISCONN)
346 #else
347 else
348 #endif
349 {
350 printf("connect() failed: %i\n",WSAGetLastError());
351 closesocket(hSocket);
(167 . 13)(151 . 8)
353 CNode::ConnectNode immediately turns the socket back to non-blocking
354 but we'll turn it back to blocking just in case
355 */
356 #ifdef WIN32
357 fNonblock = 0;
358 if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
359 #else
360 fFlags = fcntl(hSocket, F_GETFL, 0);
361 if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
362 #endif
363 {
364 closesocket(hSocket);
365 return false;
(670 . 14)(649 . 8)
367 printf("connected %s\n", addrConnect.ToString().c_str());
368
369 // Set to nonblocking
370 #ifdef WIN32
371 u_long nOne = 1;
372 if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR)
373 printf("ConnectSocket() : ioctlsocket nonblocking setting failed, error %d\n", WSAGetLastError());
374 #else
375 if (fcntl(hSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
376 printf("ConnectSocket() : fcntl nonblocking setting failed, error %d\n", errno);
377 #endif
378
379 // Add node
380 CNode* pnode = new CNode(hSocket, addrConnect, false);
(1564 . 18)(1537 . 6)
382 int nOne = 1;
383 addrLocalHost.port = htons(GetListenPort());
384
385 #ifdef WIN32
386 // Initialize Windows Sockets
387 WSADATA wsadata;
388 int ret = WSAStartup(MAKEWORD(2,2), &wsadata);
389 if (ret != NO_ERROR)
390 {
391 strError = strprintf("Error: TCP/IP socket library failed to start (WSAStartup returned error %d)", ret);
392 printf("%s\n", strError.c_str());
393 return false;
394 }
395 #endif
396
397 // Create socket for listening for incoming connections
398 hListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
399 if (hListenSocket == INVALID_SOCKET)
(1590 . 18)(1551 . 11)
401 setsockopt(hListenSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&nOne, sizeof(int));
402 #endif
403
404 #ifndef WIN32
405 // Allow binding if the port is still in TIME_WAIT state after
406 // the program was closed and restarted. Not an issue on windows.
407 setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
408 #endif
409
410 #ifdef WIN32
411 // Set to nonblocking, incoming connections will also inherit this
412 if (ioctlsocket(hListenSocket, FIONBIO, (u_long*)&nOne) == SOCKET_ERROR)
413 #else
414 if (fcntl(hListenSocket, F_SETFL, O_NONBLOCK) == SOCKET_ERROR)
415 #endif
416 {
417 strError = strprintf("Error: Couldn't set properties on socket for incoming connections (error %d)", WSAGetLastError());
418 printf("%s\n", strError.c_str());
(1643 . 21)(1597 . 6)
420 if (pnodeLocalHost == NULL)
421 pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress("127.0.0.1", 0, false, nLocalServices));
422
423 #ifdef WIN32
424 // Get local host ip
425 char pszHostName[1000] = "";
426 if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
427 {
428 vector<CAddress> vaddr;
429 if (Lookup(pszHostName, vaddr, nLocalServices, -1, true))
430 BOOST_FOREACH (const CAddress &addr, vaddr)
431 if (addr.GetByte(3) != 127)
432 {
433 addrLocalHost = addr;
434 break;
435 }
436 }
437 #else
438 // Get local host ip
439 struct ifaddrs* myaddrs;
440 if (getifaddrs(&myaddrs) == 0)
(1692 . 7)(1631 . 7)
442 }
443 freeifaddrs(myaddrs);
444 }
445 #endif
446
447 printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
448
449 if (fUseProxy || mapArgs.count("-connect") || fNoListen)
(1778 . 10)(1717 . 6)
451 if (closesocket(hListenSocket) == SOCKET_ERROR)
452 printf("closesocket(hListenSocket) failed with error %d\n", WSAGetLastError());
453
454 #ifdef WIN32
455 // Shutdown Windows Sockets
456 WSACleanup();
457 #endif
458 }
459 }
460 instance_of_cnetcleanup;