blob: c7a7e708e1b46d75cb5b236454d22c4d40498086 [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)
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.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 {
36 IPV6_ADDRESS_FLAG_NONE = 0x00,
37
38 // Temporary address is dynamic by nature and will not carry MAC
39 // address.
40 IPV6_ADDRESS_FLAG_TEMPORARY = 1 << 0,
41
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.
45 IPV6_ADDRESS_FLAG_DEPRECATED = 1 << 1,
46};
47
48// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
49class IPAddress {
50 public:
51 IPAddress() : family_(AF_UNSPEC) {
52 ::memset(&u_, 0, sizeof(u_));
53 }
54
55 explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
56 memset(&u_, 0, sizeof(u_));
57 u_.ip4 = ip4;
58 }
59
60 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) {
61 u_.ip6 = ip6;
62 }
63
64 explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) {
65 memset(&u_, 0, sizeof(u_));
66 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
67 }
68
69 IPAddress(const IPAddress& other) : family_(other.family_) {
70 ::memcpy(&u_, &other.u_, sizeof(u_));
71 }
72
73 virtual ~IPAddress() {}
74
75 const IPAddress & operator=(const IPAddress& other) {
76 family_ = other.family_;
77 ::memcpy(&u_, &other.u_, sizeof(u_));
78 return *this;
79 }
80
81 bool operator==(const IPAddress& other) const;
82 bool operator!=(const IPAddress& other) const;
83 bool operator <(const IPAddress& other) const;
84 bool operator >(const IPAddress& other) const;
Jonas Olsson3e18c822018-04-18 10:11:07 +020085
86#ifdef UNIT_TEST
87 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
88 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
89 return os << ToString();
90 }
91#endif // UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092
93 int family() const { return family_; }
94 in_addr ipv4_address() const;
95 in6_addr ipv6_address() const;
96
97 // Returns the number of bytes needed to store the raw address.
98 size_t Size() const;
99
100 // Wraps inet_ntop.
101 std::string ToString() const;
102
103 // Same as ToString but anonymizes it by hiding the last part.
104 std::string ToSensitiveString() const;
105
106 // Returns an unmapped address from a possibly-mapped address.
107 // Returns the same address if this isn't a mapped address.
108 IPAddress Normalized() const;
109
110 // Returns this address as an IPv6 address.
111 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
112 IPAddress AsIPv6Address() const;
113
114 // For socketaddress' benefit. Returns the IP in host byte order.
115 uint32_t v4AddressAsHostOrderInteger() const;
116
117 // Whether this is an unspecified IP address.
118 bool IsNil() const;
119
120 private:
121 int family_;
122 union {
123 in_addr ip4;
124 in6_addr ip6;
125 } u_;
126};
127
128// IP class which could represent IPv6 address flags which is only
129// meaningful in IPv6 case.
130class InterfaceAddress : public IPAddress {
131 public:
132 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
133
Taylor Brandstetter01cb5f22018-03-07 15:49:32 -0800134 explicit InterfaceAddress(IPAddress ip)
135 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 InterfaceAddress(IPAddress addr, int ipv6_flags)
138 : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
139
140 InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
141 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
142
143 const InterfaceAddress & operator=(const InterfaceAddress& other);
144
145 bool operator==(const InterfaceAddress& other) const;
146 bool operator!=(const InterfaceAddress& other) const;
147
148 int ipv6_flags() const { return ipv6_flags_; }
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200149
Jonas Olsson74395342018-04-03 12:22:07 +0200150 std::string ToString() const;
151
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200152 private:
153 int ipv6_flags_;
154};
155
156bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
157bool IPFromString(const std::string& str, IPAddress* out);
158bool IPFromString(const std::string& str, int flags,
159 InterfaceAddress* out);
160bool IPIsAny(const IPAddress& ip);
161bool IPIsLoopback(const IPAddress& ip);
Yuwei Huangb181f712018-01-22 17:01:28 -0800162bool IPIsLinkLocal(const IPAddress& ip);
Daniel Lazarenko2870b0a2018-01-25 10:30:22 +0100163// Identify a private network address like "192.168.111.222"
164// (see https://en.wikipedia.org/wiki/Private_network )
165bool IPIsPrivateNetwork(const IPAddress& ip);
166// Identify if an IP is "private", that is a loopback
167// or an address belonging to a link-local or a private network.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200168bool IPIsPrivate(const IPAddress& ip);
169bool IPIsUnspec(const IPAddress& ip);
170size_t HashIP(const IPAddress& ip);
171
172// These are only really applicable for IPv6 addresses.
173bool IPIs6Bone(const IPAddress& ip);
174bool IPIs6To4(const IPAddress& ip);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200175bool IPIsMacBased(const IPAddress& ip);
176bool IPIsSiteLocal(const IPAddress& ip);
177bool IPIsTeredo(const IPAddress& ip);
178bool IPIsULA(const IPAddress& ip);
179bool IPIsV4Compatibility(const IPAddress& ip);
180bool IPIsV4Mapped(const IPAddress& ip);
181
182// Returns the precedence value for this IP as given in RFC3484.
183int IPAddressPrecedence(const IPAddress& ip);
184
185// Returns 'ip' truncated to be 'length' bits long.
186IPAddress TruncateIP(const IPAddress& ip, int length);
187
188IPAddress GetLoopbackIP(int family);
189IPAddress GetAnyIP(int family);
190
191// Returns the number of contiguously set bits, counting from the MSB in network
192// byte order, in this IPAddress. Bits after the first 0 encountered are not
193// counted.
194int CountIPMaskBits(IPAddress mask);
195
196} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000197
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200198#endif // RTC_BASE_IPADDRESS_H_