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
use std::borrow::Cow;
use std::fmt;

use crate::addr::Prefix;
use crate::bgp::{BgpNlriMeta, ExampleBgpPathAttributes};
use crate::record::{LogicalTime, MessageRecord, Record, SenderIdInt};

/// Network Layer Reachability Information (NLRI)
///
/// NLRI can be pretty generic, it's not only a bunch of prefixes, it can
/// also be FlowSpec data etc.
pub trait Nlri
where
    Self: Clone,
{
}

//----------------- Route ---------------------------------------------------

/// Record based on a RFC 4271 Route.
///
/// Route is a record that holds a route as described in RFC 4271, which is a
/// NRLI used as a key and a set of (path) attributes.
#[derive(Clone)]
pub struct Route<'a, Nlri, Meta>
where
    Nlri: crate::bgp::Nlri,
    Meta: crate::record::Meta,
{
    sender_id: SenderIdInt,
    key: (LogicalTime, SenderIdInt),
    nlri: Nlri,
    attributes: Cow<'a, Meta>,
    ltime: LogicalTime,
}

impl<'a> Record<'a> for Route<'a, PrefixNlri, ExampleBgpPathAttributes> {
    type Meta = BgpNlriMeta<'a>;
    type Key = (LogicalTime, SenderIdInt);

    fn key(&'a self) -> <Self as Record<'a>>::Key {
        (self.ltime, self.sender_id)
    }

    fn meta(&'a self) -> Cow<BgpNlriMeta<'a>> {
        Cow::Owned(BgpNlriMeta {
            attributes: Cow::Borrowed(&self.attributes),
            nlri: self.nlri.clone(),
        })
    }

    fn new(key: Self::Key, meta: &'a Self::Meta) -> Self {
        Route {
            sender_id: key.1,
            key,
            nlri: meta.nlri.clone(),
            attributes: Cow::Borrowed(&meta.attributes),
            ltime: key.0,
        }
    }

    fn new_with_local_meta(key: Self::Key, local_meta: Self::Meta) -> Self {
        Route {
            sender_id: key.1,
            key,
            nlri: local_meta.nlri,
            attributes: local_meta.attributes,
            ltime: key.0,
        }
    }
}

impl<'a> MessageRecord<'a>
    for Route<'a, PrefixNlri, ExampleBgpPathAttributes>
{
    type SenderId = SenderIdInt;

    fn new(
        key: <Self as Record<'a>>::Key,
        meta: <Self as Record<'a>>::Meta,
        sender_id: Self::SenderId,
        ltime: u64,
    ) -> Self {
        Route {
            sender_id,
            key,
            nlri: meta.nlri,
            attributes: meta.attributes,
            ltime,
        }
    }

    fn sender_id(&self) -> Self::SenderId {
        self.sender_id
    }

    fn ltime(&self) -> LogicalTime {
        self.ltime
    }

    fn set_ltime(&mut self, ltime: LogicalTime) -> LogicalTime {
        self.ltime = ltime;
        ltime
    }

    fn timestamp(&self) -> LogicalTime {
        self.ltime
    }

    fn new_from_record(
        record: Self,
        sender_id: Self::SenderId,
        ltime: u64,
    ) -> Self {
        Self {
            key: record.key,
            nlri: record.nlri,
            attributes: record.attributes,
            sender_id,
            ltime,
        }
    }

    fn into_message(mut self, sender_id: SenderIdInt, ltime: u64) -> Self {
        self.sender_id = sender_id;
        self.ltime = ltime;
        self
    }
}

/// NLRI that consists of multiple prefixes.
#[derive(Clone, Debug)]
pub struct PrefixNlri {
    nlri: Vec<Prefix>,
}

impl fmt::Display for PrefixNlri {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "PrefixNlri")
    }
}

impl Nlri for PrefixNlri {}