You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netbase.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Copyright (c) 2009-2015 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef BITCOIN_NETBASE_H
  5. #define BITCOIN_NETBASE_H
  6. #if defined(HAVE_CONFIG_H)
  7. #include "config/bitcoin-config.h"
  8. #endif
  9. #include "compat.h"
  10. #include "serialize.h"
  11. #include <stdint.h>
  12. #include <string>
  13. #include <vector>
  14. extern int nConnectTimeout;
  15. extern bool fNameLookup;
  16. //! -timeout default
  17. static const int DEFAULT_CONNECT_TIMEOUT = 5000;
  18. //! -dns default
  19. static const int DEFAULT_NAME_LOOKUP = true;
  20. #ifdef WIN32
  21. // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
  22. #undef SetPort
  23. #endif
  24. enum Network
  25. {
  26. NET_UNROUTABLE = 0,
  27. NET_IPV4,
  28. NET_IPV6,
  29. NET_TOR,
  30. NET_MAX,
  31. };
  32. /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
  33. class CNetAddr
  34. {
  35. protected:
  36. unsigned char ip[16]; // in network byte order
  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. /**
  45. * Set raw IPv4 or IPv6 address (in network byte order)
  46. * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
  47. */
  48. void SetRaw(Network network, const uint8_t *data);
  49. bool SetSpecial(const std::string &strName); // for Tor addresses
  50. bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
  51. bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
  52. bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  53. bool IsRFC2544() const; // IPv4 inter-network communcations (192.18.0.0/15)
  54. bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
  55. bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
  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 tunnelling (2002::/16)
  59. bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
  60. bool IsRFC4380() const; // IPv6 Teredo tunnelling (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 IsTor() const;
  66. bool IsLocal() const;
  67. bool IsRoutable() const;
  68. bool IsValid() const;
  69. bool IsMulticast() const;
  70. enum Network GetNetwork() const;
  71. std::string ToString() const;
  72. std::string ToStringIP() const;
  73. unsigned int GetByte(int n) const;
  74. uint64_t GetHash() const;
  75. bool GetInAddr(struct in_addr* pipv4Addr) const;
  76. std::vector<unsigned char> GetGroup() const;
  77. int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
  78. CNetAddr(const struct in6_addr& pipv6Addr);
  79. bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
  80. friend bool operator==(const CNetAddr& a, const CNetAddr& b);
  81. friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
  82. friend bool operator<(const CNetAddr& a, const CNetAddr& b);
  83. ADD_SERIALIZE_METHODS;
  84. template <typename Stream, typename Operation>
  85. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  86. READWRITE(FLATDATA(ip));
  87. }
  88. friend class CSubNet;
  89. };
  90. class CSubNet
  91. {
  92. protected:
  93. /// Network (base) address
  94. CNetAddr network;
  95. /// Netmask, in network byte order
  96. uint8_t netmask[16];
  97. /// Is this value valid? (only used to signal parse errors)
  98. bool valid;
  99. public:
  100. CSubNet();
  101. explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
  102. //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
  103. explicit CSubNet(const CNetAddr &addr);
  104. bool Match(const CNetAddr &addr) const;
  105. std::string ToString() const;
  106. bool IsValid() const;
  107. friend bool operator==(const CSubNet& a, const CSubNet& b);
  108. friend bool operator!=(const CSubNet& a, const CSubNet& b);
  109. friend bool operator<(const CSubNet& a, const CSubNet& b);
  110. ADD_SERIALIZE_METHODS;
  111. template <typename Stream, typename Operation>
  112. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  113. READWRITE(network);
  114. READWRITE(FLATDATA(netmask));
  115. READWRITE(FLATDATA(valid));
  116. }
  117. };
  118. /** A combination of a network address (CNetAddr) and a (TCP) port */
  119. class CService : public CNetAddr
  120. {
  121. protected:
  122. unsigned short port; // host order
  123. public:
  124. CService();
  125. CService(const CNetAddr& ip, unsigned short port);
  126. CService(const struct in_addr& ipv4Addr, unsigned short port);
  127. CService(const struct sockaddr_in& addr);
  128. explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
  129. explicit CService(const char *pszIpPort, bool fAllowLookup = false);
  130. explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
  131. explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
  132. void Init();
  133. void SetPort(unsigned short portIn);
  134. unsigned short GetPort() const;
  135. bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
  136. bool SetSockAddr(const struct sockaddr* paddr);
  137. friend bool operator==(const CService& a, const CService& b);
  138. friend bool operator!=(const CService& a, const CService& b);
  139. friend bool operator<(const CService& a, const CService& b);
  140. std::vector<unsigned char> GetKey() const;
  141. std::string ToString() const;
  142. std::string ToStringPort() const;
  143. std::string ToStringIPPort() const;
  144. CService(const struct in6_addr& ipv6Addr, unsigned short port);
  145. CService(const struct sockaddr_in6& addr);
  146. ADD_SERIALIZE_METHODS;
  147. template <typename Stream, typename Operation>
  148. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  149. READWRITE(FLATDATA(ip));
  150. unsigned short portN = htons(port);
  151. READWRITE(FLATDATA(portN));
  152. if (ser_action.ForRead())
  153. port = ntohs(portN);
  154. }
  155. };
  156. class proxyType
  157. {
  158. public:
  159. proxyType(): randomize_credentials(false) {}
  160. proxyType(const CService &proxy, bool randomize_credentials=false): proxy(proxy), randomize_credentials(randomize_credentials) {}
  161. bool IsValid() const { return proxy.IsValid(); }
  162. CService proxy;
  163. bool randomize_credentials;
  164. };
  165. enum Network ParseNetwork(std::string net);
  166. std::string GetNetworkName(enum Network net);
  167. void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
  168. bool SetProxy(enum Network net, const proxyType &addrProxy);
  169. bool GetProxy(enum Network net, proxyType &proxyInfoOut);
  170. bool IsProxy(const CNetAddr &addr);
  171. bool SetNameProxy(const proxyType &addrProxy);
  172. bool HaveNameProxy();
  173. bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
  174. bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
  175. bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
  176. bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
  177. bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
  178. bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);
  179. /** Return readable error string for a network error code */
  180. std::string NetworkErrorString(int err);
  181. /** Close socket and set hSocket to INVALID_SOCKET */
  182. bool CloseSocket(SOCKET& hSocket);
  183. /** Disable or enable blocking-mode for a socket */
  184. bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
  185. /**
  186. * Convert milliseconds to a struct timeval for e.g. select.
  187. */
  188. struct timeval MillisToTimeval(int64_t nTimeout);
  189. #endif // BITCOIN_NETBASE_H