[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_channel_win.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <windows.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 5be7da24 | 2009-11-20 23:16:26 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 11 | #include "base/compiler_specific.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 13 | #include "base/process_util.h" |
| 14 | #include "base/rand_util.h" |
| 15 | #include "base/string_number_conversions.h" |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 16 | #include "base/threading/thread_checker.h" |
[email protected] | be1ce6a7 | 2010-08-03 14:35:22 | [diff] [blame] | 17 | #include "base/utf_string_conversions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 18 | #include "base/win/scoped_handle.h" |
[email protected] | 0ee99068 | 2012-11-17 19:20:05 | [diff] [blame] | 19 | #include "ipc/ipc_listener.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 20 | #include "ipc/ipc_logging.h" |
| 21 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 23 | namespace IPC { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 24 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 25 | Channel::ChannelImpl::State::State(ChannelImpl* channel) : is_pending(false) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 26 | memset(&context.overlapped, 0, sizeof(context.overlapped)); |
| 27 | context.handler = channel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 30 | Channel::ChannelImpl::State::~State() { |
| 31 | COMPILE_ASSERT(!offsetof(Channel::ChannelImpl::State, context), |
| 32 | starts_with_io_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | } |
| 34 | |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 35 | Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle &channel_handle, |
| 36 | Mode mode, Listener* listener) |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 37 | : ChannelReader(listener), |
| 38 | ALLOW_THIS_IN_INITIALIZER_LIST(input_state_(this)), |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 39 | ALLOW_THIS_IN_INITIALIZER_LIST(output_state_(this)), |
| 40 | pipe_(INVALID_HANDLE_VALUE), |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 41 | peer_pid_(base::kNullProcessId), |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 42 | waiting_connect_(mode & MODE_SERVER_FLAG), |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 43 | processing_incoming_(false), |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 44 | ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 45 | client_secret_(0), |
| 46 | validate_client_(false) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 47 | CreatePipe(channel_handle, mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 50 | Channel::ChannelImpl::~ChannelImpl() { |
| 51 | Close(); |
| 52 | } |
| 53 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 54 | void Channel::ChannelImpl::Close() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 55 | if (thread_check_.get()) { |
| 56 | DCHECK(thread_check_->CalledOnValidThread()); |
| 57 | } |
| 58 | |
[email protected] | 74f87acf2 | 2009-10-14 22:10:40 | [diff] [blame] | 59 | if (input_state_.is_pending || output_state_.is_pending) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 60 | CancelIo(pipe_); |
[email protected] | ee78622d | 2008-10-13 21:25:50 | [diff] [blame] | 61 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 62 | // Closing the handle at this point prevents us from issuing more requests |
| 63 | // form OnIOCompleted(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 64 | if (pipe_ != INVALID_HANDLE_VALUE) { |
| 65 | CloseHandle(pipe_); |
| 66 | pipe_ = INVALID_HANDLE_VALUE; |
| 67 | } |
| 68 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 69 | // Make sure all IO has completed. |
| 70 | base::Time start = base::Time::Now(); |
| 71 | while (input_state_.is_pending || output_state_.is_pending) { |
| 72 | MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
| 73 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 74 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | while (!output_queue_.empty()) { |
| 76 | Message* m = output_queue_.front(); |
| 77 | output_queue_.pop(); |
| 78 | delete m; |
| 79 | } |
| 80 | } |
| 81 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 82 | bool Channel::ChannelImpl::Send(Message* message) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 83 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 84 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 85 | << " with type " << message->type() |
| 86 | << " (" << output_queue_.size() << " in queue)"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | |
| 88 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 89 | Logging::GetInstance()->OnSendMessage(message, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | #endif |
| 91 | |
[email protected] | 2c391df | 2012-09-18 03:41:29 | [diff] [blame] | 92 | message->TraceMessageBegin(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | output_queue_.push(message); |
| 94 | // ensure waiting to write |
| 95 | if (!waiting_connect_) { |
| 96 | if (!output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 97 | if (!ProcessOutgoingMessages(NULL, 0)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 105 | // static |
| 106 | bool Channel::ChannelImpl::IsNamedServerInitialized( |
| 107 | const std::string& channel_id) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 108 | if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1)) |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 109 | return true; |
| 110 | // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another |
| 111 | // connection. |
| 112 | return GetLastError() == ERROR_SEM_TIMEOUT; |
| 113 | } |
| 114 | |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 115 | Channel::ChannelImpl::ReadState Channel::ChannelImpl::ReadData( |
| 116 | char* buffer, |
| 117 | int buffer_len, |
| 118 | int* /* bytes_read */) { |
| 119 | if (INVALID_HANDLE_VALUE == pipe_) |
| 120 | return READ_FAILED; |
| 121 | |
| 122 | DWORD bytes_read = 0; |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 123 | BOOL ok = ReadFile(pipe_, buffer, buffer_len, |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 124 | &bytes_read, &input_state_.context.overlapped); |
| 125 | if (!ok) { |
| 126 | DWORD err = GetLastError(); |
| 127 | if (err == ERROR_IO_PENDING) { |
| 128 | input_state_.is_pending = true; |
| 129 | return READ_PENDING; |
| 130 | } |
| 131 | LOG(ERROR) << "pipe error: " << err; |
| 132 | return READ_FAILED; |
| 133 | } |
| 134 | |
| 135 | // We could return READ_SUCCEEDED here. But the way that this code is |
| 136 | // structured we instead go back to the message loop. Our completion port |
| 137 | // will be signalled even in the "synchronously completed" state. |
| 138 | // |
| 139 | // This allows us to potentially process some outgoing messages and |
| 140 | // interleave other work on this thread when we're getting hammered with |
| 141 | // input messages. Potentially, this could be tuned to be more efficient |
| 142 | // with some testing. |
| 143 | input_state_.is_pending = true; |
| 144 | return READ_PENDING; |
| 145 | } |
| 146 | |
| 147 | bool Channel::ChannelImpl::WillDispatchInputMessage(Message* msg) { |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 148 | // Make sure we get a hello when client validation is required. |
| 149 | if (validate_client_) |
| 150 | return IsHelloMessage(*msg); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 151 | return true; |
| 152 | } |
| 153 | |
| 154 | void Channel::ChannelImpl::HandleHelloMessage(const Message& msg) { |
| 155 | // The hello message contains one parameter containing the PID. |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 156 | MessageIterator it = MessageIterator(msg); |
| 157 | int32 claimed_pid = it.NextInt(); |
| 158 | if (validate_client_ && (it.NextInt() != client_secret_)) { |
| 159 | NOTREACHED(); |
| 160 | // Something went wrong. Abort connection. |
| 161 | Close(); |
| 162 | listener()->OnChannelError(); |
| 163 | return; |
| 164 | } |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 165 | peer_pid_ = claimed_pid; |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 166 | // validation completed. |
| 167 | validate_client_ = false; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 168 | listener()->OnChannelConnected(claimed_pid); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool Channel::ChannelImpl::DidEmptyInputBuffers() { |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 172 | // We don't need to do anything here. |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 176 | // static |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 177 | const string16 Channel::ChannelImpl::PipeName( |
| 178 | const std::string& channel_id, int32* secret) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 179 | std::string name("\\\\.\\pipe\\chrome."); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 180 | |
| 181 | // Prevent the shared secret from ending up in the pipe name. |
| 182 | size_t index = channel_id.find_first_of('\\'); |
| 183 | if (index != std::string::npos) { |
| 184 | if (secret) // Retrieve the secret if asked for. |
| 185 | base::StringToInt(channel_id.substr(index + 1), secret); |
| 186 | return ASCIIToWide(name.append(channel_id.substr(0, index - 1))); |
| 187 | } |
| 188 | |
| 189 | // This case is here to support predictable named pipes in tests. |
| 190 | if (secret) |
| 191 | *secret = 0; |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 192 | return ASCIIToWide(name.append(channel_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 195 | bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 196 | Mode mode) { |
[email protected] | 5e0be64 | 2011-04-28 18:20:09 | [diff] [blame] | 197 | DCHECK_EQ(INVALID_HANDLE_VALUE, pipe_); |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 198 | string16 pipe_name; |
| 199 | // If we already have a valid pipe for channel just copy it. |
| 200 | if (channel_handle.pipe.handle) { |
| 201 | DCHECK(channel_handle.name.empty()); |
| 202 | pipe_name = L"Not Available"; // Just used for LOG |
| 203 | // Check that the given pipe confirms to the specified mode. We can |
| 204 | // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the |
| 205 | // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0. |
| 206 | DWORD flags = 0; |
| 207 | GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL); |
| 208 | DCHECK(!(flags & PIPE_TYPE_MESSAGE)); |
| 209 | if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) || |
| 210 | ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) { |
| 211 | LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; |
| 212 | return false; |
| 213 | } |
| 214 | if (!DuplicateHandle(GetCurrentProcess(), |
| 215 | channel_handle.pipe.handle, |
| 216 | GetCurrentProcess(), |
| 217 | &pipe_, |
| 218 | 0, |
| 219 | FALSE, |
| 220 | DUPLICATE_SAME_ACCESS)) { |
| 221 | LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); |
| 222 | return false; |
| 223 | } |
| 224 | } else if (mode & MODE_SERVER_FLAG) { |
| 225 | DCHECK(!channel_handle.pipe.handle); |
| 226 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 227 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 228 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
| 229 | validate_client_ = !!client_secret_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 230 | pipe_ = CreateNamedPipeW(pipe_name.c_str(), |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 231 | open_mode, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 233 | 1, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 234 | Channel::kReadBufferSize, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 235 | Channel::kReadBufferSize, |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 236 | 5000, |
| 237 | NULL); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 238 | } else if (mode & MODE_CLIENT_FLAG) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 239 | DCHECK(!channel_handle.pipe.handle); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 240 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 241 | pipe_ = CreateFileW(pipe_name.c_str(), |
| 242 | GENERIC_READ | GENERIC_WRITE, |
| 243 | 0, |
| 244 | NULL, |
| 245 | OPEN_EXISTING, |
| 246 | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION | |
| 247 | FILE_FLAG_OVERLAPPED, |
| 248 | NULL); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 249 | } else { |
| 250 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 251 | } |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 252 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 253 | if (pipe_ == INVALID_HANDLE_VALUE) { |
| 254 | // If this process is being closed, the pipe may be gone already. |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 255 | LOG(WARNING) << "Unable to create pipe \"" << pipe_name << |
[email protected] | 34088fb | 2012-05-06 23:21:55 | [diff] [blame] | 256 | "\" in " << (mode & MODE_SERVER_FLAG ? "server" : "client") |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 257 | << " mode. Error :" << GetLastError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 258 | return false; |
| 259 | } |
| 260 | |
| 261 | // Create the Hello message to be sent when Connect is called |
| 262 | scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE, |
| 263 | HELLO_MESSAGE_TYPE, |
| 264 | IPC::Message::PRIORITY_NORMAL)); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 265 | |
| 266 | // Don't send the secret to the untrusted process, and don't send a secret |
| 267 | // if the value is zero (for IPC backwards compatability). |
| 268 | int32 secret = validate_client_ ? 0 : client_secret_; |
| 269 | if (!m->WriteInt(GetCurrentProcessId()) || |
| 270 | (secret && !m->WriteUInt32(secret))) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 271 | CloseHandle(pipe_); |
| 272 | pipe_ = INVALID_HANDLE_VALUE; |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | output_queue_.push(m.release()); |
| 277 | return true; |
| 278 | } |
| 279 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 280 | bool Channel::ChannelImpl::Connect() { |
[email protected] | 1934072 | 2009-08-17 19:53:25 | [diff] [blame] | 281 | DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 282 | |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 283 | if (!thread_check_.get()) |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 284 | thread_check_.reset(new base::ThreadChecker()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 285 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | if (pipe_ == INVALID_HANDLE_VALUE) |
| 287 | return false; |
| 288 | |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 289 | MessageLoopForIO::current()->RegisterIOHandler(pipe_, this); |
| 290 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 291 | // Check to see if there is a client connected to our pipe... |
| 292 | if (waiting_connect_) |
| 293 | ProcessConnection(); |
| 294 | |
| 295 | if (!input_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 296 | // Complete setup asynchronously. By not setting input_state_.is_pending |
| 297 | // to true, we indicate to OnIOCompleted that this is the special |
| 298 | // initialization signal. |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 299 | MessageLoopForIO::current()->PostTask( |
| 300 | FROM_HERE, base::Bind(&Channel::ChannelImpl::OnIOCompleted, |
| 301 | weak_factory_.GetWeakPtr(), &input_state_.context, |
| 302 | 0, 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (!waiting_connect_) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 306 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | return true; |
| 308 | } |
| 309 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 310 | bool Channel::ChannelImpl::ProcessConnection() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 311 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 312 | if (input_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 313 | input_state_.is_pending = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 314 | |
| 315 | // Do we have a client connected to our pipe? |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 316 | if (INVALID_HANDLE_VALUE == pipe_) |
| 317 | return false; |
| 318 | |
| 319 | BOOL ok = ConnectNamedPipe(pipe_, &input_state_.context.overlapped); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 320 | |
| 321 | DWORD err = GetLastError(); |
| 322 | if (ok) { |
| 323 | // Uhm, the API documentation says that this function should never |
| 324 | // return success when used in overlapped mode. |
| 325 | NOTREACHED(); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | switch (err) { |
| 330 | case ERROR_IO_PENDING: |
| 331 | input_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | break; |
| 333 | case ERROR_PIPE_CONNECTED: |
| 334 | waiting_connect_ = false; |
| 335 | break; |
[email protected] | 20aa32c | 2009-07-14 22:25:49 | [diff] [blame] | 336 | case ERROR_NO_DATA: |
| 337 | // The pipe is being closed. |
| 338 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 339 | default: |
| 340 | NOTREACHED(); |
| 341 | return false; |
| 342 | } |
| 343 | |
| 344 | return true; |
| 345 | } |
| 346 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 347 | bool Channel::ChannelImpl::ProcessOutgoingMessages( |
| 348 | MessageLoopForIO::IOContext* context, |
| 349 | DWORD bytes_written) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 350 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 351 | // no connection? |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 352 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | |
| 354 | if (output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 355 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 356 | output_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 357 | if (!context || bytes_written == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | DWORD err = GetLastError(); |
| 359 | LOG(ERROR) << "pipe error: " << err; |
| 360 | return false; |
| 361 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 362 | // Message was sent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | DCHECK(!output_queue_.empty()); |
| 364 | Message* m = output_queue_.front(); |
| 365 | output_queue_.pop(); |
| 366 | delete m; |
| 367 | } |
| 368 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 369 | if (output_queue_.empty()) |
| 370 | return true; |
| 371 | |
| 372 | if (INVALID_HANDLE_VALUE == pipe_) |
| 373 | return false; |
| 374 | |
| 375 | // Write to pipe... |
| 376 | Message* m = output_queue_.front(); |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 377 | DCHECK(m->size() <= INT_MAX); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 378 | BOOL ok = WriteFile(pipe_, |
| 379 | m->data(), |
[email protected] | 8a86140 | 2011-01-28 19:59:11 | [diff] [blame] | 380 | static_cast<int>(m->size()), |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 381 | &bytes_written, |
| 382 | &output_state_.context.overlapped); |
| 383 | if (!ok) { |
| 384 | DWORD err = GetLastError(); |
| 385 | if (err == ERROR_IO_PENDING) { |
| 386 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 387 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 388 | DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 389 | << " with type " << m->type(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 391 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 392 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 393 | LOG(ERROR) << "pipe error: " << err; |
| 394 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 397 | DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 398 | << " with type " << m->type(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 399 | |
| 400 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 404 | void Channel::ChannelImpl::OnIOCompleted(MessageLoopForIO::IOContext* context, |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 405 | DWORD bytes_transfered, |
| 406 | DWORD error) { |
| 407 | bool ok = true; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 408 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 409 | if (context == &input_state_.context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | if (waiting_connect_) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 411 | if (!ProcessConnection()) |
| 412 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | // We may have some messages queued up to send... |
| 414 | if (!output_queue_.empty() && !output_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 415 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 416 | if (input_state_.is_pending) |
| 417 | return; |
| 418 | // else, fall-through and look for incoming messages... |
| 419 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 420 | |
| 421 | // We don't support recursion through OnMessageReceived yet! |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 422 | DCHECK(!processing_incoming_); |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame^] | 423 | base::AutoReset<bool> auto_reset_processing_incoming( |
| 424 | &processing_incoming_, true); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 425 | |
| 426 | // Process the new data. |
| 427 | if (input_state_.is_pending) { |
| 428 | // This is the normal case for everything except the initialization step. |
| 429 | input_state_.is_pending = false; |
| 430 | if (!bytes_transfered) |
| 431 | ok = false; |
| 432 | else |
| 433 | ok = AsyncReadComplete(bytes_transfered); |
| 434 | } else { |
| 435 | DCHECK(!bytes_transfered); |
| 436 | } |
| 437 | |
| 438 | // Request more data. |
| 439 | if (ok) |
| 440 | ok = ProcessIncomingMessages(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | } else { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 442 | DCHECK(context == &output_state_.context); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 443 | ok = ProcessOutgoingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 445 | if (!ok && INVALID_HANDLE_VALUE != pipe_) { |
| 446 | // We don't want to re-enter Close(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | Close(); |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 448 | listener()->OnChannelError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 452 | //------------------------------------------------------------------------------ |
| 453 | // Channel's methods simply call through to ChannelImpl. |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 454 | Channel::Channel(const IPC::ChannelHandle &channel_handle, Mode mode, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 455 | Listener* listener) |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 456 | : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | } |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 458 | |
| 459 | Channel::~Channel() { |
| 460 | delete channel_impl_; |
| 461 | } |
| 462 | |
| 463 | bool Channel::Connect() { |
| 464 | return channel_impl_->Connect(); |
| 465 | } |
| 466 | |
| 467 | void Channel::Close() { |
[email protected] | 5fc7ec59 | 2012-05-16 18:58:32 | [diff] [blame] | 468 | if (channel_impl_) |
| 469 | channel_impl_->Close(); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | void Channel::set_listener(Listener* listener) { |
| 473 | channel_impl_->set_listener(listener); |
| 474 | } |
| 475 | |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 476 | base::ProcessId Channel::peer_pid() const { |
| 477 | return channel_impl_->peer_pid(); |
| 478 | } |
| 479 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 480 | bool Channel::Send(Message* message) { |
| 481 | return channel_impl_->Send(message); |
| 482 | } |
| 483 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 484 | // static |
| 485 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
| 486 | return ChannelImpl::IsNamedServerInitialized(channel_id); |
| 487 | } |
| 488 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 489 | // static |
| 490 | std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
| 491 | // Windows pipes can be enumerated by low-privileged processes. So, we |
| 492 | // append a strong random value after the \ character. This value is not |
| 493 | // included in the pipe name, but sent as part of the client hello, to |
| 494 | // hijacking the pipe name to spoof the client. |
| 495 | |
| 496 | std::string id = prefix; |
| 497 | if (!id.empty()) |
| 498 | id.append("."); |
| 499 | |
| 500 | int secret; |
| 501 | do { // Guarantee we get a non-zero value. |
| 502 | secret = base::RandInt(0, std::numeric_limits<int>::max()); |
| 503 | } while (secret == 0); |
| 504 | |
| 505 | id.append(GenerateUniqueRandomChannelID()); |
| 506 | return id.append(base::StringPrintf("\\%d", secret)); |
| 507 | } |
| 508 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 509 | } // namespace IPC |