Browse Source

Merge #10812: [utils] Allow bitcoin-cli's -rpcconnect option to be used with square brackets

5c643241e [utils] allow square brackets for ipv6 addresses in bitcoin-cli (John Newbery)
fe4fabaf1 [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
0.15
Pieter Wuille 7 years ago
parent
commit
c5904e8714
No known key found for this signature in database
GPG Key ID: A636E97631F767E0
  1. 11
      src/bitcoin-cli.cpp
  2. 1
      src/httpserver.cpp
  3. 19
      src/netbase.cpp
  4. 1
      src/netbase.h
  5. 1
      src/test/netbase_tests.cpp
  6. 19
      src/utilstrencodings.cpp
  7. 1
      src/utilstrencodings.h

11
src/bitcoin-cli.cpp

@ -10,6 +10,7 @@
#include "chainparamsbase.h" #include "chainparamsbase.h"
#include "clientversion.h" #include "clientversion.h"
#include "fs.h" #include "fs.h"
#include "utilstrencodings.h"
#include "rpc/client.h" #include "rpc/client.h"
#include "rpc/protocol.h" #include "rpc/protocol.h"
#include "util.h" #include "util.h"
@ -191,8 +192,14 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
UniValue CallRPC(const std::string& strMethod, const UniValue& params) UniValue CallRPC(const std::string& strMethod, const UniValue& params)
{ {
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT); std::string host;
int port = GetArg("-rpcport", BaseParams().RPCPort()); // In preference order, we choose the following for the port:
// 1. -rpcport
// 2. port in -rpcconnect (ie following : in ipv4 or ]: in ipv6)
// 3. default port for chain
int port = BaseParams().RPCPort();
SplitHostPort(GetArg("-rpcconnect", DEFAULT_RPCCONNECT), port, host);
port = GetArg("-rpcport", port);
// Obtain event base // Obtain event base
raii_event_base base = obtain_event_base(); raii_event_base base = obtain_event_base();

1
src/httpserver.cpp

@ -7,6 +7,7 @@
#include "chainparamsbase.h" #include "chainparamsbase.h"
#include "compat.h" #include "compat.h"
#include "util.h" #include "util.h"
#include "utilstrencodings.h"
#include "netbase.h" #include "netbase.h"
#include "rpc/protocol.h" // For HTTP status codes #include "rpc/protocol.h" // For HTTP status codes
#include "sync.h" #include "sync.h"

19
src/netbase.cpp

@ -58,25 +58,6 @@ std::string GetNetworkName(enum Network net) {
} }
} }
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
size_t colon = in.find_last_of(':');
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
bool fHaveColon = colon != in.npos;
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
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
int32_t n;
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
in = in.substr(0, colon);
portOut = n;
}
}
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
hostOut = in.substr(1, in.size()-2);
else
hostOut = in;
}
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup) bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
{ {
vIP.clear(); vIP.clear();

1
src/netbase.h

@ -39,7 +39,6 @@ public:
enum Network ParseNetwork(std::string net); enum Network ParseNetwork(std::string net);
std::string GetNetworkName(enum Network net); std::string GetNetworkName(enum Network net);
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
bool SetProxy(enum Network net, const proxyType &addrProxy); bool SetProxy(enum Network net, const proxyType &addrProxy);
bool GetProxy(enum Network net, proxyType &proxyInfoOut); bool GetProxy(enum Network net, proxyType &proxyInfoOut);
bool IsProxy(const CNetAddr &addr); bool IsProxy(const CNetAddr &addr);

1
src/test/netbase_tests.cpp

@ -4,6 +4,7 @@
#include "netbase.h" #include "netbase.h"
#include "test/test_bitcoin.h" #include "test/test_bitcoin.h"
#include "utilstrencodings.h"
#include <string> #include <string>

19
src/utilstrencodings.cpp

@ -91,6 +91,25 @@ std::vector<unsigned char> ParseHex(const std::string& str)
return ParseHex(str.c_str()); return ParseHex(str.c_str());
} }
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
size_t colon = in.find_last_of(':');
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
bool fHaveColon = colon != in.npos;
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
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
int32_t n;
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
in = in.substr(0, colon);
portOut = n;
}
}
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
hostOut = in.substr(1, in.size()-2);
else
hostOut = in;
}
std::string EncodeBase64(const unsigned char* pch, size_t len) std::string EncodeBase64(const unsigned char* pch, size_t len)
{ {
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

1
src/utilstrencodings.h

@ -48,6 +48,7 @@ std::string DecodeBase32(const std::string& str);
std::string EncodeBase32(const unsigned char* pch, size_t len); std::string EncodeBase32(const unsigned char* pch, size_t len);
std::string EncodeBase32(const std::string& str); std::string EncodeBase32(const std::string& str);
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
std::string i64tostr(int64_t n); std::string i64tostr(int64_t n);
std::string itostr(int n); std::string itostr(int n);
int64_t atoi64(const char* psz); int64_t atoi64(const char* psz);

Loading…
Cancel
Save