]>
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 CF |
8 | #if defined(HAVE_CONFIG_H) |
9 | #include "bitcoin-config.h" | |
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 PW |
19 | extern int nConnectTimeout; |
20 | ||
52d3a481 WL |
21 | #ifdef WIN32 |
22 | // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error | |
23 | #undef SetPort | |
24 | #endif | |
67a42f92 | 25 | |
090e5b40 PW |
26 | enum Network |
27 | { | |
28 | NET_UNROUTABLE, | |
29 | NET_IPV4, | |
30 | NET_IPV6, | |
31 | NET_TOR, | |
090e5b40 | 32 | |
d077dd2a | 33 | NET_MAX, |
090e5b40 PW |
34 | }; |
35 | ||
587f929c PW |
36 | extern int nConnectTimeout; |
37 | extern bool fNameLookup; | |
090e5b40 | 38 | |
6b8de05d | 39 | /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */ |
67a42f92 PW |
40 | class CNetAddr |
41 | { | |
42 | protected: | |
43 | unsigned char ip[16]; // in network byte order | |
44 | ||
45 | public: | |
46 | CNetAddr(); | |
47 | CNetAddr(const struct in_addr& ipv4Addr); | |
c981d768 PW |
48 | explicit CNetAddr(const char *pszIp, bool fAllowLookup = false); |
49 | explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false); | |
67a42f92 PW |
50 | void Init(); |
51 | void SetIP(const CNetAddr& ip); | |
4e882b79 | 52 | bool SetSpecial(const std::string &strName); // for Tor addresses |
67a42f92 | 53 | bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0) |
4e882b79 | 54 | bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor) |
67a42f92 PW |
55 | bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12) |
56 | bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32) | |
57 | bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16) | |
814efd6f | 58 | bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16) |
c1761a5d | 59 | bool IsRFC4193() const; // IPv6 unique local (FC00::/7) |
814efd6f | 60 | bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32) |
67a42f92 PW |
61 | bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28) |
62 | bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64) | |
63 | bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96) | |
64 | bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96) | |
70f7f003 | 65 | bool IsTor() const; |
67a42f92 PW |
66 | bool IsLocal() const; |
67 | bool IsRoutable() const; | |
68 | bool IsValid() const; | |
69 | bool IsMulticast() const; | |
090e5b40 | 70 | enum Network GetNetwork() const; |
67a42f92 PW |
71 | std::string ToString() const; |
72 | std::string ToStringIP() const; | |
463a1cab | 73 | unsigned int GetByte(int n) const; |
51ed9ec9 | 74 | uint64_t GetHash() const; |
67a42f92 PW |
75 | bool GetInAddr(struct in_addr* pipv4Addr) const; |
76 | std::vector<unsigned char> GetGroup() const; | |
39857190 | 77 | int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const; |
67a42f92 PW |
78 | void print() const; |
79 | ||
80 | #ifdef USE_IPV6 | |
81 | CNetAddr(const struct in6_addr& pipv6Addr); | |
82 | bool GetIn6Addr(struct in6_addr* pipv6Addr) const; | |
83 | #endif | |
84 | ||
85 | friend bool operator==(const CNetAddr& a, const CNetAddr& b); | |
86 | friend bool operator!=(const CNetAddr& a, const CNetAddr& b); | |
87 | friend bool operator<(const CNetAddr& a, const CNetAddr& b); | |
88 | ||
89 | IMPLEMENT_SERIALIZE | |
90 | ( | |
91 | READWRITE(FLATDATA(ip)); | |
92 | ) | |
93 | }; | |
94 | ||
7e05b972 | 95 | /** A combination of a network address (CNetAddr) and a (TCP) port */ |
67a42f92 PW |
96 | class CService : public CNetAddr |
97 | { | |
98 | protected: | |
99 | unsigned short port; // host order | |
100 | ||
101 | public: | |
102 | CService(); | |
103 | CService(const CNetAddr& ip, unsigned short port); | |
104 | CService(const struct in_addr& ipv4Addr, unsigned short port); | |
105 | CService(const struct sockaddr_in& addr); | |
c981d768 PW |
106 | explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false); |
107 | explicit CService(const char *pszIpPort, bool fAllowLookup = false); | |
108 | explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false); | |
109 | explicit CService(const std::string& strIpPort, bool fAllowLookup = false); | |
67a42f92 PW |
110 | void Init(); |
111 | void SetPort(unsigned short portIn); | |
112 | unsigned short GetPort() const; | |
8f10a288 PW |
113 | bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const; |
114 | bool SetSockAddr(const struct sockaddr* paddr); | |
67a42f92 PW |
115 | friend bool operator==(const CService& a, const CService& b); |
116 | friend bool operator!=(const CService& a, const CService& b); | |
117 | friend bool operator<(const CService& a, const CService& b); | |
118 | std::vector<unsigned char> GetKey() const; | |
119 | std::string ToString() const; | |
120 | std::string ToStringPort() const; | |
121 | std::string ToStringIPPort() const; | |
122 | void print() const; | |
123 | ||
124 | #ifdef USE_IPV6 | |
125 | CService(const struct in6_addr& ipv6Addr, unsigned short port); | |
67a42f92 PW |
126 | CService(const struct sockaddr_in6& addr); |
127 | #endif | |
128 | ||
129 | IMPLEMENT_SERIALIZE | |
130 | ( | |
131 | CService* pthis = const_cast<CService*>(this); | |
132 | READWRITE(FLATDATA(ip)); | |
133 | unsigned short portN = htons(port); | |
134 | READWRITE(portN); | |
135 | if (fRead) | |
136 | pthis->port = ntohs(portN); | |
137 | ) | |
138 | }; | |
139 | ||
81bbef26 PK |
140 | typedef std::pair<CService, int> proxyType; |
141 | ||
587f929c | 142 | enum Network ParseNetwork(std::string net); |
1e8aeae1 | 143 | void SplitHostPort(std::string in, int &portOut, std::string &hostOut); |
587f929c | 144 | bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5); |
81bbef26 | 145 | bool GetProxy(enum Network net, proxyType &proxyInfoOut); |
587f929c PW |
146 | bool IsProxy(const CNetAddr &addr); |
147 | bool SetNameProxy(CService addrProxy, int nSocksVersion = 5); | |
81bbef26 | 148 | bool HaveNameProxy(); |
3a78f82a JG |
149 | bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true); |
150 | bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0); | |
67a42f92 | 151 | bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); |
3a78f82a | 152 | bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); |
67a42f92 PW |
153 | bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); |
154 | bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); | |
9bab521d | 155 | bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); |
67a42f92 | 156 | |
67a42f92 | 157 | #endif |