| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_win.h" | 5 #include "net/udp/udp_socket_win.h" |
| 6 | 6 |
| 7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
| 8 | 8 |
| 9 #include "base/eintr_wrapper.h" | 9 #include "base/eintr_wrapper.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/stats_counters.h" | 12 #include "base/metrics/stats_counters.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "net/base/address_list_net_log_param.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 18 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 19 #include "net/base/winsock_init.h" | 20 #include "net/base/winsock_init.h" |
| 20 #include "net/base/winsock_util.h" | 21 #include "net/base/winsock_util.h" |
| 22 #include "net/udp/udp_data_transfer_param.h" |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 static const int kBindRetries = 10; | 26 static const int kBindRetries = 10; |
| 25 static const int kPortStart = 1024; | 27 static const int kPortStart = 1024; |
| 26 static const int kPortEnd = 65535; | 28 static const int kPortEnd = 65535; |
| 27 | 29 |
| 28 } // namespace net | 30 } // namespace net |
| 29 | 31 |
| 30 namespace net { | 32 namespace net { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 44 net::NetLog* net_log, | 46 net::NetLog* net_log, |
| 45 const net::NetLog::Source& source) | 47 const net::NetLog::Source& source) |
| 46 : socket_(INVALID_SOCKET), | 48 : socket_(INVALID_SOCKET), |
| 47 bind_type_(bind_type), | 49 bind_type_(bind_type), |
| 48 rand_int_cb_(rand_int_cb), | 50 rand_int_cb_(rand_int_cb), |
| 49 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), | 51 ALLOW_THIS_IN_INITIALIZER_LIST(read_delegate_(this)), |
| 50 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), | 52 ALLOW_THIS_IN_INITIALIZER_LIST(write_delegate_(this)), |
| 51 recv_from_address_(NULL), | 53 recv_from_address_(NULL), |
| 52 read_callback_(NULL), | 54 read_callback_(NULL), |
| 53 write_callback_(NULL), | 55 write_callback_(NULL), |
| 54 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_SOCKET)) { | 56 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_UDP_SOCKET)) { |
| 55 EnsureWinsockInit(); | 57 EnsureWinsockInit(); |
| 56 scoped_refptr<NetLog::EventParameters> params; | 58 scoped_refptr<NetLog::EventParameters> params; |
| 57 if (source.is_valid()) | 59 if (source.is_valid()) |
| 58 params = new NetLogSourceParameter("source_dependency", source); | 60 params = new NetLogSourceParameter("source_dependency", source); |
| 59 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); | 61 net_log_.BeginEvent(NetLog::TYPE_SOCKET_ALIVE, params); |
| 60 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); | 62 memset(&read_overlapped_, 0, sizeof(read_overlapped_)); |
| 61 read_overlapped_.hEvent = WSACreateEvent(); | 63 read_overlapped_.hEvent = WSACreateEvent(); |
| 62 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); | 64 memset(&write_overlapped_, 0, sizeof(write_overlapped_)); |
| 63 write_overlapped_.hEvent = WSACreateEvent(); | 65 write_overlapped_.hEvent = WSACreateEvent(); |
| 64 if (bind_type == DatagramSocket::RANDOM_BIND) | 66 if (bind_type == DatagramSocket::RANDOM_BIND) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 IPEndPoint* address, | 145 IPEndPoint* address, |
| 144 OldCompletionCallback* callback) { | 146 OldCompletionCallback* callback) { |
| 145 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
| 146 DCHECK_NE(INVALID_SOCKET, socket_); | 148 DCHECK_NE(INVALID_SOCKET, socket_); |
| 147 DCHECK(!read_callback_); | 149 DCHECK(!read_callback_); |
| 148 DCHECK(!recv_from_address_); | 150 DCHECK(!recv_from_address_); |
| 149 DCHECK(callback); // Synchronous operation not supported. | 151 DCHECK(callback); // Synchronous operation not supported. |
| 150 DCHECK_GT(buf_len, 0); | 152 DCHECK_GT(buf_len, 0); |
| 151 | 153 |
| 152 int nread = InternalRecvFrom(buf, buf_len, address); | 154 int nread = InternalRecvFrom(buf, buf_len, address); |
| 153 if (nread != ERR_IO_PENDING) | 155 if (nread != ERR_IO_PENDING) { |
| 156 if (nread < 0) |
| 157 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, nread); |
| 154 return nread; | 158 return nread; |
| 159 } |
| 155 | 160 |
| 156 read_iobuffer_ = buf; | 161 read_iobuffer_ = buf; |
| 157 read_callback_ = callback; | 162 read_callback_ = callback; |
| 158 recv_from_address_ = address; | 163 recv_from_address_ = address; |
| 159 return ERR_IO_PENDING; | 164 return ERR_IO_PENDING; |
| 160 } | 165 } |
| 161 | 166 |
| 162 int UDPSocketWin::Write(IOBuffer* buf, | 167 int UDPSocketWin::Write(IOBuffer* buf, |
| 163 int buf_len, | 168 int buf_len, |
| 164 OldCompletionCallback* callback) { | 169 OldCompletionCallback* callback) { |
| 165 return SendToOrWrite(buf, buf_len, NULL, callback); | 170 return SendToOrWrite(buf, buf_len, NULL, callback); |
| 166 } | 171 } |
| 167 | 172 |
| 168 int UDPSocketWin::SendTo(IOBuffer* buf, | 173 int UDPSocketWin::SendTo(IOBuffer* buf, |
| 169 int buf_len, | 174 int buf_len, |
| 170 const IPEndPoint& address, | 175 const IPEndPoint& address, |
| 171 OldCompletionCallback* callback) { | 176 OldCompletionCallback* callback) { |
| 172 return SendToOrWrite(buf, buf_len, &address, callback); | 177 return SendToOrWrite(buf, buf_len, &address, callback); |
| 173 } | 178 } |
| 174 | 179 |
| 175 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, | 180 int UDPSocketWin::SendToOrWrite(IOBuffer* buf, |
| 176 int buf_len, | 181 int buf_len, |
| 177 const IPEndPoint* address, | 182 const IPEndPoint* address, |
| 178 OldCompletionCallback* callback) { | 183 OldCompletionCallback* callback) { |
| 179 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
| 180 DCHECK_NE(INVALID_SOCKET, socket_); | 185 DCHECK_NE(INVALID_SOCKET, socket_); |
| 181 DCHECK(!write_callback_); | 186 DCHECK(!write_callback_); |
| 182 DCHECK(callback); // Synchronous operation not supported. | 187 DCHECK(callback); // Synchronous operation not supported. |
| 183 DCHECK_GT(buf_len, 0); | 188 DCHECK_GT(buf_len, 0); |
| 189 DCHECK(!send_to_address_.get()); |
| 184 | 190 |
| 185 int nwrite = InternalSendTo(buf, buf_len, address); | 191 int nwrite = InternalSendTo(buf, buf_len, address); |
| 186 if (nwrite != ERR_IO_PENDING) | 192 if (nwrite != ERR_IO_PENDING) { |
| 193 if (nwrite < 0) |
| 194 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, nwrite); |
| 187 return nwrite; | 195 return nwrite; |
| 196 } |
| 188 | 197 |
| 198 if (address) |
| 199 send_to_address_.reset(new IPEndPoint(*address)); |
| 189 write_iobuffer_ = buf; | 200 write_iobuffer_ = buf; |
| 190 write_callback_ = callback; | 201 write_callback_ = callback; |
| 191 return ERR_IO_PENDING; | 202 return ERR_IO_PENDING; |
| 192 } | 203 } |
| 193 | 204 |
| 194 int UDPSocketWin::Connect(const IPEndPoint& address) { | 205 int UDPSocketWin::Connect(const IPEndPoint& address) { |
| 206 net_log_.BeginEvent( |
| 207 NetLog::TYPE_UDP_CONNECT, |
| 208 make_scoped_refptr(new NetLogStringParameter("address", |
| 209 address.ToString()))); |
| 210 int rv = InternalConnect(address); |
| 211 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_UDP_CONNECT, rv); |
| 212 return rv; |
| 213 } |
| 214 |
| 215 int UDPSocketWin::InternalConnect(const IPEndPoint& address) { |
| 195 DCHECK(!is_connected()); | 216 DCHECK(!is_connected()); |
| 196 DCHECK(!remote_address_.get()); | 217 DCHECK(!remote_address_.get()); |
| 197 int rv = CreateSocket(address); | 218 int rv = CreateSocket(address); |
| 198 if (rv < 0) | 219 if (rv < 0) |
| 199 return rv; | 220 return rv; |
| 200 | 221 |
| 201 if (bind_type_ == DatagramSocket::RANDOM_BIND) | 222 if (bind_type_ == DatagramSocket::RANDOM_BIND) |
| 202 rv = RandomBind(address); | 223 rv = RandomBind(address); |
| 203 // else connect() does the DatagramSocket::DEFAULT_BIND | 224 // else connect() does the DatagramSocket::DEFAULT_BIND |
| 204 | 225 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 c->Run(rv); | 280 c->Run(rv); |
| 260 } | 281 } |
| 261 | 282 |
| 262 void UDPSocketWin::DidCompleteRead() { | 283 void UDPSocketWin::DidCompleteRead() { |
| 263 DWORD num_bytes, flags; | 284 DWORD num_bytes, flags; |
| 264 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, | 285 BOOL ok = WSAGetOverlappedResult(socket_, &read_overlapped_, |
| 265 &num_bytes, FALSE, &flags); | 286 &num_bytes, FALSE, &flags); |
| 266 WSAResetEvent(read_overlapped_.hEvent); | 287 WSAResetEvent(read_overlapped_.hEvent); |
| 267 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | 288 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); |
| 268 if (ok) { | 289 if (ok) { |
| 269 if (!ProcessSuccessfulRead(num_bytes, recv_from_address_)) | 290 if (!ProcessSuccessfulRead(num_bytes, read_iobuffer_->data(), |
| 291 recv_from_address_)) { |
| 270 result = ERR_FAILED; | 292 result = ERR_FAILED; |
| 293 } |
| 271 } | 294 } |
| 295 if (result < 0) |
| 296 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_RECEIVE_ERROR, result); |
| 272 read_iobuffer_ = NULL; | 297 read_iobuffer_ = NULL; |
| 273 recv_from_address_ = NULL; | 298 recv_from_address_ = NULL; |
| 274 DoReadCallback(result); | 299 DoReadCallback(result); |
| 275 } | 300 } |
| 276 | 301 |
| 277 bool UDPSocketWin::ProcessSuccessfulRead(int num_bytes, IPEndPoint* address) { | 302 bool UDPSocketWin::ProcessSuccessfulRead(int num_bytes, const char* bytes, |
| 303 IPEndPoint* address) const { |
| 278 base::StatsCounter read_bytes("udp.read_bytes"); | 304 base::StatsCounter read_bytes("udp.read_bytes"); |
| 279 read_bytes.Add(num_bytes); | 305 read_bytes.Add(num_bytes); |
| 280 | 306 |
| 281 // Convert address. | 307 const struct sockaddr* addr = |
| 282 if (address) { | 308 reinterpret_cast<const struct sockaddr*>(&recv_addr_storage_); |
| 283 struct sockaddr* addr = | 309 |
| 284 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_); | 310 // Convert address. If address is NULL, use a variable on the stack to |
| 311 // hold the address, for logging. |
| 312 IPEndPoint address_on_stack; |
| 313 if (!address) { |
| 314 if (address_on_stack.FromSockAddr(addr, recv_addr_len_)) |
| 315 address = &address_on_stack; |
| 316 } else { |
| 285 if (!address->FromSockAddr(addr, recv_addr_len_)) | 317 if (!address->FromSockAddr(addr, recv_addr_len_)) |
| 286 return false; | 318 return false; |
| 287 } | 319 } |
| 288 | 320 |
| 321 net_log_.AddEvent( |
| 322 NetLog::TYPE_UDP_BYTES_RECEIVED, |
| 323 make_scoped_refptr( |
| 324 new UDPDataTransferNetLogParam(num_bytes, bytes, |
| 325 net_log_.IsLoggingBytes(), |
| 326 address))); |
| 327 |
| 289 return true; | 328 return true; |
| 290 } | 329 } |
| 291 | 330 |
| 292 void UDPSocketWin::DidCompleteWrite() { | 331 void UDPSocketWin::DidCompleteWrite() { |
| 293 DWORD num_bytes, flags; | 332 DWORD num_bytes, flags; |
| 294 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_, | 333 BOOL ok = WSAGetOverlappedResult(socket_, &write_overlapped_, |
| 295 &num_bytes, FALSE, &flags); | 334 &num_bytes, FALSE, &flags); |
| 296 WSAResetEvent(write_overlapped_.hEvent); | 335 WSAResetEvent(write_overlapped_.hEvent); |
| 297 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); | 336 int result = ok ? num_bytes : MapSystemError(WSAGetLastError()); |
| 298 if (ok) | 337 if (ok) { |
| 299 ProcessSuccessfulWrite(num_bytes); | 338 ProcessSuccessfulWrite(num_bytes, write_iobuffer_->data(), |
| 339 send_to_address_.get()); |
| 340 } else { |
| 341 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_UDP_SEND_ERROR, result); |
| 342 } |
| 343 send_to_address_.reset(); |
| 300 write_iobuffer_ = NULL; | 344 write_iobuffer_ = NULL; |
| 301 DoWriteCallback(result); | 345 DoWriteCallback(result); |
| 302 } | 346 } |
| 303 | 347 |
| 304 void UDPSocketWin::ProcessSuccessfulWrite(int num_bytes) { | 348 void UDPSocketWin::ProcessSuccessfulWrite(int num_bytes, |
| 349 const char* bytes, |
| 350 const IPEndPoint* address) const { |
| 351 net_log_.AddEvent( |
| 352 NetLog::TYPE_UDP_BYTES_SENT, |
| 353 make_scoped_refptr( |
| 354 new UDPDataTransferNetLogParam(num_bytes, bytes, |
| 355 net_log_.IsLoggingBytes(), |
| 356 address))); |
| 305 base::StatsCounter write_bytes("udp.write_bytes"); | 357 base::StatsCounter write_bytes("udp.write_bytes"); |
| 306 write_bytes.Add(num_bytes); | 358 write_bytes.Add(num_bytes); |
| 307 } | 359 } |
| 308 | 360 |
| 309 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, | 361 int UDPSocketWin::InternalRecvFrom(IOBuffer* buf, int buf_len, |
| 310 IPEndPoint* address) { | 362 IPEndPoint* address) { |
| 311 recv_addr_len_ = sizeof(recv_addr_storage_); | 363 recv_addr_len_ = sizeof(recv_addr_storage_); |
| 312 struct sockaddr* addr = | 364 struct sockaddr* addr = |
| 313 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_); | 365 reinterpret_cast<struct sockaddr*>(&recv_addr_storage_); |
| 314 | 366 |
| 315 WSABUF read_buffer; | 367 WSABUF read_buffer; |
| 316 read_buffer.buf = buf->data(); | 368 read_buffer.buf = buf->data(); |
| 317 read_buffer.len = buf_len; | 369 read_buffer.len = buf_len; |
| 318 | 370 |
| 319 DWORD flags = 0; | 371 DWORD flags = 0; |
| 320 DWORD num; | 372 DWORD num; |
| 321 AssertEventNotSignaled(read_overlapped_.hEvent); | 373 AssertEventNotSignaled(read_overlapped_.hEvent); |
| 322 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, addr, | 374 int rv = WSARecvFrom(socket_, &read_buffer, 1, &num, &flags, addr, |
| 323 &recv_addr_len_, &read_overlapped_, NULL); | 375 &recv_addr_len_, &read_overlapped_, NULL); |
| 324 if (rv == 0) { | 376 if (rv == 0) { |
| 325 if (ResetEventIfSignaled(read_overlapped_.hEvent)) { | 377 if (ResetEventIfSignaled(read_overlapped_.hEvent)) { |
| 326 if (!ProcessSuccessfulRead(num, address)) | 378 if (!ProcessSuccessfulRead(num, buf->data(), address)) |
| 327 return ERR_FAILED; | 379 return ERR_FAILED; |
| 328 return static_cast<int>(num); | 380 return static_cast<int>(num); |
| 329 } | 381 } |
| 330 } else { | 382 } else { |
| 331 int os_error = WSAGetLastError(); | 383 int os_error = WSAGetLastError(); |
| 332 if (os_error != WSA_IO_PENDING) | 384 if (os_error != WSA_IO_PENDING) |
| 333 return MapSystemError(os_error); | 385 return MapSystemError(os_error); |
| 334 } | 386 } |
| 335 read_watcher_.StartWatching(read_overlapped_.hEvent, &read_delegate_); | 387 read_watcher_.StartWatching(read_overlapped_.hEvent, &read_delegate_); |
| 336 return ERR_IO_PENDING; | 388 return ERR_IO_PENDING; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 355 write_buffer.buf = buf->data(); | 407 write_buffer.buf = buf->data(); |
| 356 write_buffer.len = buf_len; | 408 write_buffer.len = buf_len; |
| 357 | 409 |
| 358 DWORD flags = 0; | 410 DWORD flags = 0; |
| 359 DWORD num; | 411 DWORD num; |
| 360 AssertEventNotSignaled(write_overlapped_.hEvent); | 412 AssertEventNotSignaled(write_overlapped_.hEvent); |
| 361 int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags, | 413 int rv = WSASendTo(socket_, &write_buffer, 1, &num, flags, |
| 362 addr, addr_len, &write_overlapped_, NULL); | 414 addr, addr_len, &write_overlapped_, NULL); |
| 363 if (rv == 0) { | 415 if (rv == 0) { |
| 364 if (ResetEventIfSignaled(write_overlapped_.hEvent)) { | 416 if (ResetEventIfSignaled(write_overlapped_.hEvent)) { |
| 365 ProcessSuccessfulWrite(num); | 417 ProcessSuccessfulWrite(num, buf->data(), address); |
| 366 return static_cast<int>(num); | 418 return static_cast<int>(num); |
| 367 } | 419 } |
| 368 } else { | 420 } else { |
| 369 int os_error = WSAGetLastError(); | 421 int os_error = WSAGetLastError(); |
| 370 if (os_error != WSA_IO_PENDING) | 422 if (os_error != WSA_IO_PENDING) |
| 371 return MapSystemError(os_error); | 423 return MapSystemError(os_error); |
| 372 } | 424 } |
| 373 | 425 |
| 374 write_watcher_.StartWatching(write_overlapped_.hEvent, &write_delegate_); | 426 write_watcher_.StartWatching(write_overlapped_.hEvent, &write_delegate_); |
| 375 return ERR_IO_PENDING; | 427 return ERR_IO_PENDING; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 393 | 445 |
| 394 for (int i = 0; i < kBindRetries; ++i) { | 446 for (int i = 0; i < kBindRetries; ++i) { |
| 395 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); | 447 int rv = DoBind(IPEndPoint(ip, rand_int_cb_.Run(kPortStart, kPortEnd))); |
| 396 if (rv == OK || rv != ERR_ADDRESS_IN_USE) | 448 if (rv == OK || rv != ERR_ADDRESS_IN_USE) |
| 397 return rv; | 449 return rv; |
| 398 } | 450 } |
| 399 return DoBind(IPEndPoint(ip, 0)); | 451 return DoBind(IPEndPoint(ip, 0)); |
| 400 } | 452 } |
| 401 | 453 |
| 402 } // namespace net | 454 } // namespace net |
| OLD | NEW |