]> Git Repo - VerusCoin.git/blame - src/netbase.h
WIN32 -> _WIN32
[VerusCoin.git] / src / netbase.h
CommitLineData
f914f1a7 1// Copyright (c) 2009-2013 The Bitcoin Core developers
78253fcb 2// Distributed under the MIT software license, see the accompanying
3a25a2b9 3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
51ed9ec9 4
67a42f92
PW
5#ifndef BITCOIN_NETBASE_H
6#define BITCOIN_NETBASE_H
7
35b8af92 8#if defined(HAVE_CONFIG_H)
f3967bcc 9#include "config/bitcoin-config.h"
35b8af92
CF
10#endif
11
51ed9ec9
BD
12#include "compat.h"
13#include "serialize.h"
14
15#include <stdint.h>
67a42f92
PW
16#include <string>
17#include <vector>
18
67a42f92 19extern int nConnectTimeout;
cb7a3edc 20extern bool fNameLookup;
67a42f92 21
de10efd1
PK
22/** -timeout default */
23static const int DEFAULT_CONNECT_TIMEOUT = 5000;
24
9cb1ec9c 25#ifdef _WIN32
52d3a481
WL
26// In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
27#undef SetPort
28#endif
67a42f92 29
090e5b40
PW
30enum Network
31{
075cf49e 32 NET_UNROUTABLE = 0,
090e5b40
PW
33 NET_IPV4,
34 NET_IPV6,
35 NET_TOR,
090e5b40 36
d077dd2a 37 NET_MAX,
090e5b40
PW
38};
39
6b8de05d 40/** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
67a42f92
PW
41class CNetAddr
42{
43 protected:
44 unsigned char ip[16]; // in network byte order
45
46 public:
47 CNetAddr();
48 CNetAddr(const struct in_addr& ipv4Addr);
c981d768
PW
49 explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
50 explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
67a42f92
PW
51 void Init();
52 void SetIP(const CNetAddr& ip);
e16be737
WL
53
54 /**
55 * Set raw IPv4 or IPv6 address (in network byte order)
56 * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
57 */
58 void SetRaw(Network network, const uint8_t *data);
59
4e882b79 60 bool SetSpecial(const std::string &strName); // for Tor addresses
67a42f92 61 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
4e882b79 62 bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
67a42f92 63 bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
2d06c0fe
MC
64 bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
65 bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
66 bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
67a42f92
PW
67 bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
68 bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
814efd6f 69 bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
c1761a5d 70 bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
814efd6f 71 bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
67a42f92
PW
72 bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
73 bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
74 bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
75 bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
70f7f003 76 bool IsTor() const;
67a42f92
PW
77 bool IsLocal() const;
78 bool IsRoutable() const;
79 bool IsValid() const;
80 bool IsMulticast() const;
090e5b40 81 enum Network GetNetwork() const;
67a42f92
PW
82 std::string ToString() const;
83 std::string ToStringIP() const;
463a1cab 84 unsigned int GetByte(int n) const;
51ed9ec9 85 uint64_t GetHash() const;
67a42f92
PW
86 bool GetInAddr(struct in_addr* pipv4Addr) const;
87 std::vector<unsigned char> GetGroup() const;
39857190 88 int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
67a42f92 89
67a42f92
PW
90 CNetAddr(const struct in6_addr& pipv6Addr);
91 bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
67a42f92
PW
92
93 friend bool operator==(const CNetAddr& a, const CNetAddr& b);
94 friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
95 friend bool operator<(const CNetAddr& a, const CNetAddr& b);
96
3f6540ad 97 ADD_SERIALIZE_METHODS;
3d796f89 98
84881f8c 99 template <typename Stream, typename Operation>
31e9a838 100 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
84881f8c 101 READWRITE(FLATDATA(ip));
3d796f89 102 }
19e8d7be
WL
103
104 friend class CSubNet;
67a42f92
PW
105};
106
e16be737
WL
107class CSubNet
108{
109 protected:
110 /// Network (base) address
111 CNetAddr network;
112 /// Netmask, in network byte order
113 uint8_t netmask[16];
114 /// Is this value valid? (only used to signal parse errors)
115 bool valid;
116
117 public:
118 CSubNet();
119 explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
120
121 bool Match(const CNetAddr &addr) const;
122
123 std::string ToString() const;
124 bool IsValid() const;
125
126 friend bool operator==(const CSubNet& a, const CSubNet& b);
127 friend bool operator!=(const CSubNet& a, const CSubNet& b);
e5219399 128 friend bool operator<(const CSubNet& a, const CSubNet& b);
e16be737
WL
129};
130
7e05b972 131/** A combination of a network address (CNetAddr) and a (TCP) port */
67a42f92
PW
132class CService : public CNetAddr
133{
134 protected:
135 unsigned short port; // host order
136
137 public:
138 CService();
139 CService(const CNetAddr& ip, unsigned short port);
140 CService(const struct in_addr& ipv4Addr, unsigned short port);
141 CService(const struct sockaddr_in& addr);
c981d768
PW
142 explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
143 explicit CService(const char *pszIpPort, bool fAllowLookup = false);
144 explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
145 explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
67a42f92
PW
146 void Init();
147 void SetPort(unsigned short portIn);
148 unsigned short GetPort() const;
8f10a288
PW
149 bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
150 bool SetSockAddr(const struct sockaddr* paddr);
67a42f92
PW
151 friend bool operator==(const CService& a, const CService& b);
152 friend bool operator!=(const CService& a, const CService& b);
153 friend bool operator<(const CService& a, const CService& b);
154 std::vector<unsigned char> GetKey() const;
155 std::string ToString() const;
156 std::string ToStringPort() const;
157 std::string ToStringIPPort() const;
67a42f92 158
67a42f92 159 CService(const struct in6_addr& ipv6Addr, unsigned short port);
67a42f92 160 CService(const struct sockaddr_in6& addr);
67a42f92 161
3f6540ad 162 ADD_SERIALIZE_METHODS;
3d796f89 163
84881f8c 164 template <typename Stream, typename Operation>
31e9a838 165 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
84881f8c
KD
166 READWRITE(FLATDATA(ip));
167 unsigned short portN = htons(port);
aac32053 168 READWRITE(FLATDATA(portN));
47eb7659 169 if (ser_action.ForRead())
31e9a838 170 port = ntohs(portN);
3d796f89 171 }
67a42f92
PW
172};
173
67a79493
WL
174class proxyType
175{
176public:
177 proxyType(): randomize_credentials(false) {}
178 proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
179
180 bool IsValid() const { return proxy.IsValid(); }
181
182 CService proxy;
183 bool randomize_credentials;
184};
81bbef26 185
587f929c 186enum Network ParseNetwork(std::string net);
075cf49e 187std::string GetNetworkName(enum Network net);
1e8aeae1 188void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
67a79493 189bool SetProxy(enum Network net, const proxyType &addrProxy);
81bbef26 190bool GetProxy(enum Network net, proxyType &proxyInfoOut);
587f929c 191bool IsProxy(const CNetAddr &addr);
67a79493 192bool SetNameProxy(const proxyType &addrProxy);
81bbef26 193bool HaveNameProxy();
3a78f82a 194bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
67a42f92 195bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
3a78f82a 196bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
67a42f92 197bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
35e408f8
WL
198bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
199bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
a60838d0
WL
200/** Return readable error string for a network error code */
201std::string NetworkErrorString(int err);
43f510d3
WL
202/** Close socket and set hSocket to INVALID_SOCKET */
203bool CloseSocket(SOCKET& hSocket);
eaedb59e
PK
204/** Disable or enable blocking-mode for a socket */
205bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
eb5f63fe
WL
206/**
207 * Convert milliseconds to a struct timeval for e.g. select.
208 */
209struct timeval MillisToTimeval(int64_t nTimeout);
67a42f92 210
093303a8 211#endif // BITCOIN_NETBASE_H
This page took 0.21669 seconds and 4 git commands to generate.