OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/socket/tcp_client_socket_win.h" | 5 #include "net/socket/tcp_client_socket_win.h" |
6 | 6 |
7 #include <mstcpip.h> | 7 #include <mstcpip.h> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory_debug.h" | 11 #include "base/memory_debug.h" |
12 #include "base/metrics/stats_counters.h" | 12 #include "base/metrics/stats_counters.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
15 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
16 #include "net/base/address_list_net_log_param.h" | 16 #include "net/base/address_list_net_log_param.h" |
17 #include "net/base/connection_type_histograms.h" | 17 #include "net/base/connection_type_histograms.h" |
18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
20 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
21 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
22 #include "net/base/network_change_notifier.h" | 22 #include "net/base/network_change_notifier.h" |
23 #include "net/base/sys_addrinfo.h" | 23 #include "net/base/sys_addrinfo.h" |
24 #include "net/base/winsock_init.h" | 24 #include "net/base/winsock_init.h" |
| 25 #include "net/base/winsock_util.h" |
25 | 26 |
26 namespace net { | 27 namespace net { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Assert that the (manual-reset) event object is not signaled. | |
31 void AssertEventNotSignaled(WSAEVENT hEvent) { | |
32 DWORD wait_rv = WaitForSingleObject(hEvent, 0); | |
33 if (wait_rv != WAIT_TIMEOUT) { | |
34 DWORD err = ERROR_SUCCESS; | |
35 if (wait_rv == WAIT_FAILED) | |
36 err = GetLastError(); | |
37 CHECK(false); // Crash. | |
38 // This LOG statement is unreachable since we have already crashed, but it | |
39 // should prevent the compiler from optimizing away the |wait_rv| and | |
40 // |err| variables so they appear nicely on the stack in crash dumps. | |
41 VLOG(1) << "wait_rv=" << wait_rv << ", err=" << err; | |
42 } | |
43 } | |
44 | |
45 // If the (manual-reset) event object is signaled, resets it and returns true. | |
46 // Otherwise, does nothing and returns false. Called after a Winsock function | |
47 // succeeds synchronously | |
48 // | |
49 // Our testing shows that except in rare cases (when running inside QEMU), | |
50 // the event object is already signaled at this point, so we call this method | |
51 // to avoid a context switch in common cases. This is just a performance | |
52 // optimization. The code still works if this function simply returns false. | |
53 bool ResetEventIfSignaled(WSAEVENT hEvent) { | |
54 // TODO(wtc): Remove the CHECKs after enough testing. | |
55 DWORD wait_rv = WaitForSingleObject(hEvent, 0); | |
56 if (wait_rv == WAIT_TIMEOUT) | |
57 return false; // The event object is not signaled. | |
58 CHECK_EQ(WAIT_OBJECT_0, wait_rv); | |
59 BOOL ok = WSAResetEvent(hEvent); | |
60 CHECK(ok); | |
61 return true; | |
62 } | |
63 | |
64 //----------------------------------------------------------------------------- | |
65 | |
66 int MapWinsockError(int os_error) { | |
67 // There are numerous Winsock error codes, but these are the ones we thus far | |
68 // find interesting. | |
69 switch (os_error) { | |
70 case WSAEACCES: | |
71 return ERR_ACCESS_DENIED; | |
72 case WSAENETDOWN: | |
73 return ERR_INTERNET_DISCONNECTED; | |
74 case WSAETIMEDOUT: | |
75 return ERR_TIMED_OUT; | |
76 case WSAECONNRESET: | |
77 case WSAENETRESET: // Related to keep-alive | |
78 return ERR_CONNECTION_RESET; | |
79 case WSAECONNABORTED: | |
80 return ERR_CONNECTION_ABORTED; | |
81 case WSAECONNREFUSED: | |
82 return ERR_CONNECTION_REFUSED; | |
83 case WSA_IO_INCOMPLETE: | |
84 case WSAEDISCON: | |
85 // WSAEDISCON is returned by WSARecv or WSARecvFrom for message-oriented | |
86 // sockets (where a return value of zero means a zero-byte message) to | |
87 // indicate graceful connection shutdown. We should not ever see this | |
88 // error code for TCP sockets, which are byte stream oriented. | |
89 LOG(DFATAL) << "Unexpected error " << os_error | |
90 << " mapped to net::ERR_UNEXPECTED"; | |
91 return ERR_UNEXPECTED; | |
92 case WSAEHOSTUNREACH: | |
93 case WSAENETUNREACH: | |
94 return ERR_ADDRESS_UNREACHABLE; | |
95 case WSAEADDRNOTAVAIL: | |
96 return ERR_ADDRESS_INVALID; | |
97 case ERROR_SUCCESS: | |
98 return OK; | |
99 default: | |
100 LOG(WARNING) << "Unknown error " << os_error | |
101 << " mapped to net::ERR_FAILED"; | |
102 return ERR_FAILED; | |
103 } | |
104 } | |
105 | |
106 int MapConnectError(int os_error) { | 31 int MapConnectError(int os_error) { |
107 switch (os_error) { | 32 switch (os_error) { |
108 // connect fails with WSAEACCES when Windows Firewall blocks the | 33 // connect fails with WSAEACCES when Windows Firewall blocks the |
109 // connection. | 34 // connection. |
110 case WSAEACCES: | 35 case WSAEACCES: |
111 return ERR_NETWORK_ACCESS_DENIED; | 36 return ERR_NETWORK_ACCESS_DENIED; |
112 case WSAETIMEDOUT: | 37 case WSAETIMEDOUT: |
113 return ERR_CONNECTION_TIMED_OUT; | 38 return ERR_CONNECTION_TIMED_OUT; |
114 default: { | 39 default: { |
115 int net_error = MapWinsockError(os_error); | 40 int net_error = MapWinsockError(os_error); |
(...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
881 use_history_.set_was_used_to_convey_data(); | 806 use_history_.set_was_used_to_convey_data(); |
882 LogByteTransfer(net_log_, NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 807 LogByteTransfer(net_log_, NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
883 core_->write_buffer_.buf); | 808 core_->write_buffer_.buf); |
884 } | 809 } |
885 } | 810 } |
886 core_->write_iobuffer_ = NULL; | 811 core_->write_iobuffer_ = NULL; |
887 DoWriteCallback(rv); | 812 DoWriteCallback(rv); |
888 } | 813 } |
889 | 814 |
890 } // namespace net | 815 } // namespace net |
OLD | NEW |