VC2010 compile fixes
[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 #ifdef WIN32
11 #define _WIN32_WINNT 0x0501
12 #define WIN32_LEAN_AND_MEAN 1
13 #ifndef NOMINMAX
14 #define NOMINMAX
15 #endif
16 #include <winsock2.h>
17 #include <mswsock.h>
18 #include <ws2tcpip.h>
19 #else
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <net/if.h>
25 #include <ifaddrs.h>
26 #endif
27 #ifdef BSD
28 #include <netinet/in.h>
29 #endif
30
31 #include "serialize.h"
32 #include "compat.h"
33
34 extern int nConnectTimeout;
35
36 #ifdef WIN32
37 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
38 #undef SetPort
39 #endif
40
41 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
42 class CNetAddr
43 {
44     protected:
45         unsigned char ip[16]; // in network byte order
46
47     public:
48         CNetAddr();
49         CNetAddr(const struct in_addr& ipv4Addr);
50         explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
51         explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
52         void Init();
53         void SetIP(const CNetAddr& ip);
54         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
55         bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
56         bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
57         bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
58         bool IsRFC3964() const; // IPv6 6to4 tunneling (2002::/16)
59         bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
60         bool IsRFC4380() const; // IPv6 Teredo tunneling (2001::/32)
61         bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
62         bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
63         bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
64         bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
65         bool IsLocal() const;
66         bool IsRoutable() const;
67         bool IsValid() const;
68         bool IsMulticast() const;
69         std::string ToString() const;
70         std::string ToStringIP() const;
71         int GetByte(int n) const;
72         int64 GetHash() const;
73         bool GetInAddr(struct in_addr* pipv4Addr) const;
74         std::vector<unsigned char> GetGroup() const;
75         void print() const;
76
77 #ifdef USE_IPV6
78         CNetAddr(const struct in6_addr& pipv6Addr);
79         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
80 #endif
81
82         friend bool operator==(const CNetAddr& a, const CNetAddr& b);
83         friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
84         friend bool operator<(const CNetAddr& a, const CNetAddr& b);
85
86         IMPLEMENT_SERIALIZE
87             (
88              READWRITE(FLATDATA(ip));
89             )
90 };
91
92 /** A combination of a network address (CNetAddr) and a (TCP) port */
93 class CService : public CNetAddr
94 {
95     protected:
96         unsigned short port; // host order
97
98     public:
99         CService();
100         CService(const CNetAddr& ip, unsigned short port);
101         CService(const struct in_addr& ipv4Addr, unsigned short port);
102         CService(const struct sockaddr_in& addr);
103         explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
104         explicit CService(const char *pszIpPort, bool fAllowLookup = false);
105         explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
106         explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
107         void Init();
108         void SetPort(unsigned short portIn);
109         unsigned short GetPort() const;
110         bool GetSockAddr(struct sockaddr_in* paddr) const;
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, unsigned short port);
122         bool GetSockAddr6(struct sockaddr_in6* paddr) const;
123         CService(const struct sockaddr_in6& addr);
124 #endif
125
126         IMPLEMENT_SERIALIZE
127             (
128              CService* pthis = const_cast<CService*>(this);
129              READWRITE(FLATDATA(ip));
130              unsigned short portN = htons(port);
131              READWRITE(portN);
132              if (fRead)
133                  pthis->port = ntohs(portN);
134             )
135 };
136
137 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions = 0, bool fAllowLookup = true);
138 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, int nMaxSolutions = 0);
139 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
140 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, int nMaxSolutions = 0);
141 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
142 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
143
144 // Settings
145 extern int fUseProxy;
146 extern CService addrProxy;
147
148 #endif