1/*
2 * libjingle
3 * Copyright 2011, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28#ifndef TALK_BASE_IPADDRESS_H_
29#define TALK_BASE_IPADDRESS_H_
30
31#ifdef POSIX
32#include <netinet/in.h>
33#include <sys/socket.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36#endif
37#ifdef WIN32
38#include <winsock2.h>
39#include <ws2tcpip.h>
40#endif
41#include <string.h>
42#include <string>
43#include <vector>
44
45#include "talk/base/basictypes.h"
46#include "talk/base/byteorder.h"
47#ifdef WIN32
48#include "talk/base/win32.h"
49#endif
50
51namespace talk_base {
52
53// Version-agnostic IP address class, wraps a union of in_addr and in6_addr.
54class IPAddress {
55 public:
56  IPAddress() : family_(AF_UNSPEC) {
57    ::memset(&u_, 0, sizeof(u_));
58  }
59
60  explicit IPAddress(const in_addr &ip4) : family_(AF_INET) {
61    memset(&u_, 0, sizeof(u_));
62    u_.ip4 = ip4;
63  }
64
65  explicit IPAddress(const in6_addr &ip6) : family_(AF_INET6) {
66    u_.ip6 = ip6;
67  }
68
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);
72  }
73
74  IPAddress(const IPAddress &other) : family_(other.family_) {
75    ::memcpy(&u_, &other.u_, sizeof(u_));
76  }
77
78  ~IPAddress() {}
79
80  const IPAddress & operator=(const IPAddress &other) {
81    family_ = other.family_;
82    ::memcpy(&u_, &other.u_, sizeof(u_));
83    return *this;
84  }
85
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);
91
92  int family() const { return family_; }
93  in_addr ipv4_address() const;
94  in6_addr ipv6_address() const;
95
96  // Returns the number of bytes needed to store the raw address.
97  size_t Size() const;
98
99  // Wraps inet_ntop.
100  std::string ToString() const;
101
102  // Same as ToString but anonymizes it by hiding the last part.
103  std::string ToSensitiveString() const;
104
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;
108
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;
112
113  // For socketaddress' benefit. Returns the IP in host byte order.
114  uint32 v4AddressAsHostOrderInteger() const;
115
116  static void set_strip_sensitive(bool enable);
117
118 private:
119  int family_;
120  union {
121    in_addr ip4;
122    in6_addr ip6;
123  } u_;
124
125  static bool strip_sensitive_;
126};
127
128bool IPFromAddrInfo(struct addrinfo* info, IPAddress* out);
129bool IPFromString(const std::string& str, IPAddress* out);
130bool IPIsAny(const IPAddress& ip);
131bool IPIsLoopback(const IPAddress& ip);
132bool IPIsPrivate(const IPAddress& ip);
133bool IPIsUnspec(const IPAddress& ip);
134size_t HashIP(const IPAddress& ip);
135
136// These are only really applicable for IPv6 addresses.
137bool IPIs6Bone(const IPAddress& ip);
138bool IPIs6To4(const IPAddress& ip);
139bool IPIsSiteLocal(const IPAddress& ip);
140bool IPIsTeredo(const IPAddress& ip);
141bool IPIsULA(const IPAddress& ip);
142bool IPIsV4Compatibility(const IPAddress& ip);
143bool IPIsV4Mapped(const IPAddress& ip);
144
145// Returns the precedence value for this IP as given in RFC3484.
146int IPAddressPrecedence(const IPAddress& ip);
147
148// Returns 'ip' truncated to be 'length' bits long.
149IPAddress TruncateIP(const IPAddress& ip, int length);
150
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
153// counted.
154int CountIPMaskBits(IPAddress mask);
155
156}  // namespace talk_base
157
158#endif  // TALK_BASE_IPADDRESS_H_
159