Skip to main content

asterust_junos/system/
addressing.rs

1//! Name resolution and addressing settings under `system`.
2
3use crate::{active_children, arg_of, args_of, child_block, list_values, split_name, unquote};
4use asterust::config::ConfigLine;
5
6/// A DNS server from `name-server`.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[non_exhaustive]
9pub struct NameServer {
10    pub address: String,
11    /// Set when the server is reached through a routing instance such as `mgmt_junos`.
12    pub routing_instance: Option<String>,
13}
14
15/// A `static-host-mapping <hostname>` entry.
16#[derive(Clone, Debug, PartialEq, Eq)]
17#[non_exhaustive]
18pub struct StaticHostMapping {
19    pub hostname: String,
20    pub aliases: Vec<String>,
21    pub inet: Vec<String>,
22    pub inet6: Vec<String>,
23    /// The ISO system identifier, e.g. `0100.2550.0001`.
24    pub sysid: Option<String>,
25}
26
27/// A `backup-router` or `inet6-backup-router`.
28#[derive(Clone, Debug, PartialEq, Eq)]
29#[non_exhaustive]
30pub struct BackupRouter {
31    pub address: String,
32    /// Empty when the router is reachable for all destinations.
33    pub destinations: Vec<String>,
34}
35
36pub(super) fn static_host_mappings(system: &ConfigLine) -> Vec<StaticHostMapping> {
37    child_block(system, "static-host-mapping")
38        .map(|mappings| active_children(mappings).map(parse_static_host_mapping).collect())
39        .unwrap_or_default()
40}
41
42fn parse_static_host_mapping(mapping: &ConfigLine) -> StaticHostMapping {
43    let list = |keyword: &str| args_of(mapping, keyword).into_iter().flat_map(list_values).collect();
44    StaticHostMapping {
45        hostname: unquote(&mapping.text).to_string(),
46        aliases: list("alias"),
47        inet: list("inet"),
48        inet6: list("inet6"),
49        sysid: arg_of(mapping, "sysid").map(str::to_string),
50    }
51}
52
53/// `<address> [destination <prefix | [ prefixes ]>]`.
54pub(super) fn parse_backup_router(arg: &str) -> BackupRouter {
55    let (address, rest) = split_name(arg);
56    BackupRouter {
57        address: address.to_string(),
58        destinations: rest.strip_prefix("destination").map(list_values).unwrap_or_default(),
59    }
60}
61
62/// Servers written as a `name-server { <address>; ... }` block, or one per `name-server <address>` line.
63pub(super) fn name_servers(system: &ConfigLine) -> Vec<NameServer> {
64    let mut found = Vec::new();
65    for child in active_children(system) {
66        if child.text == "name-server" {
67            found.extend(active_children(child).map(|server| name_server(&server.text, server)));
68        } else if let Some(address) = child.arg("name-server") {
69            found.push(name_server(address, child));
70        }
71    }
72    found
73}
74
75fn name_server(address: &str, line: &ConfigLine) -> NameServer {
76    NameServer {
77        address: unquote(address).to_string(),
78        routing_instance: arg_of(line, "routing-instance").map(str::to_string),
79    }
80}
81
82#[cfg(test)]
83mod tests {
84    use crate::system::system;
85    use asterust::config::juniper;
86
87    #[test]
88    fn extracts_name_server_block() {
89        let system = system(&juniper::parse_braces(
90            "system {\n    name-server {\n        192.0.2.53;\n        2001:db8::53;\n    }\n}\n",
91        ))
92        .unwrap();
93        let addresses: Vec<_> = system.name_servers.iter().map(|s| s.address.as_str()).collect();
94        assert_eq!(addresses, ["192.0.2.53", "2001:db8::53"]);
95        assert_eq!(system.name_servers[0].routing_instance, None);
96    }
97
98    #[test]
99    fn name_server_in_management_instance() {
100        let system = system(&juniper::parse_braces(
101            "system {\n    name-server {\n        192.0.2.53 {\n            routing-instance mgmt_junos;\n        }\n    }\n}\n",
102        ))
103        .unwrap();
104        assert_eq!(system.name_servers[0].address, "192.0.2.53");
105        assert_eq!(system.name_servers[0].routing_instance.as_deref(), Some("mgmt_junos"));
106    }
107
108    #[test]
109    fn extracts_one_line_name_servers_and_skips_inactive() {
110        let system = system(&juniper::parse_braces(
111            "system {\n    name-server 192.0.2.53;\n    name-server 192.0.2.54;\n    inactive: name-server 192.0.2.55;\n}\n",
112        ))
113        .unwrap();
114        let addresses: Vec<_> = system.name_servers.iter().map(|s| s.address.as_str()).collect();
115        assert_eq!(addresses, ["192.0.2.53", "192.0.2.54"]);
116    }
117
118    #[test]
119    fn extracts_domain_search_list() {
120        let system = system(&juniper::parse_braces(
121            "system {\n    domain-search [ example.net lab.example.net ];\n}\n",
122        ))
123        .unwrap();
124        assert_eq!(system.domain_search, ["example.net", "lab.example.net"]);
125    }
126
127    #[test]
128    fn extracts_static_host_mappings() {
129        let system = system(&juniper::parse_braces(
130            r#"
131system {
132    static-host-mapping {
133        p1.example.net {
134            inet [ 10.255.0.2 10.255.1.2 ];
135            inet6 2001:db8::2;
136            alias [ p1 core1 ];
137            sysid 0100.2550.0002;
138        }
139        p2.example.net {
140            inet 10.255.0.3;
141        }
142    }
143}
144"#,
145        ))
146        .unwrap();
147        assert_eq!(system.static_host_mappings.len(), 2);
148
149        let p1 = &system.static_host_mappings[0];
150        assert_eq!(p1.hostname, "p1.example.net");
151        assert_eq!(p1.inet, ["10.255.0.2", "10.255.1.2"]);
152        assert_eq!(p1.inet6, ["2001:db8::2"]);
153        assert_eq!(p1.aliases, ["p1", "core1"]);
154        assert_eq!(p1.sysid.as_deref(), Some("0100.2550.0002"));
155
156        let p2 = &system.static_host_mappings[1];
157        assert_eq!(p2.inet, ["10.255.0.3"]);
158        assert!(p2.aliases.is_empty());
159        assert_eq!(p2.sysid, None);
160    }
161
162    #[test]
163    fn extracts_backup_routers() {
164        let system = system(&juniper::parse_braces(
165            "system {\n    backup-router 192.0.2.1 destination [ 10.0.0.0/8 172.16.0.0/12 ];\n    inet6-backup-router 2001:db8::1;\n}\n",
166        ))
167        .unwrap();
168        let backup = system.backup_router.unwrap();
169        assert_eq!(backup.address, "192.0.2.1");
170        assert_eq!(backup.destinations, ["10.0.0.0/8", "172.16.0.0/12"]);
171
172        let backup6 = system.inet6_backup_router.unwrap();
173        assert_eq!(backup6.address, "2001:db8::1");
174        assert!(backup6.destinations.is_empty());
175    }
176
177    #[test]
178    fn single_destination_backup_router() {
179        let system = system(&juniper::parse_braces(
180            "system {\n    backup-router 192.0.2.1 destination 10.0.0.0/8;\n}\n",
181        ))
182        .unwrap();
183        assert_eq!(system.backup_router.unwrap().destinations, ["10.0.0.0/8"]);
184    }
185
186    #[test]
187    fn addressing_settings_are_optional() {
188        let system = system(&juniper::parse_braces("system {\n    host-name pe1;\n}\n")).unwrap();
189        assert!(system.domain_search.is_empty());
190        assert!(system.static_host_mappings.is_empty());
191        assert!(system.backup_router.is_none());
192        assert!(system.inet6_backup_router.is_none());
193    }
194}