77
88#include < sync.h>
99#include < tinyformat.h>
10- #include < util/system.h>
1110#include < util/strencodings.h>
11+ #include < util/string.h>
12+ #include < util/system.h>
1213
1314#include < atomic>
1415
@@ -59,10 +60,14 @@ std::string GetNetworkName(enum Network net) {
5960 }
6061}
6162
62- bool static LookupIntern (const char *pszName , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
63+ bool static LookupIntern (const std::string& name , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
6364{
6465 vIP.clear ();
6566
67+ if (!ValidAsCString (name)) {
68+ return false ;
69+ }
70+
6671 {
6772 CNetAddr addr;
6873 // From our perspective, onion addresses are not hostnames but rather
@@ -71,7 +76,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
7176 // getaddrinfo to decode them and it wouldn't make sense to resolve
7277 // them, we return a network address representing it instead. See
7378 // CNetAddr::SetSpecial(const std::string&) for more details.
74- if (addr.SetSpecial (std::string (pszName) )) {
79+ if (addr.SetSpecial (name )) {
7580 vIP.push_back (addr);
7681 return true ;
7782 }
@@ -93,7 +98,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
9398 // hostname lookups.
9499 aiHint.ai_flags = fAllowLookup ? AI_ADDRCONFIG : AI_NUMERICHOST;
95100 struct addrinfo *aiRes = nullptr ;
96- int nErr = getaddrinfo (pszName , nullptr , &aiHint, &aiRes);
101+ int nErr = getaddrinfo (name. c_str () , nullptr , &aiHint, &aiRes);
97102 if (nErr)
98103 return false ;
99104
@@ -131,7 +136,7 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
131136/* *
132137 * Resolve a host string to its corresponding network addresses.
133138 *
134- * @param pszName The string representing a host. Could be a name or a numerical
139+ * @param name The string representing a host. Could be a name or a numerical
135140 * IP address (IPv6 addresses in their bracketed form are
136141 * allowed).
137142 * @param[out] vIP The resulting network addresses to which the specified host
@@ -143,28 +148,34 @@ bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsign
143148 * @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
144149 * for additional parameter descriptions.
145150 */
146- bool LookupHost (const char *pszName , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
151+ bool LookupHost (const std::string& name , std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup )
147152{
148- std::string strHost (pszName);
153+ if (!ValidAsCString (name)) {
154+ return false ;
155+ }
156+ std::string strHost = name;
149157 if (strHost.empty ())
150158 return false ;
151159 if (strHost.front () == ' [' && strHost.back () == ' ]' ) {
152160 strHost = strHost.substr (1 , strHost.size () - 2 );
153161 }
154162
155- return LookupIntern (strHost. c_str () , vIP, nMaxSolutions, fAllowLookup );
163+ return LookupIntern (strHost, vIP, nMaxSolutions, fAllowLookup );
156164}
157165
158166 /* *
159167 * Resolve a host string to its first corresponding network address.
160168 *
161- * @see LookupHost(const char * , std::vector<CNetAddr>&, unsigned int, bool) for
169+ * @see LookupHost(const std::string& , std::vector<CNetAddr>&, unsigned int, bool) for
162170 * additional parameter descriptions.
163171 */
164- bool LookupHost (const char *pszName , CNetAddr& addr, bool fAllowLookup )
172+ bool LookupHost (const std::string& name , CNetAddr& addr, bool fAllowLookup )
165173{
174+ if (!ValidAsCString (name)) {
175+ return false ;
176+ }
166177 std::vector<CNetAddr> vIP;
167- LookupHost (pszName , vIP, 1 , fAllowLookup );
178+ LookupHost (name , vIP, 1 , fAllowLookup );
168179 if (vIP.empty ())
169180 return false ;
170181 addr = vIP.front ();
@@ -174,7 +185,7 @@ bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
174185/* *
175186 * Resolve a service string to its corresponding service.
176187 *
177- * @param pszName The string representing a service. Could be a name or a
188+ * @param name The string representing a service. Could be a name or a
178189 * numerical IP address (IPv6 addresses should be in their
179190 * disambiguated bracketed form), optionally followed by a port
180191 * number. (e.g. example.com:8333 or
@@ -191,16 +202,17 @@ bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
191202 * @returns Whether or not the service string successfully resolved to any
192203 * resulting services.
193204 */
194- bool Lookup (const char *pszName , std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions)
205+ bool Lookup (const std::string& name , std::vector<CService>& vAddr, int portDefault, bool fAllowLookup , unsigned int nMaxSolutions)
195206{
196- if (pszName[ 0 ] == 0 )
207+ if (name. empty () || ! ValidAsCString (name)) {
197208 return false ;
209+ }
198210 int port = portDefault;
199211 std::string hostname;
200- SplitHostPort (std::string (pszName) , port, hostname);
212+ SplitHostPort (name , port, hostname);
201213
202214 std::vector<CNetAddr> vIP;
203- bool fRet = LookupIntern (hostname. c_str () , vIP, nMaxSolutions, fAllowLookup );
215+ bool fRet = LookupIntern (hostname, vIP, nMaxSolutions, fAllowLookup );
204216 if (!fRet )
205217 return false ;
206218 vAddr.resize (vIP.size ());
@@ -215,10 +227,13 @@ bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault,
215227 * @see Lookup(const char *, std::vector<CService>&, int, bool, unsigned int)
216228 * for additional parameter descriptions.
217229 */
218- bool Lookup (const char *pszName , CService& addr, int portDefault, bool fAllowLookup )
230+ bool Lookup (const std::string& name , CService& addr, int portDefault, bool fAllowLookup )
219231{
232+ if (!ValidAsCString (name)) {
233+ return false ;
234+ }
220235 std::vector<CService> vService;
221- bool fRet = Lookup (pszName , vService, portDefault, fAllowLookup , 1 );
236+ bool fRet = Lookup (name , vService, portDefault, fAllowLookup , 1 );
222237 if (!fRet )
223238 return false ;
224239 addr = vService[0 ];
@@ -235,12 +250,15 @@ bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLoo
235250 * @see Lookup(const char *, CService&, int, bool) for additional parameter
236251 * descriptions.
237252 */
238- CService LookupNumeric (const char *pszName , int portDefault)
253+ CService LookupNumeric (const std::string& name , int portDefault)
239254{
255+ if (!ValidAsCString (name)) {
256+ return {};
257+ }
240258 CService addr;
241259 // "1.2:345" will fail to resolve the ip, but will still set the port.
242260 // If the ip fails to resolve, re-init the result.
243- if (!Lookup (pszName , addr, portDefault, false ))
261+ if (!Lookup (name , addr, portDefault, false ))
244262 addr = CService ();
245263 return addr;
246264}
@@ -768,12 +786,11 @@ bool IsProxy(const CNetAddr &addr) {
768786 *
769787 * @returns Whether or not the operation succeeded.
770788 */
771- bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool * outProxyConnectionFailed)
789+ bool ConnectThroughProxy (const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool & outProxyConnectionFailed)
772790{
773791 // first connect to proxy server
774792 if (!ConnectSocketDirectly (proxy.proxy , hSocket, nTimeout, true )) {
775- if (outProxyConnectionFailed)
776- *outProxyConnectionFailed = true ;
793+ outProxyConnectionFailed = true ;
777794 return false ;
778795 }
779796 // do socks negotiation
@@ -796,23 +813,25 @@ bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int
796813 * Parse and resolve a specified subnet string into the appropriate internal
797814 * representation.
798815 *
799- * @param pszName A string representation of a subnet of the form `network
816+ * @param strSubnet A string representation of a subnet of the form `network
800817 * address [ "/", ( CIDR-style suffix | netmask ) ]`(e.g.
801818 * `2001:db8::/32`, `192.0.2.0/255.255.255.0`, or `8.8.8.8`).
802819 * @param ret The resulting internal representation of a subnet.
803820 *
804821 * @returns Whether the operation succeeded or not.
805822 */
806- bool LookupSubNet (const char * pszName , CSubNet& ret)
823+ bool LookupSubNet (const std::string& strSubnet , CSubNet& ret)
807824{
808- std::string strSubnet (pszName);
825+ if (!ValidAsCString (strSubnet)) {
826+ return false ;
827+ }
809828 size_t slash = strSubnet.find_last_of (' /' );
810829 std::vector<CNetAddr> vIP;
811830
812831 std::string strAddress = strSubnet.substr (0 , slash);
813- // TODO: Use LookupHost(const char * , CNetAddr&, bool) instead to just get
832+ // TODO: Use LookupHost(const std::string& , CNetAddr&, bool) instead to just get
814833 // one CNetAddr.
815- if (LookupHost (strAddress. c_str () , vIP, 1 , false ))
834+ if (LookupHost (strAddress, vIP, 1 , false ))
816835 {
817836 CNetAddr network = vIP[0 ];
818837 if (slash != strSubnet.npos )
@@ -827,7 +846,7 @@ bool LookupSubNet(const char* pszName, CSubNet& ret)
827846 else // If not a valid number, try full netmask syntax
828847 {
829848 // Never allow lookup for netmask
830- if (LookupHost (strNetmask. c_str () , vIP, 1 , false )) {
849+ if (LookupHost (strNetmask, vIP, 1 , false )) {
831850 ret = CSubNet (network, vIP[0 ]);
832851 return ret.IsValid ();
833852 }
0 commit comments