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