1 // Copyright (c) 2009-2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_NETBASE_H
6 #define BITCOIN_NETBASE_H
8 #if defined(HAVE_CONFIG_H)
9 #include "config/bitcoin-config.h"
13 #include "serialize.h"
19 extern int nConnectTimeout
;
20 extern bool fNameLookup
;
23 static const int DEFAULT_CONNECT_TIMEOUT
= 5000;
25 static const int DEFAULT_NAME_LOOKUP
= true;
28 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
42 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
46 unsigned char ip
[16]; // in network byte order
47 uint32_t scopeId
; // for scoped/link-local ipv6 addresses
51 CNetAddr(const struct in_addr
& ipv4Addr
);
52 explicit CNetAddr(const char *pszIp
);
53 explicit CNetAddr(const std::string
&strIp
);
55 void SetIP(const CNetAddr
& ip
);
58 * Set raw IPv4 or IPv6 address (in network byte order)
59 * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
61 void SetRaw(Network network
, const uint8_t *data
);
63 bool SetSpecial(const std::string
&strName
); // for Tor addresses
64 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
65 bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
66 bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
67 bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
68 bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
69 bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
70 bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
71 bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
72 bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
73 bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
74 bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
75 bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
76 bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
77 bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
78 bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
81 bool IsRoutable() const;
83 bool IsMulticast() const;
84 enum Network
GetNetwork() const;
85 std::string
ToString() const;
86 std::string
ToStringIP() const;
87 unsigned int GetByte(int n
) const;
88 uint64_t GetHash() const;
89 bool GetInAddr(struct in_addr
* pipv4Addr
) const;
90 std::vector
<unsigned char> GetGroup() const;
91 int GetReachabilityFrom(const CNetAddr
*paddrPartner
= NULL
) const;
93 CNetAddr(const struct in6_addr
& pipv6Addr
, const uint32_t scope
= 0);
94 bool GetIn6Addr(struct in6_addr
* pipv6Addr
) const;
96 friend bool operator==(const CNetAddr
& a
, const CNetAddr
& b
);
97 friend bool operator!=(const CNetAddr
& a
, const CNetAddr
& b
);
98 friend bool operator<(const CNetAddr
& a
, const CNetAddr
& b
);
100 ADD_SERIALIZE_METHODS
;
102 template <typename Stream
, typename Operation
>
103 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
104 READWRITE(FLATDATA(ip
));
107 friend class CSubNet
;
113 /// Network (base) address
115 /// Netmask, in network byte order
117 /// Is this value valid? (only used to signal parse errors)
122 explicit CSubNet(const std::string
&strSubnet
);
124 //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
125 explicit CSubNet(const CNetAddr
&addr
);
127 bool Match(const CNetAddr
&addr
) const;
129 std::string
ToString() const;
130 bool IsValid() const;
132 friend bool operator==(const CSubNet
& a
, const CSubNet
& b
);
133 friend bool operator!=(const CSubNet
& a
, const CSubNet
& b
);
134 friend bool operator<(const CSubNet
& a
, const CSubNet
& b
);
136 ADD_SERIALIZE_METHODS
;
138 template <typename Stream
, typename Operation
>
139 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
141 READWRITE(FLATDATA(netmask
));
142 READWRITE(FLATDATA(valid
));
146 /** A combination of a network address (CNetAddr) and a (TCP) port */
147 class CService
: public CNetAddr
150 unsigned short port
; // host order
154 CService(const CNetAddr
& ip
, unsigned short port
);
155 CService(const struct in_addr
& ipv4Addr
, unsigned short port
);
156 CService(const struct sockaddr_in
& addr
);
157 explicit CService(const char *pszIpPort
, int portDefault
);
158 explicit CService(const char *pszIpPort
);
159 explicit CService(const std::string
& strIpPort
, int portDefault
);
160 explicit CService(const std::string
& strIpPort
);
162 void SetPort(unsigned short portIn
);
163 unsigned short GetPort() const;
164 bool GetSockAddr(struct sockaddr
* paddr
, socklen_t
*addrlen
) const;
165 bool SetSockAddr(const struct sockaddr
* paddr
);
166 friend bool operator==(const CService
& a
, const CService
& b
);
167 friend bool operator!=(const CService
& a
, const CService
& b
);
168 friend bool operator<(const CService
& a
, const CService
& b
);
169 std::vector
<unsigned char> GetKey() const;
170 std::string
ToString() const;
171 std::string
ToStringPort() const;
172 std::string
ToStringIPPort() const;
174 CService(const struct in6_addr
& ipv6Addr
, unsigned short port
);
175 CService(const struct sockaddr_in6
& addr
);
177 ADD_SERIALIZE_METHODS
;
179 template <typename Stream
, typename Operation
>
180 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
181 READWRITE(FLATDATA(ip
));
182 unsigned short portN
= htons(port
);
183 READWRITE(FLATDATA(portN
));
184 if (ser_action
.ForRead())
192 proxyType(): randomize_credentials(false) {}
193 proxyType(const CService
&proxy
, bool randomize_credentials
=false): proxy(proxy
), randomize_credentials(randomize_credentials
) {}
195 bool IsValid() const { return proxy
.IsValid(); }
198 bool randomize_credentials
;
201 enum Network
ParseNetwork(std::string net
);
202 std::string
GetNetworkName(enum Network net
);
203 void SplitHostPort(std::string in
, int &portOut
, std::string
&hostOut
);
204 bool SetProxy(enum Network net
, const proxyType
&addrProxy
);
205 bool GetProxy(enum Network net
, proxyType
&proxyInfoOut
);
206 bool IsProxy(const CNetAddr
&addr
);
207 bool SetNameProxy(const proxyType
&addrProxy
);
208 bool HaveNameProxy();
209 bool LookupHost(const char *pszName
, std::vector
<CNetAddr
>& vIP
, unsigned int nMaxSolutions
, bool fAllowLookup
);
210 bool Lookup(const char *pszName
, CService
& addr
, int portDefault
, bool fAllowLookup
);
211 bool Lookup(const char *pszName
, std::vector
<CService
>& vAddr
, int portDefault
, bool fAllowLookup
, unsigned int nMaxSolutions
);
212 bool LookupNumeric(const char *pszName
, CService
& addr
, int portDefault
= 0);
213 bool ConnectSocket(const CService
&addr
, SOCKET
& hSocketRet
, int nTimeout
, bool *outProxyConnectionFailed
= 0);
214 bool ConnectSocketByName(CService
&addr
, SOCKET
& hSocketRet
, const char *pszDest
, int portDefault
, int nTimeout
, bool *outProxyConnectionFailed
= 0);
215 /** Return readable error string for a network error code */
216 std::string
NetworkErrorString(int err
);
217 /** Close socket and set hSocket to INVALID_SOCKET */
218 bool CloseSocket(SOCKET
& hSocket
);
219 /** Disable or enable blocking-mode for a socket */
220 bool SetSocketNonBlocking(SOCKET
& hSocket
, bool fNonBlocking
);
222 * Convert milliseconds to a struct timeval for e.g. select.
224 struct timeval
MillisToTimeval(int64_t nTimeout
);
226 #endif // BITCOIN_NETBASE_H