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