pub mod icmp;
pub mod tcp;
pub mod udp;
use std::io;
use std::net::{IpAddr, SocketAddr};
#[cfg(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
))]
pub(crate) fn apply_tclass_v6(socket: &socket2::Socket, tclass: Option<u32>) -> io::Result<()> {
if let Some(tclass) = tclass {
socket.set_tclass_v6(tclass)?;
}
Ok(())
}
#[cfg(not(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "linux",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"
)))]
pub(crate) fn apply_tclass_v6(_socket: &socket2::Socket, tclass: Option<u32>) -> io::Result<()> {
if tclass.is_some() {
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"IPv6 traffic class is not supported on this platform",
));
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SocketFamily {
IPV4,
IPV6,
}
impl SocketFamily {
pub fn from_ip(ip: &IpAddr) -> Self {
match ip {
IpAddr::V4(_) => SocketFamily::IPV4,
IpAddr::V6(_) => SocketFamily::IPV6,
}
}
pub fn from_socket_addr(addr: &SocketAddr) -> Self {
match addr {
SocketAddr::V4(_) => SocketFamily::IPV4,
SocketAddr::V6(_) => SocketFamily::IPV6,
}
}
pub fn is_v4(&self) -> bool {
matches!(self, SocketFamily::IPV4)
}
pub fn is_v6(&self) -> bool {
matches!(self, SocketFamily::IPV6)
}
pub(crate) fn to_domain(self) -> socket2::Domain {
match self {
SocketFamily::IPV4 => socket2::Domain::IPV4,
SocketFamily::IPV6 => socket2::Domain::IPV6,
}
}
}