]>
Commit | Line | Data |
---|---|---|
db0e8ccd | 1 | // Copyright (c) 2009-2013 The Bitcoin developers |
67a42f92 | 2 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 4 | |
67a42f92 PW |
5 | #ifndef BITCOIN_NETBASE_H |
6 | #define BITCOIN_NETBASE_H | |
7 | ||
35b8af92 | 8 | #if defined(HAVE_CONFIG_H) |
f3967bcc | 9 | #include "config/bitcoin-config.h" |
35b8af92 CF |
10 | #endif |
11 | ||
51ed9ec9 BD |
12 | #include "compat.h" |
13 | #include "serialize.h" | |
14 | ||
15 | #include <stdint.h> | |
67a42f92 PW |
16 | #include <string> |
17 | #include <vector> | |
18 | ||
67a42f92 | 19 | extern int nConnectTimeout; |
cb7a3edc | 20 | extern bool fNameLookup; |
67a42f92 | 21 | |
52d3a481 WL |
22 | #ifdef WIN32 |
23 | // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error | |
24 | #undef SetPort | |
25 | #endif | |
67a42f92 | 26 | |
090e5b40 PW |
27 | enum Network |
28 | { | |
075cf49e | 29 | NET_UNROUTABLE = 0, |
090e5b40 PW |
30 | NET_IPV4, |
31 | NET_IPV6, | |
32 | NET_TOR, | |
090e5b40 | 33 | |
d077dd2a | 34 | NET_MAX, |
090e5b40 PW |
35 | }; |
36 | ||
6b8de05d | 37 | /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */ |
67a42f92 PW |
38 | class CNetAddr |
39 | { | |
40 | protected: | |
41 | unsigned char ip[16]; // in network byte order | |
42 | ||
43 | public: | |
44 | CNetAddr(); | |
45 | CNetAddr(const struct in_addr& ipv4Addr); | |
c981d768 PW |
46 | explicit CNetAddr(const char *pszIp, bool fAllowLookup = false); |
47 | explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false); | |
67a42f92 PW |
48 | void Init(); |
49 | void SetIP(const CNetAddr& ip); | |
e16be737 WL |
50 | |
51 | /** | |
52 | * Set raw IPv4 or IPv6 address (in network byte order) | |
53 | * @note Only NET_IPV4 and NET_IPV6 are allowed for network. | |
54 | */ | |
55 | void SetRaw(Network network, const uint8_t *data); | |
56 | ||
4e882b79 | 57 | bool SetSpecial(const std::string &strName); // for Tor addresses |
67a42f92 | 58 | bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0) |
4e882b79 | 59 | bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor) |
67a42f92 PW |
60 | bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12) |
61 | bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32) | |
62 | bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16) | |
814efd6f | 63 | bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16) |
c1761a5d | 64 | bool IsRFC4193() const; // IPv6 unique local (FC00::/7) |
814efd6f | 65 | bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32) |
67a42f92 PW |
66 | bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28) |
67 | bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64) | |
68 | bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96) | |
69 | bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) | |
70f7f003 | 70 | bool IsTor() const; |
67a42f92 PW |
71 | bool IsLocal() const; |
72 | bool IsRoutable() const; | |
73 | bool IsValid() const; | |
74 | bool IsMulticast() const; | |
090e5b40 | 75 | enum Network GetNetwork() const; |
67a42f92 PW |
76 | std::string ToString() const; |
77 | std::string ToStringIP() const; | |
463a1cab | 78 | unsigned int GetByte(int n) const; |
51ed9ec9 | 79 | uint64_t GetHash() const; |
67a42f92 PW |
80 | bool GetInAddr(struct in_addr* pipv4Addr) const; |
81 | std::vector<unsigned char> GetGroup() const; | |
39857190 | 82 | int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const; |
67a42f92 | 83 | |
67a42f92 PW |
84 | CNetAddr(const struct in6_addr& pipv6Addr); |
85 | bool GetIn6Addr(struct in6_addr* pipv6Addr) const; | |
67a42f92 PW |
86 | |
87 | friend bool operator==(const CNetAddr& a, const CNetAddr& b); | |
88 | friend bool operator!=(const CNetAddr& a, const CNetAddr& b); | |
89 | friend bool operator<(const CNetAddr& a, const CNetAddr& b); | |
90 | ||
84881f8c | 91 | IMPLEMENT_SERIALIZE; |
3d796f89 | 92 | |
84881f8c KD |
93 | template <typename Stream, typename Operation> |
94 | inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { | |
3d796f89 | 95 | size_t nSerSize = 0; |
84881f8c | 96 | READWRITE(FLATDATA(ip)); |
3d796f89 KD |
97 | return nSerSize; |
98 | } | |
67a42f92 PW |
99 | }; |
100 | ||
e16be737 WL |
101 | class CSubNet |
102 | { | |
103 | protected: | |
104 | /// Network (base) address | |
105 | CNetAddr network; | |
106 | /// Netmask, in network byte order | |
107 | uint8_t netmask[16]; | |
108 | /// Is this value valid? (only used to signal parse errors) | |
109 | bool valid; | |
110 | ||
111 | public: | |
112 | CSubNet(); | |
113 | explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false); | |
114 | ||
115 | bool Match(const CNetAddr &addr) const; | |
116 | ||
117 | std::string ToString() const; | |
118 | bool IsValid() const; | |
119 | ||
120 | friend bool operator==(const CSubNet& a, const CSubNet& b); | |
121 | friend bool operator!=(const CSubNet& a, const CSubNet& b); | |
122 | }; | |
123 | ||
7e05b972 | 124 | /** A combination of a network address (CNetAddr) and a (TCP) port */ |
67a42f92 PW |
125 | class CService : public CNetAddr |
126 | { | |
127 | protected: | |
128 | unsigned short port; // host order | |
129 | ||
130 | public: | |
131 | CService(); | |
132 | CService(const CNetAddr& ip, unsigned short port); | |
133 | CService(const struct in_addr& ipv4Addr, unsigned short port); | |
134 | CService(const struct sockaddr_in& addr); | |
c981d768 PW |
135 | explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false); |
136 | explicit CService(const char *pszIpPort, bool fAllowLookup = false); | |
137 | explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false); | |
138 | explicit CService(const std::string& strIpPort, bool fAllowLookup = false); | |
67a42f92 PW |
139 | void Init(); |
140 | void SetPort(unsigned short portIn); | |
141 | unsigned short GetPort() const; | |
8f10a288 PW |
142 | bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; |
143 | bool SetSockAddr(const struct sockaddr* paddr); | |
67a42f92 PW |
144 | friend bool operator==(const CService& a, const CService& b); |
145 | friend bool operator!=(const CService& a, const CService& b); | |
146 | friend bool operator<(const CService& a, const CService& b); | |
147 | std::vector<unsigned char> GetKey() const; | |
148 | std::string ToString() const; | |
149 | std::string ToStringPort() const; | |
150 | std::string ToStringIPPort() const; | |
67a42f92 | 151 | |
67a42f92 | 152 | CService(const struct in6_addr& ipv6Addr, unsigned short port); |
67a42f92 | 153 | CService(const struct sockaddr_in6& addr); |
67a42f92 | 154 | |
84881f8c | 155 | IMPLEMENT_SERIALIZE; |
3d796f89 | 156 | |
84881f8c KD |
157 | template <typename Stream, typename Operation> |
158 | inline size_t SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { | |
3d796f89 KD |
159 | bool fRead = boost::is_same<Operation, CSerActionUnserialize>(); |
160 | size_t nSerSize = 0; | |
84881f8c KD |
161 | CService* pthis = const_cast<CService*>(this); |
162 | READWRITE(FLATDATA(ip)); | |
163 | unsigned short portN = htons(port); | |
3d796f89 KD |
164 | READWRITE(portN); |
165 | if (fRead) | |
67a42f92 | 166 | pthis->port = ntohs(portN); |
3d796f89 KD |
167 | return nSerSize; |
168 | } | |
67a42f92 PW |
169 | }; |
170 | ||
0127a9be | 171 | typedef CService proxyType; |
81bbef26 | 172 | |
587f929c | 173 | enum Network ParseNetwork(std::string net); |
075cf49e | 174 | std::string GetNetworkName(enum Network net); |
1e8aeae1 | 175 | void SplitHostPort(std::string in, int &portOut, std::string &hostOut); |
0127a9be | 176 | bool SetProxy(enum Network net, CService addrProxy); |
81bbef26 | 177 | bool GetProxy(enum Network net, proxyType &proxyInfoOut); |
587f929c | 178 | bool IsProxy(const CNetAddr &addr); |
0127a9be | 179 | bool SetNameProxy(CService addrProxy); |
81bbef26 | 180 | bool HaveNameProxy(); |
3a78f82a | 181 | bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true); |
67a42f92 | 182 | bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); |
3a78f82a | 183 | bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); |
67a42f92 PW |
184 | bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); |
185 | bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); | |
9bab521d | 186 | bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); |
a60838d0 WL |
187 | /** Return readable error string for a network error code */ |
188 | std::string NetworkErrorString(int err); | |
43f510d3 WL |
189 | /** Close socket and set hSocket to INVALID_SOCKET */ |
190 | bool CloseSocket(SOCKET& hSocket); | |
eaedb59e PK |
191 | /** Disable or enable blocking-mode for a socket */ |
192 | bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking); | |
67a42f92 | 193 | |
67a42f92 | 194 | #endif |