1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use netaddr2::{Contains, Error as NetError, NetAddr};
use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr};

/// Check that subnet contains an interface
pub fn iface_in_subnet(iface: &str, subnet: &str) -> Result<bool, Box<dyn Error>> {
    match subnet.parse::<NetAddr>() {
        Ok(NetAddr::V4(subnet4)) => {
            if let Ok(iface) = iface.parse::<Ipv4Addr>() {
                let is_in = subnet4.contains(&iface);
                Ok(is_in)
            } else {
                Ok(false)
            }
        }
        Ok(NetAddr::V6(subnet6)) => {
            if let Ok(iface) = iface.parse::<Ipv6Addr>() {
                let is_in = subnet6.contains(&iface);
                Ok(is_in)
            } else {
                Ok(false)
            }
        }
        Err(NetError::ParseError(e)) => Err(e.into()),
    }
}

/// Check that any subnet contains an interface
pub fn iface_in_any_subnet(iface: &str, subnets: &[&str]) -> Result<bool, Box<dyn Error>> {
    for subnet in subnets.iter() {
        let res = iface_in_subnet(&iface, &subnet);
        if let Ok(true) = res {
            return Ok(true);
        }
    }
    Ok(false)
}

/// Check that all subnets contains an interface
pub fn iface_in_all_subnets(iface: &str, subnets: &[&str]) -> Result<bool, Box<dyn Error>> {
    for subnet in subnets.iter() {
        let res = iface_in_subnet(&iface, &subnet);
        if let Ok(false) = res {
            return Ok(false);
        }
    }
    Ok(true)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_iface_in_subnet() {
        let res = iface_in_subnet("192.168.182.1", "192.168.182.0/24").unwrap();
        assert!(res);
    }

    #[test]
    fn test_iface_not_in_subnet() {
        let res = iface_in_subnet("192.168.183.1", "192.168.182.0/24").unwrap();
        assert!(!res);
    }

    #[test]
    fn test_iface_in_any_subnet() {
        let subnets = vec!["192.168.181.0/24", "192.168.182.0/24"];
        let res = iface_in_any_subnet("192.168.182.1", &subnets).unwrap();
        assert!(res);
    }

    #[test]
    fn test_iface_not_in_any_subnet() {
        let subnets = vec!["192.168.181.0/24", "192.168.182.0/24"];
        let res = iface_in_any_subnet("192.168.183.1", &subnets).unwrap();
        assert!(!res);
    }

    #[test]
    fn test_iface_in_all_subnets() {
        let subnets = vec!["192.168.182.0/24", "192.168.182.1/32"];
        let res = iface_in_all_subnets("192.168.182.1", &subnets).unwrap();
        assert!(res);
    }

    #[test]
    fn test_iface_not_in_all_subnets() {
        let subnets = vec!["192.168.182.0/24", "192.168.182.2/32"];
        let res = iface_in_all_subnets("192.168.182.1", &subnets).unwrap();
        assert!(!res);
    }
}