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