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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.
  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. #ifdef WIN32
  19. // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
  20. #undef SetPort
  21. #endif
  22. enum Network
  23. {
  24. NET_UNROUTABLE = 0,
  25. NET_IPV4,
  26. NET_IPV6,
  27. NET_TOR,
  28. NET_MAX,
  29. };
  30. /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
  31. class CNetAddr
  32. {
  33. protected:
  34. unsigned char ip[16]; // in network byte order
  35. public:
  36. CNetAddr();
  37. CNetAddr(const struct in_addr& ipv4Addr);
  38. explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
  39. explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
  40. void Init();
  41. void SetIP(const CNetAddr& ip);
  42. /**
  43. * Set raw IPv4 or IPv6 address (in network byte order)
  44. * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
  45. */
  46. void SetRaw(Network network, const uint8_t *data);
  47. bool SetSpecial(const std::string &strName); // for Tor addresses
  48. bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
  49. bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
  50. bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  51. bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
  52. bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
  53. bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
  54. bool IsRFC4193() const; // IPv6 unique local (FC00::/7)
  55. bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
  56. bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
  57. bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
  58. bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
  59. bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
  60. bool IsTor() const;
  61. bool IsLocal() const;
  62. bool IsRoutable() const;
  63. bool IsValid() const;
  64. bool IsMulticast() const;
  65. enum Network GetNetwork() const;
  66. std::string ToString() const;
  67. std::string ToStringIP() const;
  68. unsigned int GetByte(int n) const;
  69. uint64_t GetHash() const;
  70. bool GetInAddr(struct in_addr* pipv4Addr) const;
  71. std::vector<unsigned char> GetGroup() const;
  72. int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
  73. CNetAddr(const struct in6_addr& pipv6Addr);
  74. bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
  75. friend bool operator==(const CNetAddr& a, const CNetAddr& b);
  76. friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
  77. friend bool operator<(const CNetAddr& a, const CNetAddr& b);
  78. ADD_SERIALIZE_METHODS;
  79. template <typename Stream, typename Operation>
  80. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  81. READWRITE(FLATDATA(ip));
  82. }
  83. };
  84. class CSubNet
  85. {
  86. protected:
  87. /// Network (base) address
  88. CNetAddr network;
  89. /// Netmask, in network byte order
  90. uint8_t netmask[16];
  91. /// Is this value valid? (only used to signal parse errors)
  92. bool valid;
  93. public:
  94. CSubNet();
  95. explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false);
  96. bool Match(const CNetAddr &addr) const;
  97. std::string ToString() const;
  98. bool IsValid() const;
  99. friend bool operator==(const CSubNet& a, const CSubNet& b);
  100. friend bool operator!=(const CSubNet& a, const CSubNet& b);
  101. };
  102. /** A combination of a network address (CNetAddr) and a (TCP) port */
  103. class CService : public CNetAddr
  104. {
  105. protected:
  106. unsigned short port; // host order
  107. public:
  108. CService();
  109. CService(const CNetAddr& ip, unsigned short port);
  110. CService(const struct in_addr& ipv4Addr, unsigned short port);
  111. CService(const struct sockaddr_in& addr);
  112. explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
  113. explicit CService(const char *pszIpPort, bool fAllowLookup = false);
  114. explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
  115. explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
  116. void Init();
  117. void SetPort(unsigned short portIn);
  118. unsigned short GetPort() const;
  119. bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
  120. bool SetSockAddr(const struct sockaddr* paddr);
  121. friend bool operator==(const CService& a, const CService& b);
  122. friend bool operator!=(const CService& a, const CService& b);
  123. friend bool operator<(const CService& a, const CService& b);
  124. std::vector<unsigned char> GetKey() const;
  125. std::string ToString() const;
  126. std::string ToStringPort() const;
  127. std::string ToStringIPPort() const;
  128. CService(const struct in6_addr& ipv6Addr, unsigned short port);
  129. CService(const struct sockaddr_in6& addr);
  130. ADD_SERIALIZE_METHODS;
  131. template <typename Stream, typename Operation>
  132. inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
  133. READWRITE(FLATDATA(ip));
  134. unsigned short portN = htons(port);
  135. READWRITE(portN);
  136. if (ser_action.ForRead())
  137. port = ntohs(portN);
  138. }
  139. };
  140. typedef CService proxyType;
  141. enum Network ParseNetwork(std::string net);
  142. std::string GetNetworkName(enum Network net);
  143. void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
  144. bool SetProxy(enum Network net, CService addrProxy);
  145. bool GetProxy(enum Network net, proxyType &proxyInfoOut);
  146. bool IsProxy(const CNetAddr &addr);
  147. bool SetNameProxy(CService addrProxy);
  148. bool HaveNameProxy();
  149. bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
  150. bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
  151. bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
  152. bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
  153. bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
  154. bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
  155. /** Return readable error string for a network error code */
  156. std::string NetworkErrorString(int err);
  157. /** Close socket and set hSocket to INVALID_SOCKET */
  158. bool CloseSocket(SOCKET& hSocket);
  159. /** Disable or enable blocking-mode for a socket */
  160. bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
  161. #endif // BITCOIN_NETBASE_H