Chromium Code Reviews| 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 "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
| 6 #include <limits.h> | 6 |
| 7 #include <stdio.h> | |
| 8 #include <windows.h> | |
| 9 #include <sys/types.h> | |
| 10 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/win/scoped_handle.h" | |
| 9 | |
| 10 using base::win::ScopedHandle; | |
|
darin (slow to review)
2012/01/24 18:59:04
nit: you could put this inside the "namespace base
tommi (sloooow) - chröme
2012/01/24 21:03:25
Done.
| |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 // IMPORTANT: do not change how this name is generated because it will break | 15 // IMPORTANT: do not change how this name is generated because it will break |
| 16 // in sandboxed scenarios as we might have by-name policies that allow pipe | 16 // in sandboxed scenarios as we might have by-name policies that allow pipe |
| 17 // creation. Also keep the secure random number generation. | 17 // creation. Also keep the secure random number generation. |
| 18 const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; | 18 const wchar_t kPipeNameFormat[] = L"\\\\.\\pipe\\chrome.sync.%u.%u.%lu"; |
| 19 const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1; | 19 const size_t kPipePathMax = arraysize(kPipeNameFormat) + (3 * 10) + 1; |
| 20 | 20 |
| 21 // To avoid users sending negative message lengths to Send/Receive | 21 // To avoid users sending negative message lengths to Send/Receive |
| 22 // we clamp message lengths, which are size_t, to no more than INT_MAX. | 22 // we clamp message lengths, which are size_t, to no more than INT_MAX. |
| 23 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); | 23 const size_t kMaxMessageLength = static_cast<size_t>(INT_MAX); |
| 24 | 24 |
| 25 const int kOutBufferSize = 4096; | 25 const int kOutBufferSize = 4096; |
| 26 const int kInBufferSize = 4096; | 26 const int kInBufferSize = 4096; |
| 27 const int kDefaultTimeoutMilliSeconds = 1000; | 27 const int kDefaultTimeoutMilliSeconds = 1000; |
| 28 | 28 |
| 29 bool CreatePairImpl(HANDLE* socket_a, HANDLE* socket_b, bool overlapped) { | |
| 30 DCHECK(socket_a != socket_b); | |
| 31 DCHECK(*socket_a == SyncSocket::kInvalidHandle); | |
| 32 DCHECK(*socket_b == SyncSocket::kInvalidHandle); | |
| 33 | |
| 34 wchar_t name[kPipePathMax]; | |
| 35 ScopedHandle handle_a; | |
| 36 DWORD flags = PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE; | |
| 37 if (overlapped) | |
| 38 flags |= FILE_FLAG_OVERLAPPED; | |
| 39 | |
| 40 do { | |
| 41 unsigned int rnd_name; | |
| 42 if (rand_s(&rnd_name) != 0) | |
| 43 return false; | |
| 44 | |
| 45 swprintf(name, kPipePathMax, | |
| 46 kPipeNameFormat, | |
| 47 GetCurrentProcessId(), | |
| 48 GetCurrentThreadId(), | |
| 49 rnd_name); | |
| 50 | |
| 51 handle_a.Set(CreateNamedPipeW( | |
| 52 name, | |
| 53 flags, | |
| 54 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | |
| 55 1, | |
| 56 kOutBufferSize, | |
| 57 kInBufferSize, | |
| 58 kDefaultTimeoutMilliSeconds, | |
| 59 NULL)); | |
| 60 } while (!handle_a.IsValid() && | |
| 61 (GetLastError() == ERROR_PIPE_BUSY)); | |
| 62 | |
| 63 if (!handle_a.IsValid()) { | |
| 64 NOTREACHED(); | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 // The SECURITY_ANONYMOUS flag means that the server side (handle_a) cannot | |
| 69 // impersonate the client (handle_b). This allows us not to care which side | |
| 70 // ends up in which side of a privilege boundary. | |
| 71 flags = SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS; | |
| 72 if (overlapped) | |
| 73 flags |= FILE_FLAG_OVERLAPPED; | |
| 74 | |
| 75 ScopedHandle handle_b(CreateFileW(name, | |
| 76 GENERIC_READ | GENERIC_WRITE, | |
| 77 0, // no sharing. | |
| 78 NULL, // default security attributes. | |
| 79 OPEN_EXISTING, // opens existing pipe. | |
| 80 flags, | |
| 81 NULL)); // no template file. | |
| 82 if (!handle_b.IsValid()) { | |
| 83 DPLOG(ERROR) << "CreateFileW failed"; | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 if (!ConnectNamedPipe(handle_a, NULL)) { | |
| 88 DWORD error = GetLastError(); | |
| 89 if (error != ERROR_PIPE_CONNECTED) { | |
| 90 DPLOG(ERROR) << "ConnectNamedPipe failed"; | |
| 91 return false; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 *socket_a = handle_a.Take(); | |
| 96 *socket_b = handle_b.Take(); | |
| 97 | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 // Template function that supports calling ReadFile or WriteFile in an | |
| 102 // overlapped fashion and waits for IO completion. The function also waits | |
| 103 // on an event that can be used to cancel the operation. If the operation | |
| 104 // is cancelled, the function returns and closes the relevant socket object. | |
| 105 template <typename BufferType, typename Function> | |
| 106 size_t CancelableFileOperation(Function operation, HANDLE file, | |
| 107 BufferType* buffer, size_t length, | |
| 108 base::WaitableEvent* io_event, | |
| 109 base::WaitableEvent* cancel_event, | |
| 110 CancelableSyncSocket* socket) { | |
| 111 // The buffer must be byte size or the length check won't make much sense. | |
| 112 COMPILE_ASSERT(sizeof(buffer[0]) == sizeof(char), incorrect_buffer_type); | |
| 113 DCHECK_LE(length, kMaxMessageLength); | |
| 114 | |
| 115 OVERLAPPED ol = {0}; | |
| 116 ol.hEvent = io_event->handle(); | |
| 117 size_t count = 0; | |
| 118 while (count < length) { | |
| 119 DWORD len; | |
| 120 // The following statement is for 64 bit portability. | |
| 121 DWORD chunk = static_cast<DWORD>( | |
| 122 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); | |
| 123 // This is either the ReadFile or WriteFile call depending on whether | |
| 124 // we're receiving or sending data. | |
| 125 BOOL ok = operation(file, static_cast<BufferType*>(buffer) + count, chunk, | |
| 126 &len, &ol); | |
| 127 if (!ok) { | |
| 128 if (::GetLastError() == ERROR_IO_PENDING) { | |
| 129 base::WaitableEvent* events[] = { io_event, cancel_event }; | |
| 130 size_t signaled = WaitableEvent::WaitMany(events, arraysize(events)); | |
| 131 if (signaled == 1) { | |
| 132 VLOG(1) << "Shutdown was signaled. Closing socket."; | |
| 133 socket->Close(); | |
| 134 break; | |
| 135 } else { | |
| 136 GetOverlappedResult(file, &ol, &len, TRUE); | |
| 137 } | |
| 138 } else { | |
| 139 return (0 < count) ? count : 0; | |
| 140 } | |
| 141 } | |
| 142 count += len; | |
| 143 } | |
| 144 return count; | |
| 145 } | |
| 146 | |
| 29 } // namespace | 147 } // namespace |
| 30 | 148 |
| 31 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; | 149 const SyncSocket::Handle SyncSocket::kInvalidHandle = INVALID_HANDLE_VALUE; |
| 32 | 150 |
| 33 bool SyncSocket::CreatePair(SyncSocket* pair[2]) { | 151 SyncSocket::SyncSocket() : handle_(kInvalidHandle) {} |
| 34 Handle handles[2]; | |
| 35 SyncSocket* tmp_sockets[2]; | |
| 36 | 152 |
| 37 // Create the two SyncSocket objects first to avoid ugly cleanup issues. | 153 SyncSocket::~SyncSocket() { |
| 38 tmp_sockets[0] = new SyncSocket(kInvalidHandle); | 154 Close(); |
| 39 if (tmp_sockets[0] == NULL) { | 155 } |
| 40 return false; | |
| 41 } | |
| 42 tmp_sockets[1] = new SyncSocket(kInvalidHandle); | |
| 43 if (tmp_sockets[1] == NULL) { | |
| 44 delete tmp_sockets[0]; | |
| 45 return false; | |
| 46 } | |
| 47 | 156 |
| 48 wchar_t name[kPipePathMax]; | 157 // static |
| 49 do { | 158 bool SyncSocket::CreatePair(SyncSocket* socket_a, SyncSocket* socket_b) { |
| 50 unsigned int rnd_name; | 159 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, false); |
| 51 if (rand_s(&rnd_name) != 0) | |
| 52 return false; | |
| 53 swprintf(name, kPipePathMax, | |
| 54 kPipeNameFormat, | |
| 55 GetCurrentProcessId(), | |
| 56 GetCurrentThreadId(), | |
| 57 rnd_name); | |
| 58 handles[0] = CreateNamedPipeW( | |
| 59 name, | |
| 60 PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE, | |
| 61 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | |
| 62 1, | |
| 63 kOutBufferSize, | |
| 64 kInBufferSize, | |
| 65 kDefaultTimeoutMilliSeconds, | |
| 66 NULL); | |
| 67 } while ((handles[0] == INVALID_HANDLE_VALUE) && | |
| 68 (GetLastError() == ERROR_PIPE_BUSY)); | |
| 69 | |
| 70 if (handles[0] == INVALID_HANDLE_VALUE) { | |
| 71 NOTREACHED(); | |
| 72 return false; | |
| 73 } | |
| 74 // The SECURITY_ANONYMOUS flag means that the server side (pair[0]) cannot | |
| 75 // impersonate the client (pair[1]). This allows us not to care which side | |
| 76 // ends up in which side of a privilege boundary. | |
| 77 handles[1] = CreateFileW(name, | |
| 78 GENERIC_READ | GENERIC_WRITE, | |
| 79 0, // no sharing. | |
| 80 NULL, // default security attributes. | |
| 81 OPEN_EXISTING, // opens existing pipe. | |
| 82 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS, | |
| 83 NULL); // no template file. | |
| 84 if (handles[1] == INVALID_HANDLE_VALUE) { | |
| 85 CloseHandle(handles[0]); | |
| 86 return false; | |
| 87 } | |
| 88 if (ConnectNamedPipe(handles[0], NULL) == FALSE) { | |
| 89 DWORD error = GetLastError(); | |
| 90 if (error != ERROR_PIPE_CONNECTED) { | |
| 91 CloseHandle(handles[0]); | |
| 92 CloseHandle(handles[1]); | |
| 93 return false; | |
| 94 } | |
| 95 } | |
| 96 // Copy the handles out for successful return. | |
| 97 tmp_sockets[0]->handle_ = handles[0]; | |
| 98 pair[0] = tmp_sockets[0]; | |
| 99 tmp_sockets[1]->handle_ = handles[1]; | |
| 100 pair[1] = tmp_sockets[1]; | |
| 101 return true; | |
| 102 } | 160 } |
| 103 | 161 |
| 104 bool SyncSocket::Close() { | 162 bool SyncSocket::Close() { |
| 105 if (handle_ == kInvalidHandle) { | 163 if (handle_ == kInvalidHandle) |
| 106 return false; | 164 return false; |
| 107 } | 165 |
| 108 BOOL retval = CloseHandle(handle_); | 166 BOOL retval = CloseHandle(handle_); |
| 109 handle_ = kInvalidHandle; | 167 handle_ = kInvalidHandle; |
| 110 return retval ? true : false; | 168 return retval ? true : false; |
| 111 } | 169 } |
| 112 | 170 |
| 113 size_t SyncSocket::Send(const void* buffer, size_t length) { | 171 size_t SyncSocket::Send(const void* buffer, size_t length) { |
| 114 DCHECK_LE(length, kMaxMessageLength); | 172 DCHECK_LE(length, kMaxMessageLength); |
| 115 size_t count = 0; | 173 size_t count = 0; |
| 116 while (count < length) { | 174 while (count < length) { |
| 117 DWORD len; | 175 DWORD len; |
| 118 // The following statement is for 64 bit portability. | 176 // The following statement is for 64 bit portability. |
| 119 DWORD chunk = static_cast<DWORD>( | 177 DWORD chunk = static_cast<DWORD>( |
|
darin (slow to review)
2012/01/24 18:59:04
Perhaps an inline helper function would be useful.
tommi (sloooow) - chröme
2012/01/24 21:03:25
Done.
| |
| 120 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); | 178 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| 121 if (WriteFile(handle_, static_cast<const char*>(buffer) + count, | 179 if (WriteFile(handle_, static_cast<const char*>(buffer) + count, |
| 122 chunk, &len, NULL) == FALSE) { | 180 chunk, &len, NULL) == FALSE) { |
| 123 return (0 < count) ? count : 0; | 181 return (0 < count) ? count : 0; |
| 124 } | 182 } |
| 125 count += len; | 183 count += len; |
| 126 } | 184 } |
| 127 return count; | 185 return count; |
| 128 } | 186 } |
| 129 | 187 |
| 130 size_t SyncSocket::Receive(void* buffer, size_t length) { | 188 size_t SyncSocket::Receive(void* buffer, size_t length) { |
| 131 DCHECK_LE(length, kMaxMessageLength); | 189 DCHECK_LE(length, kMaxMessageLength); |
| 132 size_t count = 0; | 190 size_t count = 0; |
| 133 while (count < length) { | 191 while (count < length) { |
| 134 DWORD len; | 192 DWORD len; |
| 135 DWORD chunk = static_cast<DWORD>( | 193 DWORD chunk = static_cast<DWORD>( |
| 136 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); | 194 ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX); |
| 137 if (ReadFile(handle_, static_cast<char*>(buffer) + count, | 195 if (ReadFile(handle_, static_cast<char*>(buffer) + count, |
| 138 chunk, &len, NULL) == FALSE) { | 196 chunk, &len, NULL) == FALSE) { |
| 139 return (0 < count) ? count : 0; | 197 return (0 < count) ? count : 0; |
| 140 } | 198 } |
| 141 count += len; | 199 count += len; |
| 142 } | 200 } |
| 143 return count; | 201 return count; |
| 144 } | 202 } |
| 145 | 203 |
| 146 size_t SyncSocket::Peek() { | 204 size_t SyncSocket::Peek() { |
| 147 DWORD available = 0; | 205 DWORD available = 0; |
| 148 PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL); | 206 PeekNamedPipe(handle_, NULL, 0, NULL, &available, NULL); |
| 149 return available; | 207 return available; |
| 150 } | 208 } |
| 151 | 209 |
| 210 CancelableSyncSocket::CancelableSyncSocket() | |
| 211 : shutdown_event_(true, false), file_operation_(true, false) { | |
| 212 } | |
| 213 | |
| 214 CancelableSyncSocket::CancelableSyncSocket(Handle handle) | |
| 215 : SyncSocket(handle), shutdown_event_(true, false), | |
| 216 file_operation_(true, false) { | |
| 217 } | |
| 218 | |
| 219 bool CancelableSyncSocket::Shutdown() { | |
| 220 // This doesn't shut down the pipe immediately, but subsequent Receive or Send | |
| 221 // methods will fail straight away. | |
| 222 shutdown_event_.Signal(); | |
| 223 return true; | |
| 224 } | |
| 225 | |
| 226 bool CancelableSyncSocket::Close() { | |
| 227 bool ret = SyncSocket::Close(); | |
| 228 shutdown_event_.Reset(); | |
| 229 return ret; | |
| 230 } | |
| 231 | |
| 232 size_t CancelableSyncSocket::Send(const void* buffer, size_t length) { | |
| 233 return CancelableFileOperation(&WriteFile, handle_, | |
| 234 reinterpret_cast<const char*>(buffer), length, &file_operation_, | |
| 235 &shutdown_event_, this); | |
| 236 } | |
| 237 | |
| 238 size_t CancelableSyncSocket::Receive(void* buffer, size_t length) { | |
| 239 return CancelableFileOperation(&ReadFile, handle_, | |
| 240 reinterpret_cast<char*>(buffer), length, &file_operation_, | |
| 241 &shutdown_event_, this); | |
| 242 } | |
| 243 | |
| 244 // static | |
| 245 bool CancelableSyncSocket::CreatePair(CancelableSyncSocket* socket_a, | |
| 246 CancelableSyncSocket* socket_b) { | |
| 247 return CreatePairImpl(&socket_a->handle_, &socket_b->handle_, true); | |
| 248 } | |
| 249 | |
| 250 | |
| 152 } // namespace base | 251 } // namespace base |
| OLD | NEW |