Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F13115381
D8684.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
18 KB
Subscribers
None
D8684.diff
View Options
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -177,7 +177,7 @@
rpc_allow_subnets.push_back(CSubNet(localv6));
for (const std::string &strAllow : gArgs.GetArgs("-rpcallowip")) {
CSubNet subnet;
- LookupSubNet(strAllow.c_str(), subnet);
+ LookupSubNet(strAllow, subnet);
if (!subnet.IsValid()) {
uiInterface.ThreadSafeMessageBox(
strprintf(
@@ -351,8 +351,7 @@
if (bind_handle) {
CNetAddr addr;
if (i->first.empty() ||
- (LookupHost(i->first.c_str(), addr, false) &&
- addr.IsBindAny())) {
+ (LookupHost(i->first, addr, false) && addr.IsBindAny())) {
LogPrintf("WARNING: the RPC server is not safe to expose to "
"untrusted networks such as the public internet\n");
}
diff --git a/src/init.cpp b/src/init.cpp
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -2314,7 +2314,7 @@
SetReachable(NET_ONION, false);
if (proxyArg != "" && proxyArg != "0") {
CService proxyAddr;
- if (!Lookup(proxyArg.c_str(), proxyAddr, 9050, fNameLookup)) {
+ if (!Lookup(proxyArg, proxyAddr, 9050, fNameLookup)) {
return InitError(strprintf(
_("Invalid -proxy address or hostname: '%s'"), proxyArg));
}
@@ -2345,7 +2345,7 @@
SetReachable(NET_ONION, false);
} else {
CService onionProxy;
- if (!Lookup(onionArg.c_str(), onionProxy, 9050, fNameLookup)) {
+ if (!Lookup(onionArg, onionProxy, 9050, fNameLookup)) {
return InitError(strprintf(
_("Invalid -onion address or hostname: '%s'"), onionArg));
}
@@ -2366,7 +2366,7 @@
for (const std::string &strAddr : args.GetArgs("-externalip")) {
CService addrLocal;
- if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) &&
+ if (Lookup(strAddr, addrLocal, GetListenPort(), fNameLookup) &&
addrLocal.IsValid()) {
AddLocal(addrLocal, LOCAL_MANUAL);
} else {
@@ -2892,7 +2892,7 @@
for (const std::string &strBind : args.GetArgs("-bind")) {
CService addrBind;
- if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false)) {
+ if (!Lookup(strBind, addrBind, GetListenPort(), false)) {
return InitError(ResolveErrMsg("bind", strBind));
}
connOptions.vBinds.push_back(addrBind);
diff --git a/src/net.cpp b/src/net.cpp
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -429,7 +429,7 @@
}
connected = ConnectThroughProxy(
proxy, addrConnect.ToStringIP(), addrConnect.GetPort(), hSocket,
- nConnectTimeout, &proxyConnectionFailed);
+ nConnectTimeout, proxyConnectionFailed);
} else {
// no proxy needed (none set for target network)
hSocket = CreateSocket(addrConnect);
@@ -453,8 +453,9 @@
std::string host;
int port = default_port;
SplitHostPort(std::string(pszDest), port, host);
+ bool proxyConnectionFailed;
connected = ConnectThroughProxy(proxy, host, port, hSocket,
- nConnectTimeout, nullptr);
+ nConnectTimeout, proxyConnectionFailed);
}
if (!connected) {
CloseSocket(hSocket);
@@ -1838,7 +1839,7 @@
// Limits number of IPs learned from a DNS seed
unsigned int nMaxIPs = 256;
- if (LookupHost(host.c_str(), vIPs, nMaxIPs, true)) {
+ if (LookupHost(host, vIPs, nMaxIPs, true)) {
for (const CNetAddr &ip : vIPs) {
int nOneDay = 24 * 3600;
CAddress addr = CAddress(
@@ -2162,8 +2163,7 @@
}
for (const std::string &strAddNode : lAddresses) {
- CService service(
- LookupNumeric(strAddNode.c_str(), Params().GetDefaultPort()));
+ CService service(LookupNumeric(strAddNode, Params().GetDefaultPort()));
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
if (service.IsValid()) {
// strAddNode is an IP:port
diff --git a/src/net_permissions.cpp b/src/net_permissions.cpp
--- a/src/net_permissions.cpp
+++ b/src/net_permissions.cpp
@@ -104,7 +104,7 @@
const std::string strBind = str.substr(offset);
CService addrBind;
- if (!Lookup(strBind.c_str(), addrBind, 0, false)) {
+ if (!Lookup(strBind, addrBind, 0, false)) {
error = ResolveErrMsg("whitebind", strBind);
return false;
}
@@ -131,7 +131,7 @@
const std::string net = str.substr(offset);
CSubNet subnet;
- LookupSubNet(net.c_str(), subnet);
+ LookupSubNet(net, subnet);
if (!subnet.IsValid()) {
error =
strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net);
diff --git a/src/netbase.h b/src/netbase.h
--- a/src/netbase.h
+++ b/src/netbase.h
@@ -46,22 +46,22 @@
bool SetNameProxy(const proxyType &addrProxy);
bool HaveNameProxy();
bool GetNameProxy(proxyType &nameProxyOut);
-bool LookupHost(const char *pszName, std::vector<CNetAddr> &vIP,
+bool LookupHost(const std::string &name, std::vector<CNetAddr> &vIP,
unsigned int nMaxSolutions, bool fAllowLookup);
-bool LookupHost(const char *pszName, CNetAddr &addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService &addr, int portDefault,
+bool LookupHost(const std::string &name, CNetAddr &addr, bool fAllowLookup);
+bool Lookup(const std::string &name, CService &addr, int portDefault,
bool fAllowLookup);
-bool Lookup(const char *pszName, std::vector<CService> &vAddr, int portDefault,
- bool fAllowLookup, unsigned int nMaxSolutions);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
-bool LookupSubNet(const char *pszName, CSubNet &subnet);
+bool Lookup(const std::string &name, std::vector<CService> &vAddr,
+ int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
+CService LookupNumeric(const std::string &name, int portDefault = 0);
+bool LookupSubNet(const std::string &strSubnet, CSubNet &subnet);
SOCKET CreateSocket(const CService &addrConnect);
bool ConnectSocketDirectly(const CService &addrConnect,
const SOCKET &hSocketRet, int nTimeout,
bool manual_connection);
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest,
int port, const SOCKET &hSocketRet, int nTimeout,
- bool *outProxyConnectionFailed);
+ bool &outProxyConnectionFailed);
/** Return readable error string for a network error code */
std::string NetworkErrorString(int err);
/** Close socket and set hSocket to INVALID_SOCKET */
diff --git a/src/netbase.cpp b/src/netbase.cpp
--- a/src/netbase.cpp
+++ b/src/netbase.cpp
@@ -6,11 +6,11 @@
#include <netbase.h>
#include <sync.h>
+#include <tinyformat.h>
#include <util/strencodings.h>
+#include <util/string.h>
#include <util/system.h>
-#include <tinyformat.h>
-
#include <atomic>
#ifndef WIN32
@@ -69,10 +69,14 @@
}
}
-static bool LookupIntern(const char *pszName, std::vector<CNetAddr> &vIP,
+static bool LookupIntern(const std::string &name, std::vector<CNetAddr> &vIP,
unsigned int nMaxSolutions, bool fAllowLookup) {
vIP.clear();
+ if (!ValidAsCString(name)) {
+ return false;
+ }
+
{
CNetAddr addr;
// From our perspective, onion addresses are not hostnames but rather
@@ -81,7 +85,7 @@
// getaddrinfo to decode them and it wouldn't make sense to resolve
// them, we return a network address representing it instead. See
// CNetAddr::SetSpecial(const std::string&) for more details.
- if (addr.SetSpecial(std::string(pszName))) {
+ if (addr.SetSpecial(name)) {
vIP.push_back(addr);
return true;
}
@@ -103,7 +107,7 @@
// hostname lookups.
aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
struct addrinfo *aiRes = nullptr;
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr) {
return false;
}
@@ -145,7 +149,7 @@
/**
* Resolve a host string to its corresponding network addresses.
*
- * @param pszName The string representing a host. Could be a name or a numerical
+ * @param name The string representing a host. Could be a name or a numerical
* IP address (IPv6 addresses in their bracketed form are
* allowed).
* @param[out] vIP The resulting network addresses to which the specified host
@@ -157,9 +161,12 @@
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
* for additional parameter descriptions.
*/
-bool LookupHost(const char *pszName, std::vector<CNetAddr> &vIP,
+bool LookupHost(const std::string &name, std::vector<CNetAddr> &vIP,
unsigned int nMaxSolutions, bool fAllowLookup) {
- std::string strHost(pszName);
+ if (!ValidAsCString(name)) {
+ return false;
+ }
+ std::string strHost = name;
if (strHost.empty()) {
return false;
}
@@ -167,18 +174,21 @@
strHost = strHost.substr(1, strHost.size() - 2);
}
- return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
+ return LookupIntern(strHost, vIP, nMaxSolutions, fAllowLookup);
}
/**
* Resolve a host string to its first corresponding network address.
*
- * @see LookupHost(const char *, std::vector<CNetAddr>&, unsigned int, bool) for
- * additional parameter descriptions.
+ * @see LookupHost(const std::string&, std::vector<CNetAddr>&, unsigned int,
+ * bool) for additional parameter descriptions.
*/
-bool LookupHost(const char *pszName, CNetAddr &addr, bool fAllowLookup) {
+bool LookupHost(const std::string &name, CNetAddr &addr, bool fAllowLookup) {
+ if (!ValidAsCString(name)) {
+ return false;
+ }
std::vector<CNetAddr> vIP;
- LookupHost(pszName, vIP, 1, fAllowLookup);
+ LookupHost(name, vIP, 1, fAllowLookup);
if (vIP.empty()) {
return false;
}
@@ -189,7 +199,7 @@
/**
* Resolve a service string to its corresponding service.
*
- * @param pszName The string representing a service. Could be a name or a
+ * @param name The string representing a service. Could be a name or a
* numerical IP address (IPv6 addresses should be in their
* disambiguated bracketed form), optionally followed by a port
* number. (e.g. example.com:8333 or
@@ -206,18 +216,17 @@
* @returns Whether or not the service string successfully resolved to any
* resulting services.
*/
-bool Lookup(const char *pszName, std::vector<CService> &vAddr, int portDefault,
- bool fAllowLookup, unsigned int nMaxSolutions) {
- if (pszName[0] == 0) {
+bool Lookup(const std::string &name, std::vector<CService> &vAddr,
+ int portDefault, bool fAllowLookup, unsigned int nMaxSolutions) {
+ if (name.empty() || !ValidAsCString(name)) {
return false;
}
int port = portDefault;
std::string hostname;
- SplitHostPort(std::string(pszName), port, hostname);
+ SplitHostPort(name, port, hostname);
std::vector<CNetAddr> vIP;
- bool fRet =
- LookupIntern(hostname.c_str(), vIP, nMaxSolutions, fAllowLookup);
+ bool fRet = LookupIntern(hostname, vIP, nMaxSolutions, fAllowLookup);
if (!fRet) {
return false;
}
@@ -234,10 +243,13 @@
* @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
* for additional parameter descriptions.
*/
-bool Lookup(const char *pszName, CService &addr, int portDefault,
+bool Lookup(const std::string &name, CService &addr, int portDefault,
bool fAllowLookup) {
+ if (!ValidAsCString(name)) {
+ return false;
+ }
std::vector<CService> vService;
- bool fRet = Lookup(pszName, vService, portDefault, fAllowLookup, 1);
+ bool fRet = Lookup(name, vService, portDefault, fAllowLookup, 1);
if (!fRet) {
return false;
}
@@ -255,11 +267,14 @@
* @see Lookup(const char *, CService&, int, bool) for additional parameter
* descriptions.
*/
-CService LookupNumeric(const char *pszName, int portDefault) {
+CService LookupNumeric(const std::string &name, int portDefault) {
+ if (!ValidAsCString(name)) {
+ return {};
+ }
CService addr;
// "1.2:345" will fail to resolve the ip, but will still set the port.
// If the ip fails to resolve, re-init the result.
- if (!Lookup(pszName, addr, portDefault, false)) {
+ if (!Lookup(name, addr, portDefault, false)) {
addr = CService();
}
return addr;
@@ -840,12 +855,10 @@
*/
bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest,
int port, const SOCKET &hSocket, int nTimeout,
- bool *outProxyConnectionFailed) {
+ bool &outProxyConnectionFailed) {
// first connect to proxy server
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout, true)) {
- if (outProxyConnectionFailed) {
- *outProxyConnectionFailed = true;
- }
+ outProxyConnectionFailed = true;
return false;
}
// do socks negotiation
@@ -867,22 +880,24 @@
* Parse and resolve a specified subnet string into the appropriate internal
* representation.
*
- * @param pszName A string representation of a subnet of the form `network
+ * @param strSubnet A string representation of a subnet of the form `network
* address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
* `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
* @param ret The resulting internal representation of a subnet.
*
* @returns Whether the operation succeeded or not.
*/
-bool LookupSubNet(const char *pszName, CSubNet &ret) {
- std::string strSubnet(pszName);
+bool LookupSubNet(const std::string &strSubnet, CSubNet &ret) {
+ if (!ValidAsCString(strSubnet)) {
+ return false;
+ }
size_t slash = strSubnet.find_last_of('/');
std::vector<CNetAddr> vIP;
std::string strAddress = strSubnet.substr(0, slash);
- // TODO: Use LookupHost(const char *, CNetAddr&, bool) instead to just get
- // one CNetAddr.
- if (LookupHost(strAddress.c_str(), vIP, 1, false)) {
+ // TODO: Use LookupHost(const std::string&, CNetAddr&, bool) instead to just
+ // get one CNetAddr.
+ if (LookupHost(strAddress, vIP, 1, false)) {
CNetAddr network = vIP[0];
if (slash != strSubnet.npos) {
std::string strNetmask = strSubnet.substr(slash + 1);
@@ -894,7 +909,7 @@
} else {
// If not a valid number, try full netmask syntax
// Never allow lookup for netmask
- if (LookupHost(strNetmask.c_str(), vIP, 1, false)) {
+ if (LookupHost(strNetmask, vIP, 1, false)) {
ret = CSubNet(network, vIP[0]);
return ret.IsValid();
}
diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp
--- a/src/qt/optionsdialog.cpp
+++ b/src/qt/optionsdialog.cpp
@@ -414,8 +414,7 @@
int &pos) const {
Q_UNUSED(pos);
// Validate the proxy
- CService serv(
- LookupNumeric(input.toStdString().c_str(), DEFAULT_GUI_PROXY_PORT));
+ CService serv(LookupNumeric(input.toStdString(), DEFAULT_GUI_PROXY_PORT));
proxyType addrProxy = proxyType(serv, true);
if (addrProxy.IsValid()) {
return QValidator::Acceptable;
diff --git a/src/qt/rpcconsole.cpp b/src/qt/rpcconsole.cpp
--- a/src/qt/rpcconsole.cpp
+++ b/src/qt/rpcconsole.cpp
@@ -1429,7 +1429,7 @@
QString strNode = nodes.at(i).data().toString();
CSubNet possibleSubnet;
- LookupSubNet(strNode.toStdString().c_str(), possibleSubnet);
+ LookupSubNet(strNode.toStdString(), possibleSubnet);
if (possibleSubnet.IsValid() && m_node.unban(possibleSubnet)) {
clientModel->getBanTableModel()->refresh();
}
diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp
--- a/src/rpc/net.cpp
+++ b/src/rpc/net.cpp
@@ -748,10 +748,10 @@
if (!isSubnet) {
CNetAddr resolved;
- LookupHost(request.params[0].get_str().c_str(), resolved, false);
+ LookupHost(request.params[0].get_str(), resolved, false);
netAddr = resolved;
} else {
- LookupSubNet(request.params[0].get_str().c_str(), subNet);
+ LookupSubNet(request.params[0].get_str(), subNet);
}
if (!(isSubnet ? subNet.IsValid() : netAddr.IsValid())) {
diff --git a/src/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp
--- a/src/seeder/bitcoin.cpp
+++ b/src/seeder/bitcoin.cpp
@@ -198,7 +198,7 @@
}
connected = ConnectThroughProxy(
proxy, you.ToStringIP(), you.GetPort(), sock, nConnectTimeout,
- &proxyConnectionFailed);
+ proxyConnectionFailed);
} else {
// no proxy needed (none set for target network)
sock = CreateSocket(you);
diff --git a/src/test/netbase_tests.cpp b/src/test/netbase_tests.cpp
--- a/src/test/netbase_tests.cpp
+++ b/src/test/netbase_tests.cpp
@@ -101,7 +101,7 @@
}
static bool TestParse(std::string src, std::string canon) {
- CService addr(LookupNumeric(src.c_str(), 65535));
+ CService addr(LookupNumeric(src, 65535));
return canon == addr.ToString();
}
diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp
--- a/src/torcontrol.cpp
+++ b/src/torcontrol.cpp
@@ -562,7 +562,7 @@
}
return;
}
- service = LookupNumeric(std::string(service_id + ".onion").c_str(),
+ service = LookupNumeric(std::string(service_id + ".onion"),
Params().GetDefaultPort());
LogPrintf("tor: Got service ID %s, advertising service %s\n",
service_id, service.ToString());
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Mar 1, 11:00 (15 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187446
Default Alt Text
D8684.diff (18 KB)
Attached To
D8684: net: Avoid using C-style NUL-terminated strings in the netbase interface
Event Timeline
Log In to Comment