base commit
This commit is contained in:
50
net-logger/src/clasificator/ip_wrapper.rs
Normal file
50
net-logger/src/clasificator/ip_wrapper.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::net::{Ipv4Addr, Ipv6Addr};
|
||||
|
||||
pub struct Ip4Wrapper {
|
||||
raw: Ipv4Addr,
|
||||
}
|
||||
|
||||
pub struct Ip6Wrapper {
|
||||
raw: Ipv6Addr,
|
||||
}
|
||||
|
||||
pub trait IpWrapper<IpType> {
|
||||
fn is_private(&self) -> bool;
|
||||
fn get_raw(&self) -> IpType;
|
||||
}
|
||||
|
||||
impl Ip4Wrapper {
|
||||
pub fn new(ip: u32) -> Self {
|
||||
Self {
|
||||
raw: Ipv4Addr::from_bits(ip),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ip6Wrapper {
|
||||
pub fn new(ip: Ipv6Addr) -> Self {
|
||||
Self {
|
||||
raw: ip,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IpWrapper<Ipv4Addr> for Ip4Wrapper {
|
||||
fn is_private(&self) -> bool {
|
||||
self.raw.is_private()
|
||||
}
|
||||
|
||||
fn get_raw(&self) -> Ipv4Addr {
|
||||
self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl IpWrapper<Ipv6Addr> for Ip6Wrapper {
|
||||
fn is_private(&self) -> bool {
|
||||
self.raw.is_unique_local()
|
||||
}
|
||||
|
||||
fn get_raw(&self) -> Ipv6Addr {
|
||||
self.raw
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user