blob: ef1e3d817004813d837e8ae1234a175bc76887bd [file] [log] [blame]
henrike@webrtc.org47be73b2014-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
11#ifndef WEBRTC_BASE_IPADDRESS_H_
12#define WEBRTC_BASE_IPADDRESS_H_
13
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>
27
28#include "webrtc/base/basictypes.h"
29#include "webrtc/base/byteorder.h"
30#if defined(WEBRTC_WIN)
31#include "webrtc/base/win32.h"
32#endif
33
34namespace rtc {
35
guoweis@webrtc.orgb1e61452014-09-09 23:42:4036enum 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
henrike@webrtc.org47be73b2014-05-13 18:00:2649// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
50class IPAddress {
51 public:
52 IPAddress() : family_(AF_UNSPEC) {
53 ::memset(&u_, 0, sizeof(u_));
54 }
55
guoweis@webrtc.orgb1e61452014-09-09 23:42:4056 explicit IPAddress(const in_addr& ip4) : family_(AF_INET) {
henrike@webrtc.org47be73b2014-05-13 18:00:2657 memset(&u_, 0, sizeof(u_));
58 u_.ip4 = ip4;
59 }
60
guoweis@webrtc.orgb1e61452014-09-09 23:42:4061 explicit IPAddress(const in6_addr& ip6) : family_(AF_INET6) {
henrike@webrtc.org47be73b2014-05-13 18:00:2662 u_.ip6 = ip6;
63 }
64
Peter Boström07e22e62015-10-07 10:23:2165 explicit IPAddress(uint32_t ip_in_host_byte_order) : family_(AF_INET) {
henrike@webrtc.org47be73b2014-05-13 18:00:2666 memset(&u_, 0, sizeof(u_));
67 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
68 }
69
guoweis@webrtc.orgb1e61452014-09-09 23:42:4070 IPAddress(const IPAddress& other) : family_(other.family_) {
henrike@webrtc.org47be73b2014-05-13 18:00:2671 ::memcpy(&u_, &other.u_, sizeof(u_));
72 }
73
guoweis@webrtc.orgb1e61452014-09-09 23:42:4074 virtual ~IPAddress() {}
henrike@webrtc.org47be73b2014-05-13 18:00:2675
guoweis@webrtc.orgb1e61452014-09-09 23:42:4076 const IPAddress & operator=(const IPAddress& other) {
henrike@webrtc.org47be73b2014-05-13 18:00:2677 family_ = other.family_;
78 ::memcpy(&u_, &other.u_, sizeof(u_));
79 return *this;
80 }
81
guoweis@webrtc.orgb1e61452014-09-09 23:42:4082 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;
henrike@webrtc.org47be73b2014-05-13 18:00:2686 friend std::ostream& operator<<(std::ostream& os, const IPAddress& addr);
87
88 int family() const { return family_; }
89 in_addr ipv4_address() const;
90 in6_addr ipv6_address() const;
91
92 // Returns the number of bytes needed to store the raw address.
93 size_t Size() const;
94
95 // Wraps inet_ntop.
96 std::string ToString() const;
97
98 // Same as ToString but anonymizes it by hiding the last part.
99 std::string ToSensitiveString() const;
100
101 // Returns an unmapped address from a possibly-mapped address.
102 // Returns the same address if this isn't a mapped address.
103 IPAddress Normalized() const;
104
105 // Returns this address as an IPv6 address.
106 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
107 IPAddress AsIPv6Address() const;
108
109 // For socketaddress' benefit. Returns the IP in host byte order.
Peter Boström07e22e62015-10-07 10:23:21110 uint32_t v4AddressAsHostOrderInteger() const;
henrike@webrtc.org47be73b2014-05-13 18:00:26111
Guo-wei Shieh630f3b32015-08-15 16:28:41112 // Whether this is an unspecified IP address.
113 bool IsNil() const;
114
henrike@webrtc.org47be73b2014-05-13 18:00:26115 private:
116 int family_;
117 union {
118 in_addr ip4;
119 in6_addr ip6;
120 } u_;
henrike@webrtc.org47be73b2014-05-13 18:00:26121};
122
guoweis@webrtc.orgb1e61452014-09-09 23:42:40123// IP class which could represent IPv6 address flags which is only
124// meaningful in IPv6 case.
125class InterfaceAddress : public IPAddress {
126 public:
127 InterfaceAddress() : ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
128
129 InterfaceAddress(IPAddress ip)
130 : IPAddress(ip), ipv6_flags_(IPV6_ADDRESS_FLAG_NONE) {}
131
132 InterfaceAddress(IPAddress addr, int ipv6_flags)
133 : IPAddress(addr), ipv6_flags_(ipv6_flags) {}
134
135 InterfaceAddress(const in6_addr& ip6, int ipv6_flags)
136 : IPAddress(ip6), ipv6_flags_(ipv6_flags) {}
137
138 const InterfaceAddress & operator=(const InterfaceAddress& other);
139
140 bool operator==(const InterfaceAddress& other) const;
141 bool operator!=(const InterfaceAddress& other) const;
142
143 int ipv6_flags() const { return ipv6_flags_; }
144 friend std::ostream& operator<<(std::ostream& os,
145 const InterfaceAddress& addr);
146
147 private:
148 int ipv6_flags_;
149};
150
henrike@webrtc.org47be73b2014-05-13 18:00:26151bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
152bool IPFromString(const std::string& str, IPAddress* out);
guoweis@webrtc.orgb1e61452014-09-09 23:42:40153bool IPFromString(const std::string& str, int flags,
154 InterfaceAddress* out);
henrike@webrtc.org47be73b2014-05-13 18:00:26155bool IPIsAny(const IPAddress& ip);
156bool IPIsLoopback(const IPAddress& ip);
157bool IPIsPrivate(const IPAddress& ip);
158bool IPIsUnspec(const IPAddress& ip);
159size_t HashIP(const IPAddress& ip);
160
161// These are only really applicable for IPv6 addresses.
162bool IPIs6Bone(const IPAddress& ip);
163bool IPIs6To4(const IPAddress& ip);
guoweis@webrtc.org49042bf2015-03-05 04:38:29164bool IPIsLinkLocal(const IPAddress& ip);
guoweis@webrtc.org3e2b5962015-03-17 14:43:20165bool IPIsMacBased(const IPAddress& ip);
henrike@webrtc.org47be73b2014-05-13 18:00:26166bool IPIsSiteLocal(const IPAddress& ip);
167bool IPIsTeredo(const IPAddress& ip);
168bool IPIsULA(const IPAddress& ip);
169bool IPIsV4Compatibility(const IPAddress& ip);
170bool IPIsV4Mapped(const IPAddress& ip);
171
172// Returns the precedence value for this IP as given in RFC3484.
173int IPAddressPrecedence(const IPAddress& ip);
174
175// Returns 'ip' truncated to be 'length' bits long.
176IPAddress TruncateIP(const IPAddress& ip, int length);
177
Guo-wei Shieh4a3a67d2015-08-20 15:48:20178IPAddress GetLoopbackIP(int family);
Guo-wei Shieh9f29fe52015-11-10 22:47:39179IPAddress GetAnyIP(int family);
Guo-wei Shieh4a3a67d2015-08-20 15:48:20180
henrike@webrtc.org47be73b2014-05-13 18:00:26181// Returns the number of contiguously set bits, counting from the MSB in network
182// byte order, in this IPAddress. Bits after the first 0 encountered are not
183// counted.
184int CountIPMaskBits(IPAddress mask);
185
186} // namespace rtc
187
188#endif // WEBRTC_BASE_IPADDRESS_H_