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
use byteorder::{BigEndian, ReadBytesExt};
use std::io::{Error, Read};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::Header;
use crate::AFI;

/// Represents a RIB entry of a Routing Information Base.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct TABLE_DUMP {
    /// Identifies the RIB view. Normally set to 0.
    pub view_number: u16,

    /// Sequence number for the RIB entry in the RIB. Will wrap to back to 0.
    pub sequence_number: u16,

    /// The IP address of this RIB entry.
    pub prefix: IpAddr,

    /// The prefix length of this RIB entry.
    pub prefix_length: u8,

    /// Unused and should be set to 1.
    pub status: u8,

    /// Contains the 4-octet time at which this prefix was heard since 1 January 1970 00:00:00 UTC.
    pub originated_time: u32,

    /// IP address of the peer that provided the update for this RIB entry.
    pub peer_address: IpAddr,

    /// ASN of the peer that provided the update for this RIB entry.
    pub peer_as: u16,

    /// The path attributes associated with this route.
    pub attributes: Vec<u8>,
}

#[allow(non_camel_case_types)]
impl TABLE_DUMP {
    ///
    /// # Summary
    /// Used to parse TABLE_DUMP MRT records.
    ///
    /// # Panics
    /// This function does not panic.
    ///
    /// # Errors
    /// Any IO error will be returned while reading from the stream.
    /// If an ill-formatted stream or header is provided behavior will be undefined.
    ///
    /// # Safety
    /// This function does not make use of unsafe code.
    ///
    pub fn parse(header: &Header, stream: &mut Read) -> Result<TABLE_DUMP, Error> {
        let view_number = stream.read_u16::<BigEndian>()?;
        let sequence_number = stream.read_u16::<BigEndian>()?;

        let prefix = match AFI::from(header.sub_type)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };

        let prefix_length = stream.read_u8()?;
        let status = stream.read_u8()?;
        let originated_time = stream.read_u32::<BigEndian>()?;

        let peer_address = match AFI::from(header.sub_type)? {
            AFI::IPV4 => IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?)),
            AFI::IPV6 => IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?)),
        };

        let peer_as = stream.read_u16::<BigEndian>()?;
        let attribute_length = stream.read_u16::<BigEndian>()?;
        let mut attributes = vec![0; attribute_length as usize];
        stream.read_exact(&mut attributes)?;

        Ok(TABLE_DUMP {
            view_number,
            sequence_number,
            prefix,
            prefix_length,
            status,
            originated_time,
            peer_address,
            peer_as,
            attributes,
        })
    }
}

/// Used to store Routing Information Base (RIB) entries.
#[derive(Debug)]
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
pub enum TABLE_DUMP_V2 {
    PEER_INDEX_TABLE(PEER_INDEX_TABLE),
    RIB_IPV4_UNICAST(RIB_AFI),
    RIB_IPV4_MULTICAST(RIB_AFI),
    RIB_IPV6_UNICAST(RIB_AFI),
    RIB_IPV6_MULTICAST(RIB_AFI),
    RIB_GENERIC(RIB_GENERIC),
    RIB_IPV4_UNICAST_ADDPATH(RIB_AFI_ADDPATH),
    RIB_IPV4_MULTICAST_ADDPATH(RIB_AFI_ADDPATH),
    RIB_IPV6_UNICAST_ADDPATH(RIB_AFI_ADDPATH),
    RIB_IPV6_MULTICAST_ADDPATH(RIB_AFI_ADDPATH),
    RIB_GENERIC_ADDPATH(RIB_GENERIC_ADDPATH),
}

/// This record provides the BGP ID of the collector, an optional view name,
/// and a list of indexed peers.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct PEER_INDEX_TABLE {
    /// The identifier of the collector often set to its IPv4 address.
    pub collector_id: u32,

    /// Optional associated view name. Set to the empty string if empty.
    pub view_name: String,

    /// An array of peers from which messages were received.
    pub peer_entries: Vec<PeerEntry>,
}

impl PEER_INDEX_TABLE {
    fn parse(stream: &mut Read) -> Result<PEER_INDEX_TABLE, Error> {
        let collector_id = stream.read_u32::<BigEndian>()?;
        let view_name_length = stream.read_u16::<BigEndian>()?;

        let mut buffer: Vec<u8> = vec![0; view_name_length as usize];
        stream.read_exact(&mut buffer)?;
        let view_name = String::from_utf8_lossy(&buffer).to_string();

        let peer_count = stream.read_u16::<BigEndian>()?;
        let mut peer_entries: Vec<PeerEntry> = Vec::with_capacity(peer_count as usize);
        for _ in 0..peer_count {
            peer_entries.push(PeerEntry::parse(stream)?);
        }

        Ok(PEER_INDEX_TABLE {
            collector_id,
            view_name,
            peer_entries,
        })
    }
}

/// Describes a peer from which BGP messages were received.
#[derive(Debug)]
pub struct PeerEntry {
    /// Special flags in bit 0 and bit 1. Specifying the ASN and IP type.
    pub peer_type: u8,

    /// The BGP identifier of the peer. Often set to its IPv4 address.
    pub peer_bgp_id: u32,

    /// The IP address of the peer.
    pub peer_ip_address: IpAddr,

    /// The ASN of the peer.
    pub peer_as: u32,
}

impl PeerEntry {
    fn parse(stream: &mut Read) -> Result<PeerEntry, Error> {
        let peer_type = stream.read_u8()?;
        let ipv6 = (peer_type & 1) != 0;
        let as_size = (peer_type & 2) != 0;

        let peer_bgp_id = stream.read_u32::<BigEndian>()?;
        let peer_ip_address = if ipv6 {
            IpAddr::V6(Ipv6Addr::from(stream.read_u128::<BigEndian>()?))
        } else {
            IpAddr::V4(Ipv4Addr::from(stream.read_u32::<BigEndian>()?))
        };

        let peer_as = if as_size {
            stream.read_u32::<BigEndian>()?
        } else {
            u32::from(stream.read_u16::<BigEndian>()?)
        };

        Ok(PeerEntry {
            peer_type,
            peer_bgp_id,
            peer_ip_address,
            peer_as,
        })
    }
}

/// Represents a route in the Routing Information Base (RIB)
#[derive(Debug)]
pub struct RIBEntry {
    /// The index of the peer inside the PEER_INDEX_TABLE.
    pub peer_index: u16,

    /// The moment that this route was received.
    pub originated_time: u32,

    /// The BGP Path attributes associated with this route.
    pub attributes: Vec<u8>,
}

impl RIBEntry {
    fn parse(stream: &mut Read) -> Result<RIBEntry, Error> {
        let peer_index = stream.read_u16::<BigEndian>()?;
        let originated_time = stream.read_u32::<BigEndian>()?;
        let attribute_length = stream.read_u16::<BigEndian>()?;

        let mut attributes: Vec<u8> = vec![0; attribute_length as usize];
        stream.read_exact(&mut attributes)?;

        Ok(RIBEntry {
            peer_index,
            originated_time,
            attributes,
        })
    }
}

/// Represents a collection of routes for a specific IP prefix.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct RIB_AFI {
    /// A sequence number that identifies the route collection. Wraps back to zero on overflow.
    pub sequence_number: u32,

    /// The prefix length of the prefix.
    pub prefix_length: u8,

    /// The prefix in bytes rounded up to the nearest byte.
    pub prefix: Vec<u8>,

    /// A collection of routes to this prefix.
    pub entries: Vec<RIBEntry>,
}

impl RIB_AFI {
    fn parse(stream: &mut Read) -> Result<RIB_AFI, Error> {
        let sequence_number = stream.read_u32::<BigEndian>()?;

        let prefix_length: u8 = stream.read_u8()?;
        let length: u8 = (prefix_length + 7) / 8;
        let mut prefix: Vec<u8> = vec![0; length as usize];
        stream.read_exact(&mut prefix)?;

        let entry_count = stream.read_u16::<BigEndian>()?;
        let mut entries: Vec<RIBEntry> = Vec::with_capacity(entry_count as usize);
        for _ in 0..entry_count {
            entries.push(RIBEntry::parse(stream)?);
        }

        Ok(RIB_AFI {
            sequence_number,
            prefix_length,
            prefix,
            entries,
        })
    }
}

/// Represents a collection of routes for a specific IP prefix.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct RIB_GENERIC {
    /// A sequence number that identifies the route collection. Wraps back to zero on overflow.
    pub sequence_number: u32,

    /// The Address Family Identifier (AFI) of this RIB entry.
    pub afi: AFI,

    /// The Subsequent Address Family Identifier (SAFI) of this RIB entry.
    pub safi: u8,

    /// The NLRI in bytes.
    pub nlri: Vec<u8>,

    /// A collection of routes to this prefix.
    pub entries: Vec<RIBEntry>,
}

impl RIB_GENERIC {
    fn parse(stream: &mut Read) -> Result<RIB_GENERIC, Error> {
        let sequence_number = stream.read_u32::<BigEndian>()?;
        let afi = AFI::from(stream.read_u16::<BigEndian>()?)?;
        let safi = stream.read_u8()?;

        let length = match afi {
            AFI::IPV4 => {
                match safi {
                    // MPLS-labeled VPN address
                    128 => (stream.read_u8()? + 7) / 8,

                    // Default to 4.
                    _ => 4,
                }
            }
            AFI::IPV6 => 16,
        };

        let mut nlri: Vec<u8> = vec![0; length as usize];
        stream.read_exact(&mut nlri)?;

        let entry_count = stream.read_u16::<BigEndian>()?;
        let mut entries: Vec<RIBEntry> = Vec::with_capacity(entry_count as usize);
        for _ in 0..entry_count {
            entries.push(RIBEntry::parse(stream)?);
        }

        Ok(RIB_GENERIC {
            sequence_number,
            afi,
            safi,
            nlri,
            entries,
        })
    }
}

/// Represents a route in the Routing Information Base (RIB) allowing multiple paths.
#[derive(Debug)]
pub struct RIBEntryAddPath {
    /// The index of the peer inside the PEER_INDEX_TABLE.
    pub peer_index: u16,

    /// The moment that this route was received.
    pub originated_time: u32,

    /// Identifies the path together with the address prefix.
    pub path_identifier: u32,

    /// The BGP Path attributes associated with this route.
    pub attributes: Vec<u8>,
}

impl RIBEntryAddPath {
    fn parse(stream: &mut Read) -> Result<RIBEntryAddPath, Error> {
        let peer_index = stream.read_u16::<BigEndian>()?;
        let originated_time = stream.read_u32::<BigEndian>()?;
        let path_identifier = stream.read_u32::<BigEndian>()?;
        let attribute_length = stream.read_u16::<BigEndian>()?;
        let mut attributes: Vec<u8> = vec![0; attribute_length as usize];
        stream.read_exact(&mut attributes)?;

        Ok(RIBEntryAddPath {
            peer_index,
            originated_time,
            path_identifier,
            attributes,
        })
    }
}

/// Represents a collection of routes for a specific IP prefix.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct RIB_AFI_ADDPATH {
    /// A sequence number that identifies the route collection. Wraps back to zero on overflow.
    pub sequence_number: u32,

    /// The prefix length of the prefix.
    pub prefix_length: u8,

    /// The prefix in bytes rounded up to the nearest byte.
    pub prefix: Vec<u8>,

    /// A collection of routes to this prefix. Might contain multiple paths from a single peer.
    pub entries: Vec<RIBEntryAddPath>,
}

impl RIB_AFI_ADDPATH {
    fn parse(stream: &mut Read) -> Result<RIB_AFI_ADDPATH, Error> {
        let sequence_number = stream.read_u32::<BigEndian>()?;
        let prefix_length: u8 = stream.read_u8()?;
        let length: u8 = (prefix_length + 7) / 8;
        let mut prefix: Vec<u8> = vec![0; length as usize];
        stream.read_exact(&mut prefix)?;

        let entry_count = stream.read_u16::<BigEndian>()?;
        let mut entries: Vec<RIBEntryAddPath> = Vec::with_capacity(entry_count as usize);
        for _ in 0..entry_count {
            entries.push(RIBEntryAddPath::parse(stream)?);
        }

        Ok(RIB_AFI_ADDPATH {
            sequence_number,
            prefix_length,
            prefix,
            entries,
        })
    }
}

/// Represents a collection of routes for a specific IP prefix.
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct RIB_GENERIC_ADDPATH {
    /// A sequence number that identifies the route collection. Wraps back to zero on overflow.
    pub sequence_number: u32,

    /// The Address Family Identifier (AFI) of this RIB entry.
    pub afi: AFI,

    /// The Subsequent Address Family Identifier (SAFI) of this RIB entry.
    pub safi: u8,

    /// The NLRI in bytes.
    pub nlri: Vec<u8>,

    /// A collection of routes to this prefix.
    pub entries: Vec<RIBEntryAddPath>,
}

impl RIB_GENERIC_ADDPATH {
    fn parse(stream: &mut Read) -> Result<RIB_GENERIC_ADDPATH, Error> {
        let sequence_number = stream.read_u32::<BigEndian>()?;
        let afi = AFI::from(stream.read_u16::<BigEndian>()?)?;
        let safi = stream.read_u8()?;

        let length = match afi {
            AFI::IPV4 => {
                match safi {
                    // MPLS-labeled VPN address
                    128 => (stream.read_u8()? + 7) / 8,

                    // Default to 4.
                    _ => 4,
                }
            }
            AFI::IPV6 => 16,
        };

        let mut nlri: Vec<u8> = vec![0; length as usize];
        stream.read_exact(&mut nlri)?;

        let entry_count = stream.read_u16::<BigEndian>()?;
        let mut entries: Vec<RIBEntryAddPath> = Vec::with_capacity(entry_count as usize);
        for _ in 0..entry_count {
            entries.push(RIBEntryAddPath::parse(stream)?);
        }

        Ok(RIB_GENERIC_ADDPATH {
            sequence_number,
            afi,
            safi,
            nlri,
            entries,
        })
    }
}

#[allow(non_camel_case_types)]
impl TABLE_DUMP_V2 {
    ///
    /// # Summary
    /// Used to parse TABLE_DUMP_V2 MRT records.
    ///
    /// # Panics
    /// This function does not panic.
    ///
    /// # Errors
    /// Any IO error will be returned while reading from the stream.
    /// If an ill-formatted stream or header is provided behavior will be undefined.
    ///
    /// # Safety
    /// This function does not make use of unsafe code.
    ///
    pub fn parse(header: &Header, stream: &mut Read) -> Result<TABLE_DUMP_V2, Error> {
        match header.sub_type {
            1 => Ok(TABLE_DUMP_V2::PEER_INDEX_TABLE(PEER_INDEX_TABLE::parse(
                stream,
            )?)),
            2 => Ok(TABLE_DUMP_V2::RIB_IPV4_UNICAST(RIB_AFI::parse(stream)?)),
            3 => Ok(TABLE_DUMP_V2::RIB_IPV4_MULTICAST(RIB_AFI::parse(stream)?)),
            4 => Ok(TABLE_DUMP_V2::RIB_IPV6_UNICAST(RIB_AFI::parse(stream)?)),
            5 => Ok(TABLE_DUMP_V2::RIB_IPV6_MULTICAST(RIB_AFI::parse(stream)?)),
            6 => Ok(TABLE_DUMP_V2::RIB_GENERIC(RIB_GENERIC::parse(stream)?)),
            8 => Ok(TABLE_DUMP_V2::RIB_IPV4_UNICAST_ADDPATH(
                RIB_AFI_ADDPATH::parse(stream)?,
            )),
            9 => Ok(TABLE_DUMP_V2::RIB_IPV4_MULTICAST_ADDPATH(
                RIB_AFI_ADDPATH::parse(stream)?,
            )),
            10 => Ok(TABLE_DUMP_V2::RIB_IPV6_UNICAST_ADDPATH(
                RIB_AFI_ADDPATH::parse(stream)?,
            )),
            11 => Ok(TABLE_DUMP_V2::RIB_IPV6_MULTICAST_ADDPATH(
                RIB_AFI_ADDPATH::parse(stream)?,
            )),
            12 => Ok(TABLE_DUMP_V2::RIB_GENERIC_ADDPATH(
                RIB_GENERIC_ADDPATH::parse(stream)?,
            )),
            _ => {
                let msg = format!(
                    "{} is not a valid sub-type of Tabledump v2",
                    header.sub_type
                );
                Err(std::io::Error::new(std::io::ErrorKind::Other, msg))
            }
        }
    }
}