• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
11 #include "rtc_base/socket_address.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "rtc_base/numerics/safe_conversions.h"
15 
16 #if defined(WEBRTC_POSIX)
17 #include <netinet/in.h>
18 #include <sys/socket.h>
19 #include <sys/types.h>
20 #if defined(OPENBSD)
21 #include <netinet/in_systm.h>
22 #endif
23 #if !defined(__native_client__)
24 #include <netinet/ip.h>
25 #endif
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #include <unistd.h>
29 #endif
30 
31 #include "rtc_base/byte_order.h"
32 #include "rtc_base/checks.h"
33 #include "rtc_base/logging.h"
34 #include "rtc_base/net_helpers.h"
35 #include "rtc_base/strings/string_builder.h"
36 
37 #if defined(WEBRTC_WIN)
38 #include "rtc_base/win32.h"
39 #endif
40 
41 namespace rtc {
42 
SocketAddress()43 SocketAddress::SocketAddress() {
44   Clear();
45 }
46 
SocketAddress(absl::string_view hostname,int port)47 SocketAddress::SocketAddress(absl::string_view hostname, int port) {
48   SetIP(hostname);
49   SetPort(port);
50 }
51 
SocketAddress(uint32_t ip_as_host_order_integer,int port)52 SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) {
53   SetIP(IPAddress(ip_as_host_order_integer));
54   SetPort(port);
55 }
56 
SocketAddress(const IPAddress & ip,int port)57 SocketAddress::SocketAddress(const IPAddress& ip, int port) {
58   SetIP(ip);
59   SetPort(port);
60 }
61 
SocketAddress(const SocketAddress & addr)62 SocketAddress::SocketAddress(const SocketAddress& addr) {
63   this->operator=(addr);
64 }
65 
Clear()66 void SocketAddress::Clear() {
67   hostname_.clear();
68   literal_ = false;
69   ip_ = IPAddress();
70   port_ = 0;
71   scope_id_ = 0;
72 }
73 
IsNil() const74 bool SocketAddress::IsNil() const {
75   return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
76 }
77 
IsComplete() const78 bool SocketAddress::IsComplete() const {
79   return (!IPIsAny(ip_)) && (0 != port_);
80 }
81 
operator =(const SocketAddress & addr)82 SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
83   hostname_ = addr.hostname_;
84   ip_ = addr.ip_;
85   port_ = addr.port_;
86   literal_ = addr.literal_;
87   scope_id_ = addr.scope_id_;
88   return *this;
89 }
90 
SetIP(uint32_t ip_as_host_order_integer)91 void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) {
92   hostname_.clear();
93   literal_ = false;
94   ip_ = IPAddress(ip_as_host_order_integer);
95   scope_id_ = 0;
96 }
97 
SetIP(const IPAddress & ip)98 void SocketAddress::SetIP(const IPAddress& ip) {
99   hostname_.clear();
100   literal_ = false;
101   ip_ = ip;
102   scope_id_ = 0;
103 }
104 
SetIP(absl::string_view hostname)105 void SocketAddress::SetIP(absl::string_view hostname) {
106   hostname_ = std::string(hostname);
107   literal_ = IPFromString(hostname, &ip_);
108   if (!literal_) {
109     ip_ = IPAddress();
110   }
111   scope_id_ = 0;
112 }
113 
SetResolvedIP(uint32_t ip_as_host_order_integer)114 void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) {
115   ip_ = IPAddress(ip_as_host_order_integer);
116   scope_id_ = 0;
117 }
118 
SetResolvedIP(const IPAddress & ip)119 void SocketAddress::SetResolvedIP(const IPAddress& ip) {
120   ip_ = ip;
121   scope_id_ = 0;
122 }
123 
SetPort(int port)124 void SocketAddress::SetPort(int port) {
125   port_ = rtc::dchecked_cast<uint16_t>(port);
126 }
127 
ip() const128 uint32_t SocketAddress::ip() const {
129   return ip_.v4AddressAsHostOrderInteger();
130 }
131 
ipaddr() const132 const IPAddress& SocketAddress::ipaddr() const {
133   return ip_;
134 }
135 
port() const136 uint16_t SocketAddress::port() const {
137   return port_;
138 }
139 
HostAsURIString() const140 std::string SocketAddress::HostAsURIString() const {
141   // If the hostname was a literal IP string, it may need to have square
142   // brackets added (for SocketAddress::ToString()).
143   if (!literal_ && !hostname_.empty())
144     return hostname_;
145   if (ip_.family() == AF_INET6) {
146     return "[" + ip_.ToString() + "]";
147   } else {
148     return ip_.ToString();
149   }
150 }
151 
HostAsSensitiveURIString() const152 std::string SocketAddress::HostAsSensitiveURIString() const {
153   // If the hostname was a literal IP string, it may need to have square
154   // brackets added (for SocketAddress::ToString()).
155   if (!literal_ && !hostname_.empty())
156     return hostname_;
157   if (ip_.family() == AF_INET6) {
158     return "[" + ip_.ToSensitiveString() + "]";
159   } else {
160     return ip_.ToSensitiveString();
161   }
162 }
163 
PortAsString() const164 std::string SocketAddress::PortAsString() const {
165   return std::to_string(port_);
166 }
167 
ToString() const168 std::string SocketAddress::ToString() const {
169   char buf[1024];
170   rtc::SimpleStringBuilder sb(buf);
171   sb << HostAsURIString() << ":" << port();
172   return sb.str();
173 }
174 
ToSensitiveString() const175 std::string SocketAddress::ToSensitiveString() const {
176   char buf[1024];
177   rtc::SimpleStringBuilder sb(buf);
178   sb << HostAsSensitiveURIString() << ":" << port();
179   return sb.str();
180 }
181 
ToResolvedSensitiveString() const182 std::string SocketAddress::ToResolvedSensitiveString() const {
183   if (IsUnresolvedIP()) {
184     return "";
185   }
186   char buf[1024];
187   rtc::SimpleStringBuilder sb(buf);
188   sb << ipaddr().ToSensitiveString() << ":" << port();
189   return sb.str();
190 }
191 
FromString(absl::string_view str)192 bool SocketAddress::FromString(absl::string_view str) {
193   if (str.at(0) == '[') {
194     absl::string_view::size_type closebracket = str.rfind(']');
195     if (closebracket != absl::string_view::npos) {
196       absl::string_view::size_type colon = str.find(':', closebracket);
197       if (colon != absl::string_view::npos && colon > closebracket) {
198         SetPort(
199             strtoul(std::string(str.substr(colon + 1)).c_str(), nullptr, 10));
200         SetIP(str.substr(1, closebracket - 1));
201       } else {
202         return false;
203       }
204     }
205   } else {
206     absl::string_view::size_type pos = str.find(':');
207     if (absl::string_view::npos == pos)
208       return false;
209     SetPort(strtoul(std::string(str.substr(pos + 1)).c_str(), nullptr, 10));
210     SetIP(str.substr(0, pos));
211   }
212   return true;
213 }
214 
IsAnyIP() const215 bool SocketAddress::IsAnyIP() const {
216   return IPIsAny(ip_);
217 }
218 
IsLoopbackIP() const219 bool SocketAddress::IsLoopbackIP() const {
220   return IPIsLoopback(ip_) ||
221          (IPIsAny(ip_) && 0 == strcmp(hostname_.c_str(), "localhost"));
222 }
223 
IsPrivateIP() const224 bool SocketAddress::IsPrivateIP() const {
225   return IPIsPrivate(ip_);
226 }
227 
IsUnresolvedIP() const228 bool SocketAddress::IsUnresolvedIP() const {
229   return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
230 }
231 
operator ==(const SocketAddress & addr) const232 bool SocketAddress::operator==(const SocketAddress& addr) const {
233   return EqualIPs(addr) && EqualPorts(addr);
234 }
235 
operator <(const SocketAddress & addr) const236 bool SocketAddress::operator<(const SocketAddress& addr) const {
237   if (ip_ != addr.ip_)
238     return ip_ < addr.ip_;
239 
240   // We only check hostnames if both IPs are ANY or unspecified.  This matches
241   // EqualIPs().
242   if ((IPIsAny(ip_) || IPIsUnspec(ip_)) && hostname_ != addr.hostname_)
243     return hostname_ < addr.hostname_;
244 
245   return port_ < addr.port_;
246 }
247 
EqualIPs(const SocketAddress & addr) const248 bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
249   return (ip_ == addr.ip_) &&
250          ((!IPIsAny(ip_) && !IPIsUnspec(ip_)) || (hostname_ == addr.hostname_));
251 }
252 
EqualPorts(const SocketAddress & addr) const253 bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
254   return (port_ == addr.port_);
255 }
256 
Hash() const257 size_t SocketAddress::Hash() const {
258   size_t h = 0;
259   h ^= HashIP(ip_);
260   h ^= port_ | (port_ << 16);
261   return h;
262 }
263 
ToSockAddr(sockaddr_in * saddr) const264 void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
265   memset(saddr, 0, sizeof(*saddr));
266   if (ip_.family() != AF_INET) {
267     saddr->sin_family = AF_UNSPEC;
268     return;
269   }
270   saddr->sin_family = AF_INET;
271   saddr->sin_port = HostToNetwork16(port_);
272   if (IPIsAny(ip_)) {
273     saddr->sin_addr.s_addr = INADDR_ANY;
274   } else {
275     saddr->sin_addr = ip_.ipv4_address();
276   }
277 }
278 
FromSockAddr(const sockaddr_in & saddr)279 bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
280   if (saddr.sin_family != AF_INET)
281     return false;
282   SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
283   SetPort(NetworkToHost16(saddr.sin_port));
284   literal_ = false;
285   return true;
286 }
287 
ToSockAddrStorageHelper(sockaddr_storage * addr,const IPAddress & ip,uint16_t port,int scope_id)288 static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
289                                       const IPAddress& ip,
290                                       uint16_t port,
291                                       int scope_id) {
292   memset(addr, 0, sizeof(sockaddr_storage));
293   addr->ss_family = static_cast<unsigned short>(ip.family());
294   if (addr->ss_family == AF_INET6) {
295     sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
296     saddr->sin6_addr = ip.ipv6_address();
297     saddr->sin6_port = HostToNetwork16(port);
298     saddr->sin6_scope_id = scope_id;
299     return sizeof(sockaddr_in6);
300   } else if (addr->ss_family == AF_INET) {
301     sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
302     saddr->sin_addr = ip.ipv4_address();
303     saddr->sin_port = HostToNetwork16(port);
304     return sizeof(sockaddr_in);
305   }
306   return 0;
307 }
308 
ToDualStackSockAddrStorage(sockaddr_storage * addr) const309 size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage* addr) const {
310   return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
311 }
312 
ToSockAddrStorage(sockaddr_storage * addr) const313 size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
314   return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
315 }
316 
SocketAddressFromSockAddrStorage(const sockaddr_storage & addr,SocketAddress * out)317 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
318                                       SocketAddress* out) {
319   if (!out) {
320     return false;
321   }
322   if (addr.ss_family == AF_INET) {
323     const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
324     *out = SocketAddress(IPAddress(saddr->sin_addr),
325                          NetworkToHost16(saddr->sin_port));
326     return true;
327   } else if (addr.ss_family == AF_INET6) {
328     const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
329     *out = SocketAddress(IPAddress(saddr->sin6_addr),
330                          NetworkToHost16(saddr->sin6_port));
331     out->SetScopeID(saddr->sin6_scope_id);
332     return true;
333   }
334   return false;
335 }
336 
EmptySocketAddressWithFamily(int family)337 SocketAddress EmptySocketAddressWithFamily(int family) {
338   if (family == AF_INET) {
339     return SocketAddress(IPAddress(INADDR_ANY), 0);
340   } else if (family == AF_INET6) {
341     return SocketAddress(IPAddress(in6addr_any), 0);
342   }
343   return SocketAddress();
344 }
345 
346 }  // namespace rtc
347