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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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)
}

/// Check that any subnet contains any interface
pub fn any_iface_in_any_subnet(ifaces: &[&str], subnets: &[&str]) -> Result<bool, Box<dyn Error>> {
    for subnet in subnets.iter() {
        match subnet.parse::<NetAddr>() {
            Ok(NetAddr::V4(subnet4)) => {
                for iface in ifaces.iter() {
                    if let Ok(iface) = iface.parse::<Ipv4Addr>() {
                        if subnet4.contains(&iface) {
                            return Ok(true)
                        }
                    }
                }
            }
            Ok(NetAddr::V6(subnet6)) => {
                for iface in ifaces.iter() {
                    if let Ok(iface) = iface.parse::<Ipv6Addr>() {
                        if subnet6.contains(&iface) {
                            return Ok(true)
                        }
                    }
                }
            }
            Err(NetError::ParseError(e)) => return Err(e.into()),
        }
    }
    Ok(false)
}

#[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);
    }

    #[test]
    fn test_any_iface_in_any_subnet() {
        let ifaces = vec!["192.168.182.1", "192.168.182.2"];
        let subnets = vec!["192.168.181.0/24", "192.168.182.2/32"];
        let res = any_iface_in_any_subnet(&ifaces, &subnets).unwrap();
        assert!(res);
    }

    #[test]
    fn test_any_iface_not_in_any_subnet() {
        let ifaces = vec!["192.168.182.1", "192.168.182.2"];
        let subnets = vec!["192.168.181.0/24", "192.168.182.3/32"];
        let res = any_iface_in_any_subnet(&ifaces, &subnets).unwrap();
        assert!(!res);
    }
}