generated from ewpratten/rust-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
240 additions
and
145 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
//! This module contains the definitions for each binary's CLI arguments and config file structure for the sake of readability. | ||
|
||
pub mod protomask_clat; | ||
pub mod protomask; |
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,92 @@ | ||
use std::{ | ||
net::{Ipv4Addr, Ipv6Addr, SocketAddr}, | ||
path::PathBuf, | ||
}; | ||
|
||
use ipnet::{Ipv4Net, Ipv6Net}; | ||
|
||
use crate::common::rfc6052::parse_network_specific_prefix; | ||
|
||
#[derive(clap::Parser)] | ||
#[clap(author, version, about="Fast and simple NAT64", long_about = None)] | ||
pub struct Args { | ||
#[command(flatten)] | ||
config_data: Option<Config>, | ||
|
||
/// Path to a config file to read | ||
#[clap(short = 'c', long = "config", conflicts_with = "Config")] | ||
config_file: Option<PathBuf>, | ||
|
||
/// Explicitly set the interface name to use | ||
#[clap(short, long, default_value_t = ("nat%d").to_string())] | ||
pub interface: String, | ||
|
||
/// Enable verbose logging | ||
#[clap(short, long)] | ||
pub verbose: bool, | ||
} | ||
|
||
impl Args { | ||
#[allow(dead_code)] | ||
pub fn data(&self) -> Result<Config, Box<dyn std::error::Error>> { | ||
match self.config_file { | ||
Some(ref path) => { | ||
// Read the data from the config file | ||
let file = std::fs::File::open(path).map_err(|error| match error.kind() { | ||
std::io::ErrorKind::NotFound => { | ||
log::error!("Config file not found: {}", path.display()); | ||
std::process::exit(1) | ||
} | ||
_ => error, | ||
})?; | ||
let data: Config = serde_json::from_reader(file)?; | ||
|
||
// We need at least one pool prefix | ||
if data.pool_prefixes.is_empty() { | ||
log::error!("No pool prefixes specified. At least one prefix must be specified in the `pool` property of the config file"); | ||
std::process::exit(1); | ||
} | ||
|
||
Ok(data) | ||
} | ||
None => match &self.config_data { | ||
Some(data) => Ok(data.clone()), | ||
None => { | ||
log::error!("No configuration provided. Either use --config to specify a file or set the configuration via CLI args (see --help)"); | ||
std::process::exit(1) | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// Program configuration. Specifiable via either CLI args or a config file | ||
#[derive(Debug, clap::Args, serde::Deserialize, Clone)] | ||
#[group()] | ||
pub struct Config { | ||
/// IPv4 prefixes to use as NAT pool address space | ||
#[clap(long = "pool-prefix")] | ||
#[serde(rename = "pool")] | ||
pub pool_prefixes: Vec<Ipv4Net>, | ||
|
||
/// Static mapping between IPv4 and IPv6 addresses | ||
#[clap(skip)] | ||
pub static_map: Vec<(Ipv4Addr, Ipv6Addr)>, | ||
|
||
/// Enable prometheus metrics on a given address | ||
#[clap(long = "prometheus")] | ||
#[serde(rename = "prometheus_bind_addr")] | ||
pub prom_bind_addr: Option<SocketAddr>, | ||
|
||
/// RFC6052 IPv6 translation prefix | ||
#[clap(long, default_value_t = ("64:ff9b::/96").parse().unwrap(), value_parser = parse_network_specific_prefix)] | ||
#[serde( | ||
rename = "prefix", | ||
serialize_with = "crate::common::rfc6052::serialize_network_specific_prefix" | ||
)] | ||
pub translation_prefix: Ipv6Net, | ||
|
||
/// NAT reservation timeout in seconds | ||
#[clap(long, default_value = "7200")] | ||
pub reservation_timeout: u64, | ||
} |
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,81 @@ | ||
//! Commandline arguments and config file definitions for `protomask-clat` | ||
|
||
use crate::common::rfc6052::parse_network_specific_prefix; | ||
use ipnet::{Ipv4Net, Ipv6Net}; | ||
use std::{net::SocketAddr, path::PathBuf}; | ||
|
||
#[derive(Debug, clap::Parser)] | ||
#[clap(author, version, about="IPv4 to IPv6 Customer-side transLATor (CLAT)", long_about = None)] | ||
pub struct Args { | ||
#[command(flatten)] | ||
config_data: Option<Config>, | ||
|
||
/// Path to a config file to read | ||
#[clap(short = 'c', long = "config", conflicts_with = "Config")] | ||
config_file: Option<PathBuf>, | ||
|
||
/// Explicitly set the interface name to use | ||
#[clap(short, long, default_value_t = ("clat%d").to_string())] | ||
pub interface: String, | ||
|
||
/// Enable verbose logging | ||
#[clap(short, long)] | ||
pub verbose: bool, | ||
} | ||
|
||
impl Args { | ||
#[allow(dead_code)] | ||
pub fn data(&self) -> Result<Config, Box<dyn std::error::Error>> { | ||
match self.config_file { | ||
Some(ref path) => { | ||
// Read the data from the config file | ||
let file = std::fs::File::open(path).map_err(|error| match error.kind() { | ||
std::io::ErrorKind::NotFound => { | ||
log::error!("Config file not found: {}", path.display()); | ||
std::process::exit(1) | ||
} | ||
_ => error, | ||
})?; | ||
let data: Config = serde_json::from_reader(file)?; | ||
|
||
// We need at least one customer prefix | ||
if data.customer_pool.is_empty() { | ||
log::error!("No customer prefixes specified. At least one prefix must be specified in the `customer_pool` property of the config file"); | ||
std::process::exit(1); | ||
} | ||
|
||
Ok(data) | ||
} | ||
None => match &self.config_data { | ||
Some(data) => Ok(data.clone()), | ||
None => { | ||
log::error!("No configuration provided. Either use --config to specify a file or set the configuration via CLI args (see --help)"); | ||
std::process::exit(1) | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// Program configuration. Specifiable via either CLI args or a config file | ||
#[derive(Debug, clap::Args, serde::Deserialize, Clone)] | ||
#[group()] | ||
pub struct Config { | ||
/// One or more customer-side IPv4 prefixes to allow through CLAT | ||
#[clap(long = "customer-prefix")] | ||
#[serde(rename = "customer_pool")] | ||
pub customer_pool: Vec<Ipv4Net>, | ||
|
||
/// Enable prometheus metrics on a given address | ||
#[clap(long = "prometheus")] | ||
#[serde(rename = "prometheus_bind_addr")] | ||
pub prom_bind_addr: Option<SocketAddr>, | ||
|
||
/// RFC6052 IPv6 prefix to encapsulate IPv4 packets within | ||
#[clap(long="via", default_value_t = ("64:ff9b::/96").parse().unwrap(), value_parser = parse_network_specific_prefix)] | ||
#[serde( | ||
rename = "via", | ||
serialize_with = "crate::common::rfc6052::serialize_network_specific_prefix" | ||
)] | ||
pub embed_prefix: Ipv6Net, | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
pub mod logging; | ||
pub mod packet_handler; | ||
pub mod rfc6052; | ||
pub mod permissions; |
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,9 @@ | ||
use nix::unistd::Uid; | ||
|
||
/// Ensures the binary is being exxecuted as root | ||
pub fn ensure_root() { | ||
if !Uid::effective().is_root() { | ||
log::error!("This program must be run as root"); | ||
std::process::exit(1); | ||
} | ||
} |
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.
7d15bdc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary sizes for
x86_64-unknown-linux-musl
Channel:
stable
7d15bdc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary sizes for
aarch64-unknown-linux-musl
Channel:
stable