blob: 90919c2318c463af72894a1af857c8f27e73fa81 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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_SOCKETADDRESS_H_
12#define RTC_BASE_SOCKETADDRESS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
kjellandere96c45b2017-06-30 10:45:21 -070014#include <iosfwd>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020015#include <string>
Jonas Olssond7ee7202018-04-18 10:11:07 +020016#ifdef UNIT_TEST
17#include <ostream> // no-presubmit-check TODO(webrtc:8982)
18#endif // UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#include <vector>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/basictypes.h"
21#include "rtc_base/ipaddress.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023#undef SetPort
24
25struct sockaddr_in;
26struct sockaddr_storage;
27
28namespace rtc {
29
30// Records an IP address and port.
31class SocketAddress {
32 public:
33 // Creates a nil address.
34 SocketAddress();
35
36 // Creates the address with the given host and port. Host may be a
37 // literal IP string or a hostname to be resolved later.
38 // DCHECKs that port is in valid range (0 to 2^16-1).
39 SocketAddress(const std::string& hostname, int port);
40
41 // Creates the address with the given IP and port.
42 // IP is given as an integer in host byte order. V4 only, to be deprecated.
43 // DCHECKs that port is in valid range (0 to 2^16-1).
44 SocketAddress(uint32_t ip_as_host_order_integer, int port);
45
46 // Creates the address with the given IP and port.
47 // DCHECKs that port is in valid range (0 to 2^16-1).
48 SocketAddress(const IPAddress& ip, int port);
49
50 // Creates a copy of the given address.
51 SocketAddress(const SocketAddress& addr);
52
53 // Resets to the nil address.
54 void Clear();
55
56 // Determines if this is a nil address (empty hostname, any IP, null port)
57 bool IsNil() const;
58
59 // Returns true if ip and port are set.
60 bool IsComplete() const;
61
62 // Replaces our address with the given one.
63 SocketAddress& operator=(const SocketAddress& addr);
64
65 // Changes the IP of this address to the given one, and clears the hostname
66 // IP is given as an integer in host byte order. V4 only, to be deprecated..
67 void SetIP(uint32_t ip_as_host_order_integer);
68
69 // Changes the IP of this address to the given one, and clears the hostname.
70 void SetIP(const IPAddress& ip);
71
72 // Changes the hostname of this address to the given one.
73 // Does not resolve the address; use Resolve to do so.
74 void SetIP(const std::string& hostname);
75
76 // Sets the IP address while retaining the hostname. Useful for bypassing
77 // DNS for a pre-resolved IP.
78 // IP is given as an integer in host byte order. V4 only, to be deprecated.
79 void SetResolvedIP(uint32_t ip_as_host_order_integer);
80
81 // Sets the IP address while retaining the hostname. Useful for bypassing
82 // DNS for a pre-resolved IP.
83 void SetResolvedIP(const IPAddress& ip);
84
85 // Changes the port of this address to the given one.
86 // DCHECKs that port is in valid range (0 to 2^16-1).
87 void SetPort(int port);
88
89 // Returns the hostname.
90 const std::string& hostname() const { return hostname_; }
91
92 // Returns the IP address as a host byte order integer.
93 // Returns 0 for non-v4 addresses.
94 uint32_t ip() const;
95
96 const IPAddress& ipaddr() const;
97
98 int family() const {return ip_.family(); }
99
100 // Returns the port part of this address.
101 uint16_t port() const;
102
103 // Returns the scope ID associated with this address. Scope IDs are a
104 // necessary addition to IPv6 link-local addresses, with different network
105 // interfaces having different scope-ids for their link-local addresses.
106 // IPv4 address do not have scope_ids and sockaddr_in structures do not have
107 // a field for them.
108 int scope_id() const {return scope_id_; }
109 void SetScopeID(int id) { scope_id_ = id; }
110
111 // Returns the 'host' portion of the address (hostname or IP) in a form
112 // suitable for use in a URI. If both IP and hostname are present, hostname
113 // is preferred. IPv6 addresses are enclosed in square brackets ('[' and ']').
114 std::string HostAsURIString() const;
115
116 // Same as HostAsURIString but anonymizes IP addresses by hiding the last
117 // part.
118 std::string HostAsSensitiveURIString() const;
119
120 // Returns the port as a string.
121 std::string PortAsString() const;
122
123 // Returns hostname:port or [hostname]:port.
124 std::string ToString() const;
125
126 // Same as ToString but anonymizes it by hiding the last part.
127 std::string ToSensitiveString() const;
128
129 // Parses hostname:port and [hostname]:port.
130 bool FromString(const std::string& str);
131
Jonas Olssond7ee7202018-04-18 10:11:07 +0200132#ifdef UNIT_TEST
133 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
134 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
135 return os << HostAsURIString() << ":" << port();
136 }
137#endif // UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200138
139 // Determines whether this represents a missing / any IP address.
140 // That is, 0.0.0.0 or ::.
141 // Hostname and/or port may be set.
142 bool IsAnyIP() const;
143
144 // Determines whether the IP address refers to a loopback address.
145 // For v4 addresses this means the address is in the range 127.0.0.0/8.
146 // For v6 addresses this means the address is ::1.
147 bool IsLoopbackIP() const;
148
149 // Determines whether the IP address is in one of the private ranges:
150 // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12.
151 // For v6: FE80::/16 and ::1.
152 bool IsPrivateIP() const;
153
154 // Determines whether the hostname has been resolved to an IP.
155 bool IsUnresolvedIP() const;
156
157 // Determines whether this address is identical to the given one.
158 bool operator ==(const SocketAddress& addr) const;
159 inline bool operator !=(const SocketAddress& addr) const {
160 return !this->operator ==(addr);
161 }
162
163 // Compares based on IP and then port.
164 bool operator <(const SocketAddress& addr) const;
165
166 // Determines whether this address has the same IP as the one given.
167 bool EqualIPs(const SocketAddress& addr) const;
168
169 // Determines whether this address has the same port as the one given.
170 bool EqualPorts(const SocketAddress& addr) const;
171
172 // Hashes this address into a small number.
173 size_t Hash() const;
174
175 // Write this address to a sockaddr_in.
176 // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC.
177 void ToSockAddr(sockaddr_in* saddr) const;
178
179 // Read this address from a sockaddr_in.
180 bool FromSockAddr(const sockaddr_in& saddr);
181
182 // Read and write the address to/from a sockaddr_storage.
183 // Dual stack version always sets family to AF_INET6, and maps v4 addresses.
184 // The other version doesn't map, and outputs an AF_INET address for
185 // v4 or mapped addresses, and AF_INET6 addresses for others.
186 // Returns the size of the sockaddr_in or sockaddr_in6 structure that is
187 // written to the sockaddr_storage, or zero on failure.
188 size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const;
189 size_t ToSockAddrStorage(sockaddr_storage* saddr) const;
190
191 private:
192 std::string hostname_;
193 IPAddress ip_;
194 uint16_t port_;
195 int scope_id_;
196 bool literal_; // Indicates that 'hostname_' contains a literal IP string.
197};
198
199bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr,
200 SocketAddress* out);
201SocketAddress EmptySocketAddressWithFamily(int family);
202
203} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000204
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200205#endif // RTC_BASE_SOCKETADDRESS_H_