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