blob: 3f63a91b42e81e51c1492b25edc8cf371224b27f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_IP_ADDRESS_H_
12#define RTC_BASE_IP_ADDRESS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <arpa/inet.h>
16#include <netdb.h>
Yves Gerey665174f2018-06-19 15:03:05 +020017#include <netinet/in.h>
18#include <sys/socket.h>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#endif
20#if defined(WEBRTC_WIN)
21#include <winsock2.h>
22#include <ws2tcpip.h>
23#endif
24#include <string.h>
Jeroen de Borstaf242c82019-04-24 13:13:48 -070025
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020026#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Steve Anton10542f22019-01-11 09:11:00 -080028#include "rtc_base/byte_order.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/win32.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020031#endif
Mirko Bonadei35214fc2019-09-23 14:54:28 +020032#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020033
34namespace rtc {
35
36enum IPv6AddressFlag {
Yves Gerey665174f2018-06-19 15:03:05 +020037 IPV6_ADDRESS_FLAG_NONE = 0x00,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020038
39 // Temporary address is dynamic by nature and will not carry MAC
40 // address.
Yves Gerey665174f2018-06-19 15:03:05 +020041 IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020042
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 Gerey665174f2018-06-19 15:03:05 +020046 IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020047};
48
49// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020050class RTC_EXPORT IPAddress {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020051 public:
Yves Gerey665174f2018-06-19 15:03:05 +020052 IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053
54 explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
55 memset(&u_, 0, sizeof(u_));
56 u_.ip4 = ip4;
57 }
58
Yves Gerey665174f2018-06-19 15:03:05 +020059 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060
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 Gerey665174f2018-06-19 15:03:05 +020072 const IPAddress& operator=(const IPAddress& other) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 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 Gerey665174f2018-06-19 15:03:05 +020080 bool operator<(const IPAddress& other) const;
81 bool operator>(const IPAddress& other) const;
Jonas Olsson3e18c822018-04-18 10:11:07 +020082
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 Kjellanderec78f1c2017-06-29 07:52:50 +020089
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 Bonadei35214fc2019-09-23 14:54:28 +0200127class RTC_EXPORT InterfaceAddress : public IPAddress {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 public:
129 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
130
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800131 explicit InterfaceAddress(IPAddress ip)
132 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200133
134 InterfaceAddress(IPAddress addr, int ipv6_flags)
Yves Gerey665174f2018-06-19 15:03:05 +0200135 : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
Yves Gerey665174f2018-06-19 15:03:05 +0200138 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200139
Yves Gerey665174f2018-06-19 15:03:05 +0200140 const InterfaceAddress& operator=(const InterfaceAddress& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200141
142 bool operator==(const InterfaceAddress& other) const;
143 bool operator!=(const InterfaceAddress& other) const;
144
145 int ipv6_flags() const { return ipv6_flags_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200146
Jonas Olsson74395342018-04-03 12:22:07 +0200147 std::string ToString() const;
148
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149 private:
150 int ipv6_flags_;
151};
152
153bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200154RTC_EXPORT bool IPFromString(const std::string& str, IPAddress* out);
155RTC_EXPORT bool IPFromString(const std::string& str,
156 int flags,
157 InterfaceAddress* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200158bool IPIsAny(const IPAddress& ip);
159bool IPIsLoopback(const IPAddress& ip);
Yuwei Huangb181f712018-01-22 17:01:28 -0800160bool IPIsLinkLocal(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100161// Identify a private network address like "192.168.111.222"
162// (see https://en.wikipedia.org/wiki/Private_network )
163bool IPIsPrivateNetwork(const IPAddress& ip);
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700164// Identify a shared network address like "100.72.16.122"
165// (see RFC6598)
166bool IPIsSharedNetwork(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100167// Identify if an IP is "private", that is a loopback
Jeroen de Borstaf242c82019-04-24 13:13:48 -0700168// or an address belonging to a link-local, a private network or a shared
169// network.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200170RTC_EXPORT bool IPIsPrivate(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200171bool IPIsUnspec(const IPAddress& ip);
172size_t HashIP(const IPAddress& ip);
173
174// These are only really applicable for IPv6 addresses.
175bool IPIs6Bone(const IPAddress& ip);
176bool IPIs6To4(const IPAddress& ip);
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200177RTC_EXPORT bool IPIsMacBased(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200178bool IPIsSiteLocal(const IPAddress& ip);
179bool IPIsTeredo(const IPAddress& ip);
180bool IPIsULA(const IPAddress& ip);
181bool IPIsV4Compatibility(const IPAddress& ip);
182bool IPIsV4Mapped(const IPAddress& ip);
183
184// Returns the precedence value for this IP as given in RFC3484.
185int IPAddressPrecedence(const IPAddress& ip);
186
187// Returns 'ip' truncated to be 'length' bits long.
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200188RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200189
190IPAddress GetLoopbackIP(int family);
191IPAddress 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.
196int CountIPMaskBits(IPAddress mask);
197
198} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000199
Steve Anton10542f22019-01-11 09:11:00 -0800200#endif // RTC_BASE_IP_ADDRESS_H_