use std::net::{Ipv4Addr, Ipv6Addr};
use libc::{in6_addr, in_addr, sockaddr_in, sockaddr_in6};
use crate::Result;
use crate::interface::Netmask;
pub fn ipv4_from_in_addr(internet_address: &in_addr) -> Result<Ipv4Addr> {
let mut ip_addr = Ipv4Addr::from(internet_address.s_addr);
if cfg!(target_endian = "little") {
ip_addr = Ipv4Addr::from(internet_address.s_addr.swap_bytes());
}
Ok(ip_addr)
}
pub fn ipv6_from_in6_addr(internet_address: &in6_addr) -> Result<Ipv6Addr> {
let ip_addr = Ipv6Addr::from(internet_address.s6_addr);
Ok(ip_addr)
}
pub fn make_ipv4_netmask(netifa: &libc::ifaddrs) -> Netmask<Ipv4Addr> {
let sockaddr = netifa.ifa_netmask;
if sockaddr.is_null() {
return None;
}
let socket_addr = sockaddr as *mut sockaddr_in;
let internet_address = unsafe { (*socket_addr).sin_addr };
ipv4_from_in_addr(&internet_address).ok()
}
pub fn make_ipv6_netmask(netifa: &libc::ifaddrs) -> Netmask<Ipv6Addr> {
let sockaddr = netifa.ifa_netmask;
if sockaddr.is_null() {
return None;
}
let socket_addr = sockaddr as *mut sockaddr_in6;
let internet_address = unsafe { (*socket_addr).sin6_addr };
if internet_address.s6_addr[0] == 0xfe && internet_address.s6_addr[1] == 0x80 {
return None;
}
ipv6_from_in6_addr(&internet_address).ok()
}