birdc 0.4.2

Library to talk to the BIRD BGP server for administrative and instrumentation purposes
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
use crate::Message;

/// A network interface, as seen by Bird
#[derive(Debug)]
pub struct Interface {
    pub name: String,
    pub is_up: bool,
    pub index: u32,
    pub master: Option<String>,
}

impl Interface {
    /// Parse the response of a 1001 response. Returns None if `message` isn't a
    /// [Message::InterfaceList], or if we encounter an unrecoverable error during
    /// parsing.
    ///
    /// Details [here](https://gitlab.nic.cz/labs/bird/-/blob/master/nest/iface.c)
    pub fn from_enum(message: &Message) -> Option<Self> {
        if let Message::InterfaceList(content) = message {
            let mut it = content.split_ascii_whitespace();

            // parse name - we eat up any failures
            let name = if let Some(s) = it.next() {
                s
            } else {
                log::error!("ifc: unable to determine name in {content}");
                return None;
            };

            // parse state - we eat up any failures
            let is_up = if let Some(s) = it.next() {
                match s {
                    "up" => true,
                    "down" => false,
                    _ => {
                        log::error!("ifc: unknown state {s}");
                        return None;
                    }
                }
            } else {
                log::error!("ifc: unable to determine state in {content}");
                return None;
            };

            let mut index = -1_i32;
            let mut master: Option<String> = None;
            // parse things inside the brackets
            for s in it {
                let s = s.trim_matches(|c: char| c == '(' || c == ')' || c == ' ');
                if let Some(_idx) = s.strip_prefix("index=") {
                    index = _idx.parse().unwrap_or(-1);
                } else if let Some(ms) = s.strip_prefix("master=") {
                    master = Some(ms.to_owned());
                }
            }
            if index < 0 {
                log::error!("ifc: did not find an appropriate index in {content}");
                return None;
            }

            Some(Self {
                name: name.into(),
                is_up,
                index: index as u32,
                master,
            })
        } else {
            log::error!("ifc: invoked Interface::from_enum on wrong message");
            None
        }
    }
}

/// Properties of an interface (flags, MTU) as seen by Bird
#[derive(Debug)]
pub struct InterfaceProperties {
    pub iftype: InterfaceType,
    flags: u32,
    pub mtu: u32,
}

impl InterfaceProperties {
    /// Parse the response of a 1004 response. Returns None if `message` isn't a
    /// [Message::InterfaceFlags], or if we encounter an unrecoverable error during
    /// parsing.
    ///
    /// Details [here](https://gitlab.nic.cz/labs/bird/-/blob/master/nest/iface.c)
    pub fn from_enum(message: &Message) -> Option<Self> {
        if let Message::InterfaceFlags(content) = message {
            let mut it = content.split_ascii_whitespace();
            let mut flags = 0_u32;
            let mut mtu = 0;

            let iftype = if let Some(s) = it.next() {
                match s {
                    "PtP" => InterfaceType::PointToPoint,
                    "MultiAccess" => InterfaceType::MultiAccess,
                    _ => InterfaceType::Unknown(s.to_owned()),
                }
            } else {
                log::error!("ifc: did not find any iftype in {content}");
                return None;
            };
            for token in content.split_ascii_whitespace() {
                if let Some(_mtu) = token.strip_prefix("MTU=") {
                    if let Ok(m) = _mtu.parse::<u32>() {
                        mtu = m;
                    } else {
                        log::error!("ifc: found invalid mtu in line {content}");
                        return None;
                    }
                } else {
                    match token {
                        "Broadcast" => flags |= IF_FLAG_BROADCAST,
                        "Multicast" => flags |= IF_FLAG_MULTICAST,
                        "AdminUp" => flags |= IF_FLAG_ADMIN_UP,
                        "AdminDown" => flags &= !IF_FLAG_ADMIN_UP,
                        "LinkUp" => flags |= IF_FLAG_LINK_UP,
                        "LinkDown" => flags &= !IF_FLAG_LINK_UP,
                        "Loopback" => flags |= IF_FLAG_LOOPBACK,
                        "Ignored" => flags |= IF_FLAG_IGNORED,
                        _ => {}
                    }
                }
            }

            if mtu == 0 {
                log::error!("ifc: did not find any iftype in {content}");
            }

            Some(InterfaceProperties { iftype, flags, mtu })
        } else {
            log::error!("ifc: invoked InterfaceProperties::from_enum on wrong message");
            None
        }
    }

    /// Interface has broadcast address set
    #[inline]
    pub fn is_broadcast_set(&self) -> bool {
        (self.flags & IF_FLAG_BROADCAST) != 0
    }

    /// Interface supports multicast
    #[inline]
    pub fn is_multicast_set(&self) -> bool {
        (self.flags & IF_FLAG_MULTICAST) != 0
    }

    /// Interface is up & running
    #[inline]
    pub fn is_admin_up(&self) -> bool {
        (self.flags & IF_FLAG_ADMIN_UP) != 0
    }

    /// Interface has its lower link up
    #[inline]
    pub fn is_link_up(&self) -> bool {
        (self.flags & IF_FLAG_LINK_UP) != 0
    }

    /// Interface is a loopback device
    #[inline]
    pub fn is_loopback(&self) -> bool {
        (self.flags & IF_FLAG_LOOPBACK) != 0
    }

    /// Interface is ignored by routing protocols
    #[inline]
    pub fn is_ignored_for_routing(&self) -> bool {
        (self.flags & IF_FLAG_IGNORED) != 0
    }
}

/// Type of interface
#[derive(Debug, PartialEq, Eq)]
pub enum InterfaceType {
    PointToPoint,
    MultiAccess,
    Unknown(String),
}

/// IP addresses assigned to an [Interface]
#[derive(Debug)]
pub struct InterfaceAddress {
    /// IP address, in address/prefix format
    pub ip: String,
    pub scope: String,
    /// Any extra information
    pub extra_info: Option<String>,
}

impl InterfaceAddress {
    /// Parse the response of a 1003 response. Returns None if `message` isn't a
    /// [Message::InterfaceAddress], or if we encounter an unrecoverable error during
    /// parsing.
    ///
    /// Details [here](https://gitlab.nic.cz/labs/bird/-/blob/master/nest/iface.c)
    pub fn from_enum(message: &Message) -> Option<Vec<Self>> {
        let mut addresses = vec![];
        if let Message::InterfaceAddress(content) = message {
            for line in content.lines() {
                let mut it = line.split_ascii_whitespace();

                let mut scope = "undef";
                let mut extras = String::with_capacity(32);
                // process ip address and prefix length
                let ip = if let Some(s) = it.next() {
                    s
                } else {
                    log::error!("ifc: failed to find ip address in {line}");
                    return None;
                };

                // process scope and extra info
                let bc = |c| c == '(' || c == ')' || c == ' ';
                while let Some(mut s) = it.next() {
                    s = s.trim_matches(bc);
                    if s == "scope" {
                        if let Some(sc) = it.next() {
                            scope = sc.trim_matches(bc).trim_matches(',');
                        } else {
                            log::error!("ifc: encountered scope but not value in {line}");
                            return None;
                        }
                    } else {
                        if !extras.is_empty() {
                            extras.push(' ');
                        }
                        extras.push_str(s);
                    }
                }

                if !extras.is_empty() {
                    extras = extras.trim_matches(',').into();
                }

                addresses.push(InterfaceAddress {
                    ip: ip.into(),
                    scope: scope.into(),
                    extra_info: if extras.is_empty() {
                        None
                    } else {
                        Some(extras)
                    },
                })
            }

            Some(addresses)
        } else {
            log::error!("ifc: invoked InterfaceAddress::from_enum on wrong message");
            None
        }
    }
}

pub struct InterfaceSummary {
    pub name: String,
    pub state: String,
    pub ipv4_address: Option<String>,
    pub ipv6_address: Option<String>,
}

impl InterfaceSummary {
    /// Parse the response of a 1005 response. Returns None if `message` isn't a
    /// [Message::InterfaceAddress], or if we encounter an unrecoverable error during
    /// parsing.
    ///
    /// Details [here](https://gitlab.nic.cz/labs/bird/-/blob/master/nest/iface.c)
    pub fn from_enum(message: &Message) -> Option<Vec<Self>> {
        if let Message::InterfaceSummary(content) = message {
            let mut entries: Vec<Self> = vec![];
            for line in content.lines() {
                let mut it = line.split_ascii_whitespace();
                let name: String = it.next()?.into();
                let state: String = it.next()?.into();
                let mut ipv4_address = None;
                let mut ipv6_address = None;

                for addr in it {
                    if addr.contains(':') {
                        ipv6_address = Some(addr.to_owned());
                    } else {
                        ipv4_address = Some(addr.to_owned());
                    }
                }

                entries.push(InterfaceSummary {
                    name,
                    state,
                    ipv4_address,
                    ipv6_address,
                })
            }
            Some(entries)
        } else {
            log::error!("ifc: invoked InterfaceSummary::from_enum on wrong message");
            None
        }
    }
}

/// Valid broadcast address set
const IF_FLAG_BROADCAST: u32 = 1 << 2;
/// Supports multicast
const IF_FLAG_MULTICAST: u32 = 1 << 3;
/// Is a loopback device
const IF_FLAG_LOOPBACK: u32 = 1 << 5;
/// Not to be used by routing protocols (loopbacks etc.)
const IF_FLAG_IGNORED: u32 = 1 << 6;
/// Interface is running
const IF_FLAG_ADMIN_UP: u32 = 1 << 7;
/// L1 layer is up
const IF_FLAG_LINK_UP: u32 = 1 << 8;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Message;

    #[test]
    #[ignore]
    fn test_invalid() {
        let _ = env_logger::try_init();
        assert!(
            Interface::from_enum(&Message::Ok).is_none(),
            "expected None from parsing invalid message type",
        );
    }

    #[test]
    fn test_interface_parsing_without_master() {
        let _ = env_logger::try_init();
        let message = Message::InterfaceList("eth0 up (index=2)".into());
        let ifc = Interface::from_enum(&message).expect("failed to parse");
        assert_eq!(ifc.name, "eth0");
        assert!(ifc.is_up);
        assert_eq!(ifc.index, 2);
        assert!(ifc.master.is_none(), "was not expecting master");
    }

    #[test]
    fn test_interface_parsing_with_master() {
        let _ = env_logger::try_init();
        let message = Message::InterfaceList("eth1 down (index=3 master=#2)".into());
        let ifc = Interface::from_enum(&message).expect("failed to parse");
        assert_eq!(ifc.name, "eth1");
        assert!(!ifc.is_up);
        assert_eq!(ifc.index, 3);
        assert_eq!(ifc.master.expect("was expecting master"), "#2");
    }

    #[test]
    fn test_interface_properties() {
        let _ = env_logger::try_init();
        let message = Message::InterfaceFlags(
            "MultiAccess Broadcast Multicast AdminDown LinkUp MTU=9000".into(),
        );
        let props = InterfaceProperties::from_enum(&message).expect("failed to parse");
        assert_eq!(props.iftype, InterfaceType::MultiAccess);
        assert_eq!(props.mtu, 9000);
        assert!(props.is_broadcast_set());
        assert!(props.is_multicast_set());
        assert!(!props.is_admin_up());
        assert!(props.is_link_up());
    }

    #[test]
    fn test_interface_address() {
        let _ = env_logger::try_init();
        let content = "\t172.30.0.12/16 (Preferred, scope site)\n\t172.29.1.15/32 (scope univ)\n\t172.29.1.16/32 (scope univ)\n\t172.29.1.17/32 (scope univ)\n\tfe80::4495:80ff:fe71:a791/64 (Preferred, scope link)\n\tfe80::4490::72/64 (scope univ)";
        let message = Message::InterfaceAddress(content.into());
        let addresses = InterfaceAddress::from_enum(&message).expect("failed to parse");
        validate_address(&addresses[0], "172.30.0.12/16", "site", "Preferred");
        validate_address(&addresses[1], "172.29.1.15/32", "univ", "");
        validate_address(&addresses[2], "172.29.1.16/32", "univ", "");
        validate_address(&addresses[3], "172.29.1.17/32", "univ", "");
        validate_address(
            &addresses[4],
            "fe80::4495:80ff:fe71:a791/64",
            "link",
            "Preferred",
        );
        validate_address(&addresses[5], "fe80::4490::72/64", "univ", "");
    }

    #[test]
    fn test_interface_summary() {
        let _ = env_logger::try_init();
        let content = "lo         up     127.0.0.1/8        ::1/128\neth0       up     172.30.0.12/16     fe80::4495:80ff:fe71:a791/64\neth1       up     169.254.199.2/30";
        let message = Message::InterfaceSummary(content.into());
        let summaries = InterfaceSummary::from_enum(&message).expect("failed to parse");

        assert_eq!(summaries[0].name, "lo");
        assert_eq!(summaries[0].state, "up");
        assert_eq!(summaries[0].ipv4_address.as_ref().unwrap(), "127.0.0.1/8");
        assert_eq!(summaries[0].ipv6_address.as_ref().unwrap(), "::1/128");

        assert_eq!(summaries[1].name, "eth0");
        assert_eq!(summaries[1].state, "up");
        assert_eq!(
            summaries[1].ipv4_address.as_ref().unwrap(),
            "172.30.0.12/16",
        );
        assert_eq!(
            summaries[1].ipv6_address.as_ref().unwrap(),
            "fe80::4495:80ff:fe71:a791/64",
        );

        assert_eq!(summaries[2].name, "eth1");
        assert_eq!(summaries[2].state, "up");
        assert_eq!(
            summaries[2].ipv4_address.as_ref().unwrap(),
            "169.254.199.2/30",
        );
        assert!(summaries[2].ipv6_address.is_none());
    }

    fn validate_address(address: &InterfaceAddress, ip: &str, scope: &str, extras: &str) {
        assert_eq!(address.ip, ip);
        assert_eq!(address.scope, scope);
        if let Some(ref ei) = address.extra_info {
            assert_eq!(ei, extras)
        } else {
            assert_eq!(extras, "", "expected empty extra_info");
        }
    }
}