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