openthread/ot/types/
nat64.rsuse crate::prelude_internal::*;
use core::fmt::{Debug, Formatter};
use num_derive::FromPrimitive;
#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
pub enum Nat64State {
Disabled = OT_NAT64_STATE_DISABLED as isize,
NotRunning = OT_NAT64_STATE_NOT_RUNNING as isize,
Idle = OT_NAT64_STATE_IDLE as isize,
Active = OT_NAT64_STATE_ACTIVE as isize,
}
impl From<otNat64State> for Nat64State {
fn from(x: otNat64State) -> Self {
use num::FromPrimitive;
Self::from_u32(x).unwrap_or_else(|| panic!("Unknown otNat64State value: {x}"))
}
}
impl From<Nat64State> for otNat64State {
fn from(x: Nat64State) -> Self {
x as otNat64State
}
}
#[derive(Default, Clone, Copy)]
#[repr(transparent)]
pub struct Ip4Cidr(pub otIp4Cidr);
impl_ot_castable!(Ip4Cidr, otIp4Cidr);
impl Debug for Ip4Cidr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "IpAddr:{:?}, PrefixLength:{}", self.get_address_bytes(), self.get_length())
}
}
impl PartialEq for Ip4Cidr {
fn eq(&self, other: &Ip4Cidr) -> bool {
if (self.get_length() != other.get_length())
|| (self.get_address_bytes() != other.get_address_bytes())
{
return false;
}
true
}
}
impl Eq for Ip4Cidr {}
pub type Ip4Address = std::net::Ipv4Addr;
impl Ip4Cidr {
pub fn new(addr: [u8; 4], len: u8) -> Ip4Cidr {
Ip4Cidr(otIp4Cidr {
mAddress: otIp4Address { mFields: otIp4Address__bindgen_ty_1 { m8: addr } },
mLength: len,
})
}
pub fn get_address_bytes(&self) -> [u8; 4] {
unsafe { self.0.mAddress.mFields.m8 }
}
pub fn get_length(&self) -> u8 {
self.0.mLength
}
}
#[derive(Default, Clone, Copy)]
#[repr(transparent)]
pub struct Nat64AddressMapping(pub otNat64AddressMapping);
impl_ot_castable!(Nat64AddressMapping, otNat64AddressMapping);
impl Nat64AddressMapping {
pub fn get_mapping_id(&self) -> u64 {
self.0.mId
}
pub fn get_ipv4_addr(&self) -> std::net::Ipv4Addr {
unsafe { self.0.mIp4.mFields.m8.into() }
}
pub fn get_ipv6_addr(&self) -> std::net::Ipv6Addr {
unsafe { self.0.mIp6.mFields.m8.into() }
}
pub fn get_remaining_time_ms(&self) -> u32 {
self.0.mRemainingTimeMs
}
pub fn get_protocol_counters(&self) -> Nat64ProtocolCounters {
self.0.mCounters.into()
}
}
impl Debug for Nat64AddressMapping {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"mapping_id:{:?},ip4_addr:{:?},ip6_addr:{:?},remaining_time:{:?}",
self.get_mapping_id(),
self.get_ipv4_addr(),
self.get_ipv6_addr(),
self.get_remaining_time_ms()
)
}
}
#[derive(Default, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Nat64ProtocolCounters(pub otNat64ProtocolCounters);
impl_ot_castable!(Nat64ProtocolCounters, otNat64ProtocolCounters);
impl Nat64ProtocolCounters {
pub fn get_total_counters(&self) -> Nat64Counters {
self.0.mTotal.into()
}
pub fn get_icmp_counters(&self) -> Nat64Counters {
self.0.mIcmp.into()
}
pub fn get_udp_counters(&self) -> Nat64Counters {
self.0.mUdp.into()
}
pub fn get_tcp_counters(&self) -> Nat64Counters {
self.0.mTcp.into()
}
}
#[derive(Default, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Nat64Counters(pub otNat64Counters);
impl_ot_castable!(Nat64Counters, otNat64Counters);
impl Nat64Counters {
pub fn get_4_to_6_packets(&self) -> u64 {
self.0.m4To6Packets
}
pub fn get_4_to_6_bytes(&self) -> u64 {
self.0.m4To6Bytes
}
pub fn get_6_to_4_packets(&self) -> u64 {
self.0.m6To4Packets
}
pub fn get_6_to_4_bytes(&self) -> u64 {
self.0.m6To4Bytes
}
}
#[derive(Default, Clone, Copy, Debug)]
#[repr(transparent)]
pub struct Nat64ErrorCounters(pub otNat64ErrorCounters);
impl_ot_castable!(Nat64ErrorCounters, otNat64ErrorCounters);
impl Nat64ErrorCounters {
pub fn get_counter_4_to_6(&self) -> [u64; 4usize] {
self.0.mCount4To6
}
pub fn get_counter_6_to_4(&self) -> [u64; 4usize] {
self.0.mCount6To4
}
}