forked from NationTech/harmony
wip: Implement add static mapping public function
This commit is contained in:
parent
50ca6afb47
commit
0700265622
@ -8,6 +8,111 @@ pub struct DhcpConfig<'a> {
|
|||||||
opnsense: &'a mut OPNsense,
|
opnsense: &'a mut OPNsense,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DhcpError {
|
||||||
|
InvalidMacAddress(String),
|
||||||
|
InvalidIpAddress(String),
|
||||||
|
IpAddressAlreadyMapped(String),
|
||||||
|
MacAddressAlreadyMapped(String),
|
||||||
|
IpAddressOutOfRange(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for DhcpError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
DhcpError::InvalidMacAddress(mac) => write!(f, "Invalid MAC address format: {}", mac),
|
||||||
|
DhcpError::InvalidIpAddress(ip) => write!(f, "Invalid IP address format: {}", ip),
|
||||||
|
DhcpError::IpAddressAlreadyMapped(ip) => write!(f, "IP address {} is already mapped", ip),
|
||||||
|
DhcpError::MacAddressAlreadyMapped(mac) => write!(f, "MAC address {} is already mapped", mac),
|
||||||
|
DhcpError::IpAddressOutOfRange(ip) => write!(f, "IP address {} is out of interface range", ip),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for DhcpError {}
|
||||||
|
|
||||||
|
impl<'a> DhcpConfig<'a> {
|
||||||
|
pub fn new(opnsense: &'a mut OPNsense) -> Self {
|
||||||
|
Self { opnsense }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_static_mapping(
|
||||||
|
&mut self,
|
||||||
|
mac: String,
|
||||||
|
ipaddr: String,
|
||||||
|
hostname: String,
|
||||||
|
) -> Result<(), DhcpError> {
|
||||||
|
if !Self::is_valid_mac(&mac) {
|
||||||
|
return Err(DhcpError::InvalidMacAddress(mac));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !Self::is_valid_ip(&ipaddr) {
|
||||||
|
return Err(DhcpError::InvalidIpAddress(ipaddr));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !Self::is_ip_in_range(&ipaddr) {
|
||||||
|
return Err(DhcpError::IpAddressOutOfRange(ipaddr));
|
||||||
|
}
|
||||||
|
|
||||||
|
let existing_mappings = &self.opnsense.dhcpd.lan.staticmaps;
|
||||||
|
|
||||||
|
if existing_mappings.iter().any(|m| m.ipaddr == ipaddr) {
|
||||||
|
return Err(DhcpError::IpAddressAlreadyMapped(ipaddr));
|
||||||
|
}
|
||||||
|
|
||||||
|
if existing_mappings.iter().any(|m| m.mac == mac) {
|
||||||
|
return Err(DhcpError::MacAddressAlreadyMapped(mac));
|
||||||
|
}
|
||||||
|
|
||||||
|
let static_map = StaticMap {
|
||||||
|
mac,
|
||||||
|
ipaddr,
|
||||||
|
hostname,
|
||||||
|
descr: Default::default(),
|
||||||
|
winsserver: Default::default(),
|
||||||
|
dnsserver: Default::default(),
|
||||||
|
ntpserver: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.opnsense.dhcpd.lan.staticmaps.push(static_map);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_static_mappings(&self) -> &[StaticMap] {
|
||||||
|
&self.opnsense.dhcpd.lan.staticmaps
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_mac(mac: &str) -> bool {
|
||||||
|
let parts: Vec<&str> = mac.split(':').collect();
|
||||||
|
if parts.len() != 6 {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
parts.iter().all(|part| {
|
||||||
|
part.len() == 2 && part.chars().all(|c| c.is_ascii_hexdigit())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valid_ip(ip: &str) -> bool {
|
||||||
|
ip.parse::<IpAddr>().is_ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_ip_in_range(ip: &str) -> bool {
|
||||||
|
if let Ok(addr) = ip.parse::<IpAddr>() {
|
||||||
|
if let IpAddr::V4(ipv4) = addr {
|
||||||
|
let octets = ipv4.octets();
|
||||||
|
return octets[0] == 192 && octets[1] == 168 && octets[2] == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DhcpConfig<'a> {
|
||||||
|
opnsense: &'a mut OPNsense,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
impl<'a> DhcpConfig<'a> {
|
impl<'a> DhcpConfig<'a> {
|
||||||
pub fn new(opnsense: &'a mut OPNsense) -> Self {
|
pub fn new(opnsense: &'a mut OPNsense) -> Self {
|
||||||
Self { opnsense }
|
Self { opnsense }
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user