| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/udp/udp_socket_posix.h" | 5 #include "net/udp/udp_socket_posix.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <net/if.h> | 9 #include <net/if.h> |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/posix/eintr_wrapper.h" | 21 #include "base/posix/eintr_wrapper.h" |
| 22 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 23 #include "base/trace_event/trace_event.h" | 23 #include "base/trace_event/trace_event.h" |
| 24 #include "net/base/io_buffer.h" | 24 #include "net/base/io_buffer.h" |
| 25 #include "net/base/ip_address.h" | 25 #include "net/base/ip_address.h" |
| 26 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
| 27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/network_activity_monitor.h" | 28 #include "net/base/network_activity_monitor.h" |
| 29 #include "net/base/sockaddr_storage.h" | 29 #include "net/base/sockaddr_storage.h" |
| 30 #include "net/log/net_log.h" | 30 #include "net/log/net_log.h" |
| 31 #include "net/log/net_log_event_type.h" |
| 32 #include "net/log/net_log_source_type.h" |
| 31 #include "net/socket/socket_descriptor.h" | 33 #include "net/socket/socket_descriptor.h" |
| 32 #include "net/udp/udp_net_log_parameters.h" | 34 #include "net/udp/udp_net_log_parameters.h" |
| 33 | 35 |
| 34 #if defined(OS_ANDROID) | 36 #if defined(OS_ANDROID) |
| 35 #include <dlfcn.h> | 37 #include <dlfcn.h> |
| 36 // This was added in Lollipop to dlfcn.h | 38 // This was added in Lollipop to dlfcn.h |
| 37 #define RTLD_NOLOAD 4 | 39 #define RTLD_NOLOAD 4 |
| 38 #include "base/android/build_info.h" | 40 #include "base/android/build_info.h" |
| 39 #include "base/native_library.h" | 41 #include "base/native_library.h" |
| 40 #include "base/strings/utf_string_conversions.h" | 42 #include "base/strings/utf_string_conversions.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), | 83 socket_options_(SOCKET_OPTION_MULTICAST_LOOP), |
| 82 multicast_interface_(0), | 84 multicast_interface_(0), |
| 83 multicast_time_to_live_(1), | 85 multicast_time_to_live_(1), |
| 84 bind_type_(bind_type), | 86 bind_type_(bind_type), |
| 85 rand_int_cb_(rand_int_cb), | 87 rand_int_cb_(rand_int_cb), |
| 86 read_watcher_(this), | 88 read_watcher_(this), |
| 87 write_watcher_(this), | 89 write_watcher_(this), |
| 88 read_buf_len_(0), | 90 read_buf_len_(0), |
| 89 recv_from_address_(NULL), | 91 recv_from_address_(NULL), |
| 90 write_buf_len_(0), | 92 write_buf_len_(0), |
| 91 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)), | 93 net_log_(BoundNetLog::Make(net_log, NetLogSourceType::UDP_SOCKET)), |
| 92 bound_network_(NetworkChangeNotifier::kInvalidNetworkHandle) { | 94 bound_network_(NetworkChangeNotifier::kInvalidNetworkHandle) { |
| 93 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, | 95 net_log_.BeginEvent(NetLogEventType::SOCKET_ALIVE, |
| 94 source.ToEventParametersCallback()); | 96 source.ToEventParametersCallback()); |
| 95 if (bind_type == DatagramSocket::RANDOM_BIND) | 97 if (bind_type == DatagramSocket::RANDOM_BIND) |
| 96 DCHECK(!rand_int_cb.is_null()); | 98 DCHECK(!rand_int_cb.is_null()); |
| 97 } | 99 } |
| 98 | 100 |
| 99 UDPSocketPosix::~UDPSocketPosix() { | 101 UDPSocketPosix::~UDPSocketPosix() { |
| 100 Close(); | 102 Close(); |
| 101 net_log_.EndEvent(NetLog::TYPE_SOCKET_ALIVE); | 103 net_log_.EndEvent(NetLogEventType::SOCKET_ALIVE); |
| 102 } | 104 } |
| 103 | 105 |
| 104 int UDPSocketPosix::Open(AddressFamily address_family) { | 106 int UDPSocketPosix::Open(AddressFamily address_family) { |
| 105 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
| 106 DCHECK_EQ(socket_, kInvalidSocket); | 108 DCHECK_EQ(socket_, kInvalidSocket); |
| 107 | 109 |
| 108 addr_family_ = ConvertAddressFamily(address_family); | 110 addr_family_ = ConvertAddressFamily(address_family); |
| 109 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, 0); | 111 socket_ = CreatePlatformSocket(addr_family_, SOCK_DGRAM, 0); |
| 110 if (socket_ == kInvalidSocket) | 112 if (socket_ == kInvalidSocket) |
| 111 return MapSystemError(errno); | 113 return MapSystemError(errno); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 | 175 |
| 174 if (!local_address_.get()) { | 176 if (!local_address_.get()) { |
| 175 SockaddrStorage storage; | 177 SockaddrStorage storage; |
| 176 if (getsockname(socket_, storage.addr, &storage.addr_len)) | 178 if (getsockname(socket_, storage.addr, &storage.addr_len)) |
| 177 return MapSystemError(errno); | 179 return MapSystemError(errno); |
| 178 std::unique_ptr<IPEndPoint> address(new IPEndPoint()); | 180 std::unique_ptr<IPEndPoint> address(new IPEndPoint()); |
| 179 if (!address->FromSockAddr(storage.addr, storage.addr_len)) | 181 if (!address->FromSockAddr(storage.addr, storage.addr_len)) |
| 180 return ERR_ADDRESS_INVALID; | 182 return ERR_ADDRESS_INVALID; |
| 181 local_address_.reset(address.release()); | 183 local_address_.reset(address.release()); |
| 182 net_log_.AddEvent( | 184 net_log_.AddEvent( |
| 183 NetLog::TYPE_UDP_LOCAL_ADDRESS, | 185 NetLogEventType::UDP_LOCAL_ADDRESS, |
| 184 CreateNetLogUDPConnectCallback(local_address_.get(), bound_network_)); | 186 CreateNetLogUDPConnectCallback(local_address_.get(), bound_network_)); |
| 185 } | 187 } |
| 186 | 188 |
| 187 *address = *local_address_; | 189 *address = *local_address_; |
| 188 return OK; | 190 return OK; |
| 189 } | 191 } |
| 190 | 192 |
| 191 int UDPSocketPosix::Read(IOBuffer* buf, | 193 int UDPSocketPosix::Read(IOBuffer* buf, |
| 192 int buf_len, | 194 int buf_len, |
| 193 const CompletionCallback& callback) { | 195 const CompletionCallback& callback) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 DCHECK(!send_to_address_.get()); | 268 DCHECK(!send_to_address_.get()); |
| 267 if (address) { | 269 if (address) { |
| 268 send_to_address_.reset(new IPEndPoint(*address)); | 270 send_to_address_.reset(new IPEndPoint(*address)); |
| 269 } | 271 } |
| 270 write_callback_ = callback; | 272 write_callback_ = callback; |
| 271 return ERR_IO_PENDING; | 273 return ERR_IO_PENDING; |
| 272 } | 274 } |
| 273 | 275 |
| 274 int UDPSocketPosix::Connect(const IPEndPoint& address) { | 276 int UDPSocketPosix::Connect(const IPEndPoint& address) { |
| 275 DCHECK_NE(socket_, kInvalidSocket); | 277 DCHECK_NE(socket_, kInvalidSocket); |
| 276 net_log_.BeginEvent(NetLog::TYPE_UDP_CONNECT, | 278 net_log_.BeginEvent(NetLogEventType::UDP_CONNECT, |
| 277 CreateNetLogUDPConnectCallback(&address, bound_network_)); | 279 CreateNetLogUDPConnectCallback(&address, bound_network_)); |
| 278 int rv = InternalConnect(address); | 280 int rv = InternalConnect(address); |
| 279 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); | 281 net_log_.EndEventWithNetErrorCode(NetLogEventType::UDP_CONNECT, rv); |
| 280 is_connected_ = (rv == OK); | 282 is_connected_ = (rv == OK); |
| 281 return rv; | 283 return rv; |
| 282 } | 284 } |
| 283 | 285 |
| 284 int UDPSocketPosix::InternalConnect(const IPEndPoint& address) { | 286 int UDPSocketPosix::InternalConnect(const IPEndPoint& address) { |
| 285 DCHECK(CalledOnValidThread()); | 287 DCHECK(CalledOnValidThread()); |
| 286 DCHECK(!is_connected()); | 288 DCHECK(!is_connected()); |
| 287 DCHECK(!remote_address_.get()); | 289 DCHECK(!remote_address_.get()); |
| 288 | 290 |
| 289 int rv = 0; | 291 int rv = 0; |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 DCHECK(ok); | 527 DCHECK(ok); |
| 526 DoReadCallback(result); | 528 DoReadCallback(result); |
| 527 } | 529 } |
| 528 } | 530 } |
| 529 | 531 |
| 530 void UDPSocketPosix::LogRead(int result, | 532 void UDPSocketPosix::LogRead(int result, |
| 531 const char* bytes, | 533 const char* bytes, |
| 532 socklen_t addr_len, | 534 socklen_t addr_len, |
| 533 const sockaddr* addr) const { | 535 const sockaddr* addr) const { |
| 534 if (result < 0) { | 536 if (result < 0) { |
| 535 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); | 537 net_log_.AddEventWithNetErrorCode(NetLogEventType::UDP_RECEIVE_ERROR, |
| 538 result); |
| 536 return; | 539 return; |
| 537 } | 540 } |
| 538 | 541 |
| 539 if (net_log_.IsCapturing()) { | 542 if (net_log_.IsCapturing()) { |
| 540 DCHECK(addr_len > 0); | 543 DCHECK(addr_len > 0); |
| 541 DCHECK(addr); | 544 DCHECK(addr); |
| 542 | 545 |
| 543 IPEndPoint address; | 546 IPEndPoint address; |
| 544 bool is_address_valid = address.FromSockAddr(addr, addr_len); | 547 bool is_address_valid = address.FromSockAddr(addr, addr_len); |
| 545 net_log_.AddEvent( | 548 net_log_.AddEvent( |
| 546 NetLog::TYPE_UDP_BYTES_RECEIVED, | 549 NetLogEventType::UDP_BYTES_RECEIVED, |
| 547 CreateNetLogUDPDataTranferCallback( | 550 CreateNetLogUDPDataTranferCallback( |
| 548 result, bytes, | 551 result, bytes, |
| 549 is_address_valid ? &address : NULL)); | 552 is_address_valid ? &address : NULL)); |
| 550 } | 553 } |
| 551 | 554 |
| 552 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); | 555 NetworkActivityMonitor::GetInstance()->IncrementBytesReceived(result); |
| 553 } | 556 } |
| 554 | 557 |
| 555 void UDPSocketPosix::DidCompleteWrite() { | 558 void UDPSocketPosix::DidCompleteWrite() { |
| 556 int result = | 559 int result = |
| 557 InternalSendTo(write_buf_.get(), write_buf_len_, send_to_address_.get()); | 560 InternalSendTo(write_buf_.get(), write_buf_len_, send_to_address_.get()); |
| 558 | 561 |
| 559 if (result != ERR_IO_PENDING) { | 562 if (result != ERR_IO_PENDING) { |
| 560 write_buf_ = NULL; | 563 write_buf_ = NULL; |
| 561 write_buf_len_ = 0; | 564 write_buf_len_ = 0; |
| 562 send_to_address_.reset(); | 565 send_to_address_.reset(); |
| 563 write_socket_watcher_.StopWatchingFileDescriptor(); | 566 write_socket_watcher_.StopWatchingFileDescriptor(); |
| 564 DoWriteCallback(result); | 567 DoWriteCallback(result); |
| 565 } | 568 } |
| 566 } | 569 } |
| 567 | 570 |
| 568 void UDPSocketPosix::LogWrite(int result, | 571 void UDPSocketPosix::LogWrite(int result, |
| 569 const char* bytes, | 572 const char* bytes, |
| 570 const IPEndPoint* address) const { | 573 const IPEndPoint* address) const { |
| 571 if (result < 0) { | 574 if (result < 0) { |
| 572 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); | 575 net_log_.AddEventWithNetErrorCode(NetLogEventType::UDP_SEND_ERROR, result); |
| 573 return; | 576 return; |
| 574 } | 577 } |
| 575 | 578 |
| 576 if (net_log_.IsCapturing()) { | 579 if (net_log_.IsCapturing()) { |
| 577 net_log_.AddEvent( | 580 net_log_.AddEvent( |
| 578 NetLog::TYPE_UDP_BYTES_SENT, | 581 NetLogEventType::UDP_BYTES_SENT, |
| 579 CreateNetLogUDPDataTranferCallback(result, bytes, address)); | 582 CreateNetLogUDPDataTranferCallback(result, bytes, address)); |
| 580 } | 583 } |
| 581 | 584 |
| 582 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); | 585 NetworkActivityMonitor::GetInstance()->IncrementBytesSent(result); |
| 583 } | 586 } |
| 584 | 587 |
| 585 int UDPSocketPosix::InternalRecvFrom(IOBuffer* buf, | 588 int UDPSocketPosix::InternalRecvFrom(IOBuffer* buf, |
| 586 int buf_len, | 589 int buf_len, |
| 587 IPEndPoint* address) { | 590 IPEndPoint* address) { |
| 588 int bytes_transferred; | 591 int bytes_transferred; |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 872 return MapSystemError(errno); | 875 return MapSystemError(errno); |
| 873 | 876 |
| 874 return OK; | 877 return OK; |
| 875 } | 878 } |
| 876 | 879 |
| 877 void UDPSocketPosix::DetachFromThread() { | 880 void UDPSocketPosix::DetachFromThread() { |
| 878 base::NonThreadSafe::DetachFromThread(); | 881 base::NonThreadSafe::DetachFromThread(); |
| 879 } | 882 } |
| 880 | 883 |
| 881 } // namespace net | 884 } // namespace net |
| OLD | NEW |