// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
-// file COPYING or http://www.opensource.org/licenses/mit-license.php.
+// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#ifdef HAVE_CONFIG_H
#include "config/bitcoin-config.h"
#include "util.h"
#include "utilstrencodings.h"
+#ifdef __APPLE__
+#ifdef HAVE_GETADDRINFO_A
+#undef HAVE_GETADDRINFO_A
+#endif
+#endif
+
#ifdef HAVE_GETADDRINFO_A
#include <netdb.h>
#endif
-#ifndef WIN32
+#ifndef _WIN32
#if HAVE_INET_PTON
#include <arpa/inet.h>
#endif
aiHint.ai_socktype = SOCK_STREAM;
aiHint.ai_protocol = IPPROTO_TCP;
aiHint.ai_family = AF_UNSPEC;
-#ifdef WIN32
+#ifdef _WIN32
aiHint.ai_flags = fAllowLookup ? 0 : AI_NUMERICHOST;
#else
aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
*
* @note This function requires that hSocket is in non-blocking mode.
*/
-bool static InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket)
+bool static InterruptibleRecv(uint8_t* data, size_t len, int timeout, SOCKET& hSocket)
{
int64_t curTime = GetTimeMillis();
int64_t endTime = curTime + timeout;
// to break off in case of an interruption.
const int64_t maxWait = 1000;
while (len > 0 && curTime < endTime) {
- ssize_t ret = recv(hSocket, data, len, 0); // Optimistically try the recv first
+ // Optimistically try the recv first.
+ //
+ // POSIX recv() does not require a cast, as it takes a void *buf:
+ // ssize_t recv(int sockfd, void *buf, size_t len, int flags);
+ // However Windows explicitly requires a char *buf:
+ // int recv(SOCKET s, char *buf, int len, int flags);
+ ssize_t ret = recv(hSocket, reinterpret_cast<char*>(data), len, 0);
if (ret > 0) {
len -= ret;
data += ret;
CloseSocket(hSocket);
return error("Error sending to proxy");
}
- char pchRet1[2];
+ uint8_t pchRet1[2];
if (!InterruptibleRecv(pchRet1, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy response");
return error("Error sending authentication to proxy");
}
LogPrint("proxy", "SOCKS5 sending proxy authentication %s:%s\n", auth->username, auth->password);
- char pchRetA[2];
+ uint8_t pchRetA[2];
if (!InterruptibleRecv(pchRetA, 2, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy authentication response");
CloseSocket(hSocket);
return error("Error sending to proxy");
}
- char pchRet2[4];
+ uint8_t pchRet2[4];
if (!InterruptibleRecv(pchRet2, 4, SOCKS5_RECV_TIMEOUT, hSocket)) {
CloseSocket(hSocket);
return error("Error reading proxy response");
CloseSocket(hSocket);
return error("Error: malformed proxy response");
}
- char pchRet3[256];
+ uint8_t pchRet3[256];
switch (pchRet2[3])
{
case 0x01: ret = InterruptibleRecv(pchRet3, 4, SOCKS5_RECV_TIMEOUT, hSocket); break;
CloseSocket(hSocket);
return error("Error reading from proxy");
}
- int nRecv = pchRet3[0];
+ size_t nRecv = pchRet3[0];
ret = InterruptibleRecv(pchRet3, nRecv, SOCKS5_RECV_TIMEOUT, hSocket);
break;
}
#endif
//Disable Nagle's algorithm
-#ifdef WIN32
+#ifdef _WIN32
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
#else
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
return false;
}
socklen_t nRetSize = sizeof(nRet);
-#ifdef WIN32
+#ifdef _WIN32
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
#else
if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
return false;
}
}
-#ifdef WIN32
+#ifdef _WIN32
else if (WSAGetLastError() != WSAEISCONN)
#else
else
return (a.network < b.network || (a.network == b.network && memcmp(a.netmask, b.netmask, 16) < 0));
}
-#ifdef WIN32
+#ifdef _WIN32
std::string NetworkErrorString(int err)
{
char buf[256];
{
if (hSocket == INVALID_SOCKET)
return false;
-#ifdef WIN32
+#ifdef _WIN32
int ret = closesocket(hSocket);
#else
int ret = close(hSocket);
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
{
if (fNonBlocking) {
-#ifdef WIN32
+#ifdef _WIN32
u_long nOne = 1;
if (ioctlsocket(hSocket, FIONBIO, &nOne) == SOCKET_ERROR) {
#else
return false;
}
} else {
-#ifdef WIN32
+#ifdef _WIN32
u_long nZero = 0;
if (ioctlsocket(hSocket, FIONBIO, &nZero) == SOCKET_ERROR) {
#else