Skip to content

Commit c5904e8

Browse files
committed
Merge #10812: [utils] Allow bitcoin-cli's -rpcconnect option to be used with square brackets
5c64324 [utils] allow square brackets for ipv6 addresses in bitcoin-cli (John Newbery) fe4faba [refactor] move SplitHostPort() into utilstrencodings (John Newbery) Pull request description: bitcoin-cli's `-rpcconnect` can accept ipv6 addresses (as long as the libevent version is new enough), but fails to parse ipv6 with square brackets. This PR makes `bitcoin-cli` parse ipv6 in square brackets correctly. `bitcoin-cli -rpcconnect=[::1] <command>` should now be equivalent to `bitcoin-cli -rpcconnect=::1 <command>` This is useful so the `bitcoin-cli` option can now be in the same format as the `bitcoind` option. Doesn't include tests. I have a branch that fully tests `bitcoin-cli`, but that's queued behind several intermediate PRs. - first commit moves `SplitHostPort()` from libbitcoin_common into libbitcoin_util - second commit adds proper ipv6 parsing to bitcoin-cli Tree-SHA512: 249d409f10360c989474283341f458cc97364a56a7d004ae6d5f13d8bffe3a51b5dc2484d42218848e2d42cd9c0b13a1b92e94ea19b209f7e91c875c208d8409
2 parents 10b22e3 + 5c64324 commit c5904e8

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

src/bitcoin-cli.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "chainparamsbase.h"
1111
#include "clientversion.h"
1212
#include "fs.h"
13+
#include "utilstrencodings.h"
1314
#include "rpc/client.h"
1415
#include "rpc/protocol.h"
1516
#include "util.h"
@@ -191,8 +192,14 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
191192

192193
UniValue CallRPC(const std::string& strMethod, const UniValue& params)
193194
{
194-
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
195-
int port = GetArg("-rpcport", BaseParams().RPCPort());
195+
std::string host;
196+
// In preference order, we choose the following for the port:
197+
// 1. -rpcport
198+
// 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6)
199+
// 3. default port for chain
200+
int port = BaseParams().RPCPort();
201+
SplitHostPort(GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host);
202+
port = GetArg("-rpcport", port);
196203

197204
// Obtain event base
198205
raii_event_base base = obtain_event_base();

src/httpserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "chainparamsbase.h"
88
#include "compat.h"
99
#include "util.h"
10+
#include "utilstrencodings.h"
1011
#include "netbase.h"
1112
#include "rpc/protocol.h" // For HTTP status codes
1213
#include "sync.h"

src/netbase.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ std::string GetNetworkName(enum Network net) {
5858
}
5959
}
6060

61-
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
62-
size_t colon = in.find_last_of(':');
63-
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
64-
bool fHaveColon = colon != in.npos;
65-
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
66-
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
67-
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
68-
int32_t n;
69-
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
70-
in = in.substr(0, colon);
71-
portOut = n;
72-
}
73-
}
74-
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
75-
hostOut = in.substr(1, in.size()-2);
76-
else
77-
hostOut = in;
78-
}
79-
8061
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
8162
{
8263
vIP.clear();

src/netbase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class proxyType
3939

4040
enum Network ParseNetwork(std::string net);
4141
std::string GetNetworkName(enum Network net);
42-
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
4342
bool SetProxy(enum Network net, const proxyType &addrProxy);
4443
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
4544
bool IsProxy(const CNetAddr &addr);

src/test/netbase_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "netbase.h"
66
#include "test/test_bitcoin.h"
7+
#include "utilstrencodings.h"
78

89
#include <string>
910

src/utilstrencodings.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ std::vector<unsigned char> ParseHex(const std::string& str)
9191
return ParseHex(str.c_str());
9292
}
9393

94+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
95+
size_t colon = in.find_last_of(':');
96+
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
97+
bool fHaveColon = colon != in.npos;
98+
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
99+
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
100+
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
101+
int32_t n;
102+
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
103+
in = in.substr(0, colon);
104+
portOut = n;
105+
}
106+
}
107+
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
108+
hostOut = in.substr(1, in.size()-2);
109+
else
110+
hostOut = in;
111+
}
112+
94113
std::string EncodeBase64(const unsigned char* pch, size_t len)
95114
{
96115
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

src/utilstrencodings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ std::string DecodeBase32(const std::string& str);
4848
std::string EncodeBase32(const unsigned char* pch, size_t len);
4949
std::string EncodeBase32(const std::string& str);
5050

51+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
5152
std::string i64tostr(int64_t n);
5253
std::string itostr(int n);
5354
int64_t atoi64(const char* psz);

0 commit comments

Comments
 (0)