Skip to content

Commit 7bd21ce

Browse files
committed
style: rename hSocket to sock
In the arguments of `InterruptibleRecv()`, `Socks5()` and `ConnectThroughProxy()` the variable `hSocket` was previously of type `SOCKET`, but has been changed to `Sock`. Thus rename it to `sock` to imply its type, to distinguish from other `SOCKET` variables and to abide to the coding style wrt variables' names.
1 parent 04ae846 commit 7bd21ce

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/netbase.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ enum class IntrRecvError {
332332
* @param data The buffer where the read bytes should be stored.
333333
* @param len The number of bytes to read into the specified buffer.
334334
* @param timeout The total timeout in milliseconds for this read.
335-
* @param hSocket The socket (has to be in non-blocking mode) from which to read
336-
* bytes.
335+
* @param sock The socket (has to be in non-blocking mode) from which to read bytes.
337336
*
338337
* @returns An IntrRecvError indicating the resulting status of this read.
339338
* IntrRecvError::OK only if all of the specified number of bytes were
@@ -343,15 +342,15 @@ enum class IntrRecvError {
343342
* Sockets can be made non-blocking with SetSocketNonBlocking(const
344343
* SOCKET&, bool).
345344
*/
346-
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& hSocket)
345+
static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, const Sock& sock)
347346
{
348347
int64_t curTime = GetTimeMillis();
349348
int64_t endTime = curTime + timeout;
350349
// Maximum time to wait for I/O readiness. It will take up until this time
351350
// (in millis) to break off in case of an interruption.
352351
const int64_t maxWait = 1000;
353352
while (len > 0 && curTime < endTime) {
354-
ssize_t ret = hSocket.Recv(data, len, 0); // Optimistically try the recv first
353+
ssize_t ret = sock.Recv(data, len, 0); // Optimistically try the recv first
355354
if (ret > 0) {
356355
len -= ret;
357356
data += ret;
@@ -363,7 +362,7 @@ static IntrRecvError InterruptibleRecv(uint8_t* data, size_t len, int timeout, c
363362
// Only wait at most maxWait milliseconds at a time, unless
364363
// we're approaching the end of the specified total timeout
365364
int timeout_ms = std::min(endTime - curTime, maxWait);
366-
if (!hSocket.Wait(std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
365+
if (!sock.Wait(std::chrono::milliseconds{timeout_ms}, Sock::RECV)) {
367366
return IntrRecvError::NetworkError;
368367
}
369368
} else {
@@ -417,7 +416,7 @@ static std::string Socks5ErrorString(uint8_t err)
417416
* @param port The destination port.
418417
* @param auth The credentials with which to authenticate with the specified
419418
* SOCKS5 proxy.
420-
* @param hSocket The SOCKS5 proxy socket.
419+
* @param sock The SOCKS5 proxy socket.
421420
*
422421
* @returns Whether or not the operation succeeded.
423422
*
@@ -427,7 +426,7 @@ static std::string Socks5ErrorString(uint8_t err)
427426
* @see <a href="https://www.ietf.org/rfc/rfc1928.txt">RFC1928: SOCKS Protocol
428427
* Version 5</a>
429428
*/
430-
static bool Socks5(const std::string& strDest, int port, const ProxyCredentials* auth, const Sock& hSocket)
429+
static bool Socks5(const std::string& strDest, int port, const ProxyCredentials* auth, const Sock& sock)
431430
{
432431
IntrRecvError recvr;
433432
LogPrint(BCLog::NET, "SOCKS5 connecting %s\n", strDest);
@@ -445,12 +444,12 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
445444
vSocks5Init.push_back(0x01); // 1 method identifier follows...
446445
vSocks5Init.push_back(SOCKS5Method::NOAUTH);
447446
}
448-
ssize_t ret = hSocket.Send(vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
447+
ssize_t ret = sock.Send(vSocks5Init.data(), vSocks5Init.size(), MSG_NOSIGNAL);
449448
if (ret != (ssize_t)vSocks5Init.size()) {
450449
return error("Error sending to proxy");
451450
}
452451
uint8_t pchRet1[2];
453-
if ((recvr = InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
452+
if ((recvr = InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
454453
LogPrintf("Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
455454
return false;
456455
}
@@ -467,13 +466,13 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
467466
vAuth.insert(vAuth.end(), auth->username.begin(), auth->username.end());
468467
vAuth.push_back(auth->password.size());
469468
vAuth.insert(vAuth.end(), auth->password.begin(), auth->password.end());
470-
ret = hSocket.Send(vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
469+
ret = sock.Send(vAuth.data(), vAuth.size(), MSG_NOSIGNAL);
471470
if (ret != (ssize_t)vAuth.size()) {
472471
return error("Error sending authentication to proxy");
473472
}
474473
LogPrint(BCLog::PROXY, "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
475474
uint8_t pchRetA[2];
476-
if ((recvr = InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
475+
if ((recvr = InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
477476
return error("Error reading proxy authentication response");
478477
}
479478
if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
@@ -493,12 +492,12 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
493492
vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
494493
vSocks5.push_back((port >> 8) & 0xFF);
495494
vSocks5.push_back((port >> 0) & 0xFF);
496-
ret = hSocket.Send(vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
495+
ret = sock.Send(vSocks5.data(), vSocks5.size(), MSG_NOSIGNAL);
497496
if (ret != (ssize_t)vSocks5.size()) {
498497
return error("Error sending to proxy");
499498
}
500499
uint8_t pchRet2[4];
501-
if ((recvr = InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
500+
if ((recvr = InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
502501
if (recvr == IntrRecvError::Timeout) {
503502
/* If a timeout happens here, this effectively means we timed out while connecting
504503
* to the remote node. This is very common for Tor, so do not print an
@@ -522,24 +521,24 @@ static bool Socks5(const std::string& strDest, int port, const ProxyCredentials*
522521
uint8_t pchRet3[256];
523522
switch (pchRet2[3])
524523
{
525-
case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, hSocket); break;
526-
case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, SOCKS5_RECV_TIMEOUT, hSocket); break;
524+
case SOCKS5Atyp::IPV4: recvr = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, sock); break;
525+
case SOCKS5Atyp::IPV6: recvr = InterruptibleRecv(pchRet3, 16, SOCKS5_RECV_TIMEOUT, sock); break;
527526
case SOCKS5Atyp::DOMAINNAME:
528527
{
529-
recvr = InterruptibleRecv(pchRet3, 1, SOCKS5_RECV_TIMEOUT, hSocket);
528+
recvr = InterruptibleRecv(pchRet3, 1, SOCKS5_RECV_TIMEOUT, sock);
530529
if (recvr != IntrRecvError::OK) {
531530
return error("Error reading from proxy");
532531
}
533532
int nRecv = pchRet3[0];
534-
recvr = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
533+
recvr = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, sock);
535534
break;
536535
}
537536
default: return error("Error: malformed proxy response");
538537
}
539538
if (recvr != IntrRecvError::OK) {
540539
return error("Error reading from proxy");
541540
}
542-
if ((recvr = InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket)) != IntrRecvError::OK) {
541+
if ((recvr = InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, sock)) != IntrRecvError::OK) {
543542
return error("Error reading from proxy");
544543
}
545544
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest);
@@ -764,18 +763,18 @@ bool IsProxy(const CNetAddr &addr) {
764763
* @param proxy The SOCKS5 proxy.
765764
* @param strDest The destination service to which to connect.
766765
* @param port The destination port.
767-
* @param hSocket The socket on which to connect to the SOCKS5 proxy.
766+
* @param sock The socket on which to connect to the SOCKS5 proxy.
768767
* @param nTimeout Wait this many milliseconds for the connection to the SOCKS5
769768
* proxy to be established.
770769
* @param[out] outProxyConnectionFailed Whether or not the connection to the
771770
* SOCKS5 proxy failed.
772771
*
773772
* @returns Whether or not the operation succeeded.
774773
*/
775-
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& hSocket, int nTimeout, bool& outProxyConnectionFailed)
774+
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed)
776775
{
777776
// first connect to proxy server
778-
if (!ConnectSocketDirectly(proxy.proxy, hSocket.Get(), nTimeout, true)) {
777+
if (!ConnectSocketDirectly(proxy.proxy, sock.Get(), nTimeout, true)) {
779778
outProxyConnectionFailed = true;
780779
return false;
781780
}
@@ -784,11 +783,11 @@ bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int
784783
ProxyCredentials random_auth;
785784
static std::atomic_int counter(0);
786785
random_auth.username = random_auth.password = strprintf("%i", counter++);
787-
if (!Socks5(strDest, (uint16_t)port, &random_auth, hSocket)) {
786+
if (!Socks5(strDest, (uint16_t)port, &random_auth, sock)) {
788787
return false;
789788
}
790789
} else {
791-
if (!Socks5(strDest, (uint16_t)port, 0, hSocket)) {
790+
if (!Socks5(strDest, (uint16_t)port, 0, sock)) {
792791
return false;
793792
}
794793
}

src/netbase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
6868
extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
6969

7070
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocketRet, int nTimeout, bool manual_connection);
71-
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
71+
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
7272
/** Disable or enable blocking-mode for a socket */
7373
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
7474
/** Set the TCP_NODELAY flag on a socket */

0 commit comments

Comments
 (0)