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