26 #ifdef HAVE_SOCKADDR_UN
49 ai_hint.ai_socktype = SOCK_STREAM;
50 ai_hint.ai_protocol = IPPROTO_TCP;
52 ai_hint.ai_family = AF_UNSPEC;
60 ai_hint.ai_flags = allow_lookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
62 addrinfo* ai_res{
nullptr};
63 const int n_err{getaddrinfo(
name.c_str(),
nullptr, &ai_hint, &ai_res)};
65 if ((ai_hint.ai_flags & AI_ADDRCONFIG) == AI_ADDRCONFIG) {
68 ai_hint.ai_flags = (ai_hint.ai_flags & ~AI_ADDRCONFIG);
69 const int n_err_retry{getaddrinfo(
name.c_str(),
nullptr, &ai_hint, &ai_res)};
70 if (n_err_retry != 0) {
79 addrinfo* ai_trav{ai_res};
80 std::vector<CNetAddr> resolved_addresses;
81 while (ai_trav !=
nullptr) {
82 if (ai_trav->ai_family == AF_INET) {
83 assert(ai_trav->ai_addrlen >=
sizeof(sockaddr_in));
84 resolved_addresses.emplace_back(
reinterpret_cast<sockaddr_in*
>(ai_trav->ai_addr)->sin_addr);
86 if (ai_trav->ai_family == AF_INET6) {
87 assert(ai_trav->ai_addrlen >=
sizeof(sockaddr_in6));
88 const sockaddr_in6* s6{
reinterpret_cast<sockaddr_in6*
>(ai_trav->ai_addr)};
89 resolved_addresses.emplace_back(s6->sin6_addr, s6->sin6_scope_id);
91 ai_trav = ai_trav->ai_next;
95 return resolved_addresses;
101 std::string net =
ToLower(net_in);
106 LogPrintf(
"Warning: net name 'tor' is deprecated and will be removed in the future. You should use 'onion' instead.\n");
112 if (net ==
"cjdns") {
136 std::vector<std::string> names;
137 for (
int n = 0; n <
NET_MAX; ++n) {
142 if (append_unroutable) {
148 static std::vector<CNetAddr>
LookupIntern(
const std::string&
name,
unsigned int nMaxSolutions,
bool fAllowLookup,
DNSLookupFn dns_lookup_function)
162 std::vector<CNetAddr> addresses;
164 for (
const CNetAddr& resolved : dns_lookup_function(
name, fAllowLookup)) {
165 if (nMaxSolutions > 0 && addresses.size() >= nMaxSolutions) {
169 if (!resolved.IsInternal()) {
170 addresses.push_back(resolved);
177 std::vector<CNetAddr>
LookupHost(
const std::string&
name,
unsigned int nMaxSolutions,
bool fAllowLookup,
DNSLookupFn dns_lookup_function)
180 std::string strHost =
name;
181 if (strHost.empty())
return {};
182 if (strHost.front() ==
'[' && strHost.back() ==
']') {
183 strHost = strHost.substr(1, strHost.size() - 2);
186 return LookupIntern(strHost, nMaxSolutions, fAllowLookup, dns_lookup_function);
191 const std::vector<CNetAddr> addresses{
LookupHost(
name, 1, fAllowLookup, dns_lookup_function)};
192 return addresses.empty() ? std::nullopt : std::make_optional(addresses.front());
195 std::vector<CService>
Lookup(
const std::string&
name, uint16_t portDefault,
bool fAllowLookup,
unsigned int nMaxSolutions,
DNSLookupFn dns_lookup_function)
200 uint16_t port{portDefault};
201 std::string hostname;
204 const std::vector<CNetAddr> addresses{
LookupIntern(hostname, nMaxSolutions, fAllowLookup, dns_lookup_function)};
205 if (addresses.empty())
return {};
206 std::vector<CService> services;
207 services.reserve(addresses.size());
208 for (
const auto& addr : addresses)
209 services.emplace_back(addr, port);
213 std::optional<CService>
Lookup(
const std::string&
name, uint16_t portDefault,
bool fAllowLookup,
DNSLookupFn dns_lookup_function)
215 const std::vector<CService> services{
Lookup(
name, portDefault, fAllowLookup, 1, dns_lookup_function)};
217 return services.empty() ? std::nullopt : std::make_optional(services.front());
232 #ifdef HAVE_SOCKADDR_UN
240 if (str.size() + 1 >
sizeof(((sockaddr_un*)
nullptr)->sun_path))
return false;
316 auto curTime{Now<SteadyMilliseconds>()};
317 const auto endTime{curTime + timeout};
318 while (len > 0 && curTime < endTime) {
319 ssize_t
ret = sock.
Recv(data, len, 0);
323 }
else if (
ret == 0) {
330 const auto remaining = std::chrono::milliseconds{endTime - curTime};
331 const auto timeout = std::min(remaining, std::chrono::milliseconds{
MAX_WAIT_FOR_IO});
342 curTime = Now<SteadyMilliseconds>();
352 return "general failure";
354 return "connection not allowed";
356 return "network unreachable";
358 return "host unreachable";
360 return "connection refused";
362 return "TTL expired";
364 return "protocol error";
366 return "address type not supported";
377 if (strDest.size() > 255) {
382 std::vector<uint8_t> vSocks5Init;
385 vSocks5Init.push_back(0x02);
389 vSocks5Init.push_back(0x01);
395 LogPrintf(
"Socks5() connect to %s:%d failed: InterruptibleRecv() timeout or other failure\n", strDest, port);
399 LogError(
"Proxy failed to initialize\n");
404 std::vector<uint8_t> vAuth;
405 vAuth.push_back(0x01);
407 LogError(
"Proxy username or password too long\n");
410 vAuth.push_back(auth->
username.size());
412 vAuth.push_back(auth->
password.size());
418 LogError(
"Error reading proxy authentication response\n");
421 if (pchRetA[0] != 0x01 || pchRetA[1] != 0x00) {
422 LogError(
"Proxy authentication unsuccessful\n");
428 LogError(
"Proxy requested wrong authentication method %02x\n", pchRet1[1]);
431 std::vector<uint8_t> vSocks5;
434 vSocks5.push_back(0x00);
436 vSocks5.push_back(strDest.size());
437 vSocks5.insert(vSocks5.end(), strDest.begin(), strDest.end());
438 vSocks5.push_back((port >> 8) & 0xFF);
439 vSocks5.push_back((port >> 0) & 0xFF);
449 LogError(
"Error while reading proxy response\n");
454 LogError(
"Proxy failed to accept request\n");
462 if (pchRet2[2] != 0x00) {
463 LogError(
"Error: malformed proxy response\n");
466 uint8_t pchRet3[256];
467 switch (pchRet2[3]) {
473 LogError(
"Error reading from proxy\n");
476 int nRecv = pchRet3[0];
481 LogError(
"Error: malformed proxy response\n");
486 LogError(
"Error reading from proxy\n");
490 LogError(
"Error reading from proxy\n");
495 }
catch (
const std::runtime_error& e) {
496 LogError(
"Error during SOCKS5 proxy handshake: %s\n", e.what());
504 if (domain == AF_UNSPEC)
return nullptr;
507 SOCKET hSocket = socket(domain, type, protocol);
512 auto sock = std::make_unique<Sock>(hSocket);
514 if (domain != AF_INET && domain != AF_INET6 && domain != AF_UNIX) {
520 if (!sock->IsSelectable()) {
521 LogPrintf(
"Cannot create connection: non-selectable socket created (fd >= FD_SETSIZE ?)\n");
529 if (sock->SetSockOpt(SOL_SOCKET, SO_NOSIGPIPE, (
void*)&set,
sizeof(
int)) ==
SOCKET_ERROR) {
530 LogPrintf(
"Error setting SO_NOSIGPIPE on socket: %s, continuing anyway\n",
536 if (!sock->SetNonBlocking()) {
541 #ifdef HAVE_SOCKADDR_UN
542 if (domain == AF_UNIX)
return sock;
545 if (protocol == IPPROTO_TCP) {
548 if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on,
sizeof(on)) ==
SOCKET_ERROR) {
549 LogPrint(
BCLog::NET,
"Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
558 template<
typename... Args>
561 if (manual_connection) {
568 static bool ConnectToSocket(
const Sock& sock,
struct sockaddr* sockaddr, socklen_t len,
const std::string& dest_str,
bool manual_connection)
581 if (!sock.
Wait(std::chrono::milliseconds{nConnectTimeout}, requested, &occurred)) {
582 LogPrintf(
"wait for connect to %s failed: %s\n",
586 }
else if (occurred == 0) {
596 socklen_t sockerr_len =
sizeof(sockerr);
604 "connect() to %s failed after wait: %s",
632 struct sockaddr_storage sockaddr;
633 socklen_t len =
sizeof(sockaddr);
634 if (!dest.
GetSockAddr((
struct sockaddr*)&sockaddr, &len)) {
652 #ifdef HAVE_SOCKADDR_UN
653 auto sock =
CreateSock(AF_UNIX, SOCK_STREAM, 0);
661 struct sockaddr_un addrun;
662 memset(&addrun, 0,
sizeof(addrun));
663 addrun.sun_family = AF_UNIX;
665 memcpy(addrun.sun_path, path.c_str(), std::min(
sizeof(addrun.sun_path) - 1, path.length()));
666 socklen_t len =
sizeof(addrun);
668 if(!
ConnectToSocket(*sock, (
struct sockaddr*)&addrun, len, path,
true)) {
683 proxyInfo[net] = addrProxy;
690 if (!proxyInfo[net].IsValid())
692 proxyInfoOut = proxyInfo[net];
700 nameProxy = addrProxy;
706 if(!nameProxy.IsValid())
708 nameProxyOut = nameProxy;
714 return nameProxy.IsValid();
719 for (
int i = 0; i <
NET_MAX; i++) {
720 if (addr ==
static_cast<CNetAddr>(proxyInfo[i].proxy))
727 const std::string& dest,
729 bool& proxy_connection_failed)
734 proxy_connection_failed =
true;
741 static std::atomic_int counter(0);
743 if (!
Socks5(dest, port, &random_auth, *sock)) {
747 if (!
Socks5(dest, port,
nullptr, *sock)) {
762 const size_t slash_pos{subnet_str.find_last_of(
'/')};
763 const std::string str_addr{subnet_str.substr(0, slash_pos)};
764 std::optional<CNetAddr> addr{
LookupHost(str_addr,
false)};
766 if (addr.has_value()) {
768 if (slash_pos != subnet_str.npos) {
769 const std::string netmask_str{subnet_str.substr(slash_pos + 1)};
773 subnet =
CSubNet{addr.value(), netmask};
776 const std::optional<CNetAddr> full_netmask{
LookupHost(netmask_str,
false)};
777 if (full_netmask.has_value()) {
778 subnet =
CSubNet{addr.value(), full_netmask.value()};
783 subnet =
CSubNet{addr.value()};
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
A combination of a network address (CNetAddr) and a (TCP) port.
sa_family_t GetSAFamily() const
Get the address family.
bool GetSockAddr(struct sockaddr *paddr, socklen_t *addrlen) const
Obtain the IPv4/6 socket address this represents.
std::string ToStringAddrPort() const
A helper class for interruptible sleeps.
Different type to mark Mutex at global scope.
bool m_randomize_credentials
std::unique_ptr< Sock > Connect() const
std::string m_unix_socket_path
List of reachable networks.
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
RAII helper class that manages a socket and closes it automatically when it goes out of scope.
static constexpr Event SEND
If passed to Wait(), then it will wait for readiness to send to the socket.
virtual void SendComplete(Span< const unsigned char > data, std::chrono::milliseconds timeout, CThreadInterrupt &interrupt) const
Send the given data, retrying on transient errors.
virtual bool Wait(std::chrono::milliseconds timeout, Event requested, Event *occurred=nullptr) const
Wait for readiness for input (recv) or output (send).
static constexpr Event RECV
If passed to Wait(), then it will wait for readiness to read from the socket.
virtual int GetSockOpt(int level, int opt_name, void *opt_val, socklen_t *opt_len) const
getsockopt(2) wrapper.
virtual int Connect(const sockaddr *addr, socklen_t addr_len) const
connect(2) wrapper.
virtual ssize_t Recv(void *buf, size_t len, int flags) const
recv(2) wrapper.
#define WSAGetLastError()
#define LogPrintLevel(category, level,...)
#define LogPrint(category,...)
bool ContainsNoNUL(std::string_view str) noexcept
Check if a string does not contain any embedded NUL (\0) characters.
@ NET_MAX
Dummy value to indicate the number of NET_* constants.
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
IntrRecvError
Status codes that can be returned by InterruptibleRecv.
SOCKS5Atyp
Values defined for ATYPE in RFC1928.
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
SOCKS5Command
Values defined for CMD in RFC1928.
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
static Proxy proxyInfo[NET_MAX] GUARDED_BY(g_proxyinfo_mutex)
std::chrono::milliseconds g_socks5_recv_timeout
static void LogConnectFailure(bool manual_connection, const char *fmt, const Args &... args)
std::string GetNetworkName(enum Network net)
static IntrRecvError InterruptibleRecv(uint8_t *data, size_t len, std::chrono::milliseconds timeout, const Sock &sock)
Try to read a specified number of bytes from a socket.
CThreadInterrupt g_socks5_interrupt
Interrupt SOCKS5 reads or writes.
SOCKSVersion
SOCKS version.
bool SetNameProxy(const Proxy &addrProxy)
Set the name proxy to use for all connections to nodes specified by a hostname.
enum Network ParseNetwork(const std::string &net_in)
static std::vector< CNetAddr > LookupIntern(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
SOCKS5Method
Values defined for METHOD in RFC1928.
@ NOAUTH
No authentication required.
@ USER_PASS
Username/password.
@ NO_ACCEPTABLE
No acceptable methods.
bool Socks5(const std::string &strDest, uint16_t port, const ProxyCredentials *auth, const Sock &sock)
Connect to a specified destination service through an already connected SOCKS5 proxy.
std::vector< CNetAddr > WrappedGetAddrInfo(const std::string &name, bool allow_lookup)
Wrapper for getaddrinfo(3).
static std::string Socks5ErrorString(uint8_t err)
Convert SOCKS5 reply to an error message.
bool SetProxy(enum Network net, const Proxy &addrProxy)
SOCKS5Reply
Values defined for REP in RFC1928.
@ CMDUNSUPPORTED
Command not supported.
@ NETUNREACHABLE
Network unreachable.
@ GENFAILURE
General failure.
@ CONNREFUSED
Connection refused.
@ ATYPEUNSUPPORTED
Address type not supported.
@ NOTALLOWED
Connection not allowed by ruleset.
@ HOSTUNREACHABLE
Network unreachable.
std::unique_ptr< Sock > ConnectThroughProxy(const Proxy &proxy, const std::string &dest, uint16_t port, bool &proxy_connection_failed)
Connect to a specified destination service through a SOCKS5 proxy by first connecting to the SOCKS5 p...
CService MaybeFlipIPv6toCJDNS(const CService &service)
If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is re...
static GlobalMutex g_proxyinfo_mutex
std::function< std::unique_ptr< Sock >int, int, int)> CreateSock
Socket factory.
ReachableNets g_reachable_nets
bool GetProxy(enum Network net, Proxy &proxyInfoOut)
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
std::vector< std::string > GetNetworkNames(bool append_unroutable)
Return a vector of publicly routable Network names; optionally append NET_UNROUTABLE.
std::unique_ptr< Sock > ConnectDirectly(const CService &dest, bool manual_connection)
Create a socket and try to connect to the specified service.
static bool ConnectToSocket(const Sock &sock, struct sockaddr *sockaddr, socklen_t len, const std::string &dest_str, bool manual_connection)
bool IsUnixSocketPath(const std::string &name)
Check if a string is a valid UNIX domain socket path.
bool GetNameProxy(Proxy &nameProxyOut)
CService LookupNumeric(const std::string &name, uint16_t portDefault, DNSLookupFn dns_lookup_function)
Resolve a service string with a numeric IP to its first corresponding service.
bool IsProxy(const CNetAddr &addr)
std::unique_ptr< Sock > CreateSockOS(int domain, int type, int protocol)
Create a real socket from the operating system.
bool IsBadPort(uint16_t port)
Determine if a port is "bad" from the perspective of attempting to connect to a node on that port.
static const int DEFAULT_NAME_LOOKUP
-dns default
const std::string ADDR_PREFIX_UNIX
Prefix for unix domain socket addresses (which are local filesystem paths)
std::function< std::vector< CNetAddr >(const std::string &, bool)> DNSLookupFn
static const int DEFAULT_CONNECT_TIMEOUT
-timeout default
std::string NetworkErrorString(int err)
Return readable error string for a network error code.
static constexpr auto MAX_WAIT_FOR_IO
Maximum time to wait for I/O readiness.
Credentials for proxy authentication.
bool ParseUInt8(std::string_view str, uint8_t *out)
Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
bool SplitHostPort(std::string_view in, uint16_t &portOut, std::string &hostOut)
Splits socket address string into host string and port value.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.