Skip to main content

openthread/ot/types/
nat64.rs

1// Copyright 2022 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::prelude_internal::*;
6
7use core::fmt::{Debug, Formatter};
8use num_derive::FromPrimitive;
9
10/// Represents a NAT64 Translator State.
11///
12/// Functional equivalent of [`otsys::otNat64State`](crate::otsys::otNat64State).
13#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
14pub enum Nat64State {
15    /// Functional equivalent of [`otsys::OT_NAT64_STATE_DISABLED`](crate::otsys::OT_NAT64_STATE_DISABLED).
16    Disabled = OT_NAT64_STATE_DISABLED as isize,
17
18    /// Functional equivalent of [`otsys::OT_NAT64_STATE_NOT_RUNNING`](crate::otsys::OT_NAT64_STATE_NOT_RUNNING).
19    NotRunning = OT_NAT64_STATE_NOT_RUNNING as isize,
20
21    /// Functional equivalent of [`otsys::OT_NAT64_STATE_IDLE`](crate::otsys::OT_NAT64_STATE_IDLE).
22    Idle = OT_NAT64_STATE_IDLE as isize,
23
24    /// Functional equivalent of [`otsys::OT_NAT64_STATE_ACTIVE`](crate::otsys::OT_NAT64_STATE_ACTIVE).
25    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/// Data type representing an IPv4 CIDR.
42///
43/// Functional equivalent of [`otsys::otIp4Cidr`](crate::otsys::otIp4Cidr).
44#[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
70/// IPv4 address type
71pub type Ip4Address = std::net::Ipv4Addr;
72
73impl Ip4Cidr {
74    /// create a new Ip4Cidr
75    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    /// Get the address of IPv4 CIDR
83    pub fn get_address_bytes(&self) -> [u8; 4] {
84        unsafe { self.0.mAddress.mFields.m8 }
85    }
86
87    /// Get the length of IPv4 CIDR
88    pub fn get_length(&self) -> u8 {
89        self.0.mLength
90    }
91}
92
93#[derive(Default, Clone, Copy)]
94#[repr(transparent)]
95/// NAT64 Address Mapping, which is part of NAT64 telemetry
96pub struct Nat64AddressMapping(pub otNat64AddressMapping);
97
98impl_ot_castable!(Nat64AddressMapping, otNat64AddressMapping);
99
100impl Nat64AddressMapping {
101    /// Get NAT64 mapping ID
102    pub fn get_mapping_id(&self) -> u64 {
103        self.0.mId
104    }
105
106    /// Get IPv4 address
107    pub fn get_ipv4_addr(&self) -> std::net::Ipv4Addr {
108        unsafe { self.0.mIp4.mFields.m8.into() }
109    }
110
111    /// Get IPv6 address
112    pub fn get_ipv6_addr(&self) -> std::net::Ipv6Addr {
113        unsafe { self.0.mIp6.mFields.m8.into() }
114    }
115
116    /// Get the remaining time in ms
117    pub fn get_remaining_time_ms(&self) -> u32 {
118        self.0.mRemainingTimeMs
119    }
120
121    /// Get the protocol counters for this mapping
122    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)]
142/// Counters for sum of all protocols for NAT64
143pub struct Nat64ProtocolCounters(pub otNat64ProtocolCounters);
144
145impl_ot_castable!(Nat64ProtocolCounters, otNat64ProtocolCounters);
146
147impl Nat64ProtocolCounters {
148    /// Get total counters
149    pub fn get_total_counters(&self) -> Nat64Counters {
150        self.0.mTotal.into()
151    }
152
153    /// Get ICMP counters
154    pub fn get_icmp_counters(&self) -> Nat64Counters {
155        self.0.mIcmp.into()
156    }
157
158    /// Get UDP counters
159    pub fn get_udp_counters(&self) -> Nat64Counters {
160        self.0.mUdp.into()
161    }
162
163    /// Get TCP counters
164    pub fn get_tcp_counters(&self) -> Nat64Counters {
165        self.0.mTcp.into()
166    }
167}
168
169#[derive(Default, Clone, Copy, Debug)]
170#[repr(transparent)]
171/// Represents the counters for NAT64
172pub struct Nat64Counters(pub otNat64Counters);
173
174impl_ot_castable!(Nat64Counters, otNat64Counters);
175
176impl Nat64Counters {
177    /// Get IPv4 to IPv6 packets
178    pub fn get_4_to_6_packets(&self) -> u64 {
179        self.0.m4To6Packets
180    }
181
182    /// Get IPv4 to IPv6 bytes
183    pub fn get_4_to_6_bytes(&self) -> u64 {
184        self.0.m4To6Bytes
185    }
186
187    /// Get IPv6 to IPv4 packets
188    pub fn get_6_to_4_packets(&self) -> u64 {
189        self.0.m6To4Packets
190    }
191
192    /// Get IPv6 to IPv4 bytes
193    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)]
200/// Represents the error counters for NAT64
201pub struct Nat64ErrorCounters(pub otNat64ErrorCounters);
202
203impl_ot_castable!(Nat64ErrorCounters, otNat64ErrorCounters);
204
205impl Nat64ErrorCounters {
206    /// Get IPv4 to IPv6 counters
207    pub fn get_counter_4_to_6(&self) -> [u64; 4usize] {
208        self.0.mCount4To6
209    }
210
211    /// Get IPv6 to IPv4 counters
212    pub fn get_counter_6_to_4(&self) -> [u64; 4usize] {
213        self.0.mCount6To4
214    }
215}