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