diff --git a/Cargo.toml b/Cargo.toml index 29f3c46..6b03f1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.12" +version = "0.6.13" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_net.rs b/src/util_net.rs index fee405c..f6e715d 100644 --- a/src/util_net.rs +++ b/src/util_net.rs @@ -88,6 +88,41 @@ impl Display for IpAddressMask { } } +#[derive(Debug, Clone)] +pub struct IpAddressMaskGroup { + pub ip_address_mask_group: Vec, +} + +impl IpAddressMaskGroup { + pub fn parse(ip_mask_group: &[String]) -> Self { + let mut ret = vec![]; + for ip_mask_addr in ip_mask_group { + if let Some(ip_mask) = IpAddressMask::parse_ipv4(ip_mask_addr) { + ret.push(ip_mask); + } + } + Self { ip_address_mask_group: ret } + } + + pub fn is_empty(&self) -> bool { + self.ip_address_mask_group.is_empty() + } + + pub fn is_matches(&self, socket_addr: &SocketAddr) -> bool { + self.ip_address_mask_group.iter().any(|ip_address_mask| ip_address_mask.is_matches(socket_addr)) + } + + pub fn is_empty_or_matches(&self, socket_addr: &SocketAddr) -> bool { + self.is_empty() || self.is_matches(socket_addr) + } +} + +impl Display for IpAddressMaskGroup { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + write!(f, "[{}]", self.ip_address_mask_group.iter().map(|i| format!("{}", i)).collect::>().join(", ")) + } +} + #[derive(Debug, Clone)] pub struct IpAddressAndPort { pub ip: IpAddress, @@ -196,3 +231,16 @@ fn test_ip_address_port() { let ip_address_and_port = IpAddressAndPort::parse("1.1.1.1:80"); assert_eq!("1.1.1.1:80", format!("{}", ip_address_and_port.unwrap())); } + +#[test] +fn test_ip_address_mask_group_is_matches() { + let group = IpAddressMaskGroup::parse(&vec!["127.0.0.1".to_owned(), "10.0.0.0/24".to_owned()]); + let addr = SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 1)), 123); + assert_eq!(true, group.is_matches(&addr)); + let addr = SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(127, 0, 0, 2)), 123); + assert_eq!(false, group.is_matches(&addr)); + let addr = SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(10, 0, 0, 2)), 123); + assert_eq!(true, group.is_matches(&addr)); + let addr = SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::new(10, 0, 1, 2)), 123); + assert_eq!(false, group.is_matches(&addr)); +}