2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
#ifndef BITCOIN_NETBASE_H
|
|
|
|
#define BITCOIN_NETBASE_H
|
|
|
|
|
2013-05-28 01:55:01 +02:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2022-08-02 18:34:58 +02:00
|
|
|
#include <config/bitcoin-config.h>
|
2013-05-28 01:55:01 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <compat.h>
|
|
|
|
#include <netaddress.h>
|
|
|
|
#include <serialize.h>
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <util/sock.h>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2022-10-26 13:25:11 +02:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <stdint.h>
|
2012-01-03 23:33:31 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
extern int nConnectTimeout;
|
2014-05-31 12:04:34 +02:00
|
|
|
extern bool fNameLookup;
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2015-11-09 19:16:38 +01:00
|
|
|
//! -timeout default
|
2014-09-25 09:01:54 +02:00
|
|
|
static const int DEFAULT_CONNECT_TIMEOUT = 5000;
|
2015-11-09 19:16:38 +01:00
|
|
|
//! -dns default
|
|
|
|
static const int DEFAULT_NAME_LOOKUP = true;
|
2017-12-20 12:45:01 +01:00
|
|
|
static const bool DEFAULT_ALLOWPRIVATENET = false;
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2015-03-16 16:30:49 +01:00
|
|
|
class proxyType
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
proxyType(): randomize_credentials(false) {}
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit proxyType(const CService &_proxy, bool _randomize_credentials=false): proxy(_proxy), randomize_credentials(_randomize_credentials) {}
|
2015-03-16 16:30:49 +01:00
|
|
|
|
|
|
|
bool IsValid() const { return proxy.IsValid(); }
|
|
|
|
|
|
|
|
CService proxy;
|
|
|
|
bool randomize_credentials;
|
|
|
|
};
|
2012-09-23 12:55:05 +02:00
|
|
|
|
2019-08-07 06:42:54 +02:00
|
|
|
enum Network ParseNetwork(const std::string& net);
|
2014-07-30 15:32:36 +02:00
|
|
|
std::string GetNetworkName(enum Network net);
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetProxy(enum Network net, const proxyType &addrProxy);
|
2012-09-23 12:55:05 +02:00
|
|
|
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
|
2012-05-24 19:02:21 +02:00
|
|
|
bool IsProxy(const CNetAddr &addr);
|
2015-03-16 16:30:49 +01:00
|
|
|
bool SetNameProxy(const proxyType &addrProxy);
|
2012-09-23 12:55:05 +02:00
|
|
|
bool HaveNameProxy();
|
2017-09-28 17:02:53 +02:00
|
|
|
bool GetNameProxy(proxyType &nameProxyOut);
|
Merge #17754: net: Don't allow resolving of std::string with embedded NUL characters. Add tests.
7a046cdc1423963bdcbcf9bb98560af61fa90b37 tests: Avoid using C-style NUL-terminated strings as arguments (practicalswift)
fefb9165f23fe9d10ad092ec31715f906e0d2ee7 tests: Add tests to make sure lookup methods fail on std::string parameters with embedded NUL characters (practicalswift)
9574de86ad703ad942cdd0eca79f48c0d42b102b net: Avoid using C-style NUL-terminated strings as arguments in the netbase interface (practicalswift)
Pull request description:
Don't allow resolving of `std::string`:s with embedded `NUL` characters.
Avoid using C-style `NUL`-terminated strings as arguments in the `netbase` interface
Add tests.
The only place in where C-style `NUL`-terminated strings are actually needed is here:
```diff
+ if (!ValidAsCString(name)) {
+ return false;
+ }
...
- int nErr = getaddrinfo(pszName, nullptr, &aiHint, &aiRes);
+ int nErr = getaddrinfo(name.c_str(), nullptr, &aiHint, &aiRes);
if (nErr)
return false;
```
Interface changes:
```diff
-bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
+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 LookupHost(const std::string& name, CNetAddr& addr, bool fAllowLookup);
-bool Lookup(const char *pszName, CService& addr, int portDefault, 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);
+bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
-bool LookupSubNet(const char *pszName, CSubNet& subnet);
+bool LookupSubNet(const std::string& strSubnet, CSubNet& subnet);
-CService LookupNumeric(const char *pszName, int portDefault = 0);
+CService LookupNumeric(const std::string& name, int portDefault = 0);
-bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed);
+bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool& outProxyConnectionFailed);
```
It should be noted that the `ConnectThroughProxy` change (from `bool *outProxyConnectionFailed` to `bool& outProxyConnectionFailed`) has nothing to do with `NUL` handling but I thought it was worth doing when touching this file :)
ACKs for top commit:
EthanHeilman:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
laanwj:
ACK 7a046cdc1423963bdcbcf9bb98560af61fa90b37
Tree-SHA512: 66556e290db996917b54091acd591df221f72230f6b9f6b167b9195ee870ebef6e26f4cda2f6f54d00e1c362e1743bf56785d0de7cae854e6bf7d26f6caccaba
2020-01-22 20:14:12 +01:00
|
|
|
bool LookupHost(const std::string& name, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
|
|
|
|
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 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);
|
2022-10-26 13:25:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a TCP socket in the given address family.
|
|
|
|
* @param[in] address_family The socket is created in the same address family as this address.
|
|
|
|
* @return pointer to the created Sock object or unique_ptr that owns nothing in case of failure
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Sock> CreateSockTCP(const CService& address_family);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket factory. Defaults to `CreateSockTCP()`, but can be overridden by unit tests.
|
|
|
|
*/
|
|
|
|
extern std::function<std::unique_ptr<Sock>(const CService&)> CreateSock;
|
|
|
|
|
2018-04-13 19:15:35 +02:00
|
|
|
bool ConnectSocketDirectly(const CService &addrConnect, const SOCKET& hSocketRet, int nTimeout, bool manual_connection);
|
2022-10-26 13:25:11 +02:00
|
|
|
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const Sock& sock, int nTimeout, bool& outProxyConnectionFailed);
|
2014-07-09 11:00:00 +02:00
|
|
|
/** Disable or enable blocking-mode for a socket */
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
|
2017-05-18 02:26:54 +02:00
|
|
|
/** Set the TCP_NODELAY flag on a socket */
|
2017-07-24 14:58:25 +02:00
|
|
|
bool SetSocketNoDelay(const SOCKET& hSocket);
|
2017-08-09 18:06:31 +02:00
|
|
|
void InterruptSocks5(bool interrupt);
|
2012-01-03 23:33:31 +01:00
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_NETBASE_H
|