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.

netaddress.h 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright (c) 2009-2016 The Starwels 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 STARWELS_NETADDRESS_H
  5. #define STARWELS_NETADDRESS_H
  6. #if defined(HAVE_CONFIG_H)
  7. #include "config/starwels-config.h"
  8. #endif
  9. #include "compat.h"
  10. #include "serialize.h"
  11. #include <stdint.h>
  12. #include <string>
  13. #include <vector>
  14. enum Network
  15. {
  16. NET_UNROUTABLE = 0,
  17. NET_IPV4,
  18. NET_IPV6,
  19. NET_TOR,
  20. NET_INTERNAL,
  21. NET_MAX,
  22. };
  23. /** IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96)) */
  24. class CNetAddr
  25. {
  26. protected:
  27. unsigned char ip[16]; // in network byte order
  28. uint32_t scopeId; // for scoped/link-local ipv6 addresses
  29. public:
  30. CNetAddr();
  31. CNetAddr(const struct in_addr& ipv4Addr);
  32. void Init();
  33. void SetIP(const CNetAddr& ip);
  34. /**
  35. * Set raw IPv4 or IPv6 address (in network byte order)
  36. * @note Only NET_IPV4 and NET_IPV6 are allowed for network.
  37. */
  38. void SetRaw(Network network, const uint8_t *data);
  39. /**
  40. * Transform an arbitrary string into a non-routable ipv6 address.
  41. * Useful for mapping resolved addresses back to their source.
  42. */
  43. bool SetInternal(const std::string& name);
  44. bool SetSpecial(const std::string &strName); // for Tor addresses
  45. bool IsIPv4() const; // IPv4 mapped address (::FFFF:0:0/96, 0.0.0.0/0)
  46. bool IsIPv6() const; // IPv6 address (not mapped IPv4, not Tor)
  47. bool IsRFC1918() const; // IPv4 private networks (10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12)
  48. bool IsRFC2544() const; // IPv4 inter-network communications (192.18.0.0/15)
  49. bool IsRFC6598() const; // IPv4 ISP-level NAT (100.64.0.0/10)
  50. bool IsRFC5737() const; // IPv4 documentation addresses (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
  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 IsInternal() const;
  64. bool IsValid() 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 = nullptr) const;
  73. CNetAddr(const struct in6_addr& pipv6Addr, const uint32_t scope = 0);
  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) {
  81. READWRITE(FLATDATA(ip));
  82. }
  83. friend class CSubNet;
  84. };
  85. class CSubNet
  86. {
  87. protected:
  88. /// Network (base) address
  89. CNetAddr network;
  90. /// Netmask, in network byte order
  91. uint8_t netmask[16];
  92. /// Is this value valid? (only used to signal parse errors)
  93. bool valid;
  94. public:
  95. CSubNet();
  96. CSubNet(const CNetAddr &addr, int32_t mask);
  97. CSubNet(const CNetAddr &addr, const CNetAddr &mask);
  98. //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
  99. explicit CSubNet(const CNetAddr &addr);
  100. bool Match(const CNetAddr &addr) const;
  101. std::string ToString() const;
  102. bool IsValid() const;
  103. friend bool operator==(const CSubNet& a, const CSubNet& b);
  104. friend bool operator!=(const CSubNet& a, const CSubNet& b);
  105. friend bool operator<(const CSubNet& a, const CSubNet& b);
  106. ADD_SERIALIZE_METHODS;
  107. template <typename Stream, typename Operation>
  108. inline void SerializationOp(Stream& s, Operation ser_action) {
  109. READWRITE(network);
  110. READWRITE(FLATDATA(netmask));
  111. READWRITE(FLATDATA(valid));
  112. }
  113. };
  114. /** A combination of a network address (CNetAddr) and a (TCP) port */
  115. class CService : public CNetAddr
  116. {
  117. protected:
  118. unsigned short port; // host order
  119. public:
  120. CService();
  121. CService(const CNetAddr& ip, unsigned short port);
  122. CService(const struct in_addr& ipv4Addr, unsigned short port);
  123. CService(const struct sockaddr_in& addr);
  124. void Init();
  125. unsigned short GetPort() const;
  126. bool GetSockAddr(struct sockaddr* paddr, socklen_t *addrlen) const;
  127. bool SetSockAddr(const struct sockaddr* paddr);
  128. friend bool operator==(const CService& a, const CService& b);
  129. friend bool operator!=(const CService& a, const CService& b);
  130. friend bool operator<(const CService& a, const CService& b);
  131. std::vector<unsigned char> GetKey() const;
  132. std::string ToString() const;
  133. std::string ToStringPort() const;
  134. std::string ToStringIPPort() const;
  135. CService(const struct in6_addr& ipv6Addr, unsigned short port);
  136. CService(const struct sockaddr_in6& addr);
  137. ADD_SERIALIZE_METHODS;
  138. template <typename Stream, typename Operation>
  139. inline void SerializationOp(Stream& s, Operation ser_action) {
  140. READWRITE(FLATDATA(ip));
  141. unsigned short portN = htons(port);
  142. READWRITE(FLATDATA(portN));
  143. if (ser_action.ForRead())
  144. port = ntohs(portN);
  145. }
  146. };
  147. #endif // STARWELS_NETADDRESS_H