Skip to main content

asterust_junos/system/services/dhcp_local_server/
overrides.rs

1//! `overrides`
2//! `system { services { dhcp-local-server { [dhcpv6] [group <name> [interface <name>]] overrides { ... } } } }`.
3//!
4//! # See also
5//!
6//! - [overrides (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/overrides-edit-system-services.html)
7//! - [overrides (System Services DHCP)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/system-edit-overrides-dhcp-local-server.html)
8//! - [delay-offer (DHCPv4)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delay-offer.html)
9//! - [delay-advertise (DHCPv6)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delay-advertise.html)
10//! - [client-discover-match (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-discover-match-edit-system-services.html)
11//! - [client-negotiation-match (DHCPv6 Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/client-negotiation-match-edit-system-services.html)
12//! - [delegated-pool (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delegated-pool-edit-system-services.html)
13//! - [delete-binding-on-renegotiation](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/delete-binding-on-renegotiation-edit-dhcp.html)
14//! - [dual-stack (DHCP Local Server Overrides)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/dual-stack-edit-system-services-dhcp-local-server-overrides.html)
15//! - [include-option-82 (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/include-option-82-edit-system-services.html)
16//! - [interface-client-limit (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/interface-client-limit-edit-system-services.html)
17//! - [multi-address-embedded-option-response (DHCP Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/multi-address-embedded-option-response-edit-system-services.html)
18//! - [process-inform](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/process-inform-edit-system-services-overrides.html)
19//! - [rapid-commit (DHCPv6 Local Server)](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/rapid-commit-edit-system-services.html)
20//! - [always-process-option-request-option](https://networkingdocs.hpe.com/documentation/us/en/software/junos/cli-reference/topics/ref/statement/always-process-option-request-option-edit-system-services-dhcp-local-server-dhcpv6-group-overrides.html)
21
22use crate::{active_children, arg_of, child_block, has_leaf, named_children};
23use asterust::config::ConfigLine;
24
25/// An `ascii` or `hexadecimal` match of a `delay-offer` or `delay-advertise` rule.
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub struct DhcpMatch {
29    /// `equals`, `not-equals` or `starts-with`.
30    pub condition: String,
31    /// `ascii` or `hexadecimal`.
32    pub encoding: String,
33    pub value: String,
34}
35
36/// A `delay-offer` or `delay-advertise` block.
37#[derive(Clone, Debug, PartialEq, Eq)]
38#[non_exhaustive]
39pub struct DhcpDelay {
40    /// The option the delay is based on, e.g. `option-82` or `option-37`.
41    pub based_on: Option<String>,
42    pub matches: Vec<DhcpMatch>,
43    /// Seconds.
44    pub delay_time: Option<String>,
45}
46
47/// The `overrides` block of a DHCP local server, group or interface.
48#[derive(Clone, Debug, Default, PartialEq, Eq)]
49#[non_exhaustive]
50pub struct DhcpOverrides {
51    pub always_add_option_dns_server: bool,
52    pub always_process_option_request_option: bool,
53    pub asymmetric_lease_time: Option<String>,
54    pub asymmetric_prefix_lease_time: Option<String>,
55    /// `option60-and-option82` or `incoming-interface`.
56    pub client_discover_match: Option<String>,
57    pub client_negotiation_match: Option<String>,
58    pub delay_advertise: Option<DhcpDelay>,
59    pub delay_offer: Option<DhcpDelay>,
60    pub delegated_pool: Option<String>,
61    pub delete_binding_on_renegotiation: bool,
62    pub dual_stack: Option<String>,
63    pub ia_na_pool: Option<String>,
64    /// `include-option-82` is configured.
65    pub include_option_82: bool,
66    pub include_option_82_forcerenew: bool,
67    pub include_option_82_nak: bool,
68    pub interface_client_limit: Option<String>,
69    pub multi_address_embedded_option_response: bool,
70    /// From `process-inform { pool }`.
71    pub process_inform_pool: Option<String>,
72    pub protocol_attributes: Option<String>,
73    pub rapid_commit: bool,
74}
75
76fn parse_delay(delay: &ConfigLine) -> DhcpDelay {
77    let based_on = named_children(delay, "based-on").next();
78    let mut matches = Vec::new();
79    if let Some((_, block)) = based_on {
80        for condition in ["equals", "not-equals", "starts-with"] {
81            let Some(rules) = child_block(block, condition) else {
82                continue;
83            };
84            for rule in active_children(rules) {
85                if let Some((encoding, value)) = rule.text.split_once(char::is_whitespace) {
86                    matches.push(DhcpMatch {
87                        condition: condition.to_string(),
88                        encoding: encoding.to_string(),
89                        value: value.trim().trim_matches('"').to_string(),
90                    });
91                }
92            }
93        }
94    }
95    DhcpDelay {
96        based_on: based_on.map(|(option, _)| option.to_string()),
97        matches,
98        delay_time: arg_of(delay, "delay-time").map(str::to_string),
99    }
100}
101
102pub(super) fn parse_overrides(overrides: &ConfigLine) -> DhcpOverrides {
103    let field = |keyword: &str| arg_of(overrides, keyword).map(str::to_string);
104    let option_82 = child_block(overrides, "include-option-82");
105    DhcpOverrides {
106        always_add_option_dns_server: has_leaf(overrides, "always-add-option-dns-server"),
107        always_process_option_request_option: has_leaf(overrides, "always-process-option-request-option"),
108        asymmetric_lease_time: field("asymmetric-lease-time"),
109        asymmetric_prefix_lease_time: field("asymmetric-prefix-lease-time"),
110        client_discover_match: field("client-discover-match"),
111        client_negotiation_match: field("client-negotiation-match"),
112        delay_advertise: child_block(overrides, "delay-advertise").map(parse_delay),
113        delay_offer: child_block(overrides, "delay-offer").map(parse_delay),
114        delegated_pool: field("delegated-pool"),
115        delete_binding_on_renegotiation: has_leaf(overrides, "delete-binding-on-renegotiation"),
116        dual_stack: field("dual-stack"),
117        ia_na_pool: field("ia-na-pool"),
118        include_option_82: option_82.is_some(),
119        include_option_82_forcerenew: option_82.is_some_and(|o| has_leaf(o, "forcerenew")),
120        include_option_82_nak: option_82.is_some_and(|o| has_leaf(o, "nak")),
121        interface_client_limit: field("interface-client-limit"),
122        multi_address_embedded_option_response: has_leaf(overrides, "multi-address-embedded-option-response"),
123        process_inform_pool: child_block(overrides, "process-inform")
124            .and_then(|inform| arg_of(inform, "pool"))
125            .map(str::to_string),
126        protocol_attributes: field("protocol-attributes"),
127        rapid_commit: has_leaf(overrides, "rapid-commit"),
128    }
129}