00b6850b2ace71997e8cb1f3968b15be4f27f856
[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 license.txt 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 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
21 class CNetAddr
22 {
23     protected:
24         unsigned char ip[16]; // in network byte order
25
26     public:
27         CNetAddr();
28         CNetAddr(const struct in_addr& ipv4Addr);
29         explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
30         explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
31         void Init();
32         void SetIP(const CNetAddr& ip);
33         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
34         bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
35         bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
36         bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
37         bool IsRFC3964() const; // IPv6 6to4 tunneling (2002::/16)
38         bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
39         bool IsRFC4380() const; // IPv6 Teredo tunneling (2001::/32)
40         bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
41         bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
42         bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
43         bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
44         bool IsLocal() const;
45         bool IsRoutable() const;
46         bool IsValid() const;
47         bool IsMulticast() const;
48         std::string ToString() const;
49         std::string ToStringIP() const;
50         int GetByte(int n) const;
51         int64 GetHash() const;
52         bool GetInAddr(struct in_addr* pipv4Addr) const;
53         std::vector<unsigned char> GetGroup() const;
54         void print() const;
55
56 #ifdef USE_IPV6
57         CNetAddr(const struct in6_addr& pipv6Addr);
58         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
59 #endif
60
61         friend bool operator==(const CNetAddr& a, const CNetAddr& b);
62         friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
63         friend bool operator<(const CNetAddr& a, const CNetAddr& b);
64
65         IMPLEMENT_SERIALIZE
66             (
67              READWRITE(FLATDATA(ip));
68             )
69 };
70
71 /** A combination of a network address (CNetAddr) and a (TCP) port */
72 class CService : public CNetAddr
73 {
74     protected:
75         unsigned short port; // host order
76
77     public:
78         CService();
79         CService(const CNetAddr& ip, unsigned short port);
80         CService(const struct in_addr& ipv4Addr, unsigned short port);
81         CService(const struct sockaddr_in& addr);
82         explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
83         explicit CService(const char *pszIpPort, bool fAllowLookup = false);
84         explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
85         explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
86         void Init();
87         void SetPort(unsigned short portIn);
88         unsigned short GetPort() const;
89         bool GetSockAddr(struct sockaddr_in* paddr) const;
90         friend bool operator==(const CService& a, const CService& b);
91         friend bool operator!=(const CService& a, const CService& b);
92         friend bool operator<(const CService& a, const CService& b);
93         std::vector<unsigned char> GetKey() const;
94         std::string ToString() const;
95         std::string ToStringPort() const;
96         std::string ToStringIPPort() const;
97         void print() const;
98
99 #ifdef USE_IPV6
100         CService(const struct in6_addr& ipv6Addr, unsigned short port);
101         bool GetSockAddr6(struct sockaddr_in6* paddr) const;
102         CService(const struct sockaddr_in6& addr);
103 #endif
104
105         IMPLEMENT_SERIALIZE
106             (
107              CService* pthis = const_cast<CService*>(this);
108              READWRITE(FLATDATA(ip));
109              unsigned short portN = htons(port);
110              READWRITE(portN);
111              if (fRead)
112                  pthis->port = ntohs(portN);
113             )
114 };
115
116 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
117 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0);
118 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
119 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
120 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
121 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
122
123 // Settings
124 extern int fUseProxy;
125 extern CService addrProxy;
126
127 #endif