blob: 614fd8f575a3161916f93fc7e8a222aa1ba50065 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_IPADDRESS_H_
12#define RTC_BASE_IPADDRESS_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>
25#include <string>
26#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "rtc_base/byteorder.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
32
33namespace rtc {
34
35enum IPv6AddressFlag {
Yves Gerey665174f2018-06-19 15:03:05 +020036 IPV6_ADDRESS_FLAG_NONE = 0x00,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
38 // Temporary address is dynamic by nature and will not carry MAC
39 // address.
Yves Gerey665174f2018-06-19 15:03:05 +020040 IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020041
42 // Temporary address could become deprecated once the preferred
43 // lifetime is reached. It is still valid but just shouldn't be used
44 // to create new connection.
Yves Gerey665174f2018-06-19 15:03:05 +020045 IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020046};
47
48// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
49class IPAddress {
50 public:
Yves Gerey665174f2018-06-19 15:03:05 +020051 IPAddress() : family_(AF_UNSPEC) { ::memset(&u_, 0, sizeof(u_)); }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052
53 explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
54 memset(&u_, 0, sizeof(u_));
55 u_.ip4 = ip4;
56 }
57
Yves Gerey665174f2018-06-19 15:03:05 +020058 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) { u_.ip6 = ip6; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020059
60 explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) {
61 memset(&u_, 0, sizeof(u_));
62 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
63 }
64
65 IPAddress(const IPAddress& other) : family_(other.family_) {
66 ::memcpy(&u_, &other.u_, sizeof(u_));
67 }
68
69 virtual ~IPAddress() {}
70
Yves Gerey665174f2018-06-19 15:03:05 +020071 const IPAddress& operator=(const IPAddress& other) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 family_ = other.family_;
73 ::memcpy(&u_, &other.u_, sizeof(u_));
74 return *this;
75 }
76
77 bool operator==(const IPAddress& other) const;
78 bool operator!=(const IPAddress& other) const;
Yves Gerey665174f2018-06-19 15:03:05 +020079 bool operator<(const IPAddress& other) const;
80 bool operator>(const IPAddress& other) const;
Jonas Olsson3e18c822018-04-18 10:11:07 +020081
82#ifdef UNIT_TEST
83 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
84 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
85 return os << ToString();
86 }
87#endif // UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088
89 int family() const { return family_; }
90 in_addr ipv4_address() const;
91 in6_addr ipv6_address() const;
92
93 // Returns the number of bytes needed to store the raw address.
94 size_t Size() const;
95
96 // Wraps inet_ntop.
97 std::string ToString() const;
98
99 // Same as ToString but anonymizes it by hiding the last part.
100 std::string ToSensitiveString() const;
101
102 // Returns an unmapped address from a possibly-mapped address.
103 // Returns the same address if this isn't a mapped address.
104 IPAddress Normalized() const;
105
106 // Returns this address as an IPv6 address.
107 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
108 IPAddress AsIPv6Address() const;
109
110 // For socketaddress' benefit. Returns the IP in host byte order.
111 uint32_t v4AddressAsHostOrderInteger() const;
112
113 // Whether this is an unspecified IP address.
114 bool IsNil() const;
115
116 private:
117 int family_;
118 union {
119 in_addr ip4;
120 in6_addr ip6;
121 } u_;
122};
123
124// IP class which could represent IPv6 address flags which is only
125// meaningful in IPv6 case.
126class InterfaceAddress : public IPAddress {
127 public:
128 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
129
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800130 explicit InterfaceAddress(IPAddress ip)
131 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200132
133 InterfaceAddress(IPAddress addr, int ipv6_flags)
Yves Gerey665174f2018-06-19 15:03:05 +0200134 : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200135
136 InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
Yves Gerey665174f2018-06-19 15:03:05 +0200137 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138
Yves Gerey665174f2018-06-19 15:03:05 +0200139 const InterfaceAddress& operator=(const InterfaceAddress& other);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200140
141 bool operator==(const InterfaceAddress& other) const;
142 bool operator!=(const InterfaceAddress& other) const;
143
144 int ipv6_flags() const { return ipv6_flags_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200145
Jonas Olsson74395342018-04-03 12:22:07 +0200146 std::string ToString() const;
147
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200148 private:
149 int ipv6_flags_;
150};
151
152bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
153bool IPFromString(const std::string& str, IPAddress* out);
Yves Gerey665174f2018-06-19 15:03:05 +0200154bool IPFromString(const std::string& str, int flags, InterfaceAddress* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200155bool IPIsAny(const IPAddress& ip);
156bool IPIsLoopback(const IPAddress& ip);
Yuwei Huangb181f712018-01-22 17:01:28 -0800157bool IPIsLinkLocal(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100158// Identify a private network address like "192.168.111.222"
159// (see https://en.wikipedia.org/wiki/Private_network )
160bool IPIsPrivateNetwork(const IPAddress& ip);
161// Identify if an IP is "private", that is a loopback
162// or an address belonging to a link-local or a private network.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200163bool IPIsPrivate(const IPAddress& ip);
164bool IPIsUnspec(const IPAddress& ip);
165size_t HashIP(const IPAddress& ip);
166
167// These are only really applicable for IPv6 addresses.
168bool IPIs6Bone(const IPAddress& ip);
169bool IPIs6To4(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200170bool IPIsMacBased(const IPAddress& ip);
171bool IPIsSiteLocal(const IPAddress& ip);
172bool IPIsTeredo(const IPAddress& ip);
173bool IPIsULA(const IPAddress& ip);
174bool IPIsV4Compatibility(const IPAddress& ip);
175bool IPIsV4Mapped(const IPAddress& ip);
176
177// Returns the precedence value for this IP as given in RFC3484.
178int IPAddressPrecedence(const IPAddress& ip);
179
180// Returns 'ip' truncated to be 'length' bits long.
181IPAddress TruncateIP(const IPAddress& ip, int length);
182
183IPAddress GetLoopbackIP(int family);
184IPAddress GetAnyIP(int family);
185
186// Returns the number of contiguously set bits, counting from the MSB in network
187// byte order, in this IPAddress. Bits after the first 0 encountered are not
188// counted.
189int CountIPMaskBits(IPAddress mask);
190
191} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000192
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200193#endif // RTC_BASE_IPADDRESS_H_