1use std::{ffi::CStr, fmt, str::FromStr};
4
5use crate::{
6 bindings::{
7 ip4_header_t, ip6_address_t, ip6_header_t, vlib_helper_format_ip4_header,
8 vlib_helper_format_ip6_header, vlib_helper_format_vnet_sw_if_index_name,
9 vlib_helper_unformat_vnet_sw_interface, vnet_get_main,
10 },
11 vppinfra,
12};
13
14impl ip6_address_t {
15 pub const fn new() -> Self {
17 Self { as_u8: [0; 16] }
18 }
19}
20
21impl ip6_header_t {
22 pub const fn new() -> Self {
24 Self {
25 ip_version_traffic_class_and_flow_label: 0,
26 payload_length: 0,
27 protocol: 0,
28 hop_limit: 0,
29 src_address: ip6_address_t::new(),
30 dst_address: ip6_address_t::new(),
31 }
32 }
33}
34
35impl fmt::Debug for ip6_header_t {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 <Self as fmt::Display>::fmt(self, f)
38 }
39}
40
41impl fmt::Display for ip6_header_t {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 unsafe {
46 let s = vppinfra::vec::Vec::from_raw(vlib_helper_format_ip6_header(
47 std::ptr::null_mut(),
48 self,
49 std::mem::size_of::<Self>(),
50 ));
51 let cstr = CStr::from_bytes_with_nul_unchecked(&s);
52 write!(f, "{}", cstr.to_string_lossy())
53 }
54 }
55}
56
57impl Clone for ip6_header_t {
58 fn clone(&self) -> Self {
59 unsafe {
62 Self {
63 ip_version_traffic_class_and_flow_label: std::ptr::read_unaligned(
64 std::ptr::addr_of!(self.ip_version_traffic_class_and_flow_label),
65 ),
66 payload_length: std::ptr::read_unaligned(std::ptr::addr_of!(self.payload_length)),
67 protocol: self.protocol,
68 hop_limit: self.hop_limit,
69 src_address: self.src_address,
70 dst_address: self.dst_address,
71 }
72 }
73 }
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
78pub struct SwIfIndex(u32);
79
80impl SwIfIndex {
81 pub const fn new(sw_if_index: u32) -> Self {
83 Self(sw_if_index)
84 }
85}
86
87impl fmt::Display for SwIfIndex {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 unsafe {
94 let s = vppinfra::vec::Vec::from_raw(vlib_helper_format_vnet_sw_if_index_name(
95 std::ptr::null_mut(),
96 vnet_get_main(),
97 self.0,
98 ));
99 let cstr = CStr::from_bytes_with_nul_unchecked(&s);
100 write!(f, "{}", cstr.to_string_lossy())
101 }
102 }
103}
104
105impl From<u32> for SwIfIndex {
106 fn from(value: u32) -> Self {
107 Self(value)
108 }
109}
110
111impl From<SwIfIndex> for u32 {
112 fn from(value: SwIfIndex) -> Self {
113 value.0
114 }
115}
116
117impl FromStr for SwIfIndex {
118 type Err = ();
119
120 fn from_str(s: &str) -> Result<Self, Self::Err> {
121 let mut i = vppinfra::unformat::UnformatInput::from(s);
122 let mut sw_if_index = 0;
123 let ret = unsafe {
126 vlib_helper_unformat_vnet_sw_interface(i.as_ptr(), vnet_get_main(), &mut sw_if_index)
127 };
128 if ret > 0 {
129 Ok(Self(sw_if_index))
130 } else {
131 Err(())
132 }
133 }
134}
135
136impl fmt::Debug for ip4_header_t {
137 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138 <Self as fmt::Display>::fmt(self, f)
139 }
140}
141
142impl fmt::Display for ip4_header_t {
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 unsafe {
147 let s = vppinfra::vec::Vec::from_raw(vlib_helper_format_ip4_header(
148 std::ptr::null_mut(),
149 self,
150 std::mem::size_of::<Self>(),
151 ));
152 let cstr = CStr::from_bytes_with_nul_unchecked(&s);
153 write!(f, "{}", cstr.to_string_lossy())
154 }
155 }
156}