openthread/ot/types/
nat64.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use crate::prelude_internal::*;

use core::fmt::{Debug, Formatter};
use num_derive::FromPrimitive;

/// Represents a NAT64 Translator State.
///
/// Functional equivalent of [`otsys::otNat64State`](crate::otsys::otNat64State).
#[derive(Debug, Copy, Clone, Eq, Ord, PartialOrd, PartialEq, FromPrimitive)]
pub enum Nat64State {
    /// Functional equivalent of [`otsys::OT_NAT64_STATE_DISABLED`](crate::otsys::OT_NAT64_STATE_DISABLED).
    Disabled = OT_NAT64_STATE_DISABLED as isize,

    /// Functional equivalent of [`otsys::OT_NAT64_STATE_NOT_RUNNING`](crate::otsys::OT_NAT64_STATE_NOT_RUNNING).
    NotRunning = OT_NAT64_STATE_NOT_RUNNING as isize,

    /// Functional equivalent of [`otsys::OT_NAT64_STATE_IDLE`](crate::otsys::OT_NAT64_STATE_IDLE).
    Idle = OT_NAT64_STATE_IDLE as isize,

    /// Functional equivalent of [`otsys::OT_NAT64_STATE_ACTIVE`](crate::otsys::OT_NAT64_STATE_ACTIVE).
    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
    }
}

/// Data type representing an IPv4 CIDR.
///
/// Functional equivalent of [`otsys::otIp4Cidr`](crate::otsys::otIp4Cidr).
#[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 {}

/// IPv4 address type
pub type Ip4Address = std::net::Ipv4Addr;

impl Ip4Cidr {
    /// create a new Ip4Cidr
    pub fn new(addr: [u8; 4], len: u8) -> Ip4Cidr {
        Ip4Cidr(otIp4Cidr {
            mAddress: otIp4Address { mFields: otIp4Address__bindgen_ty_1 { m8: addr } },
            mLength: len,
        })
    }

    /// Get the address of IPv4 CIDR
    pub fn get_address_bytes(&self) -> [u8; 4] {
        unsafe { self.0.mAddress.mFields.m8 }
    }

    /// Get the length of IPv4 CIDR
    pub fn get_length(&self) -> u8 {
        self.0.mLength
    }
}

#[derive(Default, Clone, Copy)]
#[repr(transparent)]
/// NAT64 Address Mapping, which is part of NAT64 telemetry
pub struct Nat64AddressMapping(pub otNat64AddressMapping);

impl_ot_castable!(Nat64AddressMapping, otNat64AddressMapping);

impl Nat64AddressMapping {
    /// Get NAT64 mapping ID
    pub fn get_mapping_id(&self) -> u64 {
        self.0.mId
    }

    /// Get IPv4 address
    pub fn get_ipv4_addr(&self) -> std::net::Ipv4Addr {
        unsafe { self.0.mIp4.mFields.m8.into() }
    }

    /// Get IPv6 address
    pub fn get_ipv6_addr(&self) -> std::net::Ipv6Addr {
        unsafe { self.0.mIp6.mFields.m8.into() }
    }

    /// Get the remaining time in ms
    pub fn get_remaining_time_ms(&self) -> u32 {
        self.0.mRemainingTimeMs
    }

    /// Get the protocol counters for this mapping
    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)]
/// Counters for sum of all protocols for NAT64
pub struct Nat64ProtocolCounters(pub otNat64ProtocolCounters);

impl_ot_castable!(Nat64ProtocolCounters, otNat64ProtocolCounters);

impl Nat64ProtocolCounters {
    /// Get total counters
    pub fn get_total_counters(&self) -> Nat64Counters {
        self.0.mTotal.into()
    }

    /// Get ICMP counters
    pub fn get_icmp_counters(&self) -> Nat64Counters {
        self.0.mIcmp.into()
    }

    /// Get UDP counters
    pub fn get_udp_counters(&self) -> Nat64Counters {
        self.0.mUdp.into()
    }

    /// Get TCP counters
    pub fn get_tcp_counters(&self) -> Nat64Counters {
        self.0.mTcp.into()
    }
}

#[derive(Default, Clone, Copy, Debug)]
#[repr(transparent)]
/// Represents the counters for NAT64
pub struct Nat64Counters(pub otNat64Counters);

impl_ot_castable!(Nat64Counters, otNat64Counters);

impl Nat64Counters {
    /// Get IPv4 to IPv6 packets
    pub fn get_4_to_6_packets(&self) -> u64 {
        self.0.m4To6Packets
    }

    /// Get IPv4 to IPv6 bytes
    pub fn get_4_to_6_bytes(&self) -> u64 {
        self.0.m4To6Bytes
    }

    /// Get IPv6 to IPv4 packets
    pub fn get_6_to_4_packets(&self) -> u64 {
        self.0.m6To4Packets
    }

    /// Get IPv6 to IPv4 bytes
    pub fn get_6_to_4_bytes(&self) -> u64 {
        self.0.m6To4Bytes
    }
}

#[derive(Default, Clone, Copy, Debug)]
#[repr(transparent)]
/// Represents the error counters for NAT64
pub struct Nat64ErrorCounters(pub otNat64ErrorCounters);

impl_ot_castable!(Nat64ErrorCounters, otNat64ErrorCounters);

impl Nat64ErrorCounters {
    /// Get IPv4 to IPv6 counters
    pub fn get_counter_4_to_6(&self) -> [u64; 4usize] {
        self.0.mCount4To6
    }

    /// Get IPv6 to IPv4 counters
    pub fn get_counter_6_to_4(&self) -> [u64; 4usize] {
        self.0.mCount6To4
    }
}