use crate::prelude_internal::*;
#[allow(missing_debug_implementations)]
pub struct Nat64AddressMappingIterator<'a, T: ?Sized> {
pub ot_nat64_address_mapping_iter: otNat64AddressMappingIterator,
pub ot_instance: &'a T,
}
impl<T: ?Sized + Nat64> Iterator for Nat64AddressMappingIterator<'_, T> {
type Item = Nat64AddressMapping;
fn next(&mut self) -> Option<Self::Item> {
let mut mapping = Nat64AddressMapping::default();
self.ot_instance
.nat64_get_next_address_mapping(&mut self.ot_nat64_address_mapping_iter, &mut mapping)
.ok()
.map(|()| mapping)
}
}
pub trait Nat64 {
fn nat64_set_enabled(&self, enabled: bool);
fn nat64_set_ip4_cidr(&self, ip4_cidr: Ip4Cidr) -> Result;
fn nat64_get_cidr(&self) -> Result<Ip4Cidr>;
fn nat64_set_receive_fn<'a, F>(&'a self, f: Option<F>)
where
F: FnMut(OtMessageBox<'_>) + 'a;
fn nat64_send(&self, message: OtMessageBox<'_>) -> Result;
fn nat64_send_data_slice(&self, data_slice: &[u8]) -> Result;
fn nat64_infra_if_prefix_discover_done(
&self,
infra_if_idx: ot::NetifIndex,
ip6_prefix: openthread_sys::otIp6Prefix,
);
fn nat64_get_address_mapping_iterator(&self) -> Nat64AddressMappingIterator<'_, Self> {
let mut iter = otNat64AddressMappingIterator::default();
self.nat64_init_address_mapping_iterator(&mut iter);
Nat64AddressMappingIterator { ot_nat64_address_mapping_iter: iter, ot_instance: self }
}
fn nat64_init_address_mapping_iterator(&self, iter: &mut otNat64AddressMappingIterator);
fn nat64_get_next_address_mapping(
&self,
iter: &mut otNat64AddressMappingIterator,
mapping: &mut Nat64AddressMapping,
) -> Result;
fn nat64_get_translator_state(&self) -> Nat64State;
fn nat64_get_prefix_manager_state(&self) -> Nat64State;
fn nat64_get_error_counters(&self) -> Nat64ErrorCounters;
fn nat64_get_counters(&self) -> Nat64ProtocolCounters;
}
impl<T: Nat64 + ot::Boxable> Nat64 for ot::Box<T> {
fn nat64_set_enabled(&self, enabled: bool) {
self.as_ref().nat64_set_enabled(enabled)
}
fn nat64_set_ip4_cidr(&self, ip4_cidr: Ip4Cidr) -> Result {
self.as_ref().nat64_set_ip4_cidr(ip4_cidr)
}
fn nat64_get_cidr(&self) -> Result<Ip4Cidr> {
self.as_ref().nat64_get_cidr()
}
fn nat64_set_receive_fn<'a, F>(&'a self, f: Option<F>)
where
F: FnMut(OtMessageBox<'_>) + 'a,
{
self.as_ref().nat64_set_receive_fn(f)
}
fn nat64_send(&self, message: OtMessageBox<'_>) -> Result {
self.as_ref().nat64_send(message)
}
fn nat64_send_data_slice(&self, data_slice: &[u8]) -> Result {
self.as_ref().nat64_send_data_slice(data_slice)
}
fn nat64_infra_if_prefix_discover_done(
&self,
infra_if_idx: ot::NetifIndex,
ip6_prefix: openthread_sys::otIp6Prefix,
) {
self.as_ref().nat64_infra_if_prefix_discover_done(infra_if_idx, ip6_prefix);
}
fn nat64_init_address_mapping_iterator(&self, iter: &mut otNat64AddressMappingIterator) {
self.as_ref().nat64_init_address_mapping_iterator(iter)
}
fn nat64_get_next_address_mapping(
&self,
iter: &mut otNat64AddressMappingIterator,
mapping: &mut Nat64AddressMapping,
) -> Result {
self.as_ref().nat64_get_next_address_mapping(iter, mapping)
}
fn nat64_get_translator_state(&self) -> Nat64State {
self.as_ref().nat64_get_translator_state()
}
fn nat64_get_prefix_manager_state(&self) -> Nat64State {
self.as_ref().nat64_get_prefix_manager_state()
}
fn nat64_get_error_counters(&self) -> Nat64ErrorCounters {
self.as_ref().nat64_get_error_counters()
}
fn nat64_get_counters(&self) -> Nat64ProtocolCounters {
self.as_ref().nat64_get_counters()
}
}
impl Nat64 for Instance {
fn nat64_set_enabled(&self, enabled: bool) {
unsafe { otNat64SetEnabled(self.as_ot_ptr(), enabled) }
}
fn nat64_set_ip4_cidr(&self, ip4_cidr: Ip4Cidr) -> Result {
Error::from(unsafe { otNat64SetIp4Cidr(self.as_ot_ptr(), ip4_cidr.as_ot_ptr()) }).into()
}
fn nat64_get_cidr(&self) -> Result<Ip4Cidr> {
let mut ip4_cidr: Ip4Cidr = Default::default();
let res: Result<(), Error> =
Error::from(unsafe { otNat64GetCidr(self.as_ot_ptr(), ip4_cidr.as_ot_mut_ptr()) })
.into();
res.map(|_: ()| ip4_cidr)
}
fn nat64_set_receive_fn<'a, F>(&'a self, f: Option<F>)
where
F: FnMut(OtMessageBox<'_>) + 'a,
{
unsafe extern "C" fn _ot_nat64_receive_callback<'a, F: FnMut(OtMessageBox<'_>) + 'a>(
message: *mut otMessage,
context: *mut ::std::os::raw::c_void,
) {
trace!("_ot_nat64_receive_callback");
let message = OtMessageBox::from_ot_ptr(message)
.expect("_ot_nat64_receive_callback: Got NULL otMessage");
let sender = &mut *(context as *mut F);
sender(message)
}
let (fn_ptr, fn_box, cb): (_, _, otNat64ReceiveIp4Callback) = if let Some(f) = f {
let mut x = Box::new(f);
(
x.as_mut() as *mut F as *mut ::std::os::raw::c_void,
Some(x as Box<dyn FnMut(OtMessageBox<'_>) + 'a>),
Some(_ot_nat64_receive_callback::<F>),
)
} else {
(std::ptr::null_mut() as *mut ::std::os::raw::c_void, None, None)
};
unsafe {
otNat64SetReceiveIp4Callback(self.as_ot_ptr(), cb, fn_ptr);
self.borrow_backing().nat64_receive_fn.set(std::mem::transmute::<
Option<Box<dyn FnMut(OtMessageBox<'_>) + 'a>>,
Option<Box<dyn FnMut(OtMessageBox<'_>) + 'static>>,
>(fn_box));
}
}
fn nat64_send(&self, message: OtMessageBox<'_>) -> Result {
Error::from(unsafe { otNat64Send(self.as_ot_ptr(), message.take_ot_ptr()) }).into()
}
fn nat64_send_data_slice(&self, data_slice: &[u8]) -> Result {
if let Ok(mut msg) = Message::ip4_new(self, None) {
msg.append(data_slice)?;
self.nat64_send(msg)?;
Ok(())
} else if self.get_buffer_info().0.mFreeBuffers == 0 {
Err(ot::Error::NoBufs)
} else {
Err(ot::Error::Failed)
}
}
fn nat64_infra_if_prefix_discover_done(
&self,
infra_if_idx: ot::NetifIndex,
ip6_prefix: openthread_sys::otIp6Prefix,
) {
unsafe {
openthread_sys::otPlatInfraIfDiscoverNat64PrefixDone(
self.as_ot_ptr(),
infra_if_idx,
&ip6_prefix,
)
}
}
fn nat64_init_address_mapping_iterator(&self, iter: &mut otNat64AddressMappingIterator) {
unsafe {
otNat64InitAddressMappingIterator(self.as_ot_ptr(), iter);
}
}
fn nat64_get_next_address_mapping(
&self,
iter: &mut otNat64AddressMappingIterator,
mapping: &mut Nat64AddressMapping,
) -> Result {
Error::from(unsafe {
otNat64GetNextAddressMapping(self.as_ot_ptr(), iter, mapping.as_ot_mut_ptr())
})
.into()
}
fn nat64_get_translator_state(&self) -> Nat64State {
unsafe { otNat64GetTranslatorState(self.as_ot_ptr()).into() }
}
fn nat64_get_prefix_manager_state(&self) -> Nat64State {
unsafe { otNat64GetPrefixManagerState(self.as_ot_ptr()).into() }
}
fn nat64_get_error_counters(&self) -> Nat64ErrorCounters {
unsafe {
let mut error_counters: otNat64ErrorCounters = Default::default();
otNat64GetErrorCounters(
self.as_ot_ptr(),
(&mut error_counters) as *mut otNat64ErrorCounters,
);
error_counters.into()
}
}
fn nat64_get_counters(&self) -> Nat64ProtocolCounters {
unsafe {
let mut protocol_counters: otNat64ProtocolCounters = Default::default();
otNat64GetCounters(
self.as_ot_ptr(),
(&mut protocol_counters) as *mut otNat64ProtocolCounters,
);
protocol_counters.into()
}
}
}