Skip to main content

asterust_junos/interfaces/unit/family/address/
mod.rs

1//! `address`
2//! `interfaces { <name> { unit <n> { family <name> { address <address> { ... } } } } }`.
3//!
4//! # See also
5//!
6//! - [address (IP Address)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/address-edit-interfaces-unit-family.html)
7//! - [primary (Address on Interface)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/primary-edit-interfaces.html)
8//! - [preferred](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/preferred-edit-interfaces.html)
9//! - [arp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/arp-edit-interfaces.html)
10//! - [destination (Tunnels)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/destination-edit-interfaces.html)
11//! - [master-only](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/master-only-edit-interfaces.html)
12
13mod vrrp_group;
14
15pub use vrrp_group::{BandwidthThreshold, TrackedInterface, TrackedRoute, VrrpGroup, VrrpInheritFrom, VrrpTrack};
16
17use crate::{active_children, has_leaf, split_name};
18use asterust::config::ConfigLine;
19use vrrp_group::parse_vrrp_groups;
20
21/// A static `arp` entry of an address.
22#[derive(Clone, Debug, PartialEq, Eq)]
23#[non_exhaustive]
24pub struct AddressArp {
25    pub address: String,
26    pub mac: Option<String>,
27    /// The MAC is written with `multicast-mac`.
28    pub multicast_mac: bool,
29    /// The device also answers ARP requests for the address.
30    pub publish: bool,
31}
32
33/// An `address <address>` of a family, with its options.
34#[derive(Clone, Debug, PartialEq, Eq)]
35#[non_exhaustive]
36pub struct UnitAddress {
37    /// e.g. `"10.0.0.1/24"`.
38    pub address: String,
39    pub primary: bool,
40    pub preferred: bool,
41    pub master_only: bool,
42    pub eui_64: bool,
43    pub destination: Option<String>,
44    pub broadcast: Option<String>,
45    pub arps: Vec<AddressArp>,
46    pub vrrp_groups: Vec<VrrpGroup>,
47}
48
49pub(super) fn parse_addresses(family: &ConfigLine) -> Vec<UnitAddress> {
50    active_children(family)
51        .filter_map(|line| Some((line, line.arg("address")?)))
52        .map(|(line, arg)| parse_address(line, arg))
53        .collect()
54}
55
56fn parse_address(line: &ConfigLine, arg: &str) -> UnitAddress {
57    let (address, inline) = split_name(arg);
58    let inline: Vec<&str> = inline.split_whitespace().collect();
59    let flag = |name: &str| has_leaf(line, name) || inline.contains(&name);
60    let value = |keyword: &str| {
61        active_children(line)
62            .find_map(|c| c.arg(keyword))
63            .map(|arg| arg.trim_matches('"').to_string())
64    };
65    UnitAddress {
66        address: address.to_string(),
67        primary: flag("primary"),
68        preferred: flag("preferred"),
69        master_only: flag("master-only"),
70        eui_64: flag("eui-64"),
71        destination: value("destination"),
72        broadcast: value("broadcast"),
73        arps: active_children(line)
74            .filter_map(|c| c.arg("arp"))
75            .map(parse_arp)
76            .collect(),
77        vrrp_groups: parse_vrrp_groups(line),
78    }
79}
80
81/// `<ip-address> (mac | multicast-mac) <mac-address> [publish]`.
82fn parse_arp(arg: &str) -> AddressArp {
83    let mut words = arg.split_whitespace();
84    let address = words.next().unwrap_or_default().to_string();
85    let mut arp = AddressArp {
86        address,
87        mac: None,
88        multicast_mac: false,
89        publish: false,
90    };
91    while let Some(word) = words.next() {
92        match word {
93            "mac" => arp.mac = words.next().map(str::to_string),
94            "multicast-mac" => {
95                arp.multicast_mac = true;
96                arp.mac = words.next().map(str::to_string);
97            }
98            "publish" => arp.publish = true,
99            _ => {}
100        }
101    }
102    arp
103}