-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2445 from zmrow/static-addressing
Support static addressing via `net.toml`
- Loading branch information
Showing
35 changed files
with
1,029 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use ipnet::IpNet; | ||
use serde::Deserialize; | ||
use snafu::ResultExt; | ||
use std::collections::BTreeSet; | ||
use std::convert::TryFrom; | ||
use std::net::IpAddr; | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub(crate) struct StaticConfigV1 { | ||
pub(crate) addresses: BTreeSet<IpNet>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub(crate) struct RouteV1 { | ||
pub(crate) to: RouteTo, | ||
pub(crate) from: Option<IpAddr>, | ||
pub(crate) via: Option<IpAddr>, | ||
#[serde(rename = "route-metric")] | ||
pub(crate) route_metric: Option<u32>, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
#[serde(try_from = "String")] | ||
pub(crate) enum RouteTo { | ||
DefaultRoute, | ||
Ip(IpNet), | ||
} | ||
|
||
// Allows the user to pass the string "default" or a valid ip address prefix. We can't use an | ||
// untagged enum for this (#[serde(untagged)]) because "default" directly maps to one of the | ||
// variants. Serde will only allow the "untagged" attribute if neither variant directly matches. | ||
impl TryFrom<String> for RouteTo { | ||
type Error = error::Error; | ||
|
||
fn try_from(input: String) -> Result<Self> { | ||
let input = input.to_lowercase(); | ||
Ok(match input.as_str() { | ||
"default" => RouteTo::DefaultRoute, | ||
_ => { | ||
let ip: IpNet = input | ||
.parse() | ||
.context(error::InvalidRouteDestinationSnafu { input })?; | ||
RouteTo::Ip(ip) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
mod error { | ||
use snafu::Snafu; | ||
|
||
#[derive(Debug, Snafu)] | ||
#[snafu(visibility(pub(crate)))] | ||
pub(crate) enum Error { | ||
#[snafu(display("Invalid route destination, must be 'default' or a valid IP address prefix. Received '{}': {}", input, source))] | ||
InvalidRouteDestination { | ||
input: String, | ||
source: ipnet::AddrParseError, | ||
}, | ||
} | ||
} | ||
|
||
type Result<T> = std::result::Result<T, error::Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.