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 // Prevent the compiler from optimizing away the arguments so they appear | |
31 // nicely on the stack in crash dumps. | |
32 #pragma warning (disable: 4748) | |
33 #pragma optimize( "", off ) | |
34 | |
35 // Pass the important values as function arguments so that they are available | |
36 // in crash dumps. | |
37 void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) { | |
38 if (wait_rv != expected) { | |
39 DWORD err = ERROR_SUCCESS; | |
40 if (wait_rv == WAIT_FAILED) | |
41 err = GetLastError(); | |
42 CHECK(false); // Crash. | |
43 } | |
44 } | |
45 | |
46 #pragma optimize( "", on ) | |
47 #pragma warning (default: 4748) | |
48 | |
49 // Assert that the (manual-reset) event object is not signaled. | |
50 void AssertEventNotSignaled(WSAEVENT hEvent) { | |
51 DWORD wait_rv = WaitForSingleObject(hEvent, 0); | |
52 CheckEventWait(hEvent, wait_rv, WAIT_TIMEOUT); | |
53 } | |
54 | |
55 // If the (manual-reset) event object is signaled, resets it and returns true. | |
56 // Otherwise, does nothing and returns false. Called after a Winsock function | |
57 // succeeds synchronously | |
58 // | |
59 // Our testing shows that except in rare cases (when running inside QEMU), | |
60 // the event object is already signaled at this point, so we call this method | |
61 // to avoid a context switch in common cases. This is just a performance | |
62 // optimization. The code still works if this function simply returns false. | |
63 bool ResetEventIfSignaled(WSAEVENT hEvent) { | |
64 // TODO(wtc): Remove the CHECKs after enough testing. | |
65 DWORD wait_rv = WaitForSingleObject(hEvent, 0); | |
66 if (wait_rv == WAIT_TIMEOUT) | |
67 return false; // The event object is not signaled. | |
68 CheckEventWait(hEvent, wait_rv, WAIT_OBJECT_0); | |
69 BOOL ok = WSAResetEvent(hEvent); | |
70 CHECK(ok); | |
71 return true; | |
72 } | |
73 | |
74 //----------------------------------------------------------------------------- | |
75 | |
76 int MapWinsockError(int os_error) { | |
77 // There are numerous Winsock error codes, but these are the ones we thus far | |
78 // find interesting. | |
79 switch (os_error) { | |
80 case WSAEACCES: | |
81 return ERR_ACCESS_DENIED; | |
82 case WSAENETDOWN: | |
83 return ERR_INTERNET_DISCONNECTED; | |
84 case WSAETIMEDOUT: | |
85 return ERR_TIMED_OUT; | |
86 case WSAECONNRESET: | |
87 case WSAENETRESET: // Related to keep-alive | |
88 return ERR_CONNECTION_RESET; | |
89 case WSAECONNABORTED: | |
90 return ERR_CONNECTION_ABORTED; | |
91 case WSAECONNREFUSED: | |
92 return ERR_CONNECTION_REFUSED; | |
93 case WSA_IO_INCOMPLETE: | |
94 case WSAEDISCON: | |
95 // WSAEDISCON is returned by WSARecv or WSARecvFrom for message-oriented | |
96 // sockets (where a return value of zero means a zero-byte message) to | |
97 // indicate graceful connection shutdown. We should not ever see this | |
98 // error code for TCP sockets, which are byte stream oriented. | |
99 LOG(DFATAL) << "Unexpected error " << os_error | |
100 << " mapped to net::ERR_UNEXPECTED"; | |
101 return ERR_UNEXPECTED; | |
102 case WSAEHOSTUNREACH: | |
103 case WSAENETUNREACH: | |
104 return ERR_ADDRESS_UNREACHABLE; | |
105 case WSAEADDRNOTAVAIL: | |
106 return ERR_ADDRESS_INVALID; | |
107 case ERROR_SUCCESS: | |
108 return OK; | |
109 default: | |
110 LOG(WARNING) << "Unknown error " << os_error | |
111 << " mapped to net::ERR_FAILED"; | |
112 return ERR_FAILED; | |
113 } | |
114 } | |
115 | |
116 int MapConnectError(int os_error) { | 31 int MapConnectError(int os_error) { |
117 switch (os_error) { | 32 switch (os_error) { |
118 // connect fails with WSAEACCES when Windows Firewall blocks the | 33 // connect fails with WSAEACCES when Windows Firewall blocks the |
119 // connection. | 34 // connection. |
120 case WSAEACCES: | 35 case WSAEACCES: |
121 return ERR_NETWORK_ACCESS_DENIED; | 36 return ERR_NETWORK_ACCESS_DENIED; |
122 case WSAETIMEDOUT: | 37 case WSAETIMEDOUT: |
123 return ERR_CONNECTION_TIMED_OUT; | 38 return ERR_CONNECTION_TIMED_OUT; |
124 default: { | 39 default: { |
125 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... |
891 use_history_.set_was_used_to_convey_data(); | 806 use_history_.set_was_used_to_convey_data(); |
892 LogByteTransfer(net_log_, NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, | 807 LogByteTransfer(net_log_, NetLog::TYPE_SOCKET_BYTES_SENT, num_bytes, |
893 core_->write_buffer_.buf); | 808 core_->write_buffer_.buf); |
894 } | 809 } |
895 } | 810 } |
896 core_->write_iobuffer_ = NULL; | 811 core_->write_iobuffer_ = NULL; |
897 DoWriteCallback(rv); | 812 DoWriteCallback(rv); |
898 } | 813 } |
899 | 814 |
900 } // namespace net | 815 } // namespace net |
OLD | NEW |