Changeset View
Changeset View
Standalone View
Standalone View
src/netbase.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifdef HAVE_CONFIG_H | |||||
#include "config/bitcoin-config.h" | |||||
#endif | |||||
#include "netbase.h" | #include "netbase.h" | ||||
#include "hash.h" | #include "hash.h" | ||||
#include "random.h" | #include "random.h" | ||||
#include "sync.h" | #include "sync.h" | ||||
#include "uint256.h" | #include "uint256.h" | ||||
#include "util.h" | #include "util.h" | ||||
#include "utilstrencodings.h" | #include "utilstrencodings.h" | ||||
#include <atomic> | #include <atomic> | ||||
#ifndef WIN32 | #ifndef WIN32 | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#endif | #endif | ||||
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() | #include <boost/algorithm/string/case_conv.hpp> // for to_lower() | ||||
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith() | |||||
#if !defined(MSG_NOSIGNAL) | #if !defined(MSG_NOSIGNAL) | ||||
#define MSG_NOSIGNAL 0 | #define MSG_NOSIGNAL 0 | ||||
#endif | #endif | ||||
// Settings | // Settings | ||||
static proxyType proxyInfo[NET_MAX]; | static proxyType proxyInfo[NET_MAX]; | ||||
static proxyType nameProxy; | static proxyType nameProxy; | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | #endif | ||||
freeaddrinfo(aiRes); | freeaddrinfo(aiRes); | ||||
return (vIP.size() > 0); | return (vIP.size() > 0); | ||||
} | } | ||||
bool LookupHost(const char *pszName, std::vector<CNetAddr> &vIP, | bool LookupHost(const char *pszName, std::vector<CNetAddr> &vIP, | ||||
unsigned int nMaxSolutions, bool fAllowLookup) { | unsigned int nMaxSolutions, bool fAllowLookup) { | ||||
std::string strHost(pszName); | std::string strHost(pszName); | ||||
if (strHost.empty()) return false; | if (strHost.empty()) { | ||||
if (boost::algorithm::starts_with(strHost, "[") && | return false; | ||||
boost::algorithm::ends_with(strHost, "]")) { | } | ||||
if (strHost.front() == '[' && strHost.back() == ']') { | |||||
strHost = strHost.substr(1, strHost.size() - 2); | strHost = strHost.substr(1, strHost.size() - 2); | ||||
} | } | ||||
return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup); | return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup); | ||||
} | } | ||||
bool LookupHost(const char *pszName, CNetAddr &addr, bool fAllowLookup) { | bool LookupHost(const char *pszName, CNetAddr &addr, bool fAllowLookup) { | ||||
std::vector<CNetAddr> vIP; | std::vector<CNetAddr> vIP; | ||||
▲ Show 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
/** Credentials for proxy authentication */ | /** Credentials for proxy authentication */ | ||||
struct ProxyCredentials { | struct ProxyCredentials { | ||||
std::string username; | std::string username; | ||||
std::string password; | std::string password; | ||||
}; | }; | ||||
/** Convert SOCKS5 reply to a an error message */ | /** Convert SOCKS5 reply to an error message */ | ||||
std::string Socks5ErrorString(uint8_t err) { | static std::string Socks5ErrorString(uint8_t err) { | ||||
switch (err) { | switch (err) { | ||||
case SOCKS5Reply::GENFAILURE: | case SOCKS5Reply::GENFAILURE: | ||||
return "general failure"; | return "general failure"; | ||||
case SOCKS5Reply::NOTALLOWED: | case SOCKS5Reply::NOTALLOWED: | ||||
return "connection not allowed"; | return "connection not allowed"; | ||||
case SOCKS5Reply::NETUNREACHABLE: | case SOCKS5Reply::NETUNREACHABLE: | ||||
return "network unreachable"; | return "network unreachable"; | ||||
case SOCKS5Reply::HOSTUNREACHABLE: | case SOCKS5Reply::HOSTUNREACHABLE: | ||||
▲ Show 20 Lines • Show All 296 Lines • ▼ Show 20 Lines | |||||
bool HaveNameProxy() { | bool HaveNameProxy() { | ||||
LOCK(cs_proxyInfos); | LOCK(cs_proxyInfos); | ||||
return nameProxy.IsValid(); | return nameProxy.IsValid(); | ||||
} | } | ||||
bool IsProxy(const CNetAddr &addr) { | bool IsProxy(const CNetAddr &addr) { | ||||
LOCK(cs_proxyInfos); | LOCK(cs_proxyInfos); | ||||
for (int i = 0; i < NET_MAX; i++) { | for (int i = 0; i < NET_MAX; i++) { | ||||
if (addr == (CNetAddr)proxyInfo[i].proxy) return true; | if (addr == static_cast<CNetAddr>(proxyInfo[i].proxy)) { | ||||
return true; | |||||
} | |||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, | bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, | ||||
int port, const SOCKET &hSocket, int nTimeout, | int port, const SOCKET &hSocket, int nTimeout, | ||||
bool *outProxyConnectionFailed) { | bool *outProxyConnectionFailed) { | ||||
// first connect to proxy server | // first connect to proxy server | ||||
▲ Show 20 Lines • Show All 130 Lines • Show Last 20 Lines |