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