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