blob: c04f0c63822098dcb9a22915ef5da9a146a97fd0 [file] [log] [blame]
[email protected]a7c03d4f32012-01-24 02:36:051// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#include "ipc/ipc_channel_win.h"
[email protected]c1afbd2c2008-10-13 19:19:366
tfarina10a5c062015-09-04 18:47:577#include <stdint.h>
initial.commit09911bf2008-07-26 23:55:298#include <windows.h>
initial.commit09911bf2008-07-26 23:55:299
[email protected]5be7da242009-11-20 23:16:2610#include "base/auto_reset.h"
[email protected]72b6f8e22011-11-12 21:16:4111#include "base/bind.h"
[email protected]c1afbd2c2008-10-13 19:19:3612#include "base/compiler_specific.h"
initial.commit09911bf2008-07-26 23:55:2913#include "base/logging.h"
[email protected]7edae3d02012-12-17 20:23:4714#include "base/pickle.h"
[email protected]e66ef602013-07-24 05:15:2415#include "base/process/process_handle.h"
[email protected]5c41e6e12012-03-17 02:20:4616#include "base/rand_util.h"
[email protected]4aa794a12013-06-11 06:32:1817#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4518#include "base/strings/utf_string_conversions.h"
[email protected]327e52b2012-06-25 21:11:3619#include "base/threading/thread_checker.h"
[email protected]b90d7e802011-01-09 16:32:2020#include "base/win/scoped_handle.h"
erikchen5708aae2015-09-14 17:45:1221#include "ipc/attachment_broker.h"
[email protected]0ee990682012-11-17 19:20:0522#include "ipc/ipc_listener.h"
[email protected]946d1b22009-07-22 23:57:2123#include "ipc/ipc_logging.h"
erikcheneece6c32015-07-07 22:13:1124#include "ipc/ipc_message_attachment_set.h"
[email protected]946d1b22009-07-22 23:57:2125#include "ipc/ipc_message_utils.h"
initial.commit09911bf2008-07-26 23:55:2926
initial.commit09911bf2008-07-26 23:55:2927namespace IPC {
[email protected]935aa542010-10-15 01:59:1528
[email protected]2f60c9b2014-06-06 20:13:5129ChannelWin::State::State(ChannelWin* channel) : is_pending(false) {
[email protected]17b89142008-11-07 21:52:1530 memset(&context.overlapped, 0, sizeof(context.overlapped));
31 context.handler = channel;
initial.commit09911bf2008-07-26 23:55:2932}
33
[email protected]2f60c9b2014-06-06 20:13:5134ChannelWin::State::~State() {
anujk.sharma5a7ffe2f2015-01-22 05:39:3735 static_assert(offsetof(ChannelWin::State, context) == 0,
36 "ChannelWin::State should have context as its first data"
37 "member.");
initial.commit09911bf2008-07-26 23:55:2938}
39
erikchen27aa7d82015-06-16 21:21:0440ChannelWin::ChannelWin(const IPC::ChannelHandle& channel_handle,
41 Mode mode,
erikchen5708aae2015-09-14 17:45:1242 Listener* listener)
[email protected]d805c6a2012-03-08 12:30:2843 : ChannelReader(listener),
[email protected]ba2194d2013-04-26 19:51:3044 input_state_(this),
45 output_state_(this),
[email protected]0a6fc4b2012-04-05 02:38:3446 peer_pid_(base::kNullProcessId),
[email protected]1707726c2011-02-03 20:35:0947 waiting_connect_(mode & MODE_SERVER_FLAG),
[email protected]c1afbd2c2008-10-13 19:19:3648 processing_incoming_(false),
[email protected]2d0faf12014-06-27 04:50:4349 validate_client_(false),
[email protected]65727eb2014-07-31 12:24:2950 client_secret_(0),
erikchenf1953822015-06-16 23:16:0451 weak_factory_(this) {
[email protected]c2391b82011-05-06 17:39:0752 CreatePipe(channel_handle, mode);
initial.commit09911bf2008-07-26 23:55:2953}
54
[email protected]2f60c9b2014-06-06 20:13:5155ChannelWin::~ChannelWin() {
erikchen53d919d2015-09-11 17:33:3456 CleanUp();
[email protected]601858c02010-09-01 17:08:2057 Close();
58}
59
[email protected]2f60c9b2014-06-06 20:13:5160void ChannelWin::Close() {
rvargascd742bcb2014-11-14 19:27:3161 if (thread_check_.get())
[email protected]c1e4bff32009-01-29 00:07:0662 DCHECK(thread_check_->CalledOnValidThread());
[email protected]c1e4bff32009-01-29 00:07:0663
[email protected]74f87acf22009-10-14 22:10:4064 if (input_state_.is_pending || output_state_.is_pending)
rvargas8c2d75c2014-09-26 19:50:2665 CancelIo(pipe_.Get());
[email protected]ee78622d2008-10-13 21:25:5066
[email protected]17b89142008-11-07 21:52:1567 // Closing the handle at this point prevents us from issuing more requests
68 // form OnIOCompleted().
rvargas8c2d75c2014-09-26 19:50:2669 if (pipe_.IsValid())
70 pipe_.Close();
initial.commit09911bf2008-07-26 23:55:2971
[email protected]17b89142008-11-07 21:52:1572 // Make sure all IO has completed.
[email protected]17b89142008-11-07 21:52:1573 while (input_state_.is_pending || output_state_.is_pending) {
[email protected]fd0a773a2013-04-30 20:55:0374 base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this);
[email protected]17b89142008-11-07 21:52:1575 }
[email protected]17b89142008-11-07 21:52:1576
initial.commit09911bf2008-07-26 23:55:2977 while (!output_queue_.empty()) {
erikchen5142dc72015-09-10 21:00:1878 OutputElement* element = output_queue_.front();
initial.commit09911bf2008-07-26 23:55:2979 output_queue_.pop();
erikchen5142dc72015-09-10 21:00:1880 delete element;
initial.commit09911bf2008-07-26 23:55:2981 }
82}
83
[email protected]2f60c9b2014-06-06 20:13:5184bool ChannelWin::Send(Message* message) {
[email protected]c1e4bff32009-01-29 00:07:0685 DCHECK(thread_check_->CalledOnValidThread());
[email protected]2a9d601b2010-10-19 23:50:0086 DVLOG(2) << "sending message @" << message << " on channel @" << this
87 << " with type " << message->type()
88 << " (" << output_queue_.size() << " in queue)";
initial.commit09911bf2008-07-26 23:55:2989
erikchenc04ab34c2015-07-27 20:28:2090 if (!prelim_queue_.empty()) {
91 prelim_queue_.push(message);
92 return true;
93 }
94
95 if (message->HasBrokerableAttachments() &&
96 peer_pid_ == base::kNullProcessId) {
97 prelim_queue_.push(message);
98 return true;
99 }
100
101 return ProcessMessageForDelivery(message);
102}
103
104bool ChannelWin::ProcessMessageForDelivery(Message* message) {
erikcheneece6c32015-07-07 22:13:11105 // Sending a brokerable attachment requires a call to Channel::Send(), so
erikchenc04ab34c2015-07-27 20:28:20106 // both Send() and ProcessMessageForDelivery() may be re-entrant. Brokered
107 // attachments must be sent before the Message itself.
erikcheneece6c32015-07-07 22:13:11108 if (message->HasBrokerableAttachments()) {
erikchen5708aae2015-09-14 17:45:12109 DCHECK(GetAttachmentBroker());
erikchenc04ab34c2015-07-27 20:28:20110 DCHECK(peer_pid_ != base::kNullProcessId);
erikcheneece6c32015-07-07 22:13:11111 for (const BrokerableAttachment* attachment :
112 message->attachment_set()->PeekBrokerableAttachments()) {
erikchen5708aae2015-09-14 17:45:12113 if (!GetAttachmentBroker()->SendAttachmentToProcess(attachment,
114 peer_pid_)) {
erikchenc04ab34c2015-07-27 20:28:20115 delete message;
erikcheneece6c32015-07-07 22:13:11116 return false;
erikchenc04ab34c2015-07-27 20:28:20117 }
erikcheneece6c32015-07-07 22:13:11118 }
119 }
120
initial.commit09911bf2008-07-26 23:55:29121#ifdef IPC_MESSAGE_LOG_ENABLED
[email protected]8e8bb6d2010-12-13 08:18:55122 Logging::GetInstance()->OnSendMessage(message, "");
initial.commit09911bf2008-07-26 23:55:29123#endif
124
yuhaoz9b8157d2015-08-18 22:21:35125 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
126 "ChannelWin::ProcessMessageForDelivery",
127 message->flags(),
128 TRACE_EVENT_FLAG_FLOW_OUT);
erikchenc04ab34c2015-07-27 20:28:20129
130 // |output_queue_| takes ownership of |message|.
erikchen5142dc72015-09-10 21:00:18131 OutputElement* element = new OutputElement(message);
132 output_queue_.push(element);
133
erikchen3b325062015-09-22 21:59:10134#if USE_ATTACHMENT_BROKER
135 if (message->HasBrokerableAttachments()) {
136 // |output_queue_| takes ownership of |ids.buffer|.
137 Message::SerializedAttachmentIds ids =
138 message->SerializedIdsOfBrokerableAttachments();
139 output_queue_.push(new OutputElement(ids.buffer, ids.size));
140 }
141#endif
erikchen5142dc72015-09-10 21:00:18142
initial.commit09911bf2008-07-26 23:55:29143 // ensure waiting to write
144 if (!waiting_connect_) {
145 if (!output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36146 if (!ProcessOutgoingMessages(NULL, 0))
initial.commit09911bf2008-07-26 23:55:29147 return false;
initial.commit09911bf2008-07-26 23:55:29148 }
149 }
150
151 return true;
152}
153
erikchenc04ab34c2015-07-27 20:28:20154void ChannelWin::FlushPrelimQueue() {
155 DCHECK_NE(peer_pid_, base::kNullProcessId);
156
157 // Due to the possibly re-entrant nature of ProcessMessageForDelivery(), it
158 // is critical that |prelim_queue_| appears empty.
159 std::queue<Message*> prelim_queue;
160 prelim_queue_.swap(prelim_queue);
161
162 while (!prelim_queue.empty()) {
163 Message* m = prelim_queue.front();
erikchenf81b10a2015-10-06 21:46:37164 bool success = ProcessMessageForDelivery(m);
165 prelim_queue.pop();
166
167 if (!success)
168 break;
169 }
170
171 // Delete any unprocessed messages.
172 while (!prelim_queue.empty()) {
173 Message* m = prelim_queue.front();
174 delete m;
erikchenc04ab34c2015-07-27 20:28:20175 prelim_queue.pop();
176 }
177}
178
erikchen27aa7d82015-06-16 21:21:04179AttachmentBroker* ChannelWin::GetAttachmentBroker() {
erikchen5708aae2015-09-14 17:45:12180 return AttachmentBroker::GetGlobal();
erikchen27aa7d82015-06-16 21:21:04181}
182
[email protected]2f60c9b2014-06-06 20:13:51183base::ProcessId ChannelWin::GetPeerPID() const {
184 return peer_pid_;
185}
186
[email protected]64860882014-08-04 23:44:17187base::ProcessId ChannelWin::GetSelfPID() const {
188 return GetCurrentProcessId();
189}
190
[email protected]313c00e52011-08-09 06:46:06191// static
[email protected]2f60c9b2014-06-06 20:13:51192bool ChannelWin::IsNamedServerInitialized(
[email protected]313c00e52011-08-09 06:46:06193 const std::string& channel_id) {
[email protected]5c41e6e12012-03-17 02:20:46194 if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1))
[email protected]313c00e52011-08-09 06:46:06195 return true;
196 // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another
197 // connection.
198 return GetLastError() == ERROR_SEM_TIMEOUT;
199}
200
[email protected]2f60c9b2014-06-06 20:13:51201ChannelWin::ReadState ChannelWin::ReadData(
[email protected]215f6fd2012-03-03 08:55:45202 char* buffer,
203 int buffer_len,
204 int* /* bytes_read */) {
rvargas8c2d75c2014-09-26 19:50:26205 if (!pipe_.IsValid())
[email protected]215f6fd2012-03-03 08:55:45206 return READ_FAILED;
207
208 DWORD bytes_read = 0;
rvargas8c2d75c2014-09-26 19:50:26209 BOOL ok = ReadFile(pipe_.Get(), buffer, buffer_len,
[email protected]215f6fd2012-03-03 08:55:45210 &bytes_read, &input_state_.context.overlapped);
211 if (!ok) {
212 DWORD err = GetLastError();
213 if (err == ERROR_IO_PENDING) {
214 input_state_.is_pending = true;
215 return READ_PENDING;
216 }
217 LOG(ERROR) << "pipe error: " << err;
218 return READ_FAILED;
219 }
220
221 // We could return READ_SUCCEEDED here. But the way that this code is
222 // structured we instead go back to the message loop. Our completion port
223 // will be signalled even in the "synchronously completed" state.
224 //
225 // This allows us to potentially process some outgoing messages and
226 // interleave other work on this thread when we're getting hammered with
227 // input messages. Potentially, this could be tuned to be more efficient
228 // with some testing.
229 input_state_.is_pending = true;
230 return READ_PENDING;
231}
232
erikchende9412b82015-07-27 18:26:14233bool ChannelWin::ShouldDispatchInputMessage(Message* msg) {
[email protected]05908782012-04-03 08:49:43234 // Make sure we get a hello when client validation is required.
235 if (validate_client_)
236 return IsHelloMessage(*msg);
[email protected]215f6fd2012-03-03 08:55:45237 return true;
238}
239
erikchende9412b82015-07-27 18:26:14240bool ChannelWin::GetNonBrokeredAttachments(Message* msg) {
241 return true;
242}
243
[email protected]2f60c9b2014-06-06 20:13:51244void ChannelWin::HandleInternalMessage(const Message& msg) {
[email protected]dc875dc2013-10-15 00:07:00245 DCHECK_EQ(msg.type(), static_cast<unsigned>(Channel::HELLO_MESSAGE_TYPE));
[email protected]215f6fd2012-03-03 08:55:45246 // The hello message contains one parameter containing the PID.
brettwbd4d7112015-06-03 04:29:25247 base::PickleIterator it(msg);
tfarina10a5c062015-09-04 18:47:57248 int32_t claimed_pid;
[email protected]7edae3d02012-12-17 20:23:47249 bool failed = !it.ReadInt(&claimed_pid);
250
251 if (!failed && validate_client_) {
tfarina10a5c062015-09-04 18:47:57252 int32_t secret;
[email protected]7edae3d02012-12-17 20:23:47253 failed = it.ReadInt(&secret) ? (secret != client_secret_) : true;
254 }
255
256 if (failed) {
[email protected]5c41e6e12012-03-17 02:20:46257 NOTREACHED();
[email protected]5c41e6e12012-03-17 02:20:46258 Close();
259 listener()->OnChannelError();
260 return;
261 }
[email protected]7edae3d02012-12-17 20:23:47262
[email protected]0a6fc4b2012-04-05 02:38:34263 peer_pid_ = claimed_pid;
[email protected]7edae3d02012-12-17 20:23:47264 // Validation completed.
[email protected]05908782012-04-03 08:49:43265 validate_client_ = false;
erikchenc04ab34c2015-07-27 20:28:20266
[email protected]5c41e6e12012-03-17 02:20:46267 listener()->OnChannelConnected(claimed_pid);
erikchenf81b10a2015-10-06 21:46:37268
269 FlushPrelimQueue();
[email protected]215f6fd2012-03-03 08:55:45270}
271
erikchen3c175a32015-07-28 23:16:48272base::ProcessId ChannelWin::GetSenderPID() {
273 return GetPeerPID();
274}
275
erikchen8c73f832015-07-30 22:26:08276bool ChannelWin::IsAttachmentBrokerEndpoint() {
277 return is_attachment_broker_endpoint();
278}
279
[email protected]2f60c9b2014-06-06 20:13:51280bool ChannelWin::DidEmptyInputBuffers() {
[email protected]d805c6a2012-03-08 12:30:28281 // We don't need to do anything here.
[email protected]215f6fd2012-03-03 08:55:45282 return true;
283}
284
[email protected]313c00e52011-08-09 06:46:06285// static
tfarina10a5c062015-09-04 18:47:57286const base::string16 ChannelWin::PipeName(const std::string& channel_id,
287 int32_t* secret) {
[email protected]c2391b82011-05-06 17:39:07288 std::string name("\\\\.\\pipe\\chrome.");
[email protected]5c41e6e12012-03-17 02:20:46289
290 // Prevent the shared secret from ending up in the pipe name.
291 size_t index = channel_id.find_first_of('\\');
292 if (index != std::string::npos) {
293 if (secret) // Retrieve the secret if asked for.
294 base::StringToInt(channel_id.substr(index + 1), secret);
thestige5c64d92014-11-07 01:19:24295 return base::ASCIIToUTF16(name.append(channel_id.substr(0, index - 1)));
[email protected]5c41e6e12012-03-17 02:20:46296 }
297
298 // This case is here to support predictable named pipes in tests.
299 if (secret)
300 *secret = 0;
thestige5c64d92014-11-07 01:19:24301 return base::ASCIIToUTF16(name.append(channel_id));
initial.commit09911bf2008-07-26 23:55:29302}
303
[email protected]2f60c9b2014-06-06 20:13:51304bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle,
rvargas8c2d75c2014-09-26 19:50:26305 Mode mode) {
306 DCHECK(!pipe_.IsValid());
[email protected]905cda812013-12-20 09:04:55307 base::string16 pipe_name;
[email protected]a7c03d4f32012-01-24 02:36:05308 // If we already have a valid pipe for channel just copy it.
309 if (channel_handle.pipe.handle) {
rvargas8c2d75c2014-09-26 19:50:26310 // TODO(rvargas) crbug.com/415294: ChannelHandle should either go away in
311 // favor of two independent entities (name/file), or it should be a move-
312 // only type with a base::File member. In any case, this code should not
313 // call DuplicateHandle.
[email protected]a7c03d4f32012-01-24 02:36:05314 DCHECK(channel_handle.name.empty());
315 pipe_name = L"Not Available"; // Just used for LOG
316 // Check that the given pipe confirms to the specified mode. We can
317 // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the
318 // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0.
319 DWORD flags = 0;
320 GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL);
321 DCHECK(!(flags & PIPE_TYPE_MESSAGE));
322 if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) ||
323 ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) {
324 LOG(WARNING) << "Inconsistent open mode. Mode :" << mode;
325 return false;
326 }
rvargas8c2d75c2014-09-26 19:50:26327 HANDLE local_handle;
[email protected]a7c03d4f32012-01-24 02:36:05328 if (!DuplicateHandle(GetCurrentProcess(),
329 channel_handle.pipe.handle,
330 GetCurrentProcess(),
rvargas8c2d75c2014-09-26 19:50:26331 &local_handle,
[email protected]a7c03d4f32012-01-24 02:36:05332 0,
333 FALSE,
334 DUPLICATE_SAME_ACCESS)) {
335 LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError();
336 return false;
337 }
rvargas8c2d75c2014-09-26 19:50:26338 pipe_.Set(local_handle);
[email protected]a7c03d4f32012-01-24 02:36:05339 } else if (mode & MODE_SERVER_FLAG) {
340 DCHECK(!channel_handle.pipe.handle);
341 const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
342 FILE_FLAG_FIRST_PIPE_INSTANCE;
[email protected]5c41e6e12012-03-17 02:20:46343 pipe_name = PipeName(channel_handle.name, &client_secret_);
344 validate_client_ = !!client_secret_;
rvargas8c2d75c2014-09-26 19:50:26345 pipe_.Set(CreateNamedPipeW(pipe_name.c_str(),
346 open_mode,
347 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
348 1,
349 Channel::kReadBufferSize,
350 Channel::kReadBufferSize,
351 5000,
352 NULL));
[email protected]1707726c2011-02-03 20:35:09353 } else if (mode & MODE_CLIENT_FLAG) {
[email protected]a7c03d4f32012-01-24 02:36:05354 DCHECK(!channel_handle.pipe.handle);
[email protected]5c41e6e12012-03-17 02:20:46355 pipe_name = PipeName(channel_handle.name, &client_secret_);
rvargas8c2d75c2014-09-26 19:50:26356 pipe_.Set(CreateFileW(pipe_name.c_str(),
357 GENERIC_READ | GENERIC_WRITE,
358 0,
359 NULL,
360 OPEN_EXISTING,
forshaw4a94dff2014-12-16 21:32:53361 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS |
rvargas8c2d75c2014-09-26 19:50:26362 FILE_FLAG_OVERLAPPED,
363 NULL));
[email protected]1707726c2011-02-03 20:35:09364 } else {
365 NOTREACHED();
initial.commit09911bf2008-07-26 23:55:29366 }
[email protected]a7c03d4f32012-01-24 02:36:05367
rvargas8c2d75c2014-09-26 19:50:26368 if (!pipe_.IsValid()) {
initial.commit09911bf2008-07-26 23:55:29369 // If this process is being closed, the pipe may be gone already.
[email protected]8398bf12014-06-26 21:11:34370 PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in "
371 << (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode";
initial.commit09911bf2008-07-26 23:55:29372 return false;
373 }
374
375 // Create the Hello message to be sent when Connect is called
376 scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE,
[email protected]753bb252013-11-04 22:28:12377 HELLO_MESSAGE_TYPE,
378 IPC::Message::PRIORITY_NORMAL));
[email protected]5c41e6e12012-03-17 02:20:46379
380 // Don't send the secret to the untrusted process, and don't send a secret
381 // if the value is zero (for IPC backwards compatability).
tfarina10a5c062015-09-04 18:47:57382 int32_t secret = validate_client_ ? 0 : client_secret_;
[email protected]5c41e6e12012-03-17 02:20:46383 if (!m->WriteInt(GetCurrentProcessId()) ||
384 (secret && !m->WriteUInt32(secret))) {
rvargas8c2d75c2014-09-26 19:50:26385 pipe_.Close();
initial.commit09911bf2008-07-26 23:55:29386 return false;
387 }
388
erikchen5142dc72015-09-10 21:00:18389 OutputElement* element = new OutputElement(m.release());
390 output_queue_.push(element);
initial.commit09911bf2008-07-26 23:55:29391 return true;
392}
393
[email protected]2f60c9b2014-06-06 20:13:51394bool ChannelWin::Connect() {
[email protected]19340722009-08-17 19:53:25395 DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once";
initial.commit09911bf2008-07-26 23:55:29396
[email protected]c1e4bff32009-01-29 00:07:06397 if (!thread_check_.get())
[email protected]327e52b2012-06-25 21:11:36398 thread_check_.reset(new base::ThreadChecker());
[email protected]c1e4bff32009-01-29 00:07:06399
rvargas8c2d75c2014-09-26 19:50:26400 if (!pipe_.IsValid())
initial.commit09911bf2008-07-26 23:55:29401 return false;
402
rvargas8c2d75c2014-09-26 19:50:26403 base::MessageLoopForIO::current()->RegisterIOHandler(pipe_.Get(), this);
[email protected]c1afbd2c2008-10-13 19:19:36404
initial.commit09911bf2008-07-26 23:55:29405 // Check to see if there is a client connected to our pipe...
406 if (waiting_connect_)
407 ProcessConnection();
408
409 if (!input_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36410 // Complete setup asynchronously. By not setting input_state_.is_pending
411 // to true, we indicate to OnIOCompleted that this is the special
412 // initialization signal.
[email protected]fd0a773a2013-04-30 20:55:03413 base::MessageLoopForIO::current()->PostTask(
414 FROM_HERE,
[email protected]2f60c9b2014-06-06 20:13:51415 base::Bind(&ChannelWin::OnIOCompleted,
[email protected]fd0a773a2013-04-30 20:55:03416 weak_factory_.GetWeakPtr(),
417 &input_state_.context,
418 0,
419 0));
initial.commit09911bf2008-07-26 23:55:29420 }
421
422 if (!waiting_connect_)
[email protected]c1afbd2c2008-10-13 19:19:36423 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29424 return true;
425}
426
[email protected]2f60c9b2014-06-06 20:13:51427bool ChannelWin::ProcessConnection() {
[email protected]c1e4bff32009-01-29 00:07:06428 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15429 if (input_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36430 input_state_.is_pending = false;
initial.commit09911bf2008-07-26 23:55:29431
432 // Do we have a client connected to our pipe?
rvargas8c2d75c2014-09-26 19:50:26433 if (!pipe_.IsValid())
[email protected]17b89142008-11-07 21:52:15434 return false;
435
rvargas8c2d75c2014-09-26 19:50:26436 BOOL ok = ConnectNamedPipe(pipe_.Get(), &input_state_.context.overlapped);
initial.commit09911bf2008-07-26 23:55:29437 DWORD err = GetLastError();
438 if (ok) {
439 // Uhm, the API documentation says that this function should never
440 // return success when used in overlapped mode.
441 NOTREACHED();
442 return false;
443 }
444
445 switch (err) {
446 case ERROR_IO_PENDING:
447 input_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29448 break;
449 case ERROR_PIPE_CONNECTED:
450 waiting_connect_ = false;
451 break;
[email protected]20aa32c2009-07-14 22:25:49452 case ERROR_NO_DATA:
453 // The pipe is being closed.
454 return false;
initial.commit09911bf2008-07-26 23:55:29455 default:
456 NOTREACHED();
457 return false;
458 }
459
460 return true;
461}
462
[email protected]2f60c9b2014-06-06 20:13:51463bool ChannelWin::ProcessOutgoingMessages(
[email protected]fd0a773a2013-04-30 20:55:03464 base::MessageLoopForIO::IOContext* context,
[email protected]514411fc2008-12-10 22:28:11465 DWORD bytes_written) {
initial.commit09911bf2008-07-26 23:55:29466 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's
467 // no connection?
[email protected]c1e4bff32009-01-29 00:07:06468 DCHECK(thread_check_->CalledOnValidThread());
initial.commit09911bf2008-07-26 23:55:29469
470 if (output_state_.is_pending) {
[email protected]c1afbd2c2008-10-13 19:19:36471 DCHECK(context);
initial.commit09911bf2008-07-26 23:55:29472 output_state_.is_pending = false;
[email protected]c1afbd2c2008-10-13 19:19:36473 if (!context || bytes_written == 0) {
initial.commit09911bf2008-07-26 23:55:29474 DWORD err = GetLastError();
475 LOG(ERROR) << "pipe error: " << err;
476 return false;
477 }
[email protected]c1afbd2c2008-10-13 19:19:36478 // Message was sent.
[email protected]88ecb5f52014-01-21 14:29:36479 CHECK(!output_queue_.empty());
erikchen5142dc72015-09-10 21:00:18480 OutputElement* element = output_queue_.front();
initial.commit09911bf2008-07-26 23:55:29481 output_queue_.pop();
erikchen5142dc72015-09-10 21:00:18482 delete element;
initial.commit09911bf2008-07-26 23:55:29483 }
484
[email protected]17b89142008-11-07 21:52:15485 if (output_queue_.empty())
486 return true;
487
rvargas8c2d75c2014-09-26 19:50:26488 if (!pipe_.IsValid())
[email protected]17b89142008-11-07 21:52:15489 return false;
490
491 // Write to pipe...
erikchen5142dc72015-09-10 21:00:18492 OutputElement* element = output_queue_.front();
493 DCHECK(element->size() <= INT_MAX);
erikchen06faf0c2015-08-27 19:49:58494 BOOL ok = WriteFile(pipe_.Get(),
erikchen5142dc72015-09-10 21:00:18495 element->data(),
496 static_cast<uint32_t>(element->size()),
erikchen06faf0c2015-08-27 19:49:58497 NULL,
[email protected]17b89142008-11-07 21:52:15498 &output_state_.context.overlapped);
499 if (!ok) {
rvargascd742bcb2014-11-14 19:27:31500 DWORD write_error = GetLastError();
501 if (write_error == ERROR_IO_PENDING) {
[email protected]17b89142008-11-07 21:52:15502 output_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29503
erikchen5142dc72015-09-10 21:00:18504 const Message* m = element->get_message();
505 if (m) {
506 DVLOG(2) << "sent pending message @" << m << " on channel @" << this
507 << " with type " << m->type();
508 }
initial.commit09911bf2008-07-26 23:55:29509
[email protected]17b89142008-11-07 21:52:15510 return true;
initial.commit09911bf2008-07-26 23:55:29511 }
rvargascd742bcb2014-11-14 19:27:31512 LOG(ERROR) << "pipe error: " << write_error;
[email protected]17b89142008-11-07 21:52:15513 return false;
initial.commit09911bf2008-07-26 23:55:29514 }
515
erikchen5142dc72015-09-10 21:00:18516 const Message* m = element->get_message();
517 if (m) {
518 DVLOG(2) << "sent message @" << m << " on channel @" << this
519 << " with type " << m->type();
520 }
[email protected]17b89142008-11-07 21:52:15521
522 output_state_.is_pending = true;
initial.commit09911bf2008-07-26 23:55:29523 return true;
524}
525
[email protected]2f60c9b2014-06-06 20:13:51526void ChannelWin::OnIOCompleted(
[email protected]fd0a773a2013-04-30 20:55:03527 base::MessageLoopForIO::IOContext* context,
528 DWORD bytes_transfered,
529 DWORD error) {
[email protected]215f6fd2012-03-03 08:55:45530 bool ok = true;
[email protected]c1e4bff32009-01-29 00:07:06531 DCHECK(thread_check_->CalledOnValidThread());
[email protected]17b89142008-11-07 21:52:15532 if (context == &input_state_.context) {
initial.commit09911bf2008-07-26 23:55:29533 if (waiting_connect_) {
[email protected]17b89142008-11-07 21:52:15534 if (!ProcessConnection())
535 return;
initial.commit09911bf2008-07-26 23:55:29536 // We may have some messages queued up to send...
537 if (!output_queue_.empty() && !output_state_.is_pending)
[email protected]c1afbd2c2008-10-13 19:19:36538 ProcessOutgoingMessages(NULL, 0);
initial.commit09911bf2008-07-26 23:55:29539 if (input_state_.is_pending)
540 return;
541 // else, fall-through and look for incoming messages...
542 }
[email protected]215f6fd2012-03-03 08:55:45543
544 // We don't support recursion through OnMessageReceived yet!
initial.commit09911bf2008-07-26 23:55:29545 DCHECK(!processing_incoming_);
[email protected]997ec9f2012-11-21 04:44:14546 base::AutoReset<bool> auto_reset_processing_incoming(
547 &processing_incoming_, true);
[email protected]215f6fd2012-03-03 08:55:45548
549 // Process the new data.
550 if (input_state_.is_pending) {
551 // This is the normal case for everything except the initialization step.
552 input_state_.is_pending = false;
erikchende9412b82015-07-27 18:26:14553 if (!bytes_transfered) {
[email protected]215f6fd2012-03-03 08:55:45554 ok = false;
erikchende9412b82015-07-27 18:26:14555 } else if (pipe_.IsValid()) {
556 ok = (AsyncReadComplete(bytes_transfered) != DISPATCH_ERROR);
557 }
[email protected]215f6fd2012-03-03 08:55:45558 } else {
559 DCHECK(!bytes_transfered);
560 }
561
562 // Request more data.
563 if (ok)
erikchende9412b82015-07-27 18:26:14564 ok = (ProcessIncomingMessages() != DISPATCH_ERROR);
initial.commit09911bf2008-07-26 23:55:29565 } else {
[email protected]17b89142008-11-07 21:52:15566 DCHECK(context == &output_state_.context);
[email protected]65727eb2014-07-31 12:24:29567 CHECK(output_state_.is_pending);
[email protected]c1afbd2c2008-10-13 19:19:36568 ok = ProcessOutgoingMessages(context, bytes_transfered);
initial.commit09911bf2008-07-26 23:55:29569 }
rvargas8c2d75c2014-09-26 19:50:26570 if (!ok && pipe_.IsValid()) {
[email protected]17b89142008-11-07 21:52:15571 // We don't want to re-enter Close().
initial.commit09911bf2008-07-26 23:55:29572 Close();
[email protected]d805c6a2012-03-08 12:30:28573 listener()->OnChannelError();
initial.commit09911bf2008-07-26 23:55:29574 }
575}
576
[email protected]514411fc2008-12-10 22:28:11577//------------------------------------------------------------------------------
[email protected]2f60c9b2014-06-06 20:13:51578// Channel's methods
[email protected]514411fc2008-12-10 22:28:11579
[email protected]2f60c9b2014-06-06 20:13:51580// static
erikchen27aa7d82015-06-16 21:21:04581scoped_ptr<Channel> Channel::Create(const IPC::ChannelHandle& channel_handle,
582 Mode mode,
erikchen30dc2812015-09-24 03:26:38583 Listener* listener) {
erikchen5ea2ab72015-09-25 22:34:31584 return scoped_ptr<Channel>(new ChannelWin(channel_handle, mode, listener));
[email protected]514411fc2008-12-10 22:28:11585}
586
[email protected]313c00e52011-08-09 06:46:06587// static
588bool Channel::IsNamedServerInitialized(const std::string& channel_id) {
[email protected]2f60c9b2014-06-06 20:13:51589 return ChannelWin::IsNamedServerInitialized(channel_id);
[email protected]313c00e52011-08-09 06:46:06590}
591
[email protected]5c41e6e12012-03-17 02:20:46592// static
593std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) {
594 // Windows pipes can be enumerated by low-privileged processes. So, we
595 // append a strong random value after the \ character. This value is not
596 // included in the pipe name, but sent as part of the client hello, to
597 // hijacking the pipe name to spoof the client.
598
599 std::string id = prefix;
600 if (!id.empty())
601 id.append(".");
602
603 int secret;
604 do { // Guarantee we get a non-zero value.
605 secret = base::RandInt(0, std::numeric_limits<int>::max());
606 } while (secret == 0);
607
608 id.append(GenerateUniqueRandomChannelID());
609 return id.append(base::StringPrintf("\\%d", secret));
610}
611
[email protected]514411fc2008-12-10 22:28:11612} // namespace IPC