- A1B78A422161C6F5F3CA51A585D8C3F68E9490AF3F1BA48B7772CBE4D5A9711619707984376F9A51487AEC676F437FBF6DF38309D535DACCB966873F9608E9F1+ 397FA4CAF80112EF2A0EF2905AB0AF4DF49E437B926E5DDE97167D888423A21E5B34F42361296BC065D31F49A8BDA21AEDFC6365684BC9CADD873598803B912Cbitcoin/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;