netbase.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef ECOIN_NETBASE_H
  3. #define ECOIN_NETBASE_H
  4. #include <string>
  5. #include <vector>
  6. #include "serialize.h"
  7. #include "compat.h"
  8. extern int nConnectTimeout;
  9. #ifdef WIN32
  10. // In MSVC, this is defined as a macro, undefine it to prevent a compile and link error
  11. #undef SetPort
  12. #endif
  13. enum Network
  14. {
  15. NET_UNROUTABLE,
  16. NET_IPV4,
  17. NET_IPV6,
  18. NET_TOR,
  19. NET_I2P,
  20. NET_MAX,
  21. };
  22. extern int nConnectTimeout;
  23. extern bool fNameLookup;
  24. class CNetAddr
  25. {
  26. protected:
  27. unsigned char ip[16]; // in network byte order
  28. public:
  29. CNetAddr();
  30. CNetAddr(const struct in_addr& ipv4Addr);
  31. explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
  32. explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
  33. void Init();
  34. void SetIP(const CNetAddr& ip);
  35. bool SetSpecial(const std::string &strName); // for Tor and I2P addresses
  36. bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
  37. bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor/I2P)
  38. bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  39. bool IsRFC3849() const; // IPv6 documentation address (2001:0DB8::/32)
  40. bool IsRFC3927() const; // IPv4 autoconfig (169.254.0.0/16)
  41. bool IsRFC3964() const; // IPv6 6to4 tunnelling (2002::/16)
  42. bool IsRFC4193() const; // IPv6 unique local (FC00::/15)
  43. bool IsRFC4380() const; // IPv6 Teredo tunnelling (2001::/32)
  44. bool IsRFC4843() const; // IPv6 ORCHID (2001:10::/28)
  45. bool IsRFC4862() const; // IPv6 autoconfig (FE80::/64)
  46. bool IsRFC6052() const; // IPv6 well-known prefix (64:FF9B::/96)
  47. bool IsRFC6145() const; // IPv6 IPv4-translated address (::FFFF:0:0:0/96)
  48. bool IsTor() const;
  49. bool IsI2P() const;
  50. bool IsLocal() const;
  51. bool IsRoutable() const;
  52. bool IsValid() const;
  53. bool IsMulticast() const;
  54. enum Network GetNetwork() const;
  55. std::string ToString() const;
  56. std::string ToStringIP() const;
  57. unsigned int GetByte(int n) const;
  58. uint64 GetHash() const;
  59. bool GetInAddr(struct in_addr* pipv4Addr) const;
  60. std::vector<unsigned char> GetGroup() const;
  61. int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
  62. void print() const;
  63. #ifdef USE_IPV6
  64. CNetAddr(const struct in6_addr& pipv6Addr);
  65. bool GetIn6Addr(struct in6_addr* pipv6Addr) const;
  66. #endif
  67. friend bool operator==(const CNetAddr& a, const CNetAddr& b);
  68. friend bool operator!=(const CNetAddr& a, const CNetAddr& b);
  69. friend bool operator<(const CNetAddr& a, const CNetAddr& b);
  70. IMPLEMENT_SERIALIZE
  71. (
  72. READWRITE(FLATDATA(ip));
  73. )
  74. };
  75. class CService : public CNetAddr
  76. {
  77. protected:
  78. unsigned short port; // host order
  79. public:
  80. CService();
  81. CService(const CNetAddr& ip, unsigned short port);
  82. CService(const struct in_addr& ipv4Addr, unsigned short port);
  83. CService(const struct sockaddr_in& addr);
  84. explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
  85. explicit CService(const char *pszIpPort, bool fAllowLookup = false);
  86. explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
  87. explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
  88. void Init();
  89. void SetPort(unsigned short portIn);
  90. unsigned short GetPort() const;
  91. bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
  92. bool SetSockAddr(const struct sockaddr* paddr);
  93. friend bool operator==(const CService& a, const CService& b);
  94. friend bool operator!=(const CService& a, const CService& b);
  95. friend bool operator<(const CService& a, const CService& b);
  96. std::vector<unsigned char> GetKey() const;
  97. std::string ToString() const;
  98. std::string ToStringPort() const;
  99. std::string ToStringIPPort() const;
  100. void print() const;
  101. #ifdef USE_IPV6
  102. CService(const struct in6_addr& ipv6Addr, unsigned short port);
  103. CService(const struct sockaddr_in6& addr);
  104. #endif
  105. IMPLEMENT_SERIALIZE
  106. (
  107. CService* pthis = const_cast<CService*>(this);
  108. READWRITE(FLATDATA(ip));
  109. unsigned short portN = htons(port);
  110. READWRITE(portN);
  111. if (fRead)
  112. pthis->port = ntohs(portN);
  113. )
  114. };
  115. typedef std::pair<CService, int> proxyType;
  116. enum Network ParseNetwork(std::string net);
  117. void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
  118. bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion = 5);
  119. bool GetProxy(enum Network net, proxyType &proxyInfoOut);
  120. bool IsProxy(const CNetAddr &addr);
  121. bool SetNameProxy(CService addrProxy, int nSocksVersion = 5);
  122. bool HaveNameProxy();
  123. bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
  124. bool LookupHostNumeric(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0);
  125. bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
  126. bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
  127. bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
  128. bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
  129. bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout);
  130. #endif