forked from libp2p/rust-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 2
/
endpoint.rs
643 lines (579 loc) · 28.3 KB
/
endpoint.rs
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright 2019 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! Actual QUIC state machine.
//!
//! The `QuicEndpoint` struct wraps around a `quinn_proto::Endpoint` and adds sockets and
//! everything related to I/O.
use bytes::BytesMut;
use fnv::FnvHashMap;
use futures::{future::{self, FutureResult}, prelude::*, sync::mpsc, sync::oneshot, task};
use log::{debug, trace, warn};
use multiaddr::{Multiaddr, Protocol};
use multihash::Multihash;
use parking_lot::Mutex;
use simple_asn1::oid;
use smallvec::SmallVec;
use std::{
cmp,
fmt, io, iter,
net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
sync::{atomic, Arc, Weak},
time::Duration, time::Instant,
};
use tokio_udp::UdpSocket;
/// Background task dedicated to processing QUIC connections.
pub struct QuicEndpoint {
/// Sender for events from the public API. Processed in the background task.
outside_events: mpsc::UnboundedSender<OutToInMessage>,
/// If false, we need to start a background task that will process the sockets.
/// The background task is passed an `Arc<QuicEndpoint>`.
task_started: atomic::AtomicBool,
/// The inner structure, behind a `Mutex`. Mainly locked from within the background task, but
/// also sometimes from the outside.
inner: Mutex<QuicEndpointInner>,
}
/// An active connection. Exposed in the public API of this module.
#[derive(Debug)] // TODO: better Debug
pub struct QuicConnec {
/// Identifier for this connection in the background task state machine.
connection_handle: quinn_proto::ConnectionHandle,
}
/// An active substream. Exposed in the public API of this module.
#[derive(Debug)] // TODO: better Debug
pub struct QuicSub {
/// Identifier for this substream in the background task state machine.
id: quinn_proto::StreamId,
}
/// Message sent from the the public API of the endpoint to the background task.
enum OutToInMessage {
/// Start connecting to a remote.
Connect {
/// Address of the remote.
target: SocketAddr,
/// Channel used to send back the result once connected.
send_back: oneshot::Sender<Result<QuicConnec, io::Error>>,
},
/// Open an outbound substream.
OpenSubstream {
/// Id of the connection to open a substream on.
connection: quinn_proto::ConnectionHandle,
/// Channel used to send back the result. Will return `None` if we have reached the maximum
/// number of substreams.
send_back: oneshot::Sender<Option<QuicSub>>,
},
/// Shuts down a substream.
ShutdownSubstream {
/// Id of the connection that owns the substream.
connection: quinn_proto::ConnectionHandle,
/// Id of the substream to shut down.
stream: quinn_proto::StreamId,
},
/// Destroys a substream.
// TODO: necessary?
DestroySubstream {
/// Id of the connection that owns the substream.
connection: quinn_proto::ConnectionHandle,
/// Id of the substream to destroy.
stream: quinn_proto::StreamId,
},
}
/// Internal handling of QUIC.
struct QuicEndpointInner {
/// State machine that handles the protocol.
quinn: quinn_proto::Endpoint,
/// Receiver for messages sent through the public API.
outside_events: mpsc::UnboundedReceiver<OutToInMessage>,
/// List of active UDP sockets.
sockets: SmallVec<[QuicSocket; 1]>,
/// List of open connections.
// TODO: something more efficient so that we can insert/remove connections in the middle?
connections: Vec<QuicConnection>,
/// Configuration for outbound connections.
client_config: Arc<quinn_proto::ClientConfig>,
/// Temporary buffer used for all readings. We always read inside that same buffer, so that we
/// don't have to reallocate one every time.
// Note: `QuicEndpointInner` is always in an `Arc`, so it's not a problem to use a large buffer.
// TODO: is this buffer large enough? it seems that we silently fail if it isn't
read_buffer: [u8; 2048],
}
/// State of a socket managed by the background task.
struct QuicSocket {
/// The actual socket object.
socket: UdpSocket,
/// If Some, we are "listening" on this socket and thus it shouldn't be closed. If None,
/// we also deny any new incoming connection on this socket.
listening: Option<mpsc::Sender<(QuicConnec, SocketAddr)>>,
/// Address we are binded to.
addr: SocketAddr,
}
/// An active connection in the backgroud task state machine.
struct QuicConnection {
/// Identifier of the connection within the global state machine.
handle: quinn_proto::ConnectionHandle,
/// Addr the socket is operating on. This is used to match the element in `QuicSocket`.
socket_addr: SocketAddr,
/// List of substreams that have been opened on the connection.
substreams: FnvHashMap<quinn_proto::StreamId, QuicSubstream>,
/// List of timers that have been started.
timers: SmallVec<[(quinn_proto::Timer, tokio_timer::Delay); 4]>,
/// Sender to trigger when the connection is established.
send_back: Option<oneshot::Sender<Result<QuicConnec, io::Error>>>,
}
struct QuicSubstream {
read_notify: Option<task::Task>,
write_notify: Option<task::Task>,
}
impl QuicEndpoint {
/// Initializes a new `QuicEndpoint`.
pub fn new(keypair: libp2p_core::identity::Keypair) -> Result<Arc<QuicEndpoint>, quinn_proto::EndpointError> {
let (tx, rx) = mpsc::unbounded();
let (certificates, private_key) = {
let key = openssl::rsa::Rsa::generate(2048).unwrap();
let mut certif = openssl::x509::X509Builder::new().unwrap(); // TODO:
certif.set_version(2).unwrap();
let mut serial: [u8; 20] = rand::random();
certif.set_serial_number(&openssl::asn1::Asn1Integer::from_bn(&openssl::bn::BigNum::from_slice(&serial[..]).unwrap()).unwrap()).unwrap();
let mut x509_name = openssl::x509::X509NameBuilder::new().unwrap();
x509_name.append_entry_by_text("C", "US").unwrap();
x509_name.append_entry_by_text("ST", "CA").unwrap();
x509_name.append_entry_by_text("O", "Some organization").unwrap();
x509_name.append_entry_by_text("CN", "libp2p.io").unwrap();
let x509_name = x509_name.build();
certif.set_issuer_name(&x509_name).unwrap();
certif.set_subject_name(&x509_name).unwrap();
// TODO: libp2p specs says we shouldn't have these date fields
certif.set_not_before(&openssl::asn1::Asn1Time::days_from_now(0).unwrap()).unwrap();
certif.set_not_after(&openssl::asn1::Asn1Time::days_from_now(365).unwrap()).unwrap();
certif.set_pubkey(openssl::pkey::PKey::from_rsa(key.clone()).unwrap().as_ref()).unwrap(); // TODO: unwrap
let ext = openssl::x509::extension::BasicConstraints::new()
.critical()
//.ca()
.build().unwrap();
certif.append_extension(ext).unwrap();
let ext = openssl::x509::extension::SubjectKeyIdentifier::new()
.build(&certif.x509v3_context(None, None)).unwrap();
certif.append_extension(ext).unwrap();
let ext = openssl::x509::extension::AuthorityKeyIdentifier::new()
.issuer(true)
.keyid(true)
.build(&certif.x509v3_context(None, None)).unwrap();
certif.append_extension(ext).unwrap();
let ext = openssl::x509::extension::SubjectAlternativeName::new()
.dns("libp2p.io") // TODO: must match the domain name in the QUIC requests being made
.build(&certif.x509v3_context(None, None)).unwrap();
certif.append_extension(ext).unwrap();
// TODO:
/*{
let ext = format!("publicKey={}", bs58::encode(keypair.public().into_protobuf_encoding()).into_string()); // TODO: signature
certif.append_extension(openssl::x509::X509Extension::new(None, None, "1.3.6.1.4.1.53594.1.1", &ext).unwrap());
}*/
certif.sign(&openssl::pkey::PKey::from_rsa(key.clone()).unwrap(), openssl::hash::MessageDigest::sha256()).unwrap();
let certif_gen = certif.build();
debug_assert!(certif_gen.verify(&openssl::pkey::PKey::from_rsa(key.clone()).unwrap()).unwrap());
let pkey_bytes = key.private_key_to_der().unwrap();
(vec![rustls::Certificate(certif_gen.to_der().unwrap())], rustls::PrivateKey(pkey_bytes))
};
let client_config = {
struct DummyVerifier;//(Mutex<Option<PeerId>>);
impl rustls::ServerCertVerifier for DummyVerifier {
fn verify_server_cert(&self,
_: &rustls::RootCertStore,
certs: &[rustls::Certificate],
_: webpki::DNSNameRef,
_ocsp_response: &[u8]) -> Result<rustls::ServerCertVerified, rustls::TLSError>
{
println!("blocks: {:?}", simple_asn1::from_der(&certs[0].0));
Ok(rustls::ServerCertVerified::assertion())
}
}
let mut cfg = quinn_proto::ClientConfig::new();
cfg.versions = vec![rustls::ProtocolVersion::TLSv1_3];
cfg.enable_sni = false;
cfg.enable_early_data = true;
cfg.key_log = Arc::new(rustls::KeyLogFile::new());
cfg.set_single_client_cert(certificates.clone(), private_key.clone());
cfg.dangerous().set_certificate_verifier(Arc::new(DummyVerifier));
Arc::new(cfg)
};
let server_config = {
let mut rustls_cfg = rustls::ServerConfig::new(rustls::NoClientAuth::new()); // TODO: no, authenticate client
rustls_cfg.versions = vec![rustls::ProtocolVersion::TLSv1_3];
rustls_cfg.key_log = Arc::new(rustls::KeyLogFile::new());
rustls_cfg.set_single_cert(certificates, private_key).unwrap();
let mut cfg = quinn_proto::ServerConfig::default();
cfg.tls_config = Arc::new(rustls_cfg);
cfg
};
Ok(Arc::new(QuicEndpoint {
outside_events: tx,
inner: Mutex::new(QuicEndpointInner {
quinn: quinn_proto::Endpoint::new(
slog::Logger::root(slog::Discard, slog::o!()),
quinn_proto::Config::default(),
Some(server_config),
)?,
outside_events: rx,
connections: Vec::with_capacity(32),
sockets: SmallVec::new(),
client_config,
read_buffer: [0; 2048],
}),
task_started: atomic::AtomicBool::new(false),
}))
}
/// Ensures that the QUIC background task has properly started. Starts it if it hasn't.
fn ensure_task_started(self: Arc<Self>) {
if self.task_started.swap(true, atomic::Ordering::Relaxed) {
let me = Arc::downgrade(&self);
tokio_executor::spawn(future::poll_fn(move || {
if let Some(me) = me.upgrade() {
me.inner.lock().poll();
Ok(Async::NotReady)
} else {
Ok(Async::Ready(()))
}
}));
}
}
/// Start listening on an address. Returns a channel that will automatically produce
/// connections.
///
/// Dropping the receiver will close the listening.
pub(crate) fn start_listening(self: Arc<Self>, addr: SocketAddr)
-> io::Result<(mpsc::Receiver<(QuicConnec, SocketAddr)>, SocketAddr)>
{
self.clone().ensure_task_started();
let (tx, rx) = mpsc::channel(2);
let mut inner = self.inner.lock();
if let Some(socket) = inner.sockets.iter_mut().find(|s| s.addr == addr) {
if socket.listening.as_ref().map(|tx| !tx.is_closed()).unwrap_or(false) {
return Err(io::ErrorKind::AddrInUse.into());
} else {
socket.listening = Some(tx);
return Ok((rx, socket.addr));
}
}
let socket = UdpSocket::bind(&addr)?;
let actual_addr = socket.local_addr()?;
inner.sockets.push(QuicSocket {
socket,
listening: Some(tx),
addr: actual_addr.clone(),
});
Ok((rx, actual_addr))
}
/// Start connecting to the given target. Returns a future that resolves when we finish
/// connecting.
pub(crate) fn connect(self: Arc<Self>, target: SocketAddr)
-> oneshot::Receiver<Result<QuicConnec, io::Error>>
{
self.clone().ensure_task_started();
// TODO: have a config option for the local port to use when dialing
let (send_back, rx) = oneshot::channel();
self.outside_events.unbounded_send(OutToInMessage::Connect { target, send_back })
.expect("The receiver is stored in self as well, therefore the sender can never \
disconnect; QED");
rx
}
/// Open a substream with the target.
pub(crate) fn open_substream(&self, connection: &QuicConnec)
-> oneshot::Receiver<Option<QuicSub>>
{
let (send_back, rx) = oneshot::channel();
self.outside_events.unbounded_send(OutToInMessage::OpenSubstream { connection: connection.connection_handle, send_back })
.expect("The receiver is stored in self as well, therefore the sender can never \
disconnect; QED");
rx
}
/// Try to read data from a substream.
///
/// The API is similar to the one of `StreamMuxer::read_substream`.
pub(crate) fn read_substream(&self, connection: &QuicConnec, stream: &QuicSub, buf: &mut [u8]) -> Poll<usize, io::Error> {
let mut inner = self.inner.lock();
match inner.quinn.read(connection.connection_handle, stream.id, buf) {
Ok(n) => Ok(Async::Ready(n)),
Err(quinn_proto::ReadError::Blocked) => {
Ok(Async::NotReady) // FIXME: notify task
},
Err(quinn_proto::ReadError::Finished) => Ok(Async::Ready(0)),
Err(quinn_proto::ReadError::Reset { .. }) => Err(io::ErrorKind::BrokenPipe.into()),
}
}
/// Try to write data to a substream.
///
/// The API is similar to the one of `StreamMuxer::write_substream`.
pub(crate) fn write_substream(&self, connection: &QuicConnec, stream: &QuicSub, data: &[u8]) -> Poll<usize, io::Error> {
let mut inner = self.inner.lock();
match inner.quinn.write(connection.connection_handle, stream.id, data) {
Ok(n) => Ok(Async::Ready(n)),
Err(quinn_proto::WriteError::Blocked) => {
Ok(Async::NotReady) // FIXME: notify task
},
Err(quinn_proto::WriteError::Stopped { .. }) => Err(io::ErrorKind::BrokenPipe.into()),
}
}
/// Shuts down the substream.
///
/// Has no effect is the substream has already been shut down or destroyed.
pub(crate) fn shutdown_substream(&self, connection: &QuicConnec, stream: &QuicSub) {
self.outside_events
.unbounded_send(OutToInMessage::ShutdownSubstream { connection: connection.connection_handle, stream: stream.id })
.expect("The receiver is stored in self as well, therefore the sender can never \
disconnect; QED");
}
/// Destroys the given substream.
///
/// Has no effect is the substream has already been destroyed.
pub(crate) fn destroy_substream(&self, connection: &QuicConnec, stream: QuicSub) {
self.outside_events
.unbounded_send(OutToInMessage::DestroySubstream { connection: connection.connection_handle, stream: stream.id })
.expect("The receiver is stored in self as well, therefore the sender can never \
disconnect; QED");
}
}
impl fmt::Debug for QuicEndpoint {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("QuicEndpoint")
// TODO:
.finish()
}
}
impl QuicEndpointInner {
/// Processes the QUIC connection. The future never ends and never produces a result. In other
/// words, the result of this method should be considered as `Ok(Async::NotReady)`.
fn poll(&mut self) {
loop {
// If we set this variable to `true` in the loop, we're going to `continue`.
let mut need_repolling = false;
let now = now();
loop {
match self.outside_events.poll() {
Ok(Async::Ready(Some(OutToInMessage::Connect { target, send_back }))) => {
let socket_addr = match self.assign_dialing_socket(&target) {
Ok(p) => p,
Err(err) => { let _ = send_back.send(Err(err)); continue; }
};
match self.quinn.connect(target, &self.client_config, "libp2p.io") { // TODO: Must match the certificate
Ok(connection_id) => {
self.connections.push(QuicConnection {
handle: connection_id,
socket_addr,
substreams: Default::default(),
timers: SmallVec::new(),
send_back: Some(send_back),
});
},
Err(err) => {
panic!("{:?}", err); // TODO:
//let _ = send_back.send(Err(err));
}
}
},
Ok(Async::Ready(Some(OutToInMessage::OpenSubstream { connection, send_back }))) => {
let result = self.quinn.open(connection, quinn_proto::Directionality::Bi);
if let Some(stream) = result {
self.connections
.iter_mut().find(|c| c.handle == connection)
.expect("The list of connections is kept in sync with the external API; QED")
.substreams
.insert(stream, QuicSubstream {
read_notify: None,
write_notify: None,
});
}
let _ = send_back.send(result.map(|id| QuicSub { id }));
},
Ok(Async::Ready(Some(OutToInMessage::ShutdownSubstream { connection, stream }))) => {
self.quinn.finish(connection, stream);
},
Ok(Async::Ready(Some(OutToInMessage::DestroySubstream { connection, stream }))) => {
// TODO: call reset? not sure
if let Some(connection) = self.connections.iter_mut().find(|c| c.handle == connection) {
connection.substreams.remove(&stream);
}
},
Ok(Async::NotReady) => break,
Ok(Async::Ready(None)) | Err(_) => {
unreachable!("The sender is in the QuicEndpoint, which we know is alive \
because we have a borrow one of its fields; QED")
},
}
}
for socket in self.sockets.iter_mut() {
// TODO: call quinn.close on error
while let Async::Ready((num_read, from)) = socket.socket.poll_recv_from(&mut self.read_buffer).unwrap() { // TODO: don't unwrap :-/
let data = &self.read_buffer[..num_read];
self.quinn.handle(now, from, None, data.into());
}
// TODO: obviously bad
loop {
// TODO: don't unwrap
if let Async::NotReady = socket.socket.poll_write_ready().unwrap() {
break;
}
if let Some(transmit) = self.quinn.poll_transmit(now) {
match socket.socket.poll_send_to(&transmit.packet, &transmit.destination) {
Ok(Async::Ready(n)) => assert_eq!(n, transmit.packet.len()),
other => panic!("{:?}", other) // TODO: no
}
} else {
break;
}
}
}
while let Some((connection_id, event)) = self.quinn.poll() {
println!("event: {:?} {:?}", connection_id, event);
let connection = if let quinn_proto::Event::Handshaking = event {
debug_assert!(self.connections.iter().all(|c| c.handle != connection_id));
let socket = self.sockets.iter_mut().next().unwrap(); // TODO: no
if let Some(tx) = socket.listening.as_mut() {
// TODO: so wrong
let _ = tx.start_send((QuicConnec {
connection_handle: connection_id,
}, socket.addr)); // TODO: socket.addr is wrong; has to be the remote addr
let _ = tx.poll_complete();
}
self.connections.push(QuicConnection {
handle: connection_id,
socket_addr: socket.addr,
substreams: Default::default(),
timers: SmallVec::new(),
send_back: None, // TODO: store something actually, and don't use a FutureResult in lib.rs
});
self.connections.last_mut().expect("we just pushed to this list; QED")
} else {
self.connections.iter_mut().find(|c| c.handle == connection_id)
.expect("self.connections is always tracking the currently active connections; QED")
};
match event {
quinn_proto::Event::Handshaking => {
self.quinn.accept();
need_repolling = true;
},
quinn_proto::Event::Connected => {
// TODO: assert that it's there, once the listening side also uses that
if let Some(send_back) = connection.send_back.take() {
let _ = send_back.send(Ok(QuicConnec { connection_handle: connection.handle }));
}
},
quinn_proto::Event::ConnectionLost { reason } => {
panic!("lost connec {:?}", reason);
},
quinn_proto::Event::StreamOpened => {
let stream = self.quinn.accept_stream(connection_id)
.expect("accept_stream is guaranteed to return Some after StreamOpened; QED");
connection.substreams.insert(stream, QuicSubstream {
read_notify: None,
write_notify: None,
});
// TODO: notify
},
quinn_proto::Event::StreamReadable { stream } => {
let substream = connection.substreams
.get_mut(&stream)
.expect("connection.substreams always follow the open substreams; QED");
if let Some(task) = substream.read_notify.take() {
task.notify();
}
},
quinn_proto::Event::StreamWritable { stream } => {
let substream = connection.substreams
.get_mut(&stream)
.expect("connection.substreams always follow the open substreams; QED");
if let Some(task) = substream.write_notify.take() {
task.notify();
}
},
quinn_proto::Event::StreamFinished { stream } => {
let substream = connection.substreams
.get_mut(&stream)
.expect("connection.substreams always follow the open substreams; QED");
if let Some(task) = substream.read_notify.take() {
task.notify();
}
if let Some(task) = substream.write_notify.take() {
task.notify();
}
},
quinn_proto::Event::StreamAvailable { .. } => {},
}
}
while let Some((connection_id, timer_update)) = self.quinn.poll_timers() {
self.connections.iter_mut()
.find(|c| c.handle == connection_id)
.expect("connections always follows the quinn state machine; QED")
.inject_update(timer_update);
}
for connection in self.connections.iter_mut() {
while let Async::Ready(timer) = connection.poll_timeouts() {
self.quinn.timeout(now, connection.handle, timer);
need_repolling = true;
}
}
if !need_repolling {
break;
}
}
}
/// Chooses a socket to use to dial the target address and returns its local address, or
/// creates a new socket.
fn assign_dialing_socket(&mut self, target: &SocketAddr) -> Result<SocketAddr, io::Error> {
// TODO: IPv4 must only be assigned to IPv4 sockets and same for IPv6
Ok(self.sockets.iter().next().unwrap().addr.clone()) // TODO: no
}
}
impl QuicConnection {
/// Modifies the timers to take the update into account.
fn inject_update(&mut self, update: quinn_proto::TimerUpdate) {
match update.update {
quinn_proto::TimerSetting::Start(expiration) => {
let delay = tokio_timer::Delay::new(*EPOCH + Duration::from_millis(expiration));
self.timers.push((update.timer, delay));
},
quinn_proto::TimerSetting::Stop => {
self.timers.retain(|(t, _)| *t != update.timer);
},
}
}
/// Polls any of the timers to see if there's a timeout.
fn poll_timeouts(&mut self) -> Async<quinn_proto::Timer> {
let pos_finished = self.timers.iter_mut().position(|(_, delay)| {
match delay.poll() {
Ok(Async::Ready(())) => true,
Ok(Async::NotReady) => false,
Err(_) => true,
}
});
if let Some(pos_finished) = pos_finished {
Async::Ready(self.timers.remove(pos_finished).0)
} else {
Async::NotReady
}
}
}
lazy_static::lazy_static! {
/// Arbitrary EPOCH from which `now()` is being calculated.
static ref EPOCH: Instant = Instant::now();
}
/// Returns the number of milliseconds that have passed since an arbitrary EPOCH.
fn now() -> u64 {
let elapsed = EPOCH.elapsed();
elapsed.as_secs().saturating_mul(1_000).saturating_add(u64::from(elapsed.subsec_millis()))
}