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