We should include netinet/in.h to use sockaddr_in (POSIX.1-2001)
[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 #include <winsock2.h>
13 #include <mswsock.h>
14 #include <ws2tcpip.h>
15 #else
16 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <arpa/inet.h>
19 #include <netdb.h>
20 #include <net/if.h>
21 #include <ifaddrs.h>
22 #include <netinet/in.h>
23 #endif
24
25 #include "serialize.h"
26 #include "compat.h"
27
28 extern int nConnectTimeout;
29
30
31 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
32 class CNetAddr
33 {
34     protected:
35         unsigned char ip[16]; // in network byte order
36
37     public:
38         CNetAddr();
39         CNetAddr(const struct in_addr& ipv4Addr);
40         explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
41         explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
42         void Init();
43         void SetIP(const CNetAddr& ip);
44         bool IsIPv4() const;    // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
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 tunneling (2002::/16)
49         bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
50         bool IsRFC4380() const; // IPv6 Teredo tunneling (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 IsLocal() const;
56         bool IsRoutable() const;
57         bool IsValid() const;
58         bool IsMulticast() const;
59         std::string ToString() const;
60         std::string ToStringIP() const;
61         int GetByte(int n) const;
62         int64 GetHash() const;
63         bool GetInAddr(struct in_addr* pipv4Addr) const;
64         std::vector<unsigned char> GetGroup() const;
65         void print() const;
66
67 #ifdef USE_IPV6
68         CNetAddr(const struct in6_addr& pipv6Addr);
69         bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
70 #endif
71
72         friend bool operator==(const CNetAddr& a, const CNetAddr& b);
73         friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
74         friend bool operator<(const CNetAddr& a, const CNetAddr& b);
75
76         IMPLEMENT_SERIALIZE
77             (
78              READWRITE(FLATDATA(ip));
79             )
80 };
81
82 /** A combnation of a network address (CNetAddr) and a (TCP) port */
83 class CService : public CNetAddr
84 {
85     protected:
86         unsigned short port; // host order
87
88     public:
89         CService();
90         CService(const CNetAddr& ip, unsigned short port);
91         CService(const struct in_addr& ipv4Addr, unsigned short port);
92         CService(const struct sockaddr_in& addr);
93         explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
94         explicit CService(const char *pszIpPort, bool fAllowLookup = false);
95         explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
96         explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
97         void Init();
98         void SetPort(unsigned short portIn);
99         unsigned short GetPort() const;
100         bool GetSockAddr(struct sockaddr_in* paddr) const;
101         friend bool operator==(const CService& a, const CService& b);
102         friend bool operator!=(const CService& a, const CService& b);
103         friend bool operator<(const CService& a, const CService& b);
104         std::vector<unsigned char> GetKey() const;
105         std::string ToString() const;
106         std::string ToStringPort() const;
107         std::string ToStringIPPort() const;
108         void print() const;
109
110 #ifdef USE_IPV6
111         CService(const struct in6_addr& ipv6Addr, unsigned short port);
112         bool GetSockAddr6(struct sockaddr_in6* paddr) const;
113         CService(const struct sockaddr_in6& addr);
114 #endif
115
116         IMPLEMENT_SERIALIZE
117             (
118              CService* pthis = const_cast<CService*>(this);
119              READWRITE(FLATDATA(ip));
120              unsigned short portN = htons(port);
121              READWRITE(portN);
122              if (fRead)
123                  pthis->port = ntohs(portN);
124             )
125 };
126
127 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
128 bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0);
129 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
130 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
131 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
132 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
133
134 // Settings
135 extern int fUseProxy;
136 extern CService addrProxy;
137
138 #endif