3 * Copyright 2011, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef TALK_BASE_IPADDRESS_H_
29 #define TALK_BASE_IPADDRESS_H_
32 #include <netinet/in.h>
33 #include <sys/socket.h>
34 #include <arpa/inet.h>
45 #include "talk/base/basictypes.h"
46 #include "talk/base/byteorder.h"
48 #include "talk/base/win32.h"
53 // Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
56 IPAddress() : family_(AF_UNSPEC) {
57 ::memset(&u_, 0, sizeof(u_));
60 explicit IPAddress(const in_addr &ip4) : family_(AF_INET) {
61 memset(&u_, 0, sizeof(u_));
65 explicit IPAddress(const in6_addr &ip6) : family_(AF_INET6) {
69 explicit IPAddress(uint32 ip_in_host_byte_order) : family_(AF_INET) {
70 memset(&u_, 0, sizeof(u_));
71 u_.ip4.s_addr = HostToNetwork32(ip_in_host_byte_order);
74 IPAddress(const IPAddress &other) : family_(other.family_) {
75 ::memcpy(&u_, &other.u_, sizeof(u_));
80 const IPAddress & operator=(const IPAddress &other) {
81 family_ = other.family_;
82 ::memcpy(&u_, &other.u_, sizeof(u_));
86 bool operator==(const IPAddress &other) const;
87 bool operator!=(const IPAddress &other) const;
88 bool operator <(const IPAddress &other) const;
89 bool operator >(const IPAddress &other) const;
90 friend std::ostream& operator<<(std::ostream& os, const IPAddress& addr);
92 int family() const { return family_; }
93 in_addr ipv4_address() const;
94 in6_addr ipv6_address() const;
96 // Returns the number of bytes needed to store the raw address.
100 std::string ToString() const;
102 // Same as ToString but anonymizes it by hiding the last part.
103 std::string ToSensitiveString() const;
105 // Returns an unmapped address from a possibly-mapped address.
106 // Returns the same address if this isn't a mapped address.
107 IPAddress Normalized() const;
109 // Returns this address as an IPv6 address.
110 // Maps v4 addresses (as ::ffff:a.b.c.d), returns v6 addresses unchanged.
111 IPAddress AsIPv6Address() const;
113 // For socketaddress' benefit. Returns the IP in host byte order.
114 uint32 v4AddressAsHostOrderInteger() const;
116 static void set_strip_sensitive(bool enable);
125 static bool strip_sensitive_;
128 bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
129 bool IPFromString(const std::string& str, IPAddress* out);
130 bool IPIsAny(const IPAddress& ip);
131 bool IPIsLoopback(const IPAddress& ip);
132 bool IPIsPrivate(const IPAddress& ip);
133 bool IPIsUnspec(const IPAddress& ip);
134 size_t HashIP(const IPAddress& ip);
136 // These are only really applicable for IPv6 addresses.
137 bool IPIs6Bone(const IPAddress& ip);
138 bool IPIs6To4(const IPAddress& ip);
139 bool IPIsSiteLocal(const IPAddress& ip);
140 bool IPIsTeredo(const IPAddress& ip);
141 bool IPIsULA(const IPAddress& ip);
142 bool IPIsV4Compatibility(const IPAddress& ip);
143 bool IPIsV4Mapped(const IPAddress& ip);
145 // Returns the precedence value for this IP as given in RFC3484.
146 int IPAddressPrecedence(const IPAddress& ip);
148 // Returns 'ip' truncated to be 'length' bits long.
149 IPAddress TruncateIP(const IPAddress& ip, int length);
151 // Returns the number of contiguously set bits, counting from the MSB in network
152 // byte order, in this IPAddress. Bits after the first 0 encountered are not
154 int CountIPMaskBits(IPAddress mask);
156 } // namespace talk_base
158 #endif // TALK_BASE_IPADDRESS_H_