2015-12-13 16:58:29 +00:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 04:09:33 +00:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 14:02:28 +00:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 05:13:08 +00:00
|
|
|
|
2012-01-03 22:33:31 +00:00
|
|
|
#ifndef BITCOIN_NETBASE_H
|
|
|
|
#define BITCOIN_NETBASE_H
|
|
|
|
|
2013-05-27 23:55:01 +00:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
2014-06-23 18:04:24 +00:00
|
|
|
#include "config/bitcoin-config.h"
|
2013-05-27 23:55:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-13 05:13:08 +00:00
|
|
|
#include "compat.h"
|
|
|
|
#include "serialize.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-01-03 22:33:31 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
extern int nConnectTimeout;
|
2014-05-31 10:04:34 +00:00
|
|
|
extern bool fNameLookup;
|
2012-01-03 22:33:31 +00:00
|
|
|
|
2015-11-09 18:16:38 +00:00
|
|
|
//! -timeout default
|
2014-09-25 07:01:54 +00:00
|
|
|
static const int DEFAULT_CONNECT_TIMEOUT = 5000;
|
2015-11-09 18:16:38 +00:00
|
|
|
//! -dns default
|
|
|
|
static const int DEFAULT_NAME_LOOKUP = true;
|
2014-09-25 07:01:54 +00:00
|
|
|
|
2012-03-18 22:14:03 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
// In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
|
|
|
|
#undef SetPort
|
|
|
|
#endif
|
2012-01-03 22:33:31 +00:00
|
|
|
|
2012-04-10 18:22:04 +00:00
|
|
|
enum Network
|
|
|
|
{
|
2014-07-30 13:32:36 +00:00
|
|
|
NET_UNROUTABLE = 0,
|
2012-04-10 18:22:04 +00:00
|
|
|
NET_IPV4,
|
|
|
|
NET_IPV6,
|
|
|
|
NET_TOR,
|
|
|
|
|
2012-05-01 15:32:42 +00:00
|
|
|
NET_MAX,
|
2012-04-10 18:22:04 +00:00
|
|
|
};
|
|
|
|
|
2012-03-26 14:48:23 +00:00
|
|
|
/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
|
2012-01-03 22:33:31 +00:00
|
|
|
class CNetAddr
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned char ip[16]; // in network byte order
|
2016-04-05 22:26:38 +00:00
|
|
|
uint32_t scopeId; // for scoped/link-local ipv6 addresses
|
2012-01-03 22:33:31 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
CNetAddr();
|
|
|
|
CNetAddr(const struct in_addr& ipv4Addr);
|
2016-04-13 00:48:29 +00:00
|
|
|
explicit CNetAddr(const char *pszIp);
|
|
|
|
explicit CNetAddr(const std::string &strIp);
|
2012-01-03 22:33:31 +00:00
|
|
|
void Init();
|
|
|
|
void SetIP(const CNetAddr& ip);
|
2014-04-28 09:08:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set raw IPv4 or IPv6 address (in network byte order)
|
|
|
|
* @note Only NET_IPV4 and NET_IPV6 are allowed for network.
|
|
|
|
*/
|
|
|
|
void SetRaw(Network network, const uint8_t *data);
|
|
|
|
|
2012-10-07 12:53:17 +00:00
|
|
|
bool SetSpecial(const std::string &strName); // for Tor addresses
|
2012-01-03 22:33:31 +00:00
|
|
|
bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
|
2012-10-07 12:53:17 +00:00
|
|
|
bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
|
2012-01-03 22:33:31 +00:00
|
|
|
bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
|
2014-10-29 06:54:06 +00:00
|
|
|
bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
|
|
|
|
bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
|
|
|
|
bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
|
2012-01-03 22:33:31 +00:00
|
|
|
bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
|
|
|
|
bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
|
2012-07-26 00:48:39 +00:00
|
|
|
bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
|
2013-10-03 13:03:33 +00:00
|
|
|
bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
|
2012-07-26 00:48:39 +00:00
|
|
|
bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
|
2012-01-03 22:33:31 +00:00
|
|
|
bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
|
|
|
|
bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
|
|
|
|
bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
|
|
|
|
bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
|
2012-04-29 00:11:56 +00:00
|
|
|
bool IsTor() const;
|
2012-01-03 22:33:31 +00:00
|
|
|
bool IsLocal() const;
|
|
|
|
bool IsRoutable() const;
|
|
|
|
bool IsValid() const;
|
|
|
|
bool IsMulticast() const;
|
2012-04-10 18:22:04 +00:00
|
|
|
enum Network GetNetwork() const;
|
2012-01-03 22:33:31 +00:00
|
|
|
std::string ToString() const;
|
|
|
|
std::string ToStringIP() const;
|
2012-09-05 21:36:19 +00:00
|
|
|
unsigned int GetByte(int n) const;
|
2013-04-13 05:13:08 +00:00
|
|
|
uint64_t GetHash() const;
|
2012-01-03 22:33:31 +00:00
|
|
|
bool GetInAddr(struct in_addr* pipv4Addr) const;
|
|
|
|
std::vector<unsigned char> GetGroup() const;
|
2012-02-12 12:45:24 +00:00
|
|
|
int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
|
2012-01-03 22:33:31 +00:00
|
|
|
|
2016-04-05 22:26:38 +00:00
|
|
|
CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
|
2012-01-03 22:33:31 +00:00
|
|
|
bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
|
|
|
|
|
|
|
|
friend bool operator==(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
friend bool operator<(const CNetAddr& a, const CNetAddr& b);
|
|
|
|
|
2014-09-02 07:58:09 +00:00
|
|
|
ADD_SERIALIZE_METHODS;
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 06:42:31 +00:00
|
|
|
|
2014-08-20 20:44:38 +00:00
|
|
|
template <typename Stream, typename Operation>
|
2014-08-20 22:49:32 +00:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
2014-08-20 20:44:38 +00:00
|
|
|
READWRITE(FLATDATA(ip));
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 06:42:31 +00:00
|
|
|
}
|
2015-05-26 06:59:13 +00:00
|
|
|
|
|
|
|
friend class CSubNet;
|
2012-01-03 22:33:31 +00:00
|
|
|
};
|
|
|
|
|
2014-04-28 09:08:57 +00:00
|
|
|
class CSubNet
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
/// Network (base) address
|
|
|
|
CNetAddr network;
|
|
|
|
/// Netmask, in network byte order
|
|
|
|
uint8_t netmask[16];
|
|
|
|
/// Is this value valid? (only used to signal parse errors)
|
|
|
|
bool valid;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CSubNet();
|
2016-04-13 00:48:29 +00:00
|
|
|
explicit CSubNet(const std::string &strSubnet);
|
2014-04-28 09:08:57 +00:00
|
|
|
|
2015-06-29 18:37:22 +00:00
|
|
|
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
|
|
|
|
explicit CSubNet(const CNetAddr &addr);
|
|
|
|
|
2014-04-28 09:08:57 +00:00
|
|
|
bool Match(const CNetAddr &addr) const;
|
|
|
|
|
|
|
|
std::string ToString() const;
|
|
|
|
bool IsValid() const;
|
|
|
|
|
|
|
|
friend bool operator==(const CSubNet& a, const CSubNet& b);
|
|
|
|
friend bool operator!=(const CSubNet& a, const CSubNet& b);
|
2015-05-25 18:03:51 +00:00
|
|
|
friend bool operator<(const CSubNet& a, const CSubNet& b);
|
2015-06-19 13:27:37 +00:00
|
|
|
|
|
|
|
ADD_SERIALIZE_METHODS;
|
|
|
|
|
|
|
|
template <typename Stream, typename Operation>
|
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
|
|
|
READWRITE(network);
|
|
|
|
READWRITE(FLATDATA(netmask));
|
|
|
|
READWRITE(FLATDATA(valid));
|
|
|
|
}
|
2014-04-28 09:08:57 +00:00
|
|
|
};
|
|
|
|
|
2012-03-26 20:33:28 +00:00
|
|
|
/** A combination of a network address (CNetAddr) and a (TCP) port */
|
2012-01-03 22:33:31 +00:00
|
|
|
class CService : public CNetAddr
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
unsigned short port; // host order
|
|
|
|
|
|
|
|
public:
|
|
|
|
CService();
|
|
|
|
CService(const CNetAddr& ip, unsigned short port);
|
|
|
|
CService(const struct in_addr& ipv4Addr, unsigned short port);
|
|
|
|
CService(const struct sockaddr_in& addr);
|
2016-04-13 00:48:29 +00:00
|
|
|
explicit CService(const char *pszIpPort, int portDefault);
|
|
|
|
explicit CService(const char *pszIpPort);
|
|
|
|
explicit CService(const std::string& strIpPort, int portDefault);
|
|
|
|
explicit CService(const std::string& strIpPort);
|
2012-01-03 22:33:31 +00:00
|
|
|
void Init();
|
|
|
|
void SetPort(unsigned short portIn);
|
|
|
|
unsigned short GetPort() const;
|
2012-05-11 13:28:59 +00:00
|
|
|
bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
|
|
|
|
bool SetSockAddr(const struct sockaddr* paddr);
|
2012-01-03 22:33:31 +00:00
|
|
|
friend bool operator==(const CService& a, const CService& b);
|
|
|
|
friend bool operator!=(const CService& a, const CService& b);
|
|
|
|
friend bool operator<(const CService& a, const CService& b);
|
|
|
|
std::vector<unsigned char> GetKey() const;
|
|
|
|
std::string ToString() const;
|
|
|
|
std::string ToStringPort() const;
|
|
|
|
std::string ToStringIPPort() const;
|
|
|
|
|
|
|
|
CService(const struct in6_addr& ipv6Addr, unsigned short port);
|
|
|
|
CService(const struct sockaddr_in6& addr);
|
|
|
|
|
2014-09-02 07:58:09 +00:00
|
|
|
ADD_SERIALIZE_METHODS;
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 06:42:31 +00:00
|
|
|
|
2014-08-20 20:44:38 +00:00
|
|
|
template <typename Stream, typename Operation>
|
2014-08-20 22:49:32 +00:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
2014-08-20 20:44:38 +00:00
|
|
|
READWRITE(FLATDATA(ip));
|
|
|
|
unsigned short portN = htons(port);
|
2014-12-18 21:07:31 +00:00
|
|
|
READWRITE(FLATDATA(portN));
|
2014-09-01 19:36:46 +00:00
|
|
|
if (ser_action.ForRead())
|
2014-08-20 22:49:32 +00:00
|
|
|
port = ntohs(portN);
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 06:42:31 +00:00
|
|
|
}
|
2012-01-03 22:33:31 +00:00
|
|
|
};
|
|
|
|
|
2015-03-16 15:30:49 +00:00
|
|
|
class proxyType
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
proxyType(): randomize_credentials(false) {}
|
|
|
|
proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
|
|
|
|
|
|
|
|
bool IsValid() const { return proxy.IsValid(); }
|
|
|
|
|
|
|
|
CService proxy;
|
|
|
|
bool randomize_credentials;
|
|
|
|
};
|
2012-09-23 10:55:05 +00:00
|
|
|
|
2012-05-24 17:02:21 +00:00
|
|
|
enum Network ParseNetwork(std::string net);
|
2014-07-30 13:32:36 +00:00
|
|
|
std::string GetNetworkName(enum Network net);
|
2012-05-30 20:44:23 +00:00
|
|
|
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
|
2015-03-16 15:30:49 +00:00
|
|
|
bool SetProxy(enum Network net, const proxyType &addrProxy);
|
2012-09-23 10:55:05 +00:00
|
|
|
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
|
2012-05-24 17:02:21 +00:00
|
|
|
bool IsProxy(const CNetAddr &addr);
|
2015-03-16 15:30:49 +00:00
|
|
|
bool SetNameProxy(const proxyType &addrProxy);
|
2012-09-23 10:55:05 +00:00
|
|
|
bool HaveNameProxy();
|
2016-04-13 00:23:16 +00:00
|
|
|
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
|
|
|
|
bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
|
|
|
|
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
|
2012-01-03 22:33:31 +00:00
|
|
|
bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
|
2014-12-02 16:43:42 +00:00
|
|
|
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
|
|
|
|
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
|
2014-05-08 12:15:19 +00:00
|
|
|
/** Return readable error string for a network error code */
|
|
|
|
std::string NetworkErrorString(int err);
|
2014-07-10 10:13:03 +00:00
|
|
|
/** Close socket and set hSocket to INVALID_SOCKET */
|
|
|
|
bool CloseSocket(SOCKET& hSocket);
|
2014-07-09 09:00:00 +00:00
|
|
|
/** Disable or enable blocking-mode for a socket */
|
|
|
|
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
|
2015-08-25 18:12:08 +00:00
|
|
|
/**
|
|
|
|
* Convert milliseconds to a struct timeval for e.g. select.
|
|
|
|
*/
|
|
|
|
struct timeval MillisToTimeval(int64_t nTimeout);
|
2012-01-03 22:33:31 +00:00
|
|
|
|
2014-08-28 20:21:03 +00:00
|
|
|
#endif // BITCOIN_NETBASE_H
|