rpki 0.19.3

A library for validating and creating RPKI data.
Documentation
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
//! The RTR client.
//!
//! This module implements a generic RTR client through [`Client`]. In order
//! to use the client, you will need to provide a type that implements
//! [`PayloadTarget`] as well as one that implements [`PayloadUpdate`].
//! The former represents the place where all the information received via the
//! RTR client is stored, while the latter receives a set of updates and
//! applies it to the target.
//!
//! For more information on how to use the client, see the [`Client`] type.
use std::{cmp, error, fmt, io};
use std::future::Future;
use tokio::time::{timeout, timeout_at, Duration, Instant};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use super::payload::{Action, Payload, Timing};
use super::pdu;
use super::server::MAX_VERSION;
use super::state::State;


//------------ Configuration Constants ---------------------------------------

const IO_TIMEOUT: Duration = Duration::from_secs(10);

/// The protocol version we initially propose.
const INITIAL_VERSION: u8 = 2;


//------------ PayloadTarget -------------------------------------------------

/// A type that keeps data received via RTR.
///
/// The data of the target consisting of a set of items called VRPs for
/// Validated RPKI Payload. It is modified by atomic updates that add
/// or remove items of the set.
///
/// This trait provides a method to start and apply updates which are
/// collected into a different type that implements the companion
/// [`PayloadUpdate`] trait.
pub trait PayloadTarget {
    /// The type of a single update.
    type Update: PayloadUpdate;

    /// Starts a new update.
    ///
    /// If the update is a for a reset query, `reset` will be `true`, meaning
    /// that when the update is applied, all previous data should be removed.
    fn start(&mut self, reset: bool) -> Self::Update;

    /// Applies an update to the target.
    ///
    /// The data to apply is handed over via `update`. If `reset` is `true`,
    /// the data should replace the current data of the target. Otherwise it
    /// entries should be added and removed according to the action. The
    /// `timing` parameter contains the timing information provided by the
    /// server.
    fn apply(
        &mut self, update: Self::Update, timing: Timing
    ) -> Result<(), PayloadError>;
}


//------------ PayloadUpdate -------------------------------------------------

/// A type that can receive a data update.
///
/// The update happens by repeatedly calling the
/// [`push_update`][Self::push_update] method with a single update as received
/// by the client. The data is not filtered. It may contain duplicates and it
/// may conflict with the current data set. It is the task of the implementor
/// to deal with such situations.
///
/// A value of this type is created via [`PayloadTarget::start`] when the
/// client starts processing an update. If the update succeeds, the value is
/// applied to the target by giving it to [`PayloadTarget::apply`]. If the
/// update fails at any point, the valus is simply dropped.
pub trait PayloadUpdate {
    /// Applies a single updated payload element.
    ///
    /// The `action` argument describes whether the element is to be
    /// announced, i.e., added to the data set, or withdrawn, i.e., removed.
    /// The element itself is given via `payload`.
    ///
    /// If the update is found to be illegal for some reason, the method
    /// returns an error with the appropriate reason.
    fn push_update(
        &mut self, action: Action, payload: Payload
    ) -> Result<(), PayloadError>;
}

impl PayloadUpdate for Vec<(Action, Payload)> {
    fn push_update(
        &mut self, action: Action, payload: Payload
    ) -> Result<(), PayloadError> {
        self.push((action, payload));
        Ok(())
    }
}


//------------ Client --------------------------------------------------------

/// An RTR client.
///
/// The client wraps a socket – represented by the type argument `Sock` which
/// needs to support Tokio’s asynchronous writing and reading – and runs an
/// RTR client over it. All data received will be passed on to a
/// [`PayloadTarget`] of type `Target`.
///
/// The client keeps the socket open until either the server closes the
/// connection, an error happens, or the client is dropped. It will
/// periodically push a new dataset to the target.
pub struct Client<Sock, Target> {
    /// The socket to communicate over.
    sock: Sock,

    /// The target for the VRP set.
    target: Target,

    /// The current synchronisation state.
    ///
    /// If this is `None`, we do a reset query next.
    state: Option<State>,

    /// The RTR version to use.
    ///
    /// If this is `None` we haven’t spoken with the server yet. In this
    /// case, we use 1 and accept any version from the server. Otherwise 
    /// send this version and receving a differing version from the server
    /// is an error as it is not allowed to change its mind halfway.
    version: Option<u8>,

    /// The RTR version to start with.
    initial_version: u8,

    /// The timing parameters reported by the server.
    ///
    /// We use the `refresh` value to determine how long to wait before
    /// requesting an update. The other values we just report to the target.
    timing: Timing,

    /// The next time we should be running.
    ///
    /// If this is None, we should be running now.
    next_update: Option<Instant>,
}

impl<Sock, Target> Client<Sock, Target> {
    /// Creates a new client.
    ///
    /// The client will use `sock` for communicating with the server and
    /// `target` to send updates to.
    ///
    /// If the last state of a connection with this server is known – it can
    /// be determined by calling [`state`] on the client – it can be reused
    /// via the `state` argument. Make sure to also have the matching data in
    /// your target in this case since the there will not necessarily be a
    /// reset update. If you don’t have any state or don’t want to reuse an
    /// earlier session, simply pass `None`.
    ///
    /// [`state`]: #method.state
    pub fn new(
        sock: Sock,
        target: Target,
        state: Option<State>
    ) -> Self {
        Self::with_initial_version(INITIAL_VERSION, sock, target, state)
    }

    /// Creates a new client starting with the given RTR version.
    ///
    /// This is identical [`new`][`Self::new`] but sets the initial version
    /// to the given value. Note that `version` is quietly capped to the
    /// largest version we support.
    ///
    /// The client will downgrade to a lower version if necessary to talk to
    /// the server.
    pub fn with_initial_version(
        initial_version: u8,
        sock: Sock,
        target: Target,
        state: Option<State>
    ) -> Self {
        Client {
            sock, target, state,
            version: None,
            initial_version: cmp::min(initial_version, MAX_VERSION),
            timing: Timing::default(),
            next_update: None,
        }
    }

    /// Returns a reference to the target.
    pub fn target(&self) -> &Target {
        &self.target
    }

    /// Returns a mutable reference to the target.
    pub fn target_mut(&mut self) -> &mut Target {
        &mut self.target
    }

    /// Converts the client into its target.
    pub fn into_target(self) -> Target {
        self.target
    }

    /// Returns the current state of the session.
    ///
    /// The method will return `None` if there hasn’t been initial state and
    /// there has not been any converstation with the server yet.
    pub fn state(&self) -> Option<State> {
        self.state
    }

    /// Returns the protocol version to use.
    fn version(&self) -> u8 {
        self.version.unwrap_or(self.initial_version)
    }
}

impl<Sock, Target> Client<Sock, Target>
where
    Sock: AsyncRead + AsyncWrite + Unpin,
    Target: PayloadTarget
{
    /// Runs the client.
    ///
    /// The method will keep the client asynchronously running, fetching any
    /// new data that becomes available on the server and pushing it to the
    /// target until either the server closes the connection – in which case
    /// the method will return `Ok(())` –, an error happens – which will be
    /// returned or the future gets dropped.
    pub async fn run(&mut self) -> Result<(), io::Error> {
        loop {
            if let Err(err) = self.step().await {
                if err.kind() == io::ErrorKind::UnexpectedEof {
                    return Ok(())
                }
                else {
                    return Err(err)
                }
            }
        }
    }

    /// Preforms a single update step.
    pub async fn step(
        &mut self
    ) -> Result<(), io::Error> {
        let update = self.update().await?;
        self.apply(update).await
    }

    /// Performs a single update of the client data.
    ///
    /// The method will wait until the next update is due and the request one
    /// single update from the server. It will request a new update object
    /// from the target, apply the update to that object and, if the update
    /// succeeds, return the object.
    pub async fn update(
        &mut self
    ) -> Result<Target::Update, io::Error> {
        if let Some(instant) = self.next_update.take() {
            if let Ok(Err(err)) = timeout_at(
                instant, pdu::SerialNotify::read(&mut self.sock)
            ).await {
                return Err(err)
            }
        }

        if let Some(state) = self.state {
            if let Some(update) = self.serial(state).await? {
                self.next_update = Some(
                    Instant::now() + self.timing.refresh_duration()
                );
                return Ok(update)
            }
        }
        let res = self.reset().await;
        self.next_update = Some(
            Instant::now() + self.timing.refresh_duration()
        );
        res
    }


    /// Perform a serial query.
    ///
    /// Returns some update if the query succeeded and the client should now
    /// wait for a while. Returns `None` if the server reported a restart and
    /// we need to proceed with a reset query. Returns an error
    /// in any other case.
    async fn serial(
        &mut self, state: State
    ) -> Result<Option<Target::Update>, io::Error> {
        let start = loop {
            pdu::SerialQuery::new(
                self.version(), state,
            ).write(&mut self.sock).await?;
            self.sock.flush().await?;
            match self.try_io(FirstSerialReply::read).await? {
                FirstSerialReply::Response(start) => break start,
                FirstSerialReply::Reset => {
                    self.state = None;
                    return Ok(None)
                }
                FirstSerialReply::VersionError(version) => {
                    if self.version.is_some() {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            "version error after successful version \
                             negotiation"
                        ));
                    }
                    // We sent the query with INITIAL_VERSION, so the
                    // returned version must be less.
                    if version >= INITIAL_VERSION {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            "version error with larger version"
                        ));
                    }

                    // Try again with the requested version. This will also
                    // force the loop to terminate on next try.
                    self.version = Some(version);
                }
            }
        };
        self.check_version(start.version())?;

        let mut target = self.target.start(false);
        loop {
            match pdu::Payload::read(&mut self.sock).await? {
                Ok(Some(pdu)) => {
                    self.check_version(pdu.version())?;
                    let (action, payload) = match pdu.to_payload() {
                        Ok(some) => some,
                        Err(err) => {
                            err.write(&mut self.sock).await?;
                            return Err(io::Error::other(""));
                        }
                    };
                    if let Err(err) = target.push_update(action, payload) {
                        err.send(
                            self.version(), Some(pdu), &mut self.sock
                        ).await?;
                        return Err(io::Error::other(""));
                    }
                }
                Ok(None) => {
                    // Unsupported but legal payload: ignore.
                }
                Err(end) => {
                    self.check_version(end.version())?;
                    self.state = Some(end.state());
                    if let Some(timing) = end.timing() {
                        self.timing = timing
                    }
                    break;
                }
            }
        }
        Ok(Some(target))
    }

    /// Performs a reset query.
    pub async fn reset(&mut self) -> Result<Target::Update, io::Error> {
        let start = loop {
            pdu::ResetQuery::new(
                self.version()
            ).write(&mut self.sock).await?;
            self.sock.flush().await?;
            match self.try_io(FirstResetReply::read).await? {
                FirstResetReply::Response(start) => break start,
                FirstResetReply::VersionError(version) => {
                    if self.version.is_some() {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            "version error after successful version \
                             negotiation"
                        ));
                    }
                    // We sent the query with INITIAL_VERSION, so the
                    // returned version must be less.
                    if version >= INITIAL_VERSION {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidData,
                            "version error with larger version"
                        ));
                    }

                    // Try again with the requested version. This will also
                    // force the loop to terminate on next try.
                    self.version = Some(version);
                }
            }
        };
        self.check_version(start.version())?;
        let mut target = self.target.start(true);
        loop {
            match pdu::Payload::read(&mut self.sock).await? {
                Ok(Some(pdu)) => {
                    self.check_version(pdu.version())?;
                    let (action, payload) = match pdu.to_payload() {
                        Ok(some) => some,
                        Err(err) => {
                            err.write(&mut self.sock).await?;
                            return Err(io::Error::other(""))
                        }
                    };
                    if let Err(err) = target.push_update(action, payload) {
                        err.send(
                            self.version(), Some(pdu), &mut self.sock
                        ).await?;
                        return Err(io::Error::other(""));
                    }
                }
                Ok(None) => {
                    // Unsupported but legal payload: ignore.
                }
                Err(end) => {
                    self.check_version(end.version())?;
                    self.state = Some(end.state());
                    if let Some(timing) = end.timing() {
                        self.timing = timing
                    }
                    break;
                }
            }
        }
        Ok(target)
    }

    /// Tries to apply an update and sends errors if that fails.
    pub async fn apply(
        &mut self, update: Target::Update
    ) -> Result<(), io::Error> {
        if let Err(err) = self.target.apply(update, self.timing) {
            self.send_error(err).await?;
            Err(io::Error::other(""))
        }
        else {
            Ok(())
        }
    }

    /// Sends an error response to the server.
    pub async fn send_error(
        &mut self, err: PayloadError
    ) -> Result<(), io::Error> {
        err.send(self.version(), None, &mut self.sock).await
    }

    /// Performs some IO operation on the socket.
    ///
    /// The mutable reference to the socket is passed to the closure provided
    /// which does the actual IO. The closure is given `IO_TIMEOUT` to finsih
    /// whatever it is doing. Otherwise it is cancelled and a timeout error
    /// is returned.
    async fn try_io<'a, F, Fut, T>(
        &'a mut self, op: F
    ) -> Result<T, io::Error>
    where
        F: FnOnce(&'a mut Sock) -> Fut,
        Fut: Future<Output = Result<T, io::Error>> + 'a
    {
        match timeout(IO_TIMEOUT, op(&mut self.sock)).await {
            Ok(res) => res,
            Err(_) => {
                Err(io::Error::new(
                    io::ErrorKind::TimedOut,
                    "server response timed out"
                ))
            }
        }
    }

    /// Checks whether `version` matches the stored version.
    ///
    /// Returns an error if it doesn’t.
    fn check_version(&mut self, version: u8) -> Result<(), io::Error> {
        if let Some(stored_version) = self.version {
            if version != stored_version {
                Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "version has changed"
                ))
            }
            else {
                Ok(())
            }
        }
        else if version > INITIAL_VERSION {
            Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "server requested unsupported protocol version {version}"
                )
            ))
        }
        else {
            self.version = Some(version);
            Ok(())
        }
    }
}


//------------ FirstSerialReply ----------------------------------------------

/// The first reply from a server in response to a serial query.
enum FirstSerialReply {
    /// A cache response. Actual data is to follow.
    Response(pdu::CacheResponse),

    /// A reset response. We need to retry with a reset query.
    Reset,

    /// An unsupported version error.
    ///
    /// The included value is the version reported by the server as part of
    /// the error PDU, i.e., the version that server wants to use.
    VersionError(u8)
}

impl FirstSerialReply {
    /// Reads the first reply from a socket.
    ///
    /// If any other reply than a cache response or reset response is
    /// received or anything else goes wrong, returns an error.
    async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock
    ) -> Result<Self, io::Error> {
        let header = pdu::Header::read(sock).await?;
        match header.pdu() {
            pdu::CacheResponse::PDU => {
                pdu::CacheResponse::read_payload(
                    header, sock
                ).await.map(FirstSerialReply::Response)
            }
            pdu::CacheReset::PDU => {
                pdu::CacheReset::read_payload(
                    header, sock
                ).await.map(|_| FirstSerialReply::Reset)
            }
            pdu::Error::PDU
                if header.session()
                    == pdu::ErrorCode::UNSUPPORTED_PROTOCOL_VERSION
            => {
                pdu::Error::skip_payload(header, sock).await?;
                Ok(Self::VersionError(header.version()))
            }
            pdu::Error::PDU => {
                Err(io::Error::other(
                    format!("server reported error {}", header.session())
                ))
            }
            pdu => {
                Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("unexpected PDU {pdu}")
                ))
            }
        }
    }
}


//------------ FirstResetReply -----------------------------------------------

/// The first reply from a server in response to a reset query.
enum FirstResetReply {
    /// A cache response. Actual data is to follow.
    Response(pdu::CacheResponse),

    /// An unsupported version error.
    ///
    /// The included value is the version reported by the server as part of
    /// the error PDU, i.e., the version that server wants to use.
    VersionError(u8)
}

impl FirstResetReply {
    /// Reads the first reply from a socket.
    ///
    /// If any other reply than a cache response or reset response is
    /// received or anything else goes wrong, returns an error.
    async fn read<Sock: AsyncRead + Unpin>(
        sock: &mut Sock
    ) -> Result<Self, io::Error> {
        let header = pdu::Header::read(sock).await?;
        match header.pdu() {
            pdu::CacheResponse::PDU => {
                pdu::CacheResponse::read_payload(
                    header, sock
                ).await.map(Self::Response)
            }
            pdu::Error::PDU
                if header.session()
                    == pdu::ErrorCode::UNSUPPORTED_PROTOCOL_VERSION
            => {
                pdu::Error::skip_payload(header, sock).await?;
                Ok(Self::VersionError(header.version()))
            }
            pdu::Error::PDU => {
                Err(io::Error::other(
                    format!("server reported error {}", header.session())
                ))
            }
            pdu => {
                Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("unexpected PDU {pdu}")
                ))
            }
        }
    }
}


//------------ PayloadError --------------------------------------------------

/// A received payload update was not acceptable.
#[derive(Clone, Copy, Debug)]
pub enum PayloadError {
    /// A nonexisting record was withdrawn.
    UnknownWithdraw,

    /// An existing record was announced again.
    DuplicateAnnounce,

    /// The record is corrupt.
    Corrupt,

    /// An internal error in the receiver happend.
    Internal,
}

impl PayloadError {
    /// Returns the RTR error code corresponding to the error reason.
    fn error_code(self) -> u16 {
        match self {
            PayloadError::UnknownWithdraw => 6,
            PayloadError::DuplicateAnnounce => 7,
            PayloadError::Corrupt => 0,
            PayloadError::Internal => 1
        }
    }

    /// Sends the error as a RTR error PDU.
    async fn send(
        self, version: u8, pdu: Option<pdu::Payload>,
        sock: &mut (impl AsyncWrite + Unpin)
    ) -> Result<(), io::Error> {
        match pdu {
            Some(pdu) => {
                pdu::Error::new(
                    version, self.error_code(), pdu.as_partial_slice(), ""
                ).write(sock).await
            }
            None => {
                pdu::Error::new(
                    version, self.error_code(), "", ""
                ).write(sock).await
            }
        }
    }
}

impl fmt::Display for PayloadError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match *self {
            PayloadError::UnknownWithdraw => "withdrawal of non-existing item",
            PayloadError::DuplicateAnnounce => "duplicate announcement",
            PayloadError::Corrupt => "corrup data set",
            PayloadError::Internal => "internal error",
        })
    }
}

impl error::Error for PayloadError { }