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 11 #ifndef RTC_BASE_IP_ADDRESS_H_ 12 #define RTC_BASE_IP_ADDRESS_H_ 13 14 #if defined(WEBRTC_POSIX) 15 #include <arpa/inet.h> 16 #include <netdb.h> 17 #include <netinet/in.h> 18 #include <sys/socket.h> 19 20 #include "absl/strings/string_view.h" 21 #endif 22 #if defined(WEBRTC_WIN) 23 #include <winsock2.h> 24 #include <ws2tcpip.h> 25 #endif 26 #include <string.h> 27 28 #include <string> 29 30 #include "rtc_base/byte_order.h" 31 #if defined(WEBRTC_WIN) 32 #include "rtc_base/win32.h" 33 #endif 34 #include "absl/strings/string_view.h" 35 #include "rtc_base/system/rtc_export.h" 36 namespace rtc { 37 38 enum IPv6AddressFlag { 39 IPV6_ADDRESS_FLAG_NONE = 0x00, 40 41 // Temporary address is dynamic by nature and will not carry MAC 42 // address. 43 IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0, 44 45 // Temporary address could become deprecated once the preferred 46 // lifetime is reached. It is still valid but just shouldn't be used 47 // to create new connection. 48 IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1, 49 }; 50 51 // Version-agnostic IP address class, wraps a union of in_addr and in6_addr. 52 class RTC_EXPORT IPAddress { 53 public: IPAddress()54 IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); } 55 IPAddress(const in_addr & ip4)56 explicit IPAddress(const in_addr& ip4) : family_(AF_INET) { 57 memset(&u_, 0, sizeof(u_)); 58 u_.ip4 = ip4; 59 } 60 IPAddress(const in6_addr & ip6)61 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; } 62 IPAddress(uint32_t ip_in_host_byte_order)63 explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) { 64 memset(&u_, 0, sizeof(u_)); 65 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order); 66 } 67 IPAddress(const IPAddress & other)68 IPAddress(const IPAddress& other) : family_(other.family_) { 69 ::memcpy(&u_, &other.u_, sizeof(u_)); 70 } 71 ~IPAddress()72 virtual ~IPAddress() {} 73 74 const IPAddress& operator=(const IPAddress& other) { 75 family_ = other.family_; 76 ::memcpy(&u_, &other.u_, sizeof(u_)); 77 return *this; 78 } 79 80 bool operator==(const IPAddress& other) const; 81 bool operator!=(const IPAddress& other) const; 82 bool operator<(const IPAddress& other) const; 83 bool operator>(const IPAddress& other) const; 84 85 #ifdef WEBRTC_UNIT_TEST 86 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982) 87 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982) 88 return os << ToString(); 89 } 90 #endif // WEBRTC_UNIT_TEST 91 family()92 int family() const { return family_; } 93 in_addr ipv4_address() const; 94 in6_addr ipv6_address() const; 95 96 // Returns the number of bytes needed to store the raw address. 97 size_t Size() const; 98 99 // Wraps inet_ntop. 100 std::string ToString() const; 101 102 // Same as ToString but anonymizes it by hiding the last part. 103 std::string ToSensitiveString() const; 104 105 // Returns an unmapped address from a possibly-mapped address. 106 // Returns the same address if this isn't a mapped address. 107 IPAddress Normalized() const; 108 109 // Returns this address as an IPv6 address. 110 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged. 111 IPAddress AsIPv6Address() const; 112 113 // For socketaddress' benefit. Returns the IP in host byte order. 114 uint32_t v4AddressAsHostOrderInteger() const; 115 116 // Get the network layer overhead per packet based on the IP address family. 117 int overhead() const; 118 119 // Whether this is an unspecified IP address. 120 bool IsNil() const; 121 122 private: 123 int family_; 124 union { 125 in_addr ip4; 126 in6_addr ip6; 127 } u_; 128 }; 129 130 // IP class which could represent IPv6 address flags which is only 131 // meaningful in IPv6 case. 132 class RTC_EXPORT InterfaceAddress : public IPAddress { 133 public: InterfaceAddress()134 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {} 135 InterfaceAddress(IPAddress ip)136 explicit InterfaceAddress(IPAddress ip) 137 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {} 138 InterfaceAddress(IPAddress addr,int ipv6_flags)139 InterfaceAddress(IPAddress addr, int ipv6_flags) 140 : IPAddress(addr), ipv6_flags_(ipv6_flags) {} 141 InterfaceAddress(const in6_addr & ip6,int ipv6_flags)142 InterfaceAddress(const in6_addr& ip6, int ipv6_flags) 143 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {} 144 145 InterfaceAddress(const InterfaceAddress& other) = default; 146 const InterfaceAddress& operator=(const InterfaceAddress& other); 147 148 bool operator==(const InterfaceAddress& other) const; 149 bool operator!=(const InterfaceAddress& other) const; 150 ipv6_flags()151 int ipv6_flags() const { return ipv6_flags_; } 152 153 std::string ToString() const; 154 155 private: 156 int ipv6_flags_; 157 }; 158 159 bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out); 160 RTC_EXPORT bool IPFromString(absl::string_view str, IPAddress* out); 161 RTC_EXPORT bool IPFromString(absl::string_view str, 162 int flags, 163 InterfaceAddress* out); 164 bool IPIsAny(const IPAddress& ip); 165 bool IPIsLoopback(const IPAddress& ip); 166 bool IPIsLinkLocal(const IPAddress& ip); 167 // Identify a private network address like "192.168.111.222" 168 // (see https://en.wikipedia.org/wiki/Private_network ) 169 bool IPIsPrivateNetwork(const IPAddress& ip); 170 // Identify a shared network address like "100.72.16.122" 171 // (see RFC6598) 172 bool IPIsSharedNetwork(const IPAddress& ip); 173 // Identify if an IP is "private", that is a loopback 174 // or an address belonging to a link-local, a private network or a shared 175 // network. 176 RTC_EXPORT bool IPIsPrivate(const IPAddress& ip); 177 bool IPIsUnspec(const IPAddress& ip); 178 size_t HashIP(const IPAddress& ip); 179 180 // These are only really applicable for IPv6 addresses. 181 bool IPIs6Bone(const IPAddress& ip); 182 bool IPIs6To4(const IPAddress& ip); 183 RTC_EXPORT bool IPIsMacBased(const IPAddress& ip); 184 bool IPIsSiteLocal(const IPAddress& ip); 185 bool IPIsTeredo(const IPAddress& ip); 186 bool IPIsULA(const IPAddress& ip); 187 bool IPIsV4Compatibility(const IPAddress& ip); 188 bool IPIsV4Mapped(const IPAddress& ip); 189 190 // Returns the precedence value for this IP as given in RFC3484. 191 int IPAddressPrecedence(const IPAddress& ip); 192 193 // Returns 'ip' truncated to be 'length' bits long. 194 RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length); 195 196 IPAddress GetLoopbackIP(int family); 197 IPAddress GetAnyIP(int family); 198 199 // Returns the number of contiguously set bits, counting from the MSB in network 200 // byte order, in this IPAddress. Bits after the first 0 encountered are not 201 // counted. 202 int CountIPMaskBits(const IPAddress& mask); 203 204 } // namespace rtc 205 206 #endif // RTC_BASE_IP_ADDRESS_H_ 207