Line data Source code
1 : // Copyright (c) 2009-2013 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.
4 :
5 : #ifndef BITCOIN_NETBASE_H
6 : #define BITCOIN_NETBASE_H
7 :
8 : #if defined(HAVE_CONFIG_H)
9 : #include "config/bitcoin-config.h"
10 : #endif
11 :
12 : #include "compat.h"
13 : #include "serialize.h"
14 :
15 : #include <stdint.h>
16 : #include <string>
17 : #include <vector>
18 :
19 : extern int nConnectTimeout;
20 : extern bool fNameLookup;
21 :
22 : /** -timeout default */
23 : static const int DEFAULT_CONNECT_TIMEOUT = 5000;
24 :
25 : #ifdef WIN32
26 : // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
27 : #undef SetPort
28 : #endif
29 :
30 : enum Network
31 : {
32 : NET_UNROUTABLE = 0,
33 : NET_IPV4,
34 : NET_IPV6,
35 : NET_TOR,
36 :
37 : NET_MAX,
38 : };
39 :
40 : /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
41 : class CNetAddr
42 : {
43 : protected:
44 : unsigned char ip[16]; // in network byte order
45 :
46 : public:
47 : CNetAddr();
48 : CNetAddr(const struct in_addr& ipv4Addr);
49 : explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
50 : explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
51 : void Init();
52 : void SetIP(const CNetAddr& ip);
53 :
54 : /**
55 : * Set raw IPv4 or IPv6 address (in network byte order)
56 : * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
57 : */
58 : void SetRaw(Network network, const uint8_t *data);
59 :
60 : bool SetSpecial(const std::string &strName); // for Tor addresses
61 : bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
62 : bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
63 : bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
64 : bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
65 : bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
66 : bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
67 : bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
68 : bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
69 : bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
70 : bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
71 : bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
72 : bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
73 : bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
74 : bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
75 : bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
76 : bool IsTor() const;
77 : bool IsLocal() const;
78 : bool IsRoutable() const;
79 : bool IsValid() const;
80 : bool IsMulticast() const;
81 : enum Network GetNetwork() const;
82 : std::string ToString() const;
83 : std::string ToStringIP() const;
84 : unsigned int GetByte(int n) const;
85 : uint64_t GetHash() const;
86 : bool GetInAddr(struct in_addr* pipv4Addr) const;
87 : std::vector<unsigned char> GetGroup() const;
88 : int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
89 :
90 : CNetAddr(const struct in6_addr& pipv6Addr);
91 : bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
92 :
93 : friend bool operator==(const CNetAddr& a, const CNetAddr& b);
94 : friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
95 : friend bool operator<(const CNetAddr& a, const CNetAddr& b);
96 :
97 25 : ADD_SERIALIZE_METHODS;
98 :
99 : template <typename Stream, typename Operation>
100 0 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
101 69 : READWRITE(FLATDATA(ip));
102 0 : }
103 :
104 : friend class CSubNet;
105 : };
106 :
107 : class CSubNet
108 : {
109 : protected:
110 : /// Network (base) address
111 : CNetAddr network;
112 : /// Netmask, in network byte order
113 : uint8_t netmask[16];
114 : /// Is this value valid? (only used to signal parse errors)
115 : bool valid;
116 :
117 : public:
118 : CSubNet();
119 : explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
120 :
121 : //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
122 : explicit CSubNet(const CNetAddr &addr);
123 :
124 : bool Match(const CNetAddr &addr) const;
125 :
126 : std::string ToString() const;
127 : bool IsValid() const;
128 :
129 : friend bool operator==(const CSubNet& a, const CSubNet& b);
130 : friend bool operator!=(const CSubNet& a, const CSubNet& b);
131 : friend bool operator<(const CSubNet& a, const CSubNet& b);
132 :
133 25 : ADD_SERIALIZE_METHODS;
134 :
135 : template <typename Stream, typename Operation>
136 25 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
137 25 : READWRITE(network);
138 69 : READWRITE(FLATDATA(netmask));
139 69 : READWRITE(FLATDATA(valid));
140 25 : }
141 : };
142 :
143 : /** A combination of a network address (CNetAddr) and a (TCP) port */
144 : class CService : public CNetAddr
145 : {
146 : protected:
147 : unsigned short port; // host order
148 :
149 : public:
150 : CService();
151 : CService(const CNetAddr& ip, unsigned short port);
152 : CService(const struct in_addr& ipv4Addr, unsigned short port);
153 : CService(const struct sockaddr_in& addr);
154 : explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
155 : explicit CService(const char *pszIpPort, bool fAllowLookup = false);
156 : explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
157 : explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
158 : void Init();
159 : void SetPort(unsigned short portIn);
160 : unsigned short GetPort() const;
161 : bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
162 : bool SetSockAddr(const struct sockaddr* paddr);
163 : friend bool operator==(const CService& a, const CService& b);
164 : friend bool operator!=(const CService& a, const CService& b);
165 : friend bool operator<(const CService& a, const CService& b);
166 : std::vector<unsigned char> GetKey() const;
167 : std::string ToString() const;
168 : std::string ToStringPort() const;
169 : std::string ToStringIPPort() const;
170 :
171 : CService(const struct in6_addr& ipv6Addr, unsigned short port);
172 : CService(const struct sockaddr_in6& addr);
173 :
174 1030 : ADD_SERIALIZE_METHODS;
175 :
176 : template <typename Stream, typename Operation>
177 1030 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
178 2090 : READWRITE(FLATDATA(ip));
179 1030 : unsigned short portN = htons(port);
180 1560 : READWRITE(FLATDATA(portN));
181 1030 : if (ser_action.ForRead())
182 500 : port = ntohs(portN);
183 1030 : }
184 : };
185 :
186 : class proxyType
187 : {
188 : public:
189 1537 : proxyType(): randomize_credentials(false) {}
190 5 : proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
191 :
192 582 : bool IsValid() const { return proxy.IsValid(); }
193 :
194 : CService proxy;
195 : bool randomize_credentials;
196 : };
197 :
198 : enum Network ParseNetwork(std::string net);
199 : std::string GetNetworkName(enum Network net);
200 : void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
201 : bool SetProxy(enum Network net, const proxyType &addrProxy);
202 : bool GetProxy(enum Network net, proxyType &proxyInfoOut);
203 : bool IsProxy(const CNetAddr &addr);
204 : bool SetNameProxy(const proxyType &addrProxy);
205 : bool HaveNameProxy();
206 : bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
207 : bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
208 : bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
209 : bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
210 : bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
211 : bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
212 : /** Return readable error string for a network error code */
213 : std::string NetworkErrorString(int err);
214 : /** Close socket and set hSocket to INVALID_SOCKET */
215 : bool CloseSocket(SOCKET& hSocket);
216 : /** Disable or enable blocking-mode for a socket */
217 : bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
218 :
219 : #endif // BITCOIN_NETBASE_H
|