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