ip_address.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // Get the network layer overhead per packet based on the IP address family.
  90. int overhead() const;
  91. // Whether this is an unspecified IP address.
  92. bool IsNil() const;
  93. private:
  94. int family_;
  95. union {
  96. in_addr ip4;
  97. in6_addr ip6;
  98. } u_;
  99. };
  100. // IP class which could represent IPv6 address flags which is only
  101. // meaningful in IPv6 case.
  102. class RTC_EXPORT InterfaceAddress : public IPAddress {
  103. public:
  104. InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
  105. explicit InterfaceAddress(IPAddress ip)
  106. : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
  107. InterfaceAddress(IPAddress addr, int ipv6_flags)
  108. : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
  109. InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
  110. : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
  111. InterfaceAddress(const InterfaceAddress& other) = default;
  112. const InterfaceAddress& operator=(const InterfaceAddress& other);
  113. bool operator==(const InterfaceAddress& other) const;
  114. bool operator!=(const InterfaceAddress& other) const;
  115. int ipv6_flags() const { return ipv6_flags_; }
  116. std::string ToString() const;
  117. private:
  118. int ipv6_flags_;
  119. };
  120. bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
  121. RTC_EXPORT bool IPFromString(const std::string& str, IPAddress* out);
  122. RTC_EXPORT bool IPFromString(const std::string& str,
  123. int flags,
  124. InterfaceAddress* out);
  125. bool IPIsAny(const IPAddress& ip);
  126. bool IPIsLoopback(const IPAddress& ip);
  127. bool IPIsLinkLocal(const IPAddress& ip);
  128. // Identify a private network address like "192.168.111.222"
  129. // (see https://en.wikipedia.org/wiki/Private_network )
  130. bool IPIsPrivateNetwork(const IPAddress& ip);
  131. // Identify a shared network address like "100.72.16.122"
  132. // (see RFC6598)
  133. bool IPIsSharedNetwork(const IPAddress& ip);
  134. // Identify if an IP is "private", that is a loopback
  135. // or an address belonging to a link-local, a private network or a shared
  136. // network.
  137. RTC_EXPORT bool IPIsPrivate(const IPAddress& ip);
  138. bool IPIsUnspec(const IPAddress& ip);
  139. size_t HashIP(const IPAddress& ip);
  140. // These are only really applicable for IPv6 addresses.
  141. bool IPIs6Bone(const IPAddress& ip);
  142. bool IPIs6To4(const IPAddress& ip);
  143. RTC_EXPORT bool IPIsMacBased(const IPAddress& ip);
  144. bool IPIsSiteLocal(const IPAddress& ip);
  145. bool IPIsTeredo(const IPAddress& ip);
  146. bool IPIsULA(const IPAddress& ip);
  147. bool IPIsV4Compatibility(const IPAddress& ip);
  148. bool IPIsV4Mapped(const IPAddress& ip);
  149. // Returns the precedence value for this IP as given in RFC3484.
  150. int IPAddressPrecedence(const IPAddress& ip);
  151. // Returns 'ip' truncated to be 'length' bits long.
  152. RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length);
  153. IPAddress GetLoopbackIP(int family);
  154. IPAddress GetAnyIP(int family);
  155. // Returns the number of contiguously set bits, counting from the MSB in network
  156. // byte order, in this IPAddress. Bits after the first 0 encountered are not
  157. // counted.
  158. int CountIPMaskBits(const IPAddress& mask);
  159. } // namespace rtc
  160. #endif // RTC_BASE_IP_ADDRESS_H_