Skip to main content

asterust_junos/system/services/
dhcp_server.rs

1//! `dhcp`
2//! `system { services { dhcp { ... } } }`.
3//!
4//! The DHCP server of EX Series switches and J Series Services Routers. Its pools and static bindings take
5//! most of the same settings as the server.
6//!
7//! # See also
8//!
9//! - [dhcp](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dhcp-edit-system.html)
10//! - [pool (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/pool-edit-system.html)
11//! - [static-binding](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/static-binding-edit-system.html)
12//! - [boot-server (DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/boot-server-edit-system-services.html)
13//! - [domain-name (DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/domain-name-edit-system-services-dhcp.html)
14//! - [domain-search](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/domain-search-edit-system.html)
15//! - [maximum-lease-time (DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/maximum-lease-time-edit-system.html)
16//! - [next-server](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/next-server-edit-system.html)
17//! - [option (DHCP server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dhcp-user-option.html)
18//! - [propagate-settings](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-edit-propagate-settings.html)
19//! - [server-identifier](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/server-identifier-edit-system.html)
20//! - [sip-server](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/sip-server-edit-system.html)
21//! - [wins-server (System)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/wins-server-edit-system.html)
22
23use crate::{active_children, arg_of, args_of, child_block, list_values, named_children};
24use asterust::config::ConfigLine;
25
26/// The settings shared by the DHCP server, its pools and its static bindings.
27#[derive(Clone, Debug, Default, PartialEq, Eq)]
28#[non_exhaustive]
29pub struct DhcpServerSettings {
30    pub boot_file: Option<String>,
31    /// An address or a hostname.
32    pub boot_server: Option<String>,
33    /// Seconds.
34    pub default_lease_time: Option<String>,
35    pub domain_name: Option<String>,
36    pub domain_search: Vec<String>,
37    /// Seconds.
38    pub maximum_lease_time: Option<String>,
39    pub name_servers: Vec<String>,
40    pub next_server: Option<String>,
41    /// The lines of `option { ... }`, as written, e.g. `"15 string example.net"`.
42    pub options: Vec<String>,
43    pub propagate_settings: Option<String>,
44    pub routers: Vec<String>,
45    pub server_identifier: Option<String>,
46    /// Addresses or names.
47    pub sip_servers: Vec<String>,
48    pub wins_servers: Vec<String>,
49}
50
51/// A `pool <address>/<prefix-length>`.
52#[derive(Clone, Debug, PartialEq, Eq)]
53#[non_exhaustive]
54pub struct DhcpPool {
55    pub address: String,
56    pub address_range_low: Option<String>,
57    pub address_range_high: Option<String>,
58    pub exclude_addresses: Vec<String>,
59    pub settings: DhcpServerSettings,
60}
61
62/// A `static-binding <mac-address>`.
63#[derive(Clone, Debug, PartialEq, Eq)]
64#[non_exhaustive]
65pub struct DhcpStaticBinding {
66    pub mac_address: String,
67    pub fixed_addresses: Vec<String>,
68    pub host_name: Option<String>,
69    /// `ascii <id>` or `hexadecimal <id>`, as written.
70    pub client_identifier: Option<String>,
71    pub settings: DhcpServerSettings,
72}
73
74/// The `system services dhcp` server.
75#[derive(Clone, Debug, PartialEq, Eq)]
76#[non_exhaustive]
77pub struct DhcpServer {
78    pub settings: DhcpServerSettings,
79    pub pools: Vec<DhcpPool>,
80    pub static_bindings: Vec<DhcpStaticBinding>,
81}
82
83/// Addresses written inline, in a list, or as `<keyword> { <address>; ... }`.
84fn addresses(block: &ConfigLine, keyword: &str) -> Vec<String> {
85    let mut found: Vec<String> = args_of(block, keyword).into_iter().flat_map(list_values).collect();
86    if let Some(list) = child_block(block, keyword) {
87        found.extend(active_children(list).map(|address| address.text.clone()));
88    }
89    found
90}
91
92fn parse_settings(block: &ConfigLine) -> DhcpServerSettings {
93    let field = |keyword: &str| arg_of(block, keyword).map(str::to_string);
94    DhcpServerSettings {
95        boot_file: field("boot-file"),
96        boot_server: field("boot-server"),
97        default_lease_time: field("default-lease-time"),
98        domain_name: field("domain-name"),
99        domain_search: args_of(block, "domain-search")
100            .into_iter()
101            .flat_map(list_values)
102            .collect(),
103        maximum_lease_time: field("maximum-lease-time"),
104        name_servers: addresses(block, "name-server"),
105        next_server: field("next-server"),
106        options: child_block(block, "option")
107            .map(|options| {
108                active_children(options)
109                    .map(|o| o.text.trim_matches('"').to_string())
110                    .collect()
111            })
112            .unwrap_or_default(),
113        propagate_settings: field("propagate-settings"),
114        routers: addresses(block, "router"),
115        server_identifier: field("server-identifier"),
116        sip_servers: args_of(block, "sip-server").into_iter().flat_map(list_values).collect(),
117        wins_servers: addresses(block, "wins-server"),
118    }
119}
120
121pub(super) fn parse_dhcp_server(dhcp: &ConfigLine) -> DhcpServer {
122    DhcpServer {
123        settings: parse_settings(dhcp),
124        pools: named_children(dhcp, "pool")
125            .map(|(address, pool)| {
126                let range = child_block(pool, "address-range");
127                DhcpPool {
128                    address: address.to_string(),
129                    address_range_low: range.and_then(|r| arg_of(r, "low")).map(str::to_string),
130                    address_range_high: range.and_then(|r| arg_of(r, "high")).map(str::to_string),
131                    exclude_addresses: addresses(pool, "exclude-address"),
132                    settings: parse_settings(pool),
133                }
134            })
135            .collect(),
136        static_bindings: named_children(dhcp, "static-binding")
137            .map(|(mac, binding)| DhcpStaticBinding {
138                mac_address: mac.to_string(),
139                fixed_addresses: addresses(binding, "fixed-address"),
140                host_name: arg_of(binding, "host-name").map(str::to_string),
141                client_identifier: arg_of(binding, "client-identifier").map(str::to_string),
142                settings: parse_settings(binding),
143            })
144            .collect(),
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use crate::system::system;
151    use asterust::config::juniper;
152
153    // Not from documentation: written from the CLI Reference syntax pages. See the README "Sources".
154    #[test]
155    fn extracts_the_dhcp_server_pools_and_static_bindings() {
156        let dhcp = system(&juniper::parse_braces(
157            r#"
158system {
159    services {
160        dhcp {
161            boot-file pxelinux.0;
162            boot-server 192.0.2.8;
163            default-lease-time 3600;
164            domain-name lab.example.net;
165            domain-search [ lab.example.net example.net ];
166            maximum-lease-time 7200;
167            name-server {
168                192.0.2.53;
169                192.0.2.54;
170            }
171            next-server 192.0.2.9;
172            option {
173                15 string lab.example.net;
174            }
175            pool 10.0.0.0/24 {
176                address-range {
177                    low 10.0.0.10;
178                    high 10.0.0.200;
179                }
180                exclude-address {
181                    10.0.0.50;
182                }
183                router {
184                    10.0.0.1;
185                }
186                wins-server {
187                    10.0.0.2;
188                }
189                propagate-settings ge-0/0/0.0;
190            }
191            static-binding 00:11:22:33:44:55 {
192                fixed-address {
193                    10.0.0.60;
194                }
195                host-name printer1;
196                client-identifier ascii printer1;
197                server-identifier 192.0.2.1;
198                sip-server [ sip.example.net ];
199            }
200        }
201    }
202}
203"#,
204        ))
205        .unwrap()
206        .services
207        .unwrap()
208        .dhcp
209        .unwrap();
210        let settings = &dhcp.settings;
211        assert_eq!(settings.boot_file.as_deref(), Some("pxelinux.0"));
212        assert_eq!(settings.boot_server.as_deref(), Some("192.0.2.8"));
213        assert_eq!(settings.default_lease_time.as_deref(), Some("3600"));
214        assert_eq!(settings.domain_name.as_deref(), Some("lab.example.net"));
215        assert_eq!(settings.domain_search, ["lab.example.net", "example.net"]);
216        assert_eq!(settings.maximum_lease_time.as_deref(), Some("7200"));
217        assert_eq!(settings.name_servers, ["192.0.2.53", "192.0.2.54"]);
218        assert_eq!(settings.next_server.as_deref(), Some("192.0.2.9"));
219        assert_eq!(settings.options, ["15 string lab.example.net"]);
220
221        let pool = &dhcp.pools[0];
222        assert_eq!(pool.address, "10.0.0.0/24");
223        assert_eq!(pool.address_range_low.as_deref(), Some("10.0.0.10"));
224        assert_eq!(pool.address_range_high.as_deref(), Some("10.0.0.200"));
225        assert_eq!(pool.exclude_addresses, ["10.0.0.50"]);
226        assert_eq!(pool.settings.routers, ["10.0.0.1"]);
227        assert_eq!(pool.settings.wins_servers, ["10.0.0.2"]);
228        assert_eq!(pool.settings.propagate_settings.as_deref(), Some("ge-0/0/0.0"));
229
230        let binding = &dhcp.static_bindings[0];
231        assert_eq!(binding.mac_address, "00:11:22:33:44:55");
232        assert_eq!(binding.fixed_addresses, ["10.0.0.60"]);
233        assert_eq!(binding.host_name.as_deref(), Some("printer1"));
234        assert_eq!(binding.client_identifier.as_deref(), Some("ascii printer1"));
235        assert_eq!(binding.settings.server_identifier.as_deref(), Some("192.0.2.1"));
236        assert_eq!(binding.settings.sip_servers, ["sip.example.net"]);
237    }
238
239    #[test]
240    fn no_dhcp_server_is_none() {
241        let services = system(&juniper::parse_braces(
242            "system {\n    services {\n        telnet;\n    }\n}\n",
243        ))
244        .unwrap()
245        .services
246        .unwrap();
247        assert!(services.dhcp.is_none());
248    }
249}