blob: b58a6db3b74a2a5675b7163e177fe21b3ab118bb [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 17:11:0011#ifndef RTC_BASE_SOCKET_ADDRESS_H_
12#define RTC_BASE_SOCKET_ADDRESS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Henrik Kjellanderec78f1c2017-06-29 05:52:5014#include <string>
Ali Tofigh7fa90572022-03-17 14:47:4915
16#include "absl/strings/string_view.h"
Andrey Logvinb95d90b2020-12-09 12:49:3917#ifdef WEBRTC_UNIT_TEST
Jonas Olsson3e18c822018-04-18 08:11:0718#include <ostream> // no-presubmit-check TODO(webrtc:8982)
Andrey Logvinb95d90b2020-12-09 12:49:3919#endif // WEBRTC_UNIT_TEST
Steve Anton10542f22019-01-11 17:11:0020#include "rtc_base/ip_address.h"
Mirko Bonadei35214fc2019-09-23 12:54:2821#include "rtc_base/system/rtc_export.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2622
Henrik Kjellanderec78f1c2017-06-29 05:52:5023#undef SetPort
24
25struct sockaddr_in;
26struct sockaddr_storage;
27
28namespace rtc {
29
30// Records an IP address and port.
Mirko Bonadei35214fc2019-09-23 12:54:2831class RTC_EXPORT SocketAddress {
Henrik Kjellanderec78f1c2017-06-29 05:52:5032 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).
Ali Tofigh7fa90572022-03-17 14:47:4939 SocketAddress(absl::string_view hostname, int port);
Henrik Kjellanderec78f1c2017-06-29 05:52:5040
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.
Ali Tofigh7fa90572022-03-17 14:47:4974 void SetIP(absl::string_view hostname);
Henrik Kjellanderec78f1c2017-06-29 05:52:5075
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
Yves Gerey665174f2018-06-19 13:03:0598 int family() const { return ip_.family(); }
Henrik Kjellanderec78f1c2017-06-29 05:52:5099
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.
Yves Gerey665174f2018-06-19 13:03:05108 int scope_id() const { return scope_id_; }
Henrik Kjellanderec78f1c2017-06-29 05:52:50109 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
Yury Yarashevich41010f92023-01-19 16:56:03129 // Returns sensitive description of address in a form which both includes
130 // resolved and unresolved addresses based on their availability.
131 std::string ToSensitiveNameAndAddressString() const;
Yura Yaroshevichf97afd62021-04-12 12:56:08132
Henrik Kjellanderec78f1c2017-06-29 05:52:50133 // Parses hostname:port and [hostname]:port.
Ali Tofigh7fa90572022-03-17 14:47:49134 bool FromString(absl::string_view str);
Henrik Kjellanderec78f1c2017-06-29 05:52:50135
Andrey Logvinb95d90b2020-12-09 12:49:39136#ifdef WEBRTC_UNIT_TEST
Jonas Olsson3e18c822018-04-18 08:11:07137 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
138 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
139 return os << HostAsURIString() << ":" << port();
140 }
Andrey Logvinb95d90b2020-12-09 12:49:39141#endif // WEBRTC_UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 05:52:50142
143 // Determines whether this represents a missing / any IP address.
144 // That is, 0.0.0.0 or ::.
145 // Hostname and/or port may be set.
146 bool IsAnyIP() const;
147
148 // Determines whether the IP address refers to a loopback address.
149 // For v4 addresses this means the address is in the range 127.0.0.0/8.
150 // For v6 addresses this means the address is ::1.
151 bool IsLoopbackIP() const;
152
153 // Determines whether the IP address is in one of the private ranges:
154 // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12.
155 // For v6: FE80::/16 and ::1.
156 bool IsPrivateIP() const;
157
158 // Determines whether the hostname has been resolved to an IP.
159 bool IsUnresolvedIP() const;
160
161 // Determines whether this address is identical to the given one.
Yves Gerey665174f2018-06-19 13:03:05162 bool operator==(const SocketAddress& addr) const;
163 inline bool operator!=(const SocketAddress& addr) const {
164 return !this->operator==(addr);
Henrik Kjellanderec78f1c2017-06-29 05:52:50165 }
166
167 // Compares based on IP and then port.
Yves Gerey665174f2018-06-19 13:03:05168 bool operator<(const SocketAddress& addr) const;
Henrik Kjellanderec78f1c2017-06-29 05:52:50169
170 // Determines whether this address has the same IP as the one given.
171 bool EqualIPs(const SocketAddress& addr) const;
172
173 // Determines whether this address has the same port as the one given.
174 bool EqualPorts(const SocketAddress& addr) const;
175
176 // Hashes this address into a small number.
177 size_t Hash() const;
178
179 // Write this address to a sockaddr_in.
180 // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC.
181 void ToSockAddr(sockaddr_in* saddr) const;
182
183 // Read this address from a sockaddr_in.
184 bool FromSockAddr(const sockaddr_in& saddr);
185
186 // Read and write the address to/from a sockaddr_storage.
187 // Dual stack version always sets family to AF_INET6, and maps v4 addresses.
188 // The other version doesn't map, and outputs an AF_INET address for
189 // v4 or mapped addresses, and AF_INET6 addresses for others.
190 // Returns the size of the sockaddr_in or sockaddr_in6 structure that is
191 // written to the sockaddr_storage, or zero on failure.
192 size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const;
193 size_t ToSockAddrStorage(sockaddr_storage* saddr) const;
194
195 private:
196 std::string hostname_;
197 IPAddress ip_;
198 uint16_t port_;
199 int scope_id_;
200 bool literal_; // Indicates that 'hostname_' contains a literal IP string.
201};
202
Mirko Bonadei35214fc2019-09-23 12:54:28203RTC_EXPORT bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr,
204 SocketAddress* out);
Henrik Kjellanderec78f1c2017-06-29 05:52:50205SocketAddress EmptySocketAddressWithFamily(int family);
206
207} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26208
Steve Anton10542f22019-01-11 17:11:00209#endif // RTC_BASE_SOCKET_ADDRESS_H_