[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] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 13 | #include "base/pickle.h" |
[email protected] | e66ef60 | 2013-07-24 05:15:24 | [diff] [blame] | 14 | #include "base/process/process_handle.h" |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 15 | #include "base/rand_util.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 16 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 17 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 19 | #include "base/win/scoped_handle.h" |
[email protected] | 0ee99068 | 2012-11-17 19:20:05 | [diff] [blame] | 20 | #include "ipc/ipc_listener.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 21 | #include "ipc/ipc_logging.h" |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 22 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 23 | #include "ipc/ipc_message_utils.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | namespace IPC { |
[email protected] | 935aa54 | 2010-10-15 01:59:15 | [diff] [blame] | 26 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 27 | ChannelWin::State::State(ChannelWin* channel) : is_pending(false) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 28 | memset(&context.overlapped, 0, sizeof(context.overlapped)); |
| 29 | context.handler = channel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | } |
| 31 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 32 | ChannelWin::State::~State() { |
anujk.sharma | 5a7ffe2f | 2015-01-22 05:39:37 | [diff] [blame] | 33 | static_assert(offsetof(ChannelWin::State, context) == 0, |
| 34 | "ChannelWin::State should have context as its first data" |
| 35 | "member."); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | } |
| 37 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 38 | ChannelWin::ChannelWin(const IPC::ChannelHandle& channel_handle, |
| 39 | Mode mode, |
| 40 | Listener* listener, |
| 41 | AttachmentBroker* broker) |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 42 | : ChannelReader(listener), |
[email protected] | ba2194d | 2013-04-26 19:51:30 | [diff] [blame] | 43 | input_state_(this), |
| 44 | output_state_(this), |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 45 | peer_pid_(base::kNullProcessId), |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 46 | waiting_connect_(mode & MODE_SERVER_FLAG), |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 47 | processing_incoming_(false), |
[email protected] | 2d0faf1 | 2014-06-27 04:50:43 | [diff] [blame] | 48 | validate_client_(false), |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 49 | client_secret_(0), |
erikchen | f195382 | 2015-06-16 23:16:04 | [diff] [blame] | 50 | broker_(broker), |
| 51 | weak_factory_(this) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 52 | CreatePipe(channel_handle, mode); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | } |
| 54 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 55 | ChannelWin::~ChannelWin() { |
[email protected] | 601858c0 | 2010-09-01 17:08:20 | [diff] [blame] | 56 | Close(); |
| 57 | } |
| 58 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 59 | void ChannelWin::Close() { |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 60 | if (thread_check_.get()) |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 61 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 62 | |
[email protected] | 74f87acf2 | 2009-10-14 22:10:40 | [diff] [blame] | 63 | if (input_state_.is_pending || output_state_.is_pending) |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 64 | CancelIo(pipe_.Get()); |
[email protected] | ee78622d | 2008-10-13 21:25:50 | [diff] [blame] | 65 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 66 | // Closing the handle at this point prevents us from issuing more requests |
| 67 | // form OnIOCompleted(). |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 68 | if (pipe_.IsValid()) |
| 69 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 71 | // Make sure all IO has completed. |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 72 | while (input_state_.is_pending || output_state_.is_pending) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 73 | base::MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 74 | } |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 75 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | while (!output_queue_.empty()) { |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 77 | Message* m = output_queue_.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | output_queue_.pop(); |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 79 | delete m; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 83 | bool ChannelWin::Send(Message* message) { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 84 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 2a9d601b | 2010-10-19 23:50:00 | [diff] [blame] | 85 | DVLOG(2) << "sending message @" << message << " on channel @" << this |
| 86 | << " with type " << message->type() |
| 87 | << " (" << output_queue_.size() << " in queue)"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 89 | if (!prelim_queue_.empty()) { |
| 90 | prelim_queue_.push(message); |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | if (message->HasBrokerableAttachments() && |
| 95 | peer_pid_ == base::kNullProcessId) { |
| 96 | prelim_queue_.push(message); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | return ProcessMessageForDelivery(message); |
| 101 | } |
| 102 | |
| 103 | bool ChannelWin::ProcessMessageForDelivery(Message* message) { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 104 | // Sending a brokerable attachment requires a call to Channel::Send(), so |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 105 | // both Send() and ProcessMessageForDelivery() may be re-entrant. Brokered |
| 106 | // attachments must be sent before the Message itself. |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 107 | if (message->HasBrokerableAttachments()) { |
| 108 | DCHECK(broker_); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 109 | DCHECK(peer_pid_ != base::kNullProcessId); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 110 | for (const BrokerableAttachment* attachment : |
| 111 | message->attachment_set()->PeekBrokerableAttachments()) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 112 | if (!broker_->SendAttachmentToProcess(attachment, peer_pid_)) { |
| 113 | delete message; |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 114 | return false; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 115 | } |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | #ifdef IPC_MESSAGE_LOG_ENABLED |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 120 | Logging::GetInstance()->OnSendMessage(message, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | #endif |
| 122 | |
yuhaoz | 9b8157d | 2015-08-18 22:21:35 | [diff] [blame] | 123 | TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), |
| 124 | "ChannelWin::ProcessMessageForDelivery", |
| 125 | message->flags(), |
| 126 | TRACE_EVENT_FLAG_FLOW_OUT); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 127 | |
| 128 | // |output_queue_| takes ownership of |message|. |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 129 | output_queue_.push(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | // ensure waiting to write |
| 131 | if (!waiting_connect_) { |
| 132 | if (!output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 133 | if (!ProcessOutgoingMessages(NULL, 0)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | return true; |
| 139 | } |
| 140 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 141 | void ChannelWin::FlushPrelimQueue() { |
| 142 | DCHECK_NE(peer_pid_, base::kNullProcessId); |
| 143 | |
| 144 | // Due to the possibly re-entrant nature of ProcessMessageForDelivery(), it |
| 145 | // is critical that |prelim_queue_| appears empty. |
| 146 | std::queue<Message*> prelim_queue; |
| 147 | prelim_queue_.swap(prelim_queue); |
| 148 | |
| 149 | while (!prelim_queue.empty()) { |
| 150 | Message* m = prelim_queue.front(); |
| 151 | ProcessMessageForDelivery(m); |
| 152 | prelim_queue.pop(); |
| 153 | } |
| 154 | } |
| 155 | |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 156 | AttachmentBroker* ChannelWin::GetAttachmentBroker() { |
| 157 | return broker_; |
| 158 | } |
| 159 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 160 | base::ProcessId ChannelWin::GetPeerPID() const { |
| 161 | return peer_pid_; |
| 162 | } |
| 163 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 164 | base::ProcessId ChannelWin::GetSelfPID() const { |
| 165 | return GetCurrentProcessId(); |
| 166 | } |
| 167 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 168 | // static |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 169 | bool ChannelWin::IsNamedServerInitialized( |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 170 | const std::string& channel_id) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 171 | if (WaitNamedPipe(PipeName(channel_id, NULL).c_str(), 1)) |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 172 | return true; |
| 173 | // If ERROR_SEM_TIMEOUT occurred, the pipe exists but is handling another |
| 174 | // connection. |
| 175 | return GetLastError() == ERROR_SEM_TIMEOUT; |
| 176 | } |
| 177 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 178 | ChannelWin::ReadState ChannelWin::ReadData( |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 179 | char* buffer, |
| 180 | int buffer_len, |
| 181 | int* /* bytes_read */) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 182 | if (!pipe_.IsValid()) |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 183 | return READ_FAILED; |
| 184 | |
| 185 | DWORD bytes_read = 0; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 186 | BOOL ok = ReadFile(pipe_.Get(), buffer, buffer_len, |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 187 | &bytes_read, &input_state_.context.overlapped); |
| 188 | if (!ok) { |
| 189 | DWORD err = GetLastError(); |
| 190 | if (err == ERROR_IO_PENDING) { |
| 191 | input_state_.is_pending = true; |
| 192 | return READ_PENDING; |
| 193 | } |
| 194 | LOG(ERROR) << "pipe error: " << err; |
| 195 | return READ_FAILED; |
| 196 | } |
| 197 | |
| 198 | // We could return READ_SUCCEEDED here. But the way that this code is |
| 199 | // structured we instead go back to the message loop. Our completion port |
| 200 | // will be signalled even in the "synchronously completed" state. |
| 201 | // |
| 202 | // This allows us to potentially process some outgoing messages and |
| 203 | // interleave other work on this thread when we're getting hammered with |
| 204 | // input messages. Potentially, this could be tuned to be more efficient |
| 205 | // with some testing. |
| 206 | input_state_.is_pending = true; |
| 207 | return READ_PENDING; |
| 208 | } |
| 209 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 210 | bool ChannelWin::ShouldDispatchInputMessage(Message* msg) { |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 211 | // Make sure we get a hello when client validation is required. |
| 212 | if (validate_client_) |
| 213 | return IsHelloMessage(*msg); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 214 | return true; |
| 215 | } |
| 216 | |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 217 | bool ChannelWin::GetNonBrokeredAttachments(Message* msg) { |
| 218 | return true; |
| 219 | } |
| 220 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 221 | void ChannelWin::HandleInternalMessage(const Message& msg) { |
[email protected] | dc875dc | 2013-10-15 00:07:00 | [diff] [blame] | 222 | DCHECK_EQ(msg.type(), static_cast<unsigned>(Channel::HELLO_MESSAGE_TYPE)); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 223 | // The hello message contains one parameter containing the PID. |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 224 | base::PickleIterator it(msg); |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 225 | int32 claimed_pid; |
| 226 | bool failed = !it.ReadInt(&claimed_pid); |
| 227 | |
| 228 | if (!failed && validate_client_) { |
| 229 | int32 secret; |
| 230 | failed = it.ReadInt(&secret) ? (secret != client_secret_) : true; |
| 231 | } |
| 232 | |
| 233 | if (failed) { |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 234 | NOTREACHED(); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 235 | Close(); |
| 236 | listener()->OnChannelError(); |
| 237 | return; |
| 238 | } |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 239 | |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 240 | peer_pid_ = claimed_pid; |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 241 | // Validation completed. |
[email protected] | 0590878 | 2012-04-03 08:49:43 | [diff] [blame] | 242 | validate_client_ = false; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 243 | |
| 244 | FlushPrelimQueue(); |
| 245 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 246 | listener()->OnChannelConnected(claimed_pid); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 247 | } |
| 248 | |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 249 | base::ProcessId ChannelWin::GetSenderPID() { |
| 250 | return GetPeerPID(); |
| 251 | } |
| 252 | |
erikchen | 8c73f83 | 2015-07-30 22:26:08 | [diff] [blame] | 253 | bool ChannelWin::IsAttachmentBrokerEndpoint() { |
| 254 | return is_attachment_broker_endpoint(); |
| 255 | } |
| 256 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 257 | bool ChannelWin::DidEmptyInputBuffers() { |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 258 | // We don't need to do anything here. |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 259 | return true; |
| 260 | } |
| 261 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 262 | // static |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 263 | const base::string16 ChannelWin::PipeName( |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 264 | const std::string& channel_id, int32* secret) { |
[email protected] | c2391b8 | 2011-05-06 17:39:07 | [diff] [blame] | 265 | std::string name("\\\\.\\pipe\\chrome."); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 266 | |
| 267 | // Prevent the shared secret from ending up in the pipe name. |
| 268 | size_t index = channel_id.find_first_of('\\'); |
| 269 | if (index != std::string::npos) { |
| 270 | if (secret) // Retrieve the secret if asked for. |
| 271 | base::StringToInt(channel_id.substr(index + 1), secret); |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 272 | return base::ASCIIToUTF16(name.append(channel_id.substr(0, index - 1))); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // This case is here to support predictable named pipes in tests. |
| 276 | if (secret) |
| 277 | *secret = 0; |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 278 | return base::ASCIIToUTF16(name.append(channel_id)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 281 | bool ChannelWin::CreatePipe(const IPC::ChannelHandle &channel_handle, |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 282 | Mode mode) { |
| 283 | DCHECK(!pipe_.IsValid()); |
[email protected] | 905cda81 | 2013-12-20 09:04:55 | [diff] [blame] | 284 | base::string16 pipe_name; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 285 | // If we already have a valid pipe for channel just copy it. |
| 286 | if (channel_handle.pipe.handle) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 287 | // TODO(rvargas) crbug.com/415294: ChannelHandle should either go away in |
| 288 | // favor of two independent entities (name/file), or it should be a move- |
| 289 | // only type with a base::File member. In any case, this code should not |
| 290 | // call DuplicateHandle. |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 291 | DCHECK(channel_handle.name.empty()); |
| 292 | pipe_name = L"Not Available"; // Just used for LOG |
| 293 | // Check that the given pipe confirms to the specified mode. We can |
| 294 | // only check for PIPE_TYPE_MESSAGE & PIPE_SERVER_END flags since the |
| 295 | // other flags (PIPE_TYPE_BYTE, and PIPE_CLIENT_END) are defined as 0. |
| 296 | DWORD flags = 0; |
| 297 | GetNamedPipeInfo(channel_handle.pipe.handle, &flags, NULL, NULL, NULL); |
| 298 | DCHECK(!(flags & PIPE_TYPE_MESSAGE)); |
| 299 | if (((mode & MODE_SERVER_FLAG) && !(flags & PIPE_SERVER_END)) || |
| 300 | ((mode & MODE_CLIENT_FLAG) && (flags & PIPE_SERVER_END))) { |
| 301 | LOG(WARNING) << "Inconsistent open mode. Mode :" << mode; |
| 302 | return false; |
| 303 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 304 | HANDLE local_handle; |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 305 | if (!DuplicateHandle(GetCurrentProcess(), |
| 306 | channel_handle.pipe.handle, |
| 307 | GetCurrentProcess(), |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 308 | &local_handle, |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 309 | 0, |
| 310 | FALSE, |
| 311 | DUPLICATE_SAME_ACCESS)) { |
| 312 | LOG(WARNING) << "DuplicateHandle failed. Error :" << GetLastError(); |
| 313 | return false; |
| 314 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 315 | pipe_.Set(local_handle); |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 316 | } else if (mode & MODE_SERVER_FLAG) { |
| 317 | DCHECK(!channel_handle.pipe.handle); |
| 318 | const DWORD open_mode = PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 319 | FILE_FLAG_FIRST_PIPE_INSTANCE; |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 320 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
| 321 | validate_client_ = !!client_secret_; |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 322 | pipe_.Set(CreateNamedPipeW(pipe_name.c_str(), |
| 323 | open_mode, |
| 324 | PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 325 | 1, |
| 326 | Channel::kReadBufferSize, |
| 327 | Channel::kReadBufferSize, |
| 328 | 5000, |
| 329 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 330 | } else if (mode & MODE_CLIENT_FLAG) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 331 | DCHECK(!channel_handle.pipe.handle); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 332 | pipe_name = PipeName(channel_handle.name, &client_secret_); |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 333 | pipe_.Set(CreateFileW(pipe_name.c_str(), |
| 334 | GENERIC_READ | GENERIC_WRITE, |
| 335 | 0, |
| 336 | NULL, |
| 337 | OPEN_EXISTING, |
forshaw | 4a94dff | 2014-12-16 21:32:53 | [diff] [blame] | 338 | SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 339 | FILE_FLAG_OVERLAPPED, |
| 340 | NULL)); |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 341 | } else { |
| 342 | NOTREACHED(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | } |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 344 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 345 | if (!pipe_.IsValid()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | // If this process is being closed, the pipe may be gone already. |
[email protected] | 8398bf1 | 2014-06-26 21:11:34 | [diff] [blame] | 347 | PLOG(WARNING) << "Unable to create pipe \"" << pipe_name << "\" in " |
| 348 | << (mode & MODE_SERVER_FLAG ? "server" : "client") << " mode"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | |
| 352 | // Create the Hello message to be sent when Connect is called |
| 353 | scoped_ptr<Message> m(new Message(MSG_ROUTING_NONE, |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 354 | HELLO_MESSAGE_TYPE, |
| 355 | IPC::Message::PRIORITY_NORMAL)); |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 356 | |
| 357 | // Don't send the secret to the untrusted process, and don't send a secret |
| 358 | // if the value is zero (for IPC backwards compatability). |
| 359 | int32 secret = validate_client_ ? 0 : client_secret_; |
| 360 | if (!m->WriteInt(GetCurrentProcessId()) || |
| 361 | (secret && !m->WriteUInt32(secret))) { |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 362 | pipe_.Close(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 366 | output_queue_.push(m.release()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 367 | return true; |
| 368 | } |
| 369 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 370 | bool ChannelWin::Connect() { |
[email protected] | 1934072 | 2009-08-17 19:53:25 | [diff] [blame] | 371 | DLOG_IF(WARNING, thread_check_.get()) << "Connect called more than once"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 373 | if (!thread_check_.get()) |
[email protected] | 327e52b | 2012-06-25 21:11:36 | [diff] [blame] | 374 | thread_check_.reset(new base::ThreadChecker()); |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 375 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 376 | if (!pipe_.IsValid()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | return false; |
| 378 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 379 | base::MessageLoopForIO::current()->RegisterIOHandler(pipe_.Get(), this); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 380 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | // Check to see if there is a client connected to our pipe... |
| 382 | if (waiting_connect_) |
| 383 | ProcessConnection(); |
| 384 | |
| 385 | if (!input_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 386 | // Complete setup asynchronously. By not setting input_state_.is_pending |
| 387 | // to true, we indicate to OnIOCompleted that this is the special |
| 388 | // initialization signal. |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 389 | base::MessageLoopForIO::current()->PostTask( |
| 390 | FROM_HERE, |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 391 | base::Bind(&ChannelWin::OnIOCompleted, |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 392 | weak_factory_.GetWeakPtr(), |
| 393 | &input_state_.context, |
| 394 | 0, |
| 395 | 0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | if (!waiting_connect_) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 399 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 400 | return true; |
| 401 | } |
| 402 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 403 | bool ChannelWin::ProcessConnection() { |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 404 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 405 | if (input_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 406 | input_state_.is_pending = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | |
| 408 | // Do we have a client connected to our pipe? |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 409 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 410 | return false; |
| 411 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 412 | BOOL ok = ConnectNamedPipe(pipe_.Get(), &input_state_.context.overlapped); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 413 | DWORD err = GetLastError(); |
| 414 | if (ok) { |
| 415 | // Uhm, the API documentation says that this function should never |
| 416 | // return success when used in overlapped mode. |
| 417 | NOTREACHED(); |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | switch (err) { |
| 422 | case ERROR_IO_PENDING: |
| 423 | input_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | break; |
| 425 | case ERROR_PIPE_CONNECTED: |
| 426 | waiting_connect_ = false; |
| 427 | break; |
[email protected] | 20aa32c | 2009-07-14 22:25:49 | [diff] [blame] | 428 | case ERROR_NO_DATA: |
| 429 | // The pipe is being closed. |
| 430 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 431 | default: |
| 432 | NOTREACHED(); |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | return true; |
| 437 | } |
| 438 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 439 | bool ChannelWin::ProcessOutgoingMessages( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 440 | base::MessageLoopForIO::IOContext* context, |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 441 | DWORD bytes_written) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
| 443 | // no connection? |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 444 | DCHECK(thread_check_->CalledOnValidThread()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | |
| 446 | if (output_state_.is_pending) { |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 447 | DCHECK(context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 448 | output_state_.is_pending = false; |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 449 | if (!context || bytes_written == 0) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | DWORD err = GetLastError(); |
| 451 | LOG(ERROR) << "pipe error: " << err; |
| 452 | return false; |
| 453 | } |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 454 | // Message was sent. |
[email protected] | 88ecb5f5 | 2014-01-21 14:29:36 | [diff] [blame] | 455 | CHECK(!output_queue_.empty()); |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 456 | Message* m = output_queue_.front(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | output_queue_.pop(); |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 458 | delete m; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 461 | if (output_queue_.empty()) |
| 462 | return true; |
| 463 | |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 464 | if (!pipe_.IsValid()) |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 465 | return false; |
| 466 | |
| 467 | // Write to pipe... |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 468 | Message* m = output_queue_.front(); |
| 469 | DCHECK(m->size() <= INT_MAX); |
| 470 | BOOL ok = WriteFile(pipe_.Get(), |
| 471 | m->data(), |
| 472 | static_cast<uint32>(m->size()), |
| 473 | NULL, |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 474 | &output_state_.context.overlapped); |
| 475 | if (!ok) { |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 476 | DWORD write_error = GetLastError(); |
| 477 | if (write_error == ERROR_IO_PENDING) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 478 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 480 | DVLOG(2) << "sent pending message @" << m << " on channel @" << this |
| 481 | << " with type " << m->type(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 482 | |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 483 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | } |
rvargas | cd742bcb | 2014-11-14 19:27:31 | [diff] [blame] | 485 | LOG(ERROR) << "pipe error: " << write_error; |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 486 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | } |
| 488 | |
erikchen | 06faf0c | 2015-08-27 19:49:58 | [diff] [blame^] | 489 | DVLOG(2) << "sent message @" << m << " on channel @" << this |
| 490 | << " with type " << m->type(); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 491 | |
| 492 | output_state_.is_pending = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 496 | void ChannelWin::OnIOCompleted( |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 497 | base::MessageLoopForIO::IOContext* context, |
| 498 | DWORD bytes_transfered, |
| 499 | DWORD error) { |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 500 | bool ok = true; |
[email protected] | c1e4bff3 | 2009-01-29 00:07:06 | [diff] [blame] | 501 | DCHECK(thread_check_->CalledOnValidThread()); |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 502 | if (context == &input_state_.context) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 503 | if (waiting_connect_) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 504 | if (!ProcessConnection()) |
| 505 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | // We may have some messages queued up to send... |
| 507 | if (!output_queue_.empty() && !output_state_.is_pending) |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 508 | ProcessOutgoingMessages(NULL, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | if (input_state_.is_pending) |
| 510 | return; |
| 511 | // else, fall-through and look for incoming messages... |
| 512 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 513 | |
| 514 | // We don't support recursion through OnMessageReceived yet! |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | DCHECK(!processing_incoming_); |
[email protected] | 997ec9f | 2012-11-21 04:44:14 | [diff] [blame] | 516 | base::AutoReset<bool> auto_reset_processing_incoming( |
| 517 | &processing_incoming_, true); |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 518 | |
| 519 | // Process the new data. |
| 520 | if (input_state_.is_pending) { |
| 521 | // This is the normal case for everything except the initialization step. |
| 522 | input_state_.is_pending = false; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 523 | if (!bytes_transfered) { |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 524 | ok = false; |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 525 | } else if (pipe_.IsValid()) { |
| 526 | ok = (AsyncReadComplete(bytes_transfered) != DISPATCH_ERROR); |
| 527 | } |
[email protected] | 215f6fd | 2012-03-03 08:55:45 | [diff] [blame] | 528 | } else { |
| 529 | DCHECK(!bytes_transfered); |
| 530 | } |
| 531 | |
| 532 | // Request more data. |
| 533 | if (ok) |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 534 | ok = (ProcessIncomingMessages() != DISPATCH_ERROR); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | } else { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 536 | DCHECK(context == &output_state_.context); |
[email protected] | 65727eb | 2014-07-31 12:24:29 | [diff] [blame] | 537 | CHECK(output_state_.is_pending); |
[email protected] | c1afbd2c | 2008-10-13 19:19:36 | [diff] [blame] | 538 | ok = ProcessOutgoingMessages(context, bytes_transfered); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 539 | } |
rvargas | 8c2d75c | 2014-09-26 19:50:26 | [diff] [blame] | 540 | if (!ok && pipe_.IsValid()) { |
[email protected] | 17b8914 | 2008-11-07 21:52:15 | [diff] [blame] | 541 | // We don't want to re-enter Close(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 542 | Close(); |
[email protected] | d805c6a | 2012-03-08 12:30:28 | [diff] [blame] | 543 | listener()->OnChannelError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 547 | //------------------------------------------------------------------------------ |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 548 | // Channel's methods |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 549 | |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 550 | // static |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 551 | scoped_ptr<Channel> Channel::Create(const IPC::ChannelHandle& channel_handle, |
| 552 | Mode mode, |
| 553 | Listener* listener, |
| 554 | AttachmentBroker* broker) { |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 555 | return scoped_ptr<Channel>( |
erikchen | 27aa7d8 | 2015-06-16 21:21:04 | [diff] [blame] | 556 | new ChannelWin(channel_handle, mode, listener, broker)); |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 557 | } |
| 558 | |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 559 | // static |
| 560 | bool Channel::IsNamedServerInitialized(const std::string& channel_id) { |
[email protected] | 2f60c9b | 2014-06-06 20:13:51 | [diff] [blame] | 561 | return ChannelWin::IsNamedServerInitialized(channel_id); |
[email protected] | 313c00e5 | 2011-08-09 06:46:06 | [diff] [blame] | 562 | } |
| 563 | |
[email protected] | 5c41e6e1 | 2012-03-17 02:20:46 | [diff] [blame] | 564 | // static |
| 565 | std::string Channel::GenerateVerifiedChannelID(const std::string& prefix) { |
| 566 | // Windows pipes can be enumerated by low-privileged processes. So, we |
| 567 | // append a strong random value after the \ character. This value is not |
| 568 | // included in the pipe name, but sent as part of the client hello, to |
| 569 | // hijacking the pipe name to spoof the client. |
| 570 | |
| 571 | std::string id = prefix; |
| 572 | if (!id.empty()) |
| 573 | id.append("."); |
| 574 | |
| 575 | int secret; |
| 576 | do { // Guarantee we get a non-zero value. |
| 577 | secret = base::RandInt(0, std::numeric_limits<int>::max()); |
| 578 | } while (secret == 0); |
| 579 | |
| 580 | id.append(GenerateUniqueRandomChannelID()); |
| 581 | return id.append(base::StringPrintf("\\%d", secret)); |
| 582 | } |
| 583 | |
[email protected] | 514411fc | 2008-12-10 22:28:11 | [diff] [blame] | 584 | } // namespace IPC |