1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/protocol/quic_channel_factory.h"
#include <vector>
#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/quic/crypto/crypto_framer.h"
#include "net/quic/crypto/crypto_handshake_message.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/crypto/quic_random.h"
#include "net/quic/p2p/quic_p2p_crypto_config.h"
#include "net/quic/p2p/quic_p2p_session.h"
#include "net/quic/p2p/quic_p2p_stream.h"
#include "net/quic/quic_clock.h"
#include "net/quic/quic_connection_helper.h"
#include "net/quic/quic_default_packet_writer.h"
#include "net/quic/quic_protocol.h"
#include "net/socket/stream_socket.h"
#include "remoting/base/constants.h"
#include "remoting/protocol/datagram_channel_factory.h"
#include "remoting/protocol/p2p_datagram_socket.h"
#include "remoting/protocol/quic_channel.h"
namespace remoting {
namespace protocol {
namespace {
// The maximum receive window sizes for QUIC sessions and streams. These are
// the same values that are used in chrome.
const int kQuicSessionMaxRecvWindowSize = 15 * 1024 * 1024; // 15 MB
const int kQuicStreamMaxRecvWindowSize = 6 * 1024 * 1024; // 6 MB
class P2PQuicPacketWriter : public net::QuicPacketWriter {
public:
P2PQuicPacketWriter(net::QuicConnection* connection,
P2PDatagramSocket* socket)
: connection_(connection), socket_(socket), weak_factory_(this) {}
~P2PQuicPacketWriter() override {}
// QuicPacketWriter interface.
net::WriteResult WritePacket(const char* buffer,
size_t buf_len,
const net::IPAddressNumber& self_address,
const net::IPEndPoint& peer_address) override {
DCHECK(!write_blocked_);
scoped_refptr<net::StringIOBuffer> buf(
new net::StringIOBuffer(std::string(buffer, buf_len)));
int result = socket_->Send(buf, buf_len,
base::Bind(&P2PQuicPacketWriter::OnSendComplete,
weak_factory_.GetWeakPtr()));
net::WriteStatus status = net::WRITE_STATUS_OK;
if (result < 0) {
if (result == net::ERR_IO_PENDING) {
status = net::WRITE_STATUS_BLOCKED;
write_blocked_ = true;
} else {
status = net::WRITE_STATUS_ERROR;
}
}
return net::WriteResult(status, result);
}
bool IsWriteBlockedDataBuffered() const override {
// P2PDatagramSocket::Send() method buffer the data until the Send is
// unblocked.
return true;
}
bool IsWriteBlocked() const override { return write_blocked_; }
void SetWritable() override { write_blocked_ = false; }
net::QuicByteCount GetMaxPacketSize(const net::IPEndPoint& peer_address) const
override {
return net::kMaxPacketSize;
}
private:
void OnSendComplete(int result){
DCHECK_NE(result, net::ERR_IO_PENDING);
write_blocked_ = false;
if (result < 0) {
connection_->OnWriteError(result);
}
connection_->OnCanWrite();
}
net::QuicConnection* connection_;
P2PDatagramSocket* socket_;
// Whether a write is currently in flight.
bool write_blocked_ = false;
base::WeakPtrFactory<P2PQuicPacketWriter> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(P2PQuicPacketWriter);
};
class QuicPacketWriterFactory
: public net::QuicConnection::PacketWriterFactory {
public:
explicit QuicPacketWriterFactory(P2PDatagramSocket* socket)
: socket_(socket) {}
~QuicPacketWriterFactory() override {}
net::QuicPacketWriter* Create(
net::QuicConnection* connection) const override {
return new P2PQuicPacketWriter(connection, socket_);
}
private:
P2PDatagramSocket* socket_;
};
class P2PDatagramSocketAdapter : public net::Socket {
public:
explicit P2PDatagramSocketAdapter(scoped_ptr<P2PDatagramSocket> socket)
: socket_(socket.Pass()) {}
~P2PDatagramSocketAdapter() override {}
int Read(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) override {
return socket_->Recv(buf, buf_len, callback);
}
int Write(net::IOBuffer* buf, int buf_len,
const net::CompletionCallback& callback) override {
return socket_->Send(buf, buf_len, callback);
}
int SetReceiveBufferSize(int32_t size) override {
NOTREACHED();
return net::ERR_FAILED;
}
int SetSendBufferSize(int32_t size) override {
NOTREACHED();
return net::ERR_FAILED;
}
private:
scoped_ptr<P2PDatagramSocket> socket_;
};
} // namespace
class QuicChannelFactory::Core : public net::QuicP2PSession::Delegate {
public:
Core(const std::string& session_id, bool is_server);
virtual ~Core();
// Called from ~QuicChannelFactory() to synchronously release underlying
// socket. Core is destroyed later asynchronously.
void Close();
// Implementation of all all methods for QuicChannelFactory.
const std::string& CreateSessionInitiateConfigMessage();
bool ProcessSessionAcceptConfigMessage(const std::string& message);
bool ProcessSessionInitiateConfigMessage(const std::string& message);
const std::string& CreateSessionAcceptConfigMessage();
void Start(DatagramChannelFactory* factory, const std::string& shared_secret);
void CreateChannel(const std::string& name,
const ChannelCreatedCallback& callback);
void CancelChannelCreation(const std::string& name);
private:
friend class QuicChannelFactory;
struct PendingChannel {
PendingChannel(const std::string& name,
const ChannelCreatedCallback& callback)
: name(name), callback(callback) {}
std::string name;
ChannelCreatedCallback callback;
};
// QuicP2PSession::Delegate interface.
void OnIncomingStream(net::QuicP2PStream* stream) override;
void OnConnectionClosed(net::QuicErrorCode error) override;
void OnBaseChannelReady(scoped_ptr<P2PDatagramSocket> socket);
void OnNameReceived(QuicChannel* channel);
void OnChannelDestroyed(int stream_id);
std::string session_id_;
bool is_server_;
DatagramChannelFactory* base_channel_factory_ = nullptr;
net::QuicConfig quic_config_;
std::string shared_secret_;
std::string session_initiate_quic_config_message_;
std::string session_accept_quic_config_message_;
net::QuicClock quic_clock_;
net::QuicConnectionHelper quic_helper_;
scoped_ptr<net::QuicP2PSession> quic_session_;
bool connected_ = false;
std::vector<PendingChannel*> pending_channels_;
std::vector<QuicChannel*> unnamed_incoming_channels_;
base::WeakPtrFactory<Core> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(Core);
};
QuicChannelFactory::Core::Core(const std::string& session_id, bool is_server)
: session_id_(session_id),
is_server_(is_server),
quic_helper_(base::ThreadTaskRunnerHandle::Get().get(),
&quic_clock_,
net::QuicRandom::GetInstance()),
weak_factory_(this) {
quic_config_.SetInitialSessionFlowControlWindowToSend(
kQuicSessionMaxRecvWindowSize);
quic_config_.SetInitialStreamFlowControlWindowToSend(
kQuicStreamMaxRecvWindowSize);
}
QuicChannelFactory::Core::~Core() {}
void QuicChannelFactory::Core::Close() {
DCHECK(pending_channels_.empty());
// Cancel creation of the base channel if it hasn't finished.
if (base_channel_factory_)
base_channel_factory_->CancelChannelCreation(kQuicChannelName);
if (quic_session_ && quic_session_->connection()->connected())
quic_session_->connection()->CloseConnection(net::QUIC_NO_ERROR, false);
DCHECK(unnamed_incoming_channels_.empty());
}
void QuicChannelFactory::Core::Start(DatagramChannelFactory* factory,
const std::string& shared_secret) {
base_channel_factory_ = factory;
shared_secret_ = shared_secret;
base_channel_factory_->CreateChannel(
kQuicChannelName,
base::Bind(&Core::OnBaseChannelReady, weak_factory_.GetWeakPtr()));
}
const std::string&
QuicChannelFactory::Core::CreateSessionInitiateConfigMessage() {
DCHECK(!is_server_);
net::CryptoHandshakeMessage handshake_message;
handshake_message.set_tag(net::kCHLO);
quic_config_.ToHandshakeMessage(&handshake_message);
session_initiate_quic_config_message_ =
handshake_message.GetSerialized().AsStringPiece().as_string();
return session_initiate_quic_config_message_;
}
bool QuicChannelFactory::Core::ProcessSessionAcceptConfigMessage(
const std::string& message) {
DCHECK(!is_server_);
session_accept_quic_config_message_ = message;
scoped_ptr<net::CryptoHandshakeMessage> parsed_message(
net::CryptoFramer::ParseMessage(message));
if (!parsed_message) {
LOG(ERROR) << "Received invalid QUIC config.";
return false;
}
if (parsed_message->tag() != net::kSHLO) {
LOG(ERROR) << "Received QUIC handshake message with unexpected tag "
<< parsed_message->tag();
return false;
}
std::string error_message;
net::QuicErrorCode error = quic_config_.ProcessPeerHello(
*parsed_message, net::SERVER, &error_message);
if (error != net::QUIC_NO_ERROR) {
LOG(ERROR) << "Failed to process QUIC handshake message: "
<< error_message;
return false;
}
return true;
}
bool QuicChannelFactory::Core::ProcessSessionInitiateConfigMessage(
const std::string& message) {
DCHECK(is_server_);
session_initiate_quic_config_message_ = message;
scoped_ptr<net::CryptoHandshakeMessage> parsed_message(
net::CryptoFramer::ParseMessage(message));
if (!parsed_message) {
LOG(ERROR) << "Received invalid QUIC config.";
return false;
}
if (parsed_message->tag() != net::kCHLO) {
LOG(ERROR) << "Received QUIC handshake message with unexpected tag "
<< parsed_message->tag();
return false;
}
std::string error_message;
net::QuicErrorCode error = quic_config_.ProcessPeerHello(
*parsed_message, net::CLIENT, &error_message);
if (error != net::QUIC_NO_ERROR) {
LOG(ERROR) << "Failed to process QUIC handshake message: "
<< error_message;
return false;
}
return true;
}
const std::string&
QuicChannelFactory::Core::CreateSessionAcceptConfigMessage() {
DCHECK(is_server_);
if (session_initiate_quic_config_message_.empty()) {
// Don't send quic-config to the client if the client didn't include the
// config in the session-initiate message.
DCHECK(session_accept_quic_config_message_.empty());
return session_accept_quic_config_message_;
}
net::CryptoHandshakeMessage handshake_message;
handshake_message.set_tag(net::kSHLO);
quic_config_.ToHandshakeMessage(&handshake_message);
session_accept_quic_config_message_ =
handshake_message.GetSerialized().AsStringPiece().as_string();
return session_accept_quic_config_message_;
}
// StreamChannelFactory interface.
void QuicChannelFactory::Core::CreateChannel(
const std::string& name,
const ChannelCreatedCallback& callback) {
if (quic_session_ && quic_session_->connection()->connected()) {
if (!is_server_) {
net::QuicP2PStream* stream = quic_session_->CreateOutgoingDynamicStream();
scoped_ptr<QuicChannel> channel(new QuicClientChannel(
stream, base::Bind(&Core::OnChannelDestroyed, base::Unretained(this),
stream->id()),
name));
callback.Run(channel.Pass());
} else {
// On the server side wait for the client to create a QUIC stream and
// send the name. The channel will be connected in OnNameReceived().
pending_channels_.push_back(new PendingChannel(name, callback));
}
} else if (!base_channel_factory_) {
// Fail synchronously if we failed to connect transport.
callback.Run(nullptr);
} else {
// Still waiting for the transport.
pending_channels_.push_back(new PendingChannel(name, callback));
}
}
void QuicChannelFactory::Core::CancelChannelCreation(const std::string& name) {
for (auto it = pending_channels_.begin(); it != pending_channels_.end();
++it) {
if ((*it)->name == name) {
delete *it;
pending_channels_.erase(it);
return;
}
}
}
void QuicChannelFactory::Core::OnBaseChannelReady(
scoped_ptr<P2PDatagramSocket> socket) {
base_channel_factory_ = nullptr;
// Failed to connect underlying transport connection. Fail all pending
// channel.
if (!socket) {
while (!pending_channels_.empty()) {
scoped_ptr<PendingChannel> pending_channel(pending_channels_.front());
pending_channels_.erase(pending_channels_.begin());
pending_channel->callback.Run(nullptr);
}
return;
}
QuicPacketWriterFactory writer_factory(socket.get());
net::IPAddressNumber ip(net::kIPv4AddressSize, 0);
scoped_ptr<net::QuicConnection> quic_connection(new net::QuicConnection(
0, net::IPEndPoint(ip, 0), &quic_helper_, writer_factory,
true /* owns_writer */,
is_server_ ? net::Perspective::IS_SERVER : net::Perspective::IS_CLIENT,
net::QuicSupportedVersions()));
net::QuicP2PCryptoConfig quic_crypto_config(shared_secret_);
quic_crypto_config.set_hkdf_input_suffix(
session_id_ + '\0' + kQuicChannelName + '\0' +
session_initiate_quic_config_message_ +
session_accept_quic_config_message_);
quic_session_.reset(new net::QuicP2PSession(
quic_config_, quic_crypto_config, quic_connection.Pass(),
make_scoped_ptr(new P2PDatagramSocketAdapter(socket.Pass()))));
quic_session_->SetDelegate(this);
quic_session_->Initialize();
if (!is_server_) {
// On the client create streams for all pending channels and send a name for
// each channel.
while (!pending_channels_.empty()) {
scoped_ptr<PendingChannel> pending_channel(pending_channels_.front());
pending_channels_.erase(pending_channels_.begin());
net::QuicP2PStream* stream = quic_session_->CreateOutgoingDynamicStream();
scoped_ptr<QuicChannel> channel(new QuicClientChannel(
stream, base::Bind(&Core::OnChannelDestroyed, base::Unretained(this),
stream->id()),
pending_channel->name));
pending_channel->callback.Run(channel.Pass());
}
}
}
void QuicChannelFactory::Core::OnIncomingStream(net::QuicP2PStream* stream) {
QuicServerChannel* channel = new QuicServerChannel(
stream, base::Bind(&Core::OnChannelDestroyed, base::Unretained(this),
stream->id()));
unnamed_incoming_channels_.push_back(channel);
channel->ReceiveName(
base::Bind(&Core::OnNameReceived, base::Unretained(this), channel));
}
void QuicChannelFactory::Core::OnConnectionClosed(net::QuicErrorCode error) {
if (error != net::QUIC_NO_ERROR)
LOG(ERROR) << "QUIC connection was closed, error_code=" << error;
while (!pending_channels_.empty()) {
scoped_ptr<PendingChannel> pending_channel(pending_channels_.front());
pending_channels_.erase(pending_channels_.begin());
pending_channel->callback.Run(nullptr);
}
}
void QuicChannelFactory::Core::OnNameReceived(QuicChannel* channel) {
DCHECK(is_server_);
scoped_ptr<QuicChannel> owned_channel(channel);
auto it = std::find(unnamed_incoming_channels_.begin(),
unnamed_incoming_channels_.end(), channel);
DCHECK(it != unnamed_incoming_channels_.end());
unnamed_incoming_channels_.erase(it);
if (channel->name().empty()) {
// Failed to read a name for incoming channel.
return;
}
for (auto it = pending_channels_.begin();
it != pending_channels_.end(); ++it) {
if ((*it)->name == channel->name()) {
scoped_ptr<PendingChannel> pending_channel(*it);
pending_channels_.erase(it);
pending_channel->callback.Run(owned_channel.Pass());
return;
}
}
LOG(ERROR) << "Unexpected incoming channel: " << channel->name();
}
void QuicChannelFactory::Core::OnChannelDestroyed(int stream_id) {
if (quic_session_)
quic_session_->CloseStream(stream_id);
}
QuicChannelFactory::QuicChannelFactory(const std::string& session_id,
bool is_server)
: core_(new Core(session_id, is_server)) {}
QuicChannelFactory::~QuicChannelFactory() {
core_->Close();
base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, core_.release());
}
const std::string& QuicChannelFactory::CreateSessionInitiateConfigMessage() {
return core_->CreateSessionInitiateConfigMessage();
}
bool QuicChannelFactory::ProcessSessionAcceptConfigMessage(
const std::string& message) {
return core_->ProcessSessionAcceptConfigMessage(message);
}
bool QuicChannelFactory::ProcessSessionInitiateConfigMessage(
const std::string& message) {
return core_->ProcessSessionInitiateConfigMessage(message);
}
const std::string& QuicChannelFactory::CreateSessionAcceptConfigMessage() {
return core_->CreateSessionAcceptConfigMessage();
}
void QuicChannelFactory::Start(DatagramChannelFactory* factory,
const std::string& shared_secret) {
core_->Start(factory, shared_secret);
}
void QuicChannelFactory::CreateChannel(const std::string& name,
const ChannelCreatedCallback& callback) {
core_->CreateChannel(name, callback);
}
void QuicChannelFactory::CancelChannelCreation(const std::string& name) {
core_->CancelChannelCreation(name);
}
net::QuicP2PSession* QuicChannelFactory::GetP2PSessionForTests() {
return core_->quic_session_.get();
}
} // namespace protocol
} // namespace remoting
|