2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
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.
11 #include "webrtc/base/socketaddress.h"
13 #if defined(WEBRTC_POSIX)
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
18 #include <netinet/in_systm.h>
20 #if !defined(__native_client__)
21 #include <netinet/ip.h>
23 #include <arpa/inet.h>
30 #include "webrtc/base/byteorder.h"
31 #include "webrtc/base/common.h"
32 #include "webrtc/base/logging.h"
33 #include "webrtc/base/nethelpers.h"
35 #if defined(WEBRTC_WIN)
36 #include "webrtc/base/win32.h"
41 SocketAddress::SocketAddress() {
45 SocketAddress::SocketAddress(const std::string& hostname, int port) {
50 SocketAddress::SocketAddress(uint32 ip_as_host_order_integer, int port) {
51 SetIP(IPAddress(ip_as_host_order_integer));
55 SocketAddress::SocketAddress(const IPAddress& ip, int port) {
60 SocketAddress::SocketAddress(const SocketAddress& addr) {
61 this->operator=(addr);
64 void SocketAddress::Clear() {
72 bool SocketAddress::IsNil() const {
73 return hostname_.empty() && IPIsUnspec(ip_) && 0 == port_;
76 bool SocketAddress::IsComplete() const {
77 return (!IPIsAny(ip_)) && (0 != port_);
80 SocketAddress& SocketAddress::operator=(const SocketAddress& addr) {
81 hostname_ = addr.hostname_;
84 literal_ = addr.literal_;
85 scope_id_ = addr.scope_id_;
89 void SocketAddress::SetIP(uint32 ip_as_host_order_integer) {
92 ip_ = IPAddress(ip_as_host_order_integer);
96 void SocketAddress::SetIP(const IPAddress& ip) {
103 void SocketAddress::SetIP(const std::string& hostname) {
104 hostname_ = hostname;
105 literal_ = IPFromString(hostname, &ip_);
112 void SocketAddress::SetResolvedIP(uint32 ip_as_host_order_integer) {
113 ip_ = IPAddress(ip_as_host_order_integer);
117 void SocketAddress::SetResolvedIP(const IPAddress& ip) {
122 void SocketAddress::SetPort(int port) {
123 ASSERT((0 <= port) && (port < 65536));
127 uint32 SocketAddress::ip() const {
128 return ip_.v4AddressAsHostOrderInteger();
131 const IPAddress& SocketAddress::ipaddr() const {
135 uint16 SocketAddress::port() const {
139 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())
144 if (ip_.family() == AF_INET6) {
145 return "[" + ip_.ToString() + "]";
147 return ip_.ToString();
151 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())
156 if (ip_.family() == AF_INET6) {
157 return "[" + ip_.ToSensitiveString() + "]";
159 return ip_.ToSensitiveString();
163 std::string SocketAddress::PortAsString() const {
164 std::ostringstream ost;
169 std::string SocketAddress::ToString() const {
170 std::ostringstream ost;
175 std::string SocketAddress::ToSensitiveString() const {
176 std::ostringstream ost;
177 ost << HostAsSensitiveURIString() << ":" << port();
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(), NULL, 10));
188 SetIP(str.substr(1, closebracket - 1));
194 std::string::size_type pos = str.find(':');
195 if (std::string::npos == pos)
197 SetPort(strtoul(str.substr(pos + 1).c_str(), NULL, 10));
198 SetIP(str.substr(0, pos));
203 std::ostream& operator<<(std::ostream& os, const SocketAddress& addr) {
204 os << addr.HostAsURIString() << ":" << addr.port();
208 bool SocketAddress::IsAnyIP() const {
212 bool SocketAddress::IsLoopbackIP() const {
213 return IPIsLoopback(ip_) || (IPIsAny(ip_) &&
214 0 == strcmp(hostname_.c_str(), "localhost"));
217 bool SocketAddress::IsPrivateIP() const {
218 return IPIsPrivate(ip_);
221 bool SocketAddress::IsUnresolvedIP() const {
222 return IPIsUnspec(ip_) && !literal_ && !hostname_.empty();
225 bool SocketAddress::operator==(const SocketAddress& addr) const {
226 return EqualIPs(addr) && EqualPorts(addr);
229 bool SocketAddress::operator<(const SocketAddress& addr) const {
232 else if (addr.ip_ < ip_)
235 // We only check hostnames if both IPs are zero. This matches EqualIPs()
236 if (addr.IsAnyIP()) {
237 if (hostname_ < addr.hostname_)
239 else if (addr.hostname_ < hostname_)
243 return port_ < addr.port_;
246 bool SocketAddress::EqualIPs(const SocketAddress& addr) const {
247 return (ip_ == addr.ip_) &&
248 ((!IPIsAny(ip_)) || (hostname_ == addr.hostname_));
251 bool SocketAddress::EqualPorts(const SocketAddress& addr) const {
252 return (port_ == addr.port_);
255 size_t SocketAddress::Hash() const {
258 h ^= port_ | (port_ << 16);
262 void SocketAddress::ToSockAddr(sockaddr_in* saddr) const {
263 memset(saddr, 0, sizeof(*saddr));
264 if (ip_.family() != AF_INET) {
265 saddr->sin_family = AF_UNSPEC;
268 saddr->sin_family = AF_INET;
269 saddr->sin_port = HostToNetwork16(port_);
271 saddr->sin_addr.s_addr = INADDR_ANY;
273 saddr->sin_addr = ip_.ipv4_address();
277 bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) {
278 if (saddr.sin_family != AF_INET)
280 SetIP(NetworkToHost32(saddr.sin_addr.s_addr));
281 SetPort(NetworkToHost16(saddr.sin_port));
286 static size_t ToSockAddrStorageHelper(sockaddr_storage* addr,
287 IPAddress ip, int port, int scope_id) {
288 memset(addr, 0, sizeof(sockaddr_storage));
289 addr->ss_family = ip.family();
290 if (addr->ss_family == AF_INET6) {
291 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr);
292 saddr->sin6_addr = ip.ipv6_address();
293 saddr->sin6_port = HostToNetwork16(port);
294 saddr->sin6_scope_id = scope_id;
295 return sizeof(sockaddr_in6);
296 } else if (addr->ss_family == AF_INET) {
297 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr);
298 saddr->sin_addr = ip.ipv4_address();
299 saddr->sin_port = HostToNetwork16(port);
300 return sizeof(sockaddr_in);
305 size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const {
306 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_);
309 size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const {
310 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_);
313 std::string SocketAddress::IPToString(uint32 ip_as_host_order_integer) {
314 return IPAddress(ip_as_host_order_integer).ToString();
317 std::string IPToSensitiveString(uint32 ip_as_host_order_integer) {
318 return IPAddress(ip_as_host_order_integer).ToSensitiveString();
321 bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) {
323 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr) == 0)
325 *ip = NetworkToHost32(addr.s_addr);
329 bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) {
331 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) {
333 *ip = IPAddress(addr4);
339 if (rtc::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) {
341 *ip = IPAddress(addr6);
348 uint32 SocketAddress::StringToIP(const std::string& hostname) {
350 StringToIP(hostname, &ip);
354 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr,
355 SocketAddress* out) {
359 if (addr.ss_family == AF_INET) {
360 const sockaddr_in* saddr = reinterpret_cast<const sockaddr_in*>(&addr);
361 *out = SocketAddress(IPAddress(saddr->sin_addr),
362 NetworkToHost16(saddr->sin_port));
364 } else if (addr.ss_family == AF_INET6) {
365 const sockaddr_in6* saddr = reinterpret_cast<const sockaddr_in6*>(&addr);
366 *out = SocketAddress(IPAddress(saddr->sin6_addr),
367 NetworkToHost16(saddr->sin6_port));
368 out->SetScopeID(saddr->sin6_scope_id);
374 SocketAddress EmptySocketAddressWithFamily(int family) {
375 if (family == AF_INET) {
376 return SocketAddress(IPAddress(INADDR_ANY), 0);
377 } else if (family == AF_INET6) {
378 return SocketAddress(IPAddress(in6addr_any), 0);
380 return SocketAddress();