MCPcopy Create free account
hub / github.com/ElementsProject/elements / CreateSockTCP

Function CreateSockTCP

src/netbase.cpp:486–527  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

484}
485
486std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
487{
488 // Create a sockaddr from the specified service.
489 struct sockaddr_storage sockaddr;
490 socklen_t len = sizeof(sockaddr);
491 if (!address_family.GetSockAddr((struct sockaddr*)&sockaddr, &len)) {
492 LogPrintf("Cannot create socket for %s: unsupported network\n", address_family.ToString());
493 return nullptr;
494 }
495
496 // Create a TCP socket in the address family of the specified service.
497 SOCKET hSocket = socket(((struct sockaddr*)&sockaddr)->sa_family, SOCK_STREAM, IPPROTO_TCP);
498 if (hSocket == INVALID_SOCKET) {
499 return nullptr;
500 }
501
502 // Ensure that waiting for I/O on this socket won't result in undefined
503 // behavior.
504 if (!IsSelectableSocket(hSocket)) {
505 CloseSocket(hSocket);
506 LogPrintf("Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
507 return nullptr;
508 }
509
510#ifdef SO_NOSIGPIPE
511 int set = 1;
512 // Set the no-sigpipe option on the socket for BSD systems, other UNIXes
513 // should use the MSG_NOSIGNAL flag for every send.
514 setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
515#endif
516
517 // Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
518 SetSocketNoDelay(hSocket);
519
520 // Set the non-blocking option on the socket.
521 if (!SetSocketNonBlocking(hSocket, true)) {
522 CloseSocket(hSocket);
523 LogPrintf("Error setting socket to non-blocking: %s\n", NetworkErrorString(WSAGetLastError()));
524 return nullptr;
525 }
526 return std::make_unique<Sock>(hSocket);
527}
528
529std::function<std::unique_ptr<Sock>(const CService&)> CreateSock = CreateSockTCP;
530

Callers

nothing calls this directly

Calls 6

IsSelectableSocketFunction · 0.85
CloseSocketFunction · 0.85
SetSocketNoDelayFunction · 0.85
NetworkErrorStringFunction · 0.85
GetSockAddrMethod · 0.80
ToStringMethod · 0.45

Tested by

no test coverage detected