openthread/ot/types/
nat64.rs1use crate::prelude_internal::*;
6
7use core::fmt::{Debug, Formatter};
8use num_derive::FromPrimitive;
9
10#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
14pub enum Nat64State {
15 Disabled = OT_NAT64_STATE_DISABLED as isize,
17
18 NotRunning = OT_NAT64_STATE_NOT_RUNNING as isize,
20
21 Idle = OT_NAT64_STATE_IDLE as isize,
23
24 Active = OT_NAT64_STATE_ACTIVE as isize,
26}
27
28impl From<otNat64State> for Nat64State {
29 fn from(x: otNat64State) -> Self {
30 use num::FromPrimitive;
31 Self::from_u32(x).unwrap_or_else(|| panic!("Unknown otNat64State value: {x}"))
32 }
33}
34
35impl From<Nat64State> for otNat64State {
36 fn from(x: Nat64State) -> Self {
37 x as otNat64State
38 }
39}
40
41#[derive(Default, Clone, Copy)]
45#[repr(transparent)]
46pub struct Ip4Cidr(pub otIp4Cidr);
47
48impl_ot_castable!(Ip4Cidr, otIp4Cidr);
49
50impl Debug for Ip4Cidr {
51 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
52 write!(
53 f,
54 "IpAddr:{}, PrefixLength:{}",
55 std::net::Ipv4Addr::from(self.get_address_bytes()),
56 self.get_length()
57 )
58 }
59}
60
61impl PartialEq for Ip4Cidr {
62 fn eq(&self, other: &Ip4Cidr) -> bool {
63 self.get_length() == other.get_length()
64 && self.get_address_bytes() == other.get_address_bytes()
65 }
66}
67
68impl Eq for Ip4Cidr {}
69
70pub type Ip4Address = std::net::Ipv4Addr;
72
73impl Ip4Cidr {
74 pub fn new(addr: [u8; 4], len: u8) -> Ip4Cidr {
76 Ip4Cidr(otIp4Cidr {
77 mAddress: otIp4Address { mFields: otIp4Address__bindgen_ty_1 { m8: addr } },
78 mLength: len,
79 })
80 }
81
82 pub fn get_address_bytes(&self) -> [u8; 4] {
84 unsafe { self.0.mAddress.mFields.m8 }
85 }
86
87 pub fn get_length(&self) -> u8 {
89 self.0.mLength
90 }
91}
92
93#[derive(Default, Clone, Copy)]
94#[repr(transparent)]
95pub struct Nat64AddressMapping(pub otNat64AddressMapping);
97
98impl_ot_castable!(Nat64AddressMapping, otNat64AddressMapping);
99
100impl Nat64AddressMapping {
101 pub fn get_mapping_id(&self) -> u64 {
103 self.0.mId
104 }
105
106 pub fn get_ipv4_addr(&self) -> std::net::Ipv4Addr {
108 unsafe { self.0.mIp4.mFields.m8.into() }
109 }
110
111 pub fn get_ipv6_addr(&self) -> std::net::Ipv6Addr {
113 unsafe { self.0.mIp6.mFields.m8.into() }
114 }
115
116 pub fn get_remaining_time_ms(&self) -> u32 {
118 self.0.mRemainingTimeMs
119 }
120
121 pub fn get_protocol_counters(&self) -> Nat64ProtocolCounters {
123 self.0.mCounters.into()
124 }
125}
126
127impl Debug for Nat64AddressMapping {
128 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
129 write!(
130 f,
131 "mapping_id:{:?},ip4_addr:{:?},ip6_addr:{:?},remaining_time:{:?}",
132 self.get_mapping_id(),
133 self.get_ipv4_addr(),
134 self.get_ipv6_addr(),
135 self.get_remaining_time_ms()
136 )
137 }
138}
139
140#[derive(Default, Clone, Copy, Debug)]
141#[repr(transparent)]
142pub struct Nat64ProtocolCounters(pub otNat64ProtocolCounters);
144
145impl_ot_castable!(Nat64ProtocolCounters, otNat64ProtocolCounters);
146
147impl Nat64ProtocolCounters {
148 pub fn get_total_counters(&self) -> Nat64Counters {
150 self.0.mTotal.into()
151 }
152
153 pub fn get_icmp_counters(&self) -> Nat64Counters {
155 self.0.mIcmp.into()
156 }
157
158 pub fn get_udp_counters(&self) -> Nat64Counters {
160 self.0.mUdp.into()
161 }
162
163 pub fn get_tcp_counters(&self) -> Nat64Counters {
165 self.0.mTcp.into()
166 }
167}
168
169#[derive(Default, Clone, Copy, Debug)]
170#[repr(transparent)]
171pub struct Nat64Counters(pub otNat64Counters);
173
174impl_ot_castable!(Nat64Counters, otNat64Counters);
175
176impl Nat64Counters {
177 pub fn get_4_to_6_packets(&self) -> u64 {
179 self.0.m4To6Packets
180 }
181
182 pub fn get_4_to_6_bytes(&self) -> u64 {
184 self.0.m4To6Bytes
185 }
186
187 pub fn get_6_to_4_packets(&self) -> u64 {
189 self.0.m6To4Packets
190 }
191
192 pub fn get_6_to_4_bytes(&self) -> u64 {
194 self.0.m6To4Bytes
195 }
196}
197
198#[derive(Default, Clone, Copy, Debug)]
199#[repr(transparent)]
200pub struct Nat64ErrorCounters(pub otNat64ErrorCounters);
202
203impl_ot_castable!(Nat64ErrorCounters, otNat64ErrorCounters);
204
205impl Nat64ErrorCounters {
206 pub fn get_counter_4_to_6(&self) -> [u64; 4usize] {
208 self.0.mCount4To6
209 }
210
211 pub fn get_counter_6_to_4(&self) -> [u64; 4usize] {
213 self.0.mCount6To4
214 }
215}