blob: e7d649acbfa19851bb964bd71f9cb05bac5039d5 [file] [log] [blame]
henrike@webrtc.orgf7795df2014-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
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
36// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
37class IPAddress {
38 public:
39 IPAddress() : family_(AF_UNSPEC) {
40 ::memset(&u_, 0, sizeof(u_));
41 }
42
43 explicit IPAddress(const in_addr &ip4) : family_(AF_INET) {
44 memset(&u_, 0, sizeof(u_));
45 u_.ip4 = ip4;
46 }
47
48 explicit IPAddress(const in6_addr &ip6) : family_(AF_INET6) {
49 u_.ip6 = ip6;
50 }
51
52 explicit IPAddress(uint32 ip_in_host_byte_order) : family_(AF_INET) {
53 memset(&u_, 0, sizeof(u_));
54 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
55 }
56
57 IPAddress(const IPAddress &other) : family_(other.family_) {
58 ::memcpy(&u_, &other.u_, sizeof(u_));
59 }
60
61 ~IPAddress() {}
62
63 const IPAddress & operator=(const IPAddress &other) {
64 family_ = other.family_;
65 ::memcpy(&u_, &other.u_, sizeof(u_));
66 return *this;
67 }
68
69 bool operator==(const IPAddress &other) const;
70 bool operator!=(const IPAddress &other) const;
71 bool operator <(const IPAddress &other) const;
72 bool operator >(const IPAddress &other) const;
73 friend std::ostream& operator<<(std::ostream& os, const IPAddress& addr);
74
75 int family() const { return family_; }
76 in_addr ipv4_address() const;
77 in6_addr ipv6_address() const;
78
79 // Returns the number of bytes needed to store the raw address.
80 size_t Size() const;
81
82 // Wraps inet_ntop.
83 std::string ToString() const;
84
85 // Same as ToString but anonymizes it by hiding the last part.
86 std::string ToSensitiveString() const;
87
88 // Returns an unmapped address from a possibly-mapped address.
89 // Returns the same address if this isn't a mapped address.
90 IPAddress Normalized() const;
91
92 // Returns this address as an IPv6 address.
93 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
94 IPAddress AsIPv6Address() const;
95
96 // For socketaddress' benefit. Returns the IP in host byte order.
97 uint32 v4AddressAsHostOrderInteger() const;
98
99 static void set_strip_sensitive(bool enable);
100
101 private:
102 int family_;
103 union {
104 in_addr ip4;
105 in6_addr ip6;
106 } u_;
107
108 static bool strip_sensitive_;
109};
110
111bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
112bool IPFromString(const std::string& str, IPAddress* out);
113bool IPIsAny(const IPAddress& ip);
114bool IPIsLoopback(const IPAddress& ip);
115bool IPIsPrivate(const IPAddress& ip);
116bool IPIsUnspec(const IPAddress& ip);
117size_t HashIP(const IPAddress& ip);
118
119// These are only really applicable for IPv6 addresses.
120bool IPIs6Bone(const IPAddress& ip);
121bool IPIs6To4(const IPAddress& ip);
122bool IPIsSiteLocal(const IPAddress& ip);
123bool IPIsTeredo(const IPAddress& ip);
124bool IPIsULA(const IPAddress& ip);
125bool IPIsV4Compatibility(const IPAddress& ip);
126bool IPIsV4Mapped(const IPAddress& ip);
127
128// Returns the precedence value for this IP as given in RFC3484.
129int IPAddressPrecedence(const IPAddress& ip);
130
131// Returns 'ip' truncated to be 'length' bits long.
132IPAddress TruncateIP(const IPAddress& ip, int length);
133
134// Returns the number of contiguously set bits, counting from the MSB in network
135// byte order, in this IPAddress. Bits after the first 0 encountered are not
136// counted.
137int CountIPMaskBits(IPAddress mask);
138
139} // namespace rtc
140
141#endif // WEBRTC_BASE_IPADDRESS_H_