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

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