henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef RTC_BASE_IP_ADDRESS_H_ |
| 12 | #define RTC_BASE_IP_ADDRESS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #if defined(WEBRTC_POSIX) |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 15 | #include <arpa/inet.h> |
| 16 | #include <netdb.h> |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 17 | #include <netinet/in.h> |
| 18 | #include <sys/socket.h> |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 19 | #endif |
| 20 | #if defined(WEBRTC_WIN) |
| 21 | #include <winsock2.h> |
| 22 | #include <ws2tcpip.h> |
| 23 | #endif |
| 24 | #include <string.h> |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 25 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 26 | #include <string> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 27 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/byte_order.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 29 | #if defined(WEBRTC_WIN) |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 30 | #include "rtc_base/win32.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 31 | #endif |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 32 | #include "rtc_base/system/rtc_export.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 33 | |
| 34 | namespace rtc { |
| 35 | |
| 36 | enum IPv6AddressFlag { |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 37 | IPV6_ADDRESS_FLAG_NONE = 0x00, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 38 | |
| 39 | // Temporary address is dynamic by nature and will not carry MAC |
| 40 | // address. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 41 | IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 46 | IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1, |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | // Version-agnostic IP address class, wraps a union of in_addr and in6_addr. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 50 | class RTC_EXPORT IPAddress { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 51 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 52 | IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 53 | |
| 54 | explicit IPAddress(const in_addr& ip4) : family_(AF_INET) { |
| 55 | memset(&u_, 0, sizeof(u_)); |
| 56 | u_.ip4 = ip4; |
| 57 | } |
| 58 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 59 | explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 60 | |
| 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 | |
| 66 | IPAddress(const IPAddress& other) : family_(other.family_) { |
| 67 | ::memcpy(&u_, &other.u_, sizeof(u_)); |
| 68 | } |
| 69 | |
| 70 | virtual ~IPAddress() {} |
| 71 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 72 | const IPAddress& operator=(const IPAddress& other) { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 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; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 80 | bool operator<(const IPAddress& other) const; |
| 81 | bool operator>(const IPAddress& other) const; |
Jonas Olsson | 3e18c82 | 2018-04-18 08:11:07 | [diff] [blame] | 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 |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 89 | |
| 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 | // Whether this is an unspecified IP address. |
| 115 | bool IsNil() const; |
| 116 | |
| 117 | private: |
| 118 | int family_; |
| 119 | union { |
| 120 | in_addr ip4; |
| 121 | in6_addr ip6; |
| 122 | } u_; |
| 123 | }; |
| 124 | |
| 125 | // IP class which could represent IPv6 address flags which is only |
| 126 | // meaningful in IPv6 case. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 127 | class RTC_EXPORT InterfaceAddress : public IPAddress { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 128 | public: |
| 129 | InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {} |
| 130 | |
Taylor Brandstetter | 01cb5f2 | 2018-03-07 23:49:32 | [diff] [blame] | 131 | explicit InterfaceAddress(IPAddress ip) |
| 132 | : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 133 | |
| 134 | InterfaceAddress(IPAddress addr, int ipv6_flags) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 135 | : IPAddress(addr), ipv6_flags_(ipv6_flags) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 136 | |
| 137 | InterfaceAddress(const in6_addr& ip6, int ipv6_flags) |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 138 | : IPAddress(ip6), ipv6_flags_(ipv6_flags) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 139 | |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 140 | const InterfaceAddress& operator=(const InterfaceAddress& other); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 141 | |
| 142 | bool operator==(const InterfaceAddress& other) const; |
| 143 | bool operator!=(const InterfaceAddress& other) const; |
| 144 | |
| 145 | int ipv6_flags() const { return ipv6_flags_; } |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 146 | |
Jonas Olsson | 7439534 | 2018-04-03 10:22:07 | [diff] [blame] | 147 | std::string ToString() const; |
| 148 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 149 | private: |
| 150 | int ipv6_flags_; |
| 151 | }; |
| 152 | |
| 153 | bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out); |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 154 | RTC_EXPORT bool IPFromString(const std::string& str, IPAddress* out); |
| 155 | RTC_EXPORT bool IPFromString(const std::string& str, |
| 156 | int flags, |
| 157 | InterfaceAddress* out); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 158 | bool IPIsAny(const IPAddress& ip); |
| 159 | bool IPIsLoopback(const IPAddress& ip); |
Yuwei Huang | b181f71 | 2018-01-23 01:01:28 | [diff] [blame] | 160 | bool IPIsLinkLocal(const IPAddress& ip); |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 161 | // Identify a private network address like "192.168.111.222" |
| 162 | // (see https://en.wikipedia.org/wiki/Private_network ) |
| 163 | bool IPIsPrivateNetwork(const IPAddress& ip); |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 164 | // Identify a shared network address like "100.72.16.122" |
| 165 | // (see RFC6598) |
| 166 | bool IPIsSharedNetwork(const IPAddress& ip); |
Daniel Lazarenko | 2870b0a | 2018-01-25 09:30:22 | [diff] [blame] | 167 | // Identify if an IP is "private", that is a loopback |
Jeroen de Borst | af242c8 | 2019-04-24 20:13:48 | [diff] [blame] | 168 | // or an address belonging to a link-local, a private network or a shared |
| 169 | // network. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 170 | RTC_EXPORT bool IPIsPrivate(const IPAddress& ip); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 171 | bool IPIsUnspec(const IPAddress& ip); |
| 172 | size_t HashIP(const IPAddress& ip); |
| 173 | |
| 174 | // These are only really applicable for IPv6 addresses. |
| 175 | bool IPIs6Bone(const IPAddress& ip); |
| 176 | bool IPIs6To4(const IPAddress& ip); |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 177 | RTC_EXPORT bool IPIsMacBased(const IPAddress& ip); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 178 | bool IPIsSiteLocal(const IPAddress& ip); |
| 179 | bool IPIsTeredo(const IPAddress& ip); |
| 180 | bool IPIsULA(const IPAddress& ip); |
| 181 | bool IPIsV4Compatibility(const IPAddress& ip); |
| 182 | bool IPIsV4Mapped(const IPAddress& ip); |
| 183 | |
| 184 | // Returns the precedence value for this IP as given in RFC3484. |
| 185 | int IPAddressPrecedence(const IPAddress& ip); |
| 186 | |
| 187 | // Returns 'ip' truncated to be 'length' bits long. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 188 | RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 189 | |
| 190 | IPAddress GetLoopbackIP(int family); |
| 191 | IPAddress GetAnyIP(int family); |
| 192 | |
| 193 | // Returns the number of contiguously set bits, counting from the MSB in network |
| 194 | // byte order, in this IPAddress. Bits after the first 0 encountered are not |
| 195 | // counted. |
| 196 | int CountIPMaskBits(IPAddress mask); |
| 197 | |
| 198 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 199 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 200 | #endif // RTC_BASE_IP_ADDRESS_H_ |