1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_NETBASE_H
6 #define BITCOIN_NETBASE_H
8 #if defined(HAVE_CONFIG_H)
9 #include "bitcoin-config.h"
13 #include "serialize.h"
19 extern int nConnectTimeout;
22 // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
36 extern int nConnectTimeout;
37 extern bool fNameLookup;
39 /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
43 unsigned char ip[16]; // in network byte order
47 CNetAddr(const struct in_addr& ipv4Addr);
48 explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
49 explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
51 void SetIP(const CNetAddr& ip);
54 * Set raw IPv4 or IPv6 address (in network byte order)
55 * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
57 void SetRaw(Network network, const uint8_t *data);
59 bool SetSpecial(const std::string &strName); // for Tor addresses
60 bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
61 bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
62 bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
63 bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
64 bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
65 bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
66 bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
67 bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
68 bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
69 bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
70 bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
71 bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
74 bool IsRoutable() const;
76 bool IsMulticast() const;
77 enum Network GetNetwork() const;
78 std::string ToString() const;
79 std::string ToStringIP() const;
80 unsigned int GetByte(int n) const;
81 uint64_t GetHash() const;
82 bool GetInAddr(struct in_addr* pipv4Addr) const;
83 std::vector<unsigned char> GetGroup() const;
84 int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
87 CNetAddr(const struct in6_addr& pipv6Addr);
88 bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
90 friend bool operator==(const CNetAddr& a, const CNetAddr& b);
91 friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
92 friend bool operator<(const CNetAddr& a, const CNetAddr& b);
96 READWRITE(FLATDATA(ip));
103 /// Network (base) address
105 /// Netmask, in network byte order
107 /// Is this value valid? (only used to signal parse errors)
112 explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
114 bool Match(const CNetAddr &addr) const;
116 std::string ToString() const;
117 bool IsValid() const;
119 friend bool operator==(const CSubNet& a, const CSubNet& b);
120 friend bool operator!=(const CSubNet& a, const CSubNet& b);
123 /** A combination of a network address (CNetAddr) and a (TCP) port */
124 class CService : public CNetAddr
127 unsigned short port; // host order
131 CService(const CNetAddr& ip, unsigned short port);
132 CService(const struct in_addr& ipv4Addr, unsigned short port);
133 CService(const struct sockaddr_in& addr);
134 explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
135 explicit CService(const char *pszIpPort, bool fAllowLookup = false);
136 explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
137 explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
139 void SetPort(unsigned short portIn);
140 unsigned short GetPort() const;
141 bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
142 bool SetSockAddr(const struct sockaddr* paddr);
143 friend bool operator==(const CService& a, const CService& b);
144 friend bool operator!=(const CService& a, const CService& b);
145 friend bool operator<(const CService& a, const CService& b);
146 std::vector<unsigned char> GetKey() const;
147 std::string ToString() const;
148 std::string ToStringPort() const;
149 std::string ToStringIPPort() const;
152 CService(const struct in6_addr& ipv6Addr, unsigned short port);
153 CService(const struct sockaddr_in6& addr);
157 CService* pthis = const_cast<CService*>(this);
158 READWRITE(FLATDATA(ip));
159 unsigned short portN = htons(port);
162 pthis->port = ntohs(portN);
166 typedef std::pair<CService, int> proxyType;
168 enum Network ParseNetwork(std::string net);
169 void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
170 bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5);
171 bool GetProxy(enum Network net, proxyType &proxyInfoOut);
172 bool IsProxy(const CNetAddr &addr);
173 bool SetNameProxy(CService addrProxy, int nSocksVersion = 5);
174 bool HaveNameProxy();
175 bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
176 bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
177 bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
178 bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
179 bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
180 bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
181 /** Return readable error string for a network error code */
182 std::string NetworkErrorString(int err);