mrt_ingester 0.2.0

High-performance parser for MRT (Multi-threaded Routing Toolkit) routing data files
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
# mrt-rs API/ABI reference

This document describes the public API surface of `mrt-rs` as it exists in this repository.
The goal is to help implement a compatible library with the same API/ABI behavior.

Notes:
- The crate is Rust-native; ABI stability is **not** guaranteed for most types because they do not
  use `repr(C)` (except `AFI`, which is `repr(u16)`). If you need ABI compatibility, match
  field order and Rust ABI expectations exactly for the same compiler/version.
- All parsing functions read from a stream and return `std::io::Result<T>`; they do not use unsafe
  code and generally return `ErrorKind::Other` or `ErrorKind::InvalidData` on invalid input.

## ABI compatibility checklist

- Pin the Rust toolchain version and target triple to match your intended consumers.
- Keep all public type names, modules, and re-exports identical (paths matter for Rust users).
- Match enum variant order and struct field order exactly as declared here.
- Do not add `repr(C)` unless you also match the original layout decisions; it changes layout.
- Preserve integer sizes (`u16`, `u32`, `u8`) and `std::net` address types exactly.
- Use the same error kinds and return types (`std::io::Result`, `Option`).
- Avoid feature flags that alter public signatures or type layouts unless you keep defaults identical.
- If you expose a C ABI, put it behind separate `extern "C"` wrappers so the Rust API stays intact.

## Minimal compatible crate skeleton (example)

This skeleton shows the public shape only; fill in parsing logic to match behavior.

```
compat-mrt/
  Cargo.toml
  src/
    lib.rs
    records/
      bgp.rs
      bgp4plus.rs
      bgp4mp.rs
      isis.rs
      ospf.rs
      rip.rs
      tabledump.rs
```

`Cargo.toml`:
```toml
[package]
name = "mrt_rs"
version = "0.0.0"
edition = "2018"

[dependencies]
byteorder = "1"
```

`src/lib.rs` (public API surface only):
```rust
#![deny(missing_docs)]

use std::io::{Error, Read};

pub mod records {
    pub mod bgp;
    pub mod bgp4plus;
    pub mod bgp4mp;
    pub mod isis;
    pub mod ospf;
    pub mod rip;
    pub mod tabledump;
}

pub use records::bgp;
pub use records::bgp4mp;
pub use records::bgp4plus;
pub use records::isis;
pub use records::ospf;
pub use records::rip;
pub use records::tabledump;

#[derive(Debug)]
#[repr(u16)]
pub enum AFI {
    IPV4 = 1,
    IPV6 = 2,
}

impl AFI {
    pub fn size(&self) -> u32 {
        match self {
            AFI::IPV4 => 4,
            AFI::IPV6 => 16,
        }
    }
}

#[derive(Debug)]
pub struct Header {
    pub timestamp: u32,
    pub extended: u32,
    pub record_type: u16,
    pub sub_type: u16,
    pub length: u32,
}

#[derive(Debug)]
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
pub enum Record {
    NULL,
    START,
    DIE,
    I_AM_DEAD,
    PEER_DOWN,
    BGP(records::bgp::BGP),
    RIP(records::rip::RIP),
    IDRP,
    RIPNG(records::rip::RIPNG),
    BGP4PLUS(records::bgp4plus::BGP4PLUS),
    BGP4PLUS_01(records::bgp4plus::BGP4PLUS),
    OSPFv2(records::ospf::OSPFv2),
    TABLE_DUMP(records::tabledump::TABLE_DUMP),
    TABLE_DUMP_V2(records::tabledump::TABLE_DUMP_V2),
    BGP4MP(records::bgp4mp::BGP4MP),
    BGP4MP_ET(records::bgp4mp::BGP4MP),
    ISIS(Vec<u8>),
    ISIS_ET(Vec<u8>),
    OSPFv3(records::ospf::OSPFv3),
    OSPFv3_ET(records::ospf::OSPFv3),
}

pub fn read(_stream: &mut impl Read) -> Result<Option<(Header, Record)>, Error> {
    unimplemented!("parse MRT records and return Header + Record")
}
```

## Crate root: `mrt_rs`

### Modules (public)
- `mrt_rs::records`
  - `mrt_rs::records::bgp`
  - `mrt_rs::records::bgp4plus`
  - `mrt_rs::records::bgp4mp`
  - `mrt_rs::records::isis`
  - `mrt_rs::records::ospf`
  - `mrt_rs::records::rip`
  - `mrt_rs::records::tabledump`

The following modules are re-exported at the crate root for convenience:
- `mrt_rs::bgp`
- `mrt_rs::bgp4plus`
- `mrt_rs::bgp4mp`
- `mrt_rs::isis`
- `mrt_rs::ospf`
- `mrt_rs::rip`
- `mrt_rs::tabledump`

### Enums and structs

#### `enum AFI` (`repr(u16)`)
Represents Address Family Identifier.
- Variants:
  - `AFI::IPV4 = 1`
  - `AFI::IPV6 = 2`
- Methods:
  - `pub fn size(&self) -> u32`
    - Returns 4 for `IPV4`, 16 for `IPV6`.

#### `struct Header`
Represents the MRT header that precedes every record.
- Fields:
  - `pub timestamp: u32` (UNIX seconds)
  - `pub extended: u32` (microsecond resolution, 0 if not present)
  - `pub record_type: u16`
  - `pub sub_type: u16`
  - `pub length: u32` (payload length, excluding header)

#### `enum Record`
Represents a fully-parsed MRT record.
- Variants:
  - `NULL`
  - `START`
  - `DIE`
  - `I_AM_DEAD`
  - `PEER_DOWN`
  - `BGP(mrt_rs::records::bgp::BGP)`
  - `RIP(mrt_rs::records::rip::RIP)`
  - `IDRP`
  - `RIPNG(mrt_rs::records::rip::RIPNG)`
  - `BGP4PLUS(mrt_rs::records::bgp4plus::BGP4PLUS)`
  - `BGP4PLUS_01(mrt_rs::records::bgp4plus::BGP4PLUS)`
  - `OSPFv2(mrt_rs::records::ospf::OSPFv2)`
  - `TABLE_DUMP(mrt_rs::records::tabledump::TABLE_DUMP)`
  - `TABLE_DUMP_V2(mrt_rs::records::tabledump::TABLE_DUMP_V2)`
  - `BGP4MP(mrt_rs::records::bgp4mp::BGP4MP)`
  - `BGP4MP_ET(mrt_rs::records::bgp4mp::BGP4MP)`
  - `ISIS(Vec<u8>)`
  - `ISIS_ET(Vec<u8>)`
  - `OSPFv3(mrt_rs::records::ospf::OSPFv3)`
  - `OSPFv3_ET(mrt_rs::records::ospf::OSPFv3)`

### Functions

#### `pub fn read(stream: &mut impl std::io::Read) -> std::io::Result<Option<(Header, Record)>>`
Reads the next MRT record from the stream.
- Returns `Ok(None)` on EOF at the beginning of a record.
- Returns `Ok(Some((header, record)))` on success.
- For unknown/unsupported types it returns an `std::io::Error`.

## Module: `mrt_rs::records::bgp`

### `enum BGP`
- Variants:
  - `NULL`
  - `UPDATE(MESSAGE)`
  - `PREF_UPDATE`
  - `STATE_CHANGE(STATE_CHANGE)`
  - `SYNC(SYNC)`
  - `OPEN(MESSAGE)`
  - `NOTIFY(MESSAGE)`
  - `KEEPALIVE(MESSAGE)`

### `struct MESSAGE`
Represents BGP UPDATE/OPEN/NOTIFY/KEEPALIVE for IPv4 peers (deprecated MRT type).
- Fields:
  - `pub peer_as: u16`
  - `pub peer_ip: std::net::Ipv4Addr`
  - `pub local_as: u16`
  - `pub local_ip: std::net::Ipv4Addr`
  - `pub message: Vec<u8>`

### `struct STATE_CHANGE`
- Fields:
  - `pub peer_as: u16`
  - `pub peer_ip: std::net::Ipv4Addr`
  - `pub old_state: u16`
  - `pub new_state: u16`

### `struct SYNC`
Deprecated RIB recording pointer.
- Fields:
  - `pub view_number: u16`
  - `pub filename: Vec<u8>` (NULL-terminated bytes in the file format)

## Module: `mrt_rs::records::bgp4plus`

### `enum BGP4PLUS`
- Variants:
  - `NULL`
  - `UPDATE(MESSAGE)`
  - `PREF_UPDATE`
  - `STATE_CHANGE(STATE_CHANGE)`
  - `SYNC(SYNC)`
  - `OPEN(MESSAGE)`
  - `NOTIFY(MESSAGE)`
  - `KEEPALIVE(MESSAGE)`

### `struct MESSAGE`
Represents BGP UPDATE/OPEN/NOTIFY/KEEPALIVE for IPv6 peers (deprecated MRT type).
- Fields:
  - `pub peer_as: u16`
  - `pub peer_ip: std::net::Ipv6Addr`
  - `pub local_as: u16`
  - `pub local_ip: std::net::Ipv6Addr`
  - `pub message: Vec<u8>`

### `struct STATE_CHANGE`
- Fields:
  - `pub peer_as: u16`
  - `pub peer_ip: std::net::Ipv6Addr`
  - `pub old_state: u16`
  - `pub new_state: u16`

### `struct SYNC`
Deprecated RIB recording pointer.
- Fields:
  - `pub view_number: u16`
  - `pub filename: Vec<u8>` (NULL-terminated bytes in the file format)

## Module: `mrt_rs::records::bgp4mp`

### `enum BGP4MP`
- Variants:
  - `STATE_CHANGE(STATE_CHANGE)`
  - `MESSAGE(MESSAGE)`
  - `ENTRY(ENTRY)`
  - `SNAPSHOT(SNAPSHOT)`
  - `MESSAGE_AS4(MESSAGE_AS4)`
  - `STATE_CHANGE_AS4(STATE_CHANGE_AS4)`
  - `MESSAGE_LOCAL(MESSAGE)`
  - `MESSAGE_AS4_LOCAL(MESSAGE_AS4)`
  - `MESSAGE_ADDPATH(MESSAGE)`
  - `MESSAGE_AS4_ADDPATH(MESSAGE_AS4)`
  - `MESSAGE_LOCAL_ADDPATH(MESSAGE)`
  - `MESSAGE_AS4_LOCAL_ADDPATH(MESSAGE_AS4)`

### `struct STATE_CHANGE`
16-bit ASN state change.
- Fields:
  - `pub peer_as: u16`
  - `pub local_as: u16`
  - `pub interface: u16`
  - `pub peer_address: std::net::IpAddr`
  - `pub local_address: std::net::IpAddr`
  - `pub old_state: u16`
  - `pub new_state: u16`

### `struct MESSAGE`
16-bit ASN BGP message.
- Fields:
  - `pub peer_as: u16`
  - `pub local_as: u16`
  - `pub interface: u16`
  - `pub peer_address: std::net::IpAddr`
  - `pub local_address: std::net::IpAddr`
  - `pub message: Vec<u8>`

### `struct MESSAGE_AS4`
32-bit ASN BGP message.
- Fields:
  - `pub peer_as: u32`
  - `pub local_as: u32`
  - `pub interface: u16`
  - `pub peer_address: std::net::IpAddr`
  - `pub local_address: std::net::IpAddr`
  - `pub message: Vec<u8>`

### `struct STATE_CHANGE_AS4`
32-bit ASN state change.
- Fields:
  - `pub peer_as: u32`
  - `pub local_as: u32`
  - `pub interface: u16`
  - `pub peer_address: std::net::IpAddr`
  - `pub local_address: std::net::IpAddr`
  - `pub old_state: u16`
  - `pub new_state: u16`

### `struct SNAPSHOT`
Deprecated snapshot pointer.
- Fields:
  - `pub view_number: u16`
  - `pub filename: Vec<u8>` (NULL-terminated bytes in the file format)

### `struct ENTRY`
Deprecated RIB entry format.
- Fields:
  - `pub peer_as: u16`
  - `pub local_as: u16`
  - `pub interface: u16`
  - `pub peer_address: std::net::IpAddr`
  - `pub local_address: std::net::IpAddr`
  - `pub view_number: u16`
  - `pub status: u16`
  - `pub time_last_change: u32`
  - `pub next_hop: std::net::IpAddr`
  - `pub afi: u16`
  - `pub safi: u8`
  - `pub prefix_length: u8`
  - `pub prefix: Vec<u8>`
  - `pub attributes: Vec<u8>`

## Module: `mrt_rs::records::tabledump`

### `struct TABLE_DUMP`
Represents a TABLE_DUMP record (RIB entry).
- Fields:
  - `pub view_number: u16`
  - `pub sequence_number: u16`
  - `pub prefix: std::net::IpAddr`
  - `pub prefix_length: u8`
  - `pub status: u8`
  - `pub originated_time: u32`
  - `pub peer_address: std::net::IpAddr`
  - `pub peer_as: u16`
  - `pub attributes: Vec<u8>`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<TABLE_DUMP>`

### `enum TABLE_DUMP_V2`
Represents TABLE_DUMP_V2 and RFC8050 Add-Path variants.
- Variants:
  - `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)`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<TABLE_DUMP_V2>`

### `struct PEER_INDEX_TABLE`
- Fields:
  - `pub collector_id: u32`
  - `pub view_name: String`
  - `pub peer_entries: Vec<PeerEntry>`

### `struct PeerEntry`
- Fields:
  - `pub peer_type: u8`
  - `pub peer_bgp_id: u32`
  - `pub peer_ip_address: std::net::IpAddr`
  - `pub peer_as: u32`

### `struct RIBEntry`
- Fields:
  - `pub peer_index: u16`
  - `pub originated_time: u32`
  - `pub attributes: Vec<u8>`

### `struct RIB_AFI`
- Fields:
  - `pub sequence_number: u32`
  - `pub prefix_length: u8`
  - `pub prefix: Vec<u8>`
  - `pub entries: Vec<RIBEntry>`

### `struct RIB_GENERIC`
- Fields:
  - `pub sequence_number: u32`
  - `pub afi: AFI`
  - `pub safi: u8`
  - `pub nlri: Vec<u8>`
  - `pub entries: Vec<RIBEntry>`

### `struct RIBEntryAddPath`
- Fields:
  - `pub peer_index: u16`
  - `pub originated_time: u32`
  - `pub path_identifier: u32`
  - `pub attributes: Vec<u8>`

### `struct RIB_AFI_ADDPATH`
- Fields:
  - `pub sequence_number: u32`
  - `pub prefix_length: u8`
  - `pub prefix: Vec<u8>`
  - `pub entries: Vec<RIBEntryAddPath>`

### `struct RIB_GENERIC_ADDPATH`
- Fields:
  - `pub sequence_number: u32`
  - `pub afi: AFI`
  - `pub safi: u8`
  - `pub nlri: Vec<u8>`
  - `pub entries: Vec<RIBEntryAddPath>`

## Module: `mrt_rs::records::rip`

### `struct RIP`
- Fields:
  - `pub remote: std::net::Ipv4Addr`
  - `pub local: std::net::Ipv4Addr`
  - `pub message: Vec<u8>`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<RIP>`

### `struct RIPNG`
- Fields:
  - `pub remote: std::net::Ipv6Addr`
  - `pub local: std::net::Ipv6Addr`
  - `pub message: Vec<u8>`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<RIPNG>`

## Module: `mrt_rs::records::ospf`

### `struct OSPFv2`
- Fields:
  - `pub remote: std::net::Ipv4Addr`
  - `pub local: std::net::Ipv4Addr`
  - `pub message: Vec<u8>`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<OSPFv2>`

### `struct OSPFv3`
- Fields:
  - `pub remote: std::net::IpAddr`
  - `pub local: std::net::IpAddr`
  - `pub message: Vec<u8>`
- Methods:
  - `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<OSPFv3>`

## Module: `mrt_rs::records::isis`

### `fn parse`
- Signature: `pub fn parse(header: &Header, stream: impl std::io::Read) -> std::io::Result<Vec<u8>>`
- Behavior: reads exactly `header.length` bytes and returns them as a `Vec<u8>`.

## Behavior notes

- `read` interprets `record_type` values as:
  - 0 NULL, 1 START, 2 DIE, 3 I_AM_DEAD, 4 PEER_DOWN,
  - 5 BGP, 6 RIP, 7 IDRP, 8 RIPNG, 9 BGP4PLUS, 10 BGP4PLUS_01,
  - 11 OSPFv2, 12 TABLE_DUMP, 13 TABLE_DUMP_V2, 16 BGP4MP, 17 BGP4MP_ET,
  - 32 ISIS, 33 ISIS_ET, 48 OSPFv3, 49 OSPFv3_ET.
- `Header.extended` is set only for the `*_ET` record types: `BGP4MP_ET` (17), `ISIS_ET` (33),
  and `OSPFv3_ET` (49). For all other record types it remains 0.
- All parsing is big-endian and consumes exactly `header.length` bytes for the record body
  (except where noted by record format definition).