]> Git Repo - VerusCoin.git/blob - src/netbase.h
checkpoints.cpp depends on main, it can use mapBlockIndex directly
[VerusCoin.git] / src / netbase.h
1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 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 #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
26
27 enum Network
28 {
29     NET_UNROUTABLE = 0,
30     NET_IPV4,
31     NET_IPV6,
32     NET_TOR,
33
34     NET_MAX,
35 };
36
37 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
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);
46         explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
47         explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
48         void Init();
49         void SetIP(const CNetAddr& ip);
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
57         bool SetSpecial(const std::string &strName); // for Tor addresses
58         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
59         bool IsIPv6() const;    // IPv6 address (not mapped IPv4, not Tor)
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)
63         bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
64         bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
65         bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
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)
70         bool IsTor() const;
71         bool IsLocal() const;
72         bool IsRoutable() const;
73         bool IsValid() const;
74         bool IsMulticast() const;
75         enum Network GetNetwork() const;
76         std::string ToString() const;
77         std::string ToStringIP() const;
78         unsigned int GetByte(int n) const;
79         uint64_t GetHash() const;
80         bool GetInAddr(struct in_addr* pipv4Addr) const;
81         std::vector<unsigned char> GetGroup() const;
82         int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
83
84         CNetAddr(const struct in6_addr& pipv6Addr);
85         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
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
91         ADD_SERIALIZE_METHODS;
92
93         template <typename Stream, typename Operation>
94         inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
95             READWRITE(FLATDATA(ip));
96         }
97 };
98
99 class CSubNet
100 {
101     protected:
102         /// Network (base) address
103         CNetAddr network;
104         /// Netmask, in network byte order
105         uint8_t netmask[16];
106         /// Is this value valid? (only used to signal parse errors)
107         bool valid;
108
109     public:
110         CSubNet();
111         explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
112
113         bool Match(const CNetAddr &addr) const;
114
115         std::string ToString() const;
116         bool IsValid() const;
117
118         friend bool operator==(const CSubNet& a, const CSubNet& b);
119         friend bool operator!=(const CSubNet& a, const CSubNet& b);
120 };
121
122 /** A combination of a network address (CNetAddr) and a (TCP) port */
123 class CService : public CNetAddr
124 {
125     protected:
126         unsigned short port; // host order
127
128     public:
129         CService();
130         CService(const CNetAddr& ip, unsigned short port);
131         CService(const struct in_addr& ipv4Addr, unsigned short port);
132         CService(const struct sockaddr_in& addr);
133         explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
134         explicit CService(const char *pszIpPort, bool fAllowLookup = false);
135         explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
136         explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
137         void Init();
138         void SetPort(unsigned short portIn);
139         unsigned short GetPort() const;
140         bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
141         bool SetSockAddr(const struct sockaddr* paddr);
142         friend bool operator==(const CService& a, const CService& b);
143         friend bool operator!=(const CService& a, const CService& b);
144         friend bool operator<(const CService& a, const CService& b);
145         std::vector<unsigned char> GetKey() const;
146         std::string ToString() const;
147         std::string ToStringPort() const;
148         std::string ToStringIPPort() const;
149
150         CService(const struct in6_addr& ipv6Addr, unsigned short port);
151         CService(const struct sockaddr_in6& addr);
152
153         ADD_SERIALIZE_METHODS;
154
155         template <typename Stream, typename Operation>
156         inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
157             READWRITE(FLATDATA(ip));
158             unsigned short portN = htons(port);
159             READWRITE(portN);
160             if (ser_action.ForRead())
161                  port = ntohs(portN);
162         }
163 };
164
165 typedef CService proxyType;
166
167 enum Network ParseNetwork(std::string net);
168 std::string GetNetworkName(enum Network net);
169 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
170 bool SetProxy(enum Network net, CService addrProxy);
171 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
172 bool IsProxy(const CNetAddr &addr);
173 bool SetNameProxy(CService addrProxy);
174 bool HaveNameProxy();
175 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
176 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
177 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
178 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
179 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
180 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
181 /** Return readable error string for a network error code */
182 std::string NetworkErrorString(int err);
183 /** Close socket and set hSocket to INVALID_SOCKET */
184 bool CloseSocket(SOCKET& hSocket);
185 /** Disable or enable blocking-mode for a socket */
186 bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
187
188 #endif // BITCOIN_NETBASE_H
This page took 0.034815 seconds and 4 git commands to generate.