blob: 6ee3d37bcece3b965ac7fe691ca3f02574a21fc9 [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>
Jonas Olsson3e18c822018-04-18 10:11:07 +020015#ifdef UNIT_TEST
16#include <ostream> // no-presubmit-check TODO(webrtc:8982)
17#endif // 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
127 // Parses hostname:port and [hostname]:port.
128 bool FromString(const std::string& str);
129
Jonas Olsson3e18c822018-04-18 10:11:07 +0200130#ifdef UNIT_TEST
131 inline std::ostream& operator<<( // no-presubmit-check TODO(webrtc:8982)
132 std::ostream& os) { // no-presubmit-check TODO(webrtc:8982)
133 return os << HostAsURIString() << ":" << port();
134 }
135#endif // UNIT_TEST
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200136
137 // Determines whether this represents a missing / any IP address.
138 // That is, 0.0.0.0 or ::.
139 // Hostname and/or port may be set.
140 bool IsAnyIP() const;
141
142 // Determines whether the IP address refers to a loopback address.
143 // For v4 addresses this means the address is in the range 127.0.0.0/8.
144 // For v6 addresses this means the address is ::1.
145 bool IsLoopbackIP() const;
146
147 // Determines whether the IP address is in one of the private ranges:
148 // For v4: 127.0.0.0/8 10.0.0.0/8 192.168.0.0/16 172.16.0.0/12.
149 // For v6: FE80::/16 and ::1.
150 bool IsPrivateIP() const;
151
152 // Determines whether the hostname has been resolved to an IP.
153 bool IsUnresolvedIP() const;
154
155 // Determines whether this address is identical to the given one.
Yves Gerey665174f2018-06-19 15:03:05 +0200156 bool operator==(const SocketAddress& addr) const;
157 inline bool operator!=(const SocketAddress& addr) const {
158 return !this->operator==(addr);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200159 }
160
161 // Compares based on IP and then port.
Yves Gerey665174f2018-06-19 15:03:05 +0200162 bool operator<(const SocketAddress& addr) const;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200163
164 // Determines whether this address has the same IP as the one given.
165 bool EqualIPs(const SocketAddress& addr) const;
166
167 // Determines whether this address has the same port as the one given.
168 bool EqualPorts(const SocketAddress& addr) const;
169
170 // Hashes this address into a small number.
171 size_t Hash() const;
172
173 // Write this address to a sockaddr_in.
174 // If IPv6, will zero out the sockaddr_in and sets family to AF_UNSPEC.
175 void ToSockAddr(sockaddr_in* saddr) const;
176
177 // Read this address from a sockaddr_in.
178 bool FromSockAddr(const sockaddr_in& saddr);
179
180 // Read and write the address to/from a sockaddr_storage.
181 // Dual stack version always sets family to AF_INET6, and maps v4 addresses.
182 // The other version doesn't map, and outputs an AF_INET address for
183 // v4 or mapped addresses, and AF_INET6 addresses for others.
184 // Returns the size of the sockaddr_in or sockaddr_in6 structure that is
185 // written to the sockaddr_storage, or zero on failure.
186 size_t ToDualStackSockAddrStorage(sockaddr_storage* saddr) const;
187 size_t ToSockAddrStorage(sockaddr_storage* saddr) const;
188
189 private:
190 std::string hostname_;
191 IPAddress ip_;
192 uint16_t port_;
193 int scope_id_;
194 bool literal_; // Indicates that 'hostname_' contains a literal IP string.
195};
196
Mirko Bonadei35214fc2019-09-23 14:54:28 +0200197RTC_EXPORT bool SocketAddressFromSockAddrStorage(const sockaddr_storage& saddr,
198 SocketAddress* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200199SocketAddress EmptySocketAddressWithFamily(int family);
200
201} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202
Steve Anton10542f22019-01-11 09:11:00 -0800203#endif // RTC_BASE_SOCKET_ADDRESS_H_