Skip to content

Commit a89c3f5

Browse files
committed
netbase: extend Proxy class to wrap UNIX socket as well as TCP
1 parent 3a7d654 commit a89c3f5

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

src/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
483483
addr_bind = CAddress{conn.me, NODE_NONE};
484484
}
485485
} else if (use_proxy) {
486-
LogPrintLevel(BCLog::PROXY, BCLog::Level::Debug, "Using proxy: %s to connect to %s:%s\n", proxy.proxy.ToStringAddrPort(), addrConnect.ToStringAddr(), addrConnect.GetPort());
486+
LogPrintLevel(BCLog::PROXY, BCLog::Level::Debug, "Using proxy: %s to connect to %s:%s\n", proxy.ToString(), addrConnect.ToStringAddr(), addrConnect.GetPort());
487487
sock = ConnectThroughProxy(proxy, addrConnect.ToStringAddr(), addrConnect.GetPort(), proxyConnectionFailed);
488488
} else {
489489
// no proxy needed (none set for target network)

src/netbase.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ CService LookupNumeric(const std::string& name, uint16_t portDefault, DNSLookupF
216216
return Lookup(name, portDefault, /*fAllowLookup=*/false, dns_lookup_function).value_or(CService{});
217217
}
218218

219+
bool IsUnixSocketPath(const std::string& name)
220+
{
221+
#if HAVE_SOCKADDR_UN
222+
if (name.find(ADDR_PREFIX_UNIX) != 0) return false;
223+
224+
// Split off "unix:" prefix
225+
std::string str{name.substr(ADDR_PREFIX_UNIX.length())};
226+
227+
// Path size limit is platform-dependent
228+
// see https://manpages.ubuntu.com/manpages/xenial/en/man7/unix.7.html
229+
if (str.size() + 1 > sizeof(((sockaddr_un*)nullptr)->sun_path)) return false;
230+
231+
return true;
232+
#else
233+
return false;
234+
#endif
235+
}
236+
219237
/** SOCKS version */
220238
enum SOCKSVersion: uint8_t {
221239
SOCKS4 = 0x04,

src/netbase.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ static const int DEFAULT_CONNECT_TIMEOUT = 5000;
2727
//! -dns default
2828
static const int DEFAULT_NAME_LOOKUP = true;
2929

30+
/** Prefix for unix domain socket addresses (which are local filesystem paths) */
31+
const std::string ADDR_PREFIX_UNIX = "unix:";
32+
3033
enum class ConnectionDirection {
3134
None = 0,
3235
In = (1U << 0),
@@ -43,16 +46,44 @@ static inline bool operator&(ConnectionDirection a, ConnectionDirection b) {
4346
return (underlying(a) & underlying(b));
4447
}
4548

49+
/**
50+
* Check if a string is a valid UNIX domain socket path
51+
*
52+
* @param name The string provided by the user representing a local path
53+
*
54+
* @returns Whether the string has proper format, length, and points to an existing file path
55+
*/
56+
bool IsUnixSocketPath(const std::string& name);
57+
4658
class Proxy
4759
{
4860
public:
49-
Proxy(): randomize_credentials(false) {}
50-
explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
51-
52-
bool IsValid() const { return proxy.IsValid(); }
61+
Proxy(): m_is_unix_socket(false), randomize_credentials(false) {}
62+
explicit Proxy(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), m_is_unix_socket(false), randomize_credentials(_randomize_credentials) {}
63+
explicit Proxy(const std::string path, bool _randomize_credentials=false): m_unix_socket_path(path), m_is_unix_socket(true), randomize_credentials(_randomize_credentials) {}
5364

5465
CService proxy;
66+
std::string m_unix_socket_path;
67+
bool m_is_unix_socket;
5568
bool randomize_credentials;
69+
70+
bool IsValid() const
71+
{
72+
if (m_is_unix_socket) return IsUnixSocketPath(m_unix_socket_path);
73+
return proxy.IsValid();
74+
}
75+
76+
sa_family_t GetFamily() const
77+
{
78+
if (m_is_unix_socket) return AF_UNIX;
79+
return proxy.GetSAFamily();
80+
}
81+
82+
std::string ToString() const
83+
{
84+
if (m_is_unix_socket) return m_unix_socket_path;
85+
return proxy.ToStringAddrPort();
86+
}
5687
};
5788

5889
/** Credentials for proxy authentication */

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ static UniValue GetNetworksInfo()
607607
obj.pushKV("name", GetNetworkName(network));
608608
obj.pushKV("limited", !g_reachable_nets.Contains(network));
609609
obj.pushKV("reachable", g_reachable_nets.Contains(network));
610-
obj.pushKV("proxy", proxy.IsValid() ? proxy.proxy.ToStringAddrPort() : std::string());
610+
obj.pushKV("proxy", proxy.IsValid() ? proxy.ToString() : std::string());
611611
obj.pushKV("proxy_randomize_credentials", proxy.randomize_credentials);
612612
networks.push_back(obj);
613613
}

0 commit comments

Comments
 (0)