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