blob: 3f63a91b42e81e51c1492b25edc8cf371224b27f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_IP_ADDRESS_H_
12#define RTC_BASE_IP_ADDRESS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#if defined(WEBRTC_POSIX)
Henrik Kjellanderec78f1c2017-06-29 05:52:5015#include <arpa/inet.h>
16#include <netdb.h>
Yves Gerey665174f2018-06-19 13:03:0517#include <netinet/in.h>
18#include <sys/socket.h>
Henrik Kjellanderec78f1c2017-06-29 05:52:5019#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 20:13:4825
Henrik Kjellanderec78f1c2017-06-29 05:52:5026#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:2627
Steve Anton10542f22019-01-11 17:11:0028#include "rtc_base/byte_order.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5029#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "rtc_base/win32.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5031#endif
Mirko Bonadei35214fc2019-09-23 12:54:2832#include "rtc_base/system/rtc_export.h"
Henrik Kjellanderec78f1c2017-06-29 05:52:5033
34namespace rtc {
35
36enum IPv6AddressFlag {
Yves Gerey665174f2018-06-19 13:03:0537 IPV6_ADDRESS_FLAG_NONE = 0x00,
Henrik Kjellanderec78f1c2017-06-29 05:52:5038
39 // Temporary address is dynamic by nature and will not carry MAC
40 // address.
Yves Gerey665174f2018-06-19 13:03:0541 IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
Henrik Kjellanderec78f1c2017-06-29 05:52:5042
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 13:03:0546 IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
Henrik Kjellanderec78f1c2017-06-29 05:52:5047};
48
49// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
Mirko Bonadei35214fc2019-09-23 12:54:2850class RTC_EXPORT IPAddress {
Henrik Kjellanderec78f1c2017-06-29 05:52:5051 public:
Yves Gerey665174f2018-06-19 13:03:0552 IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5053
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 13:03:0559 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; }
Henrik Kjellanderec78f1c2017-06-29 05:52:5060
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 13:03:0572 const IPAddress& operator=(const IPAddress& other) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5073 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 13:03:0580 bool operator<(const IPAddress& other) const;
81 bool operator>(const IPAddress& other) const;
Jonas Olsson3e18c822018-04-18 08:11:0782
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 05:52:5089
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 12:54:28127class RTC_EXPORT InterfaceAddress : public IPAddress {
Henrik Kjellanderec78f1c2017-06-29 05:52:50128 public:
129 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
130
Taylor Brandstetter01cb5f22018-03-07 23:49:32131 explicit InterfaceAddress(IPAddress ip)
132 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
Henrik Kjellanderec78f1c2017-06-29 05:52:50133
134 InterfaceAddress(IPAddress addr, int ipv6_flags)
Yves Gerey665174f2018-06-19 13:03:05135 : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 05:52:50136
137 InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
Yves Gerey665174f2018-06-19 13:03:05138 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 05:52:50139
Yves Gerey665174f2018-06-19 13:03:05140 const InterfaceAddress& operator=(const InterfaceAddress& other);
Henrik Kjellanderec78f1c2017-06-29 05:52:50141
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 05:52:50146
Jonas Olsson74395342018-04-03 10:22:07147 std::string ToString() const;
148
Henrik Kjellanderec78f1c2017-06-29 05:52:50149 private:
150 int ipv6_flags_;
151};
152
153bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
Mirko Bonadei35214fc2019-09-23 12:54:28154RTC_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 05:52:50158bool IPIsAny(const IPAddress& ip);
159bool IPIsLoopback(const IPAddress& ip);
Yuwei Huangb181f712018-01-23 01:01:28160bool IPIsLinkLocal(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 09:30:22161// 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 20:13:48164// Identify a shared network address like "100.72.16.122"
165// (see RFC6598)
166bool IPIsSharedNetwork(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 09:30:22167// Identify if an IP is "private", that is a loopback
Jeroen de Borstaf242c82019-04-24 20:13:48168// or an address belonging to a link-local, a private network or a shared
169// network.
Mirko Bonadei35214fc2019-09-23 12:54:28170RTC_EXPORT bool IPIsPrivate(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 05:52:50171bool 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 12:54:28177RTC_EXPORT bool IPIsMacBased(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 05:52:50178bool 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 12:54:28188RTC_EXPORT IPAddress TruncateIP(const IPAddress& ip, int length);
Henrik Kjellanderec78f1c2017-06-29 05:52:50189
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:26199
Steve Anton10542f22019-01-11 17:11:00200#endif // RTC_BASE_IP_ADDRESS_H_