OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 SocketAddress::SocketAddress() { | 41 SocketAddress::SocketAddress() { |
42 Clear(); | 42 Clear(); |
43 } | 43 } |
44 | 44 |
45 SocketAddress::SocketAddress(const std::string& hostname, int port) { | 45 SocketAddress::SocketAddress(const std::string& hostname, int port) { |
46 SetIP(hostname); | 46 SetIP(hostname); |
47 SetPort(port); | 47 SetPort(port); |
48 } | 48 } |
49 | 49 |
50 SocketAddress::SocketAddress(uint32 ip_as_host_order_integer, int port) { | 50 SocketAddress::SocketAddress(uint32_t ip_as_host_order_integer, int port) { |
51 SetIP(IPAddress(ip_as_host_order_integer)); | 51 SetIP(IPAddress(ip_as_host_order_integer)); |
52 SetPort(port); | 52 SetPort(port); |
53 } | 53 } |
54 | 54 |
55 SocketAddress::SocketAddress(const IPAddress& ip, int port) { | 55 SocketAddress::SocketAddress(const IPAddress& ip, int port) { |
56 SetIP(ip); | 56 SetIP(ip); |
57 SetPort(port); | 57 SetPort(port); |
58 } | 58 } |
59 | 59 |
60 SocketAddress::SocketAddress(const SocketAddress& addr) { | 60 SocketAddress::SocketAddress(const SocketAddress& addr) { |
(...skipping 18 matching lines...) Expand all Loading... |
79 | 79 |
80 SocketAddress& SocketAddress::operator=(const SocketAddress& addr) { | 80 SocketAddress& SocketAddress::operator=(const SocketAddress& addr) { |
81 hostname_ = addr.hostname_; | 81 hostname_ = addr.hostname_; |
82 ip_ = addr.ip_; | 82 ip_ = addr.ip_; |
83 port_ = addr.port_; | 83 port_ = addr.port_; |
84 literal_ = addr.literal_; | 84 literal_ = addr.literal_; |
85 scope_id_ = addr.scope_id_; | 85 scope_id_ = addr.scope_id_; |
86 return *this; | 86 return *this; |
87 } | 87 } |
88 | 88 |
89 void SocketAddress::SetIP(uint32 ip_as_host_order_integer) { | 89 void SocketAddress::SetIP(uint32_t ip_as_host_order_integer) { |
90 hostname_.clear(); | 90 hostname_.clear(); |
91 literal_ = false; | 91 literal_ = false; |
92 ip_ = IPAddress(ip_as_host_order_integer); | 92 ip_ = IPAddress(ip_as_host_order_integer); |
93 scope_id_ = 0; | 93 scope_id_ = 0; |
94 } | 94 } |
95 | 95 |
96 void SocketAddress::SetIP(const IPAddress& ip) { | 96 void SocketAddress::SetIP(const IPAddress& ip) { |
97 hostname_.clear(); | 97 hostname_.clear(); |
98 literal_ = false; | 98 literal_ = false; |
99 ip_ = ip; | 99 ip_ = ip; |
100 scope_id_ = 0; | 100 scope_id_ = 0; |
101 } | 101 } |
102 | 102 |
103 void SocketAddress::SetIP(const std::string& hostname) { | 103 void SocketAddress::SetIP(const std::string& hostname) { |
104 hostname_ = hostname; | 104 hostname_ = hostname; |
105 literal_ = IPFromString(hostname, &ip_); | 105 literal_ = IPFromString(hostname, &ip_); |
106 if (!literal_) { | 106 if (!literal_) { |
107 ip_ = IPAddress(); | 107 ip_ = IPAddress(); |
108 } | 108 } |
109 scope_id_ = 0; | 109 scope_id_ = 0; |
110 } | 110 } |
111 | 111 |
112 void SocketAddress::SetResolvedIP(uint32 ip_as_host_order_integer) { | 112 void SocketAddress::SetResolvedIP(uint32_t ip_as_host_order_integer) { |
113 ip_ = IPAddress(ip_as_host_order_integer); | 113 ip_ = IPAddress(ip_as_host_order_integer); |
114 scope_id_ = 0; | 114 scope_id_ = 0; |
115 } | 115 } |
116 | 116 |
117 void SocketAddress::SetResolvedIP(const IPAddress& ip) { | 117 void SocketAddress::SetResolvedIP(const IPAddress& ip) { |
118 ip_ = ip; | 118 ip_ = ip; |
119 scope_id_ = 0; | 119 scope_id_ = 0; |
120 } | 120 } |
121 | 121 |
122 void SocketAddress::SetPort(int port) { | 122 void SocketAddress::SetPort(int port) { |
123 ASSERT((0 <= port) && (port < 65536)); | 123 ASSERT((0 <= port) && (port < 65536)); |
124 port_ = static_cast<uint16>(port); | 124 port_ = static_cast<uint16_t>(port); |
125 } | 125 } |
126 | 126 |
127 uint32 SocketAddress::ip() const { | 127 uint32_t SocketAddress::ip() const { |
128 return ip_.v4AddressAsHostOrderInteger(); | 128 return ip_.v4AddressAsHostOrderInteger(); |
129 } | 129 } |
130 | 130 |
131 const IPAddress& SocketAddress::ipaddr() const { | 131 const IPAddress& SocketAddress::ipaddr() const { |
132 return ip_; | 132 return ip_; |
133 } | 133 } |
134 | 134 |
135 uint16 SocketAddress::port() const { | 135 uint16_t SocketAddress::port() const { |
136 return port_; | 136 return port_; |
137 } | 137 } |
138 | 138 |
139 std::string SocketAddress::HostAsURIString() const { | 139 std::string SocketAddress::HostAsURIString() const { |
140 // If the hostname was a literal IP string, it may need to have square | 140 // If the hostname was a literal IP string, it may need to have square |
141 // brackets added (for SocketAddress::ToString()). | 141 // brackets added (for SocketAddress::ToString()). |
142 if (!literal_ && !hostname_.empty()) | 142 if (!literal_ && !hostname_.empty()) |
143 return hostname_; | 143 return hostname_; |
144 if (ip_.family() == AF_INET6) { | 144 if (ip_.family() == AF_INET6) { |
145 return "[" + ip_.ToString() + "]"; | 145 return "[" + ip_.ToString() + "]"; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) { | 272 bool SocketAddress::FromSockAddr(const sockaddr_in& saddr) { |
273 if (saddr.sin_family != AF_INET) | 273 if (saddr.sin_family != AF_INET) |
274 return false; | 274 return false; |
275 SetIP(NetworkToHost32(saddr.sin_addr.s_addr)); | 275 SetIP(NetworkToHost32(saddr.sin_addr.s_addr)); |
276 SetPort(NetworkToHost16(saddr.sin_port)); | 276 SetPort(NetworkToHost16(saddr.sin_port)); |
277 literal_ = false; | 277 literal_ = false; |
278 return true; | 278 return true; |
279 } | 279 } |
280 | 280 |
281 static size_t ToSockAddrStorageHelper(sockaddr_storage* addr, | 281 static size_t ToSockAddrStorageHelper(sockaddr_storage* addr, |
282 IPAddress ip, uint16 port, int scope_id) { | 282 IPAddress ip, |
| 283 uint16_t port, |
| 284 int scope_id) { |
283 memset(addr, 0, sizeof(sockaddr_storage)); | 285 memset(addr, 0, sizeof(sockaddr_storage)); |
284 addr->ss_family = static_cast<unsigned short>(ip.family()); | 286 addr->ss_family = static_cast<unsigned short>(ip.family()); |
285 if (addr->ss_family == AF_INET6) { | 287 if (addr->ss_family == AF_INET6) { |
286 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr); | 288 sockaddr_in6* saddr = reinterpret_cast<sockaddr_in6*>(addr); |
287 saddr->sin6_addr = ip.ipv6_address(); | 289 saddr->sin6_addr = ip.ipv6_address(); |
288 saddr->sin6_port = HostToNetwork16(port); | 290 saddr->sin6_port = HostToNetwork16(port); |
289 saddr->sin6_scope_id = scope_id; | 291 saddr->sin6_scope_id = scope_id; |
290 return sizeof(sockaddr_in6); | 292 return sizeof(sockaddr_in6); |
291 } else if (addr->ss_family == AF_INET) { | 293 } else if (addr->ss_family == AF_INET) { |
292 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr); | 294 sockaddr_in* saddr = reinterpret_cast<sockaddr_in*>(addr); |
293 saddr->sin_addr = ip.ipv4_address(); | 295 saddr->sin_addr = ip.ipv4_address(); |
294 saddr->sin_port = HostToNetwork16(port); | 296 saddr->sin_port = HostToNetwork16(port); |
295 return sizeof(sockaddr_in); | 297 return sizeof(sockaddr_in); |
296 } | 298 } |
297 return 0; | 299 return 0; |
298 } | 300 } |
299 | 301 |
300 size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const { | 302 size_t SocketAddress::ToDualStackSockAddrStorage(sockaddr_storage *addr) const { |
301 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_); | 303 return ToSockAddrStorageHelper(addr, ip_.AsIPv6Address(), port_, scope_id_); |
302 } | 304 } |
303 | 305 |
304 size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const { | 306 size_t SocketAddress::ToSockAddrStorage(sockaddr_storage* addr) const { |
305 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_); | 307 return ToSockAddrStorageHelper(addr, ip_, port_, scope_id_); |
306 } | 308 } |
307 | 309 |
308 std::string SocketAddress::IPToString(uint32 ip_as_host_order_integer) { | 310 std::string SocketAddress::IPToString(uint32_t ip_as_host_order_integer) { |
309 return IPAddress(ip_as_host_order_integer).ToString(); | 311 return IPAddress(ip_as_host_order_integer).ToString(); |
310 } | 312 } |
311 | 313 |
312 std::string IPToSensitiveString(uint32 ip_as_host_order_integer) { | 314 std::string IPToSensitiveString(uint32_t ip_as_host_order_integer) { |
313 return IPAddress(ip_as_host_order_integer).ToSensitiveString(); | 315 return IPAddress(ip_as_host_order_integer).ToSensitiveString(); |
314 } | 316 } |
315 | 317 |
316 bool SocketAddress::StringToIP(const std::string& hostname, uint32* ip) { | 318 bool SocketAddress::StringToIP(const std::string& hostname, uint32_t* ip) { |
317 in_addr addr; | 319 in_addr addr; |
318 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr) == 0) | 320 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr) == 0) |
319 return false; | 321 return false; |
320 *ip = NetworkToHost32(addr.s_addr); | 322 *ip = NetworkToHost32(addr.s_addr); |
321 return true; | 323 return true; |
322 } | 324 } |
323 | 325 |
324 bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) { | 326 bool SocketAddress::StringToIP(const std::string& hostname, IPAddress* ip) { |
325 in_addr addr4; | 327 in_addr addr4; |
326 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) { | 328 if (rtc::inet_pton(AF_INET, hostname.c_str(), &addr4) > 0) { |
327 if (ip) { | 329 if (ip) { |
328 *ip = IPAddress(addr4); | 330 *ip = IPAddress(addr4); |
329 } | 331 } |
330 return true; | 332 return true; |
331 } | 333 } |
332 | 334 |
333 in6_addr addr6; | 335 in6_addr addr6; |
334 if (rtc::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) { | 336 if (rtc::inet_pton(AF_INET6, hostname.c_str(), &addr6) > 0) { |
335 if (ip) { | 337 if (ip) { |
336 *ip = IPAddress(addr6); | 338 *ip = IPAddress(addr6); |
337 } | 339 } |
338 return true; | 340 return true; |
339 } | 341 } |
340 return false; | 342 return false; |
341 } | 343 } |
342 | 344 |
343 uint32 SocketAddress::StringToIP(const std::string& hostname) { | 345 uint32_t SocketAddress::StringToIP(const std::string& hostname) { |
344 uint32 ip = 0; | 346 uint32_t ip = 0; |
345 StringToIP(hostname, &ip); | 347 StringToIP(hostname, &ip); |
346 return ip; | 348 return ip; |
347 } | 349 } |
348 | 350 |
349 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr, | 351 bool SocketAddressFromSockAddrStorage(const sockaddr_storage& addr, |
350 SocketAddress* out) { | 352 SocketAddress* out) { |
351 if (!out) { | 353 if (!out) { |
352 return false; | 354 return false; |
353 } | 355 } |
354 if (addr.ss_family == AF_INET) { | 356 if (addr.ss_family == AF_INET) { |
(...skipping 14 matching lines...) Expand all Loading... |
369 SocketAddress EmptySocketAddressWithFamily(int family) { | 371 SocketAddress EmptySocketAddressWithFamily(int family) { |
370 if (family == AF_INET) { | 372 if (family == AF_INET) { |
371 return SocketAddress(IPAddress(INADDR_ANY), 0); | 373 return SocketAddress(IPAddress(INADDR_ANY), 0); |
372 } else if (family == AF_INET6) { | 374 } else if (family == AF_INET6) { |
373 return SocketAddress(IPAddress(in6addr_any), 0); | 375 return SocketAddress(IPAddress(in6addr_any), 0); |
374 } | 376 } |
375 return SocketAddress(); | 377 return SocketAddress(); |
376 } | 378 } |
377 | 379 |
378 } // namespace rtc | 380 } // namespace rtc |
OLD | NEW |