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.

200 lines
6.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2011 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_IP_ADDRESS_H_
  11. #define RTC_BASE_IP_ADDRESS_H_
  12. #if defined(WEBRTC_POSIX)
  13. #include <arpa/inet.h>
  14. #include <netdb.h>
  15. #include <netinet/in.h>
  16. #include <sys/socket.h>
  17. #endif
  18. #if defined(WEBRTC_WIN)
  19. #include <winsock2.h>
  20. #include <ws2tcpip.h>
  21. #endif
  22. #include <string.h>
  23. #include <string>
  24. #include "rtc_base/byte_order.h"
  25. #if defined(WEBRTC_WIN)
  26. #include "rtc_base/win32.h"
  27. #endif
  28. #include "rtc_base/system/rtc_export.h"
  29. namespace rtc {
  30. enum IPv6AddressFlag {
  31. IPV6_ADDRESS_FLAG_NONE = 0x00,
  32. // Temporary address is dynamic by nature and will not carry MAC
  33. // address.
  34. IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
  35. // Temporary address could become deprecated once the preferred
  36. // lifetime is reached. It is still valid but just shouldn't be used
  37. // to create new connection.
  38. IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
  39. };
  40. // Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
  41. class RTC_EXPORT IPAddress {
  42. public:
  43. IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); }
  44. explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
  45. memset(&u_, 0, sizeof(u_));
  46. u_.ip4 = ip4;
  47. }
  48. explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; }
  49. explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) {
  50. memset(&u_, 0, sizeof(u_));
  51. u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
  52. }
  53. IPAddress(const IPAddress& other) : family_(other.family_) {
  54. ::memcpy(&u_, &other.u_, sizeof(u_));
  55. }
  56. virtual ~IPAddress() {}
  57. const IPAddress& operator=(const IPAddress& other) {
  58. family_ = other.family_;
  59. ::memcpy(&u_, &other.u_, sizeof(u_));
  60. return *this;
  61. }
  62. bool operator==(const IPAddress& other) const;
  63. bool operator!=(const IPAddress& other) const;
  64. bool operator<(const IPAddress& other) const;
  65. bool operator>(const IPAddress& other) const;
  66. #ifdef UNIT_TEST
  67. inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
  68. std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
  69. return os << ToString();
  70. }
  71. #endif // UNIT_TEST
  72. int family() const { return family_; }
  73. in_addr ipv4_address() const;
  74. in6_addr ipv6_address() const;
  75. // Returns the number of bytes needed to store the raw address.
  76. size_t Size() const;
  77. // Wraps inet_ntop.
  78. std::string ToString() const;
  79. // Same as ToString but anonymizes it by hiding the last part.
  80. std::string ToSensitiveString() const;
  81. // Returns an unmapped address from a possibly-mapped address.
  82. // Returns the same address if this isn't a mapped address.
  83. IPAddress Normalized() const;
  84. // Returns this address as an IPv6 address.
  85. // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
  86. IPAddress AsIPv6Address() const;
  87. // For socketaddress' benefit. Returns the IP in host byte order.
  88. uint32_t v4AddressAsHostOrderInteger() const;
  89. // Whether this is an unspecified IP address.
  90. bool IsNil() const;
  91. private:
  92. int family_;
  93. union {
  94. in_addr ip4;
  95. in6_addr ip6;
  96. } u_;
  97. };
  98. // IP class which could represent IPv6 address flags which is only
  99. // meaningful in IPv6 case.
  100. class RTC_EXPORT InterfaceAddress : public IPAddress {
  101. public:
  102. InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
  103. explicit InterfaceAddress(IPAddress ip)
  104. : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
  105. InterfaceAddress(IPAddress addr, int ipv6_flags)
  106. : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
  107. InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
  108. : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
  109. const InterfaceAddress& operator=(const InterfaceAddress& other);
  110. bool operator==(const InterfaceAddress& other) const;
  111. bool operator!=(const InterfaceAddress& other) const;
  112. int ipv6_flags() const { return ipv6_flags_; }
  113. std::string ToString() const;
  114. private:
  115. int ipv6_flags_;
  116. };
  117. bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
  118. RTC_EXPORT bool IPFromString(const std::string& str, IPAddress* out);
  119. RTC_EXPORT bool IPFromString(const std::string& str,
  120. int flags,
  121. InterfaceAddress* out);
  122. bool IPIsAny(const IPAddress& ip);
  123. bool IPIsLoopback(const IPAddress& ip);
  124. bool IPIsLinkLocal(const IPAddress& ip);
  125. // Identify a private network address like "192.168.111.222"
  126. // (see https://en.wikipedia.org/wiki/Private_network )
  127. bool IPIsPrivateNetwork(const IPAddress& ip);
  128. // Identify a shared network address like "100.72.16.122"
  129. // (see RFC6598)
  130. bool IPIsSharedNetwork(const IPAddress& ip);
  131. // Identify if an IP is "private", that is a loopback
  132. // or an address belonging to a link-local, a private network or a shared
  133. // network.
  134. RTC_EXPORT bool IPIsPrivate(const IPAddress& ip);
  135. bool IPIsUnspec(const IPAddress& ip);
  136. size_t HashIP(const IPAddress& ip);
  137. // These are only really applicable for IPv6 addresses.
  138. bool IPIs6Bone(const IPAddress& ip);
  139. bool IPIs6To4(const IPAddress& ip);
  140. RTC_EXPORT bool IPIsMacBased(const IPAddress& ip);
  141. bool IPIsSiteLocal(const IPAddress& ip);
  142. bool IPIsTeredo(const IPAddress& ip);
  143. bool IPIsULA(const IPAddress& ip);
  144. bool IPIsV4Compatibility(const IPAddress& ip);
  145. bool IPIsV4Mapped(const IPAddress& ip);
  146. // Returns the precedence value for this IP as given in RFC3484.
  147. int IPAddressPrecedence(const IPAddress& ip);
  148. // Returns 'ip' truncated to be 'length' bits long.
  149. RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length);
  150. IPAddress GetLoopbackIP(int family);
  151. IPAddress GetAnyIP(int family);
  152. // Returns the number of contiguously set bits, counting from the MSB in network
  153. // byte order, in this IPAddress. Bits after the first 0 encountered are not
  154. // counted.
  155. int CountIPMaskBits(IPAddress mask);
  156. } // namespace rtc
  157. #endif // RTC_BASE_IP_ADDRESS_H_