Skip to main content

network_types/
igmp.rs

1use core::mem;
2
3use crate::{getter_be, setter_be};
4
5#[repr(C)]
6#[derive(Debug, Copy, Clone)]
7#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
8#[cfg_attr(feature = "wincode", wincode(assert_zero_copy))]
9pub struct IGMPv1Hdr {
10    /// Version/Type
11    pub vt: u8,
12
13    /// Unused field
14    pub _unused: u8,
15
16    /// Checksum
17    pub checksum: [u8; 2],
18
19    /// Group Address
20    pub group_address: [u8; 4],
21}
22
23impl IGMPv1Hdr {
24    /// The size of the IGMPv1 header in bytes (8 bytes).
25    pub const LEN: usize = mem::size_of::<IGMPv1Hdr>();
26
27    #[inline]
28    pub fn version(&self) -> u8 {
29        (self.vt >> 4) & 0xF
30    }
31
32    #[inline]
33    pub fn type_(&self) -> u8 {
34        self.vt & 0xF
35    }
36
37    #[inline]
38    pub fn checksum(&self) -> u16 {
39        // SAFETY: Pointer arithmetic in bounds of the struct.
40        unsafe { getter_be!(self, checksum, u16) }
41    }
42
43    #[inline]
44    pub fn set_checksum(&mut self, checksum: u16) {
45        // SAFETY: Pointer arithmetic in bounds of the struct.
46        unsafe { setter_be!(self, checksum, checksum) }
47    }
48
49    #[inline]
50    pub fn group_address(&self) -> u32 {
51        // SAFETY: Pointer arithmetic in bounds of the struct.
52        unsafe { getter_be!(self, group_address, u32) }
53    }
54
55    #[inline]
56    pub fn set_group_address(&mut self, group_address: u32) {
57        // SAFETY: Pointer arithmetic in bounds of the struct.
58        unsafe { setter_be!(self, group_address, group_address) }
59    }
60}
61
62#[repr(C)]
63#[derive(Debug, Copy, Clone)]
64#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
65#[cfg_attr(feature = "wincode", wincode(assert_zero_copy))]
66pub struct IGMPv2Hdr {
67    /// Message type
68    pub message_type: u8,
69
70    /// Maximum Response Time
71    pub max_response_time: u8,
72
73    /// Checksum
74    pub checksum: [u8; 2],
75
76    /// Group Address
77    pub group_address: [u8; 4],
78}
79
80impl IGMPv2Hdr {
81    /// The size of the IGMPv2 header in bytes (8 bytes).
82    pub const LEN: usize = mem::size_of::<IGMPv2Hdr>();
83
84    #[inline]
85    pub fn checksum(&self) -> u16 {
86        // SAFETY: Pointer arithmetic in bounds of the struct.
87        unsafe { getter_be!(self, checksum, u16) }
88    }
89
90    #[inline]
91    pub fn set_checksum(&mut self, checksum: u16) {
92        // SAFETY: Pointer arithmetic in bounds of the struct.
93        unsafe { setter_be!(self, checksum, checksum) }
94    }
95
96    #[inline]
97    pub fn group_address(&self) -> u32 {
98        // SAFETY: Pointer arithmetic in bounds of the struct.
99        unsafe { getter_be!(self, group_address, u32) }
100    }
101
102    #[inline]
103    pub fn set_group_address(&mut self, group_address: u32) {
104        // SAFETY: Pointer arithmetic in bounds of the struct.
105        unsafe { setter_be!(self, group_address, group_address) }
106    }
107}
108
109#[repr(C)]
110#[derive(Debug, Copy, Clone)]
111#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
112#[cfg_attr(feature = "wincode", wincode(assert_zero_copy))]
113pub struct IGMPv3MembershipQueryHdr {
114    /// Message type
115    pub message_type: u8,
116
117    /// Maximum Response Time
118    pub max_response_time: u8,
119
120    /// Checksum
121    pub checksum: [u8; 2],
122
123    /// Group Address
124    pub group_address: [u8; 4],
125
126    pub rsq: u8,
127
128    /// Querier's Query Interval Code
129    pub qqic: u8,
130
131    /// Number of Sources
132    pub nb_sources: [u8; 2],
133}
134
135impl IGMPv3MembershipQueryHdr {
136    pub const LEN: usize = mem::size_of::<IGMPv3MembershipQueryHdr>();
137
138    #[inline]
139    pub fn checksum(&self) -> u16 {
140        // SAFETY: Pointer arithmetic in bounds of the struct.
141        unsafe { getter_be!(self, checksum, u16) }
142    }
143
144    #[inline]
145    pub fn set_checksum(&mut self, checksum: u16) {
146        // SAFETY: Pointer arithmetic in bounds of the struct.
147        unsafe { setter_be!(self, checksum, checksum) }
148    }
149
150    #[inline]
151    pub fn group_address(&self) -> u32 {
152        // SAFETY: Pointer arithmetic in bounds of the struct.
153        unsafe { getter_be!(self, group_address, u32) }
154    }
155
156    #[inline]
157    pub fn set_group_address(&mut self, group_address: u32) {
158        // SAFETY: Pointer arithmetic in bounds of the struct.
159        unsafe { setter_be!(self, group_address, group_address) }
160    }
161
162    #[inline]
163    pub fn s(&self) -> u8 {
164        (self.rsq >> 3) & 1
165    }
166
167    #[inline]
168    pub fn qrv(&self) -> u8 {
169        self.rsq & 0b111
170    }
171
172    #[inline]
173    pub fn nb_sources(&self) -> u32 {
174        // SAFETY: Pointer arithmetic in bounds of the struct.
175        unsafe { getter_be!(self, nb_sources, u32) }
176    }
177
178    #[inline]
179    pub fn set_nb_sources(&mut self, nb_sources: u32) {
180        // SAFETY: Pointer arithmetic in bounds of the struct.
181        unsafe { setter_be!(self, nb_sources, nb_sources) }
182    }
183}
184
185#[repr(C)]
186#[derive(Debug, Copy, Clone)]
187#[cfg_attr(feature = "wincode", derive(wincode::SchemaRead, wincode::SchemaWrite))]
188#[cfg_attr(feature = "wincode", wincode(assert_zero_copy))]
189pub struct IGMPv3MembershipReportHdr {
190    /// Message type
191    pub message_type: u8,
192
193    _unused_1: u8,
194
195    /// Checksum
196    pub checksum: [u8; 2],
197
198    _unused_2: [u8; 2],
199
200    /// Number of Sources
201    pub nb_group_records: [u8; 2],
202}
203
204impl IGMPv3MembershipReportHdr {
205    pub const LEN: usize = mem::size_of::<IGMPv3MembershipReportHdr>();
206
207    #[inline]
208    pub fn checksum(&self) -> u16 {
209        // SAFETY: Pointer arithmetic in bounds of the struct.
210        unsafe { getter_be!(self, checksum, u16) }
211    }
212
213    #[inline]
214    pub fn set_checksum(&mut self, checksum: u16) {
215        // SAFETY: Pointer arithmetic in bounds of the struct.
216        unsafe { setter_be!(self, checksum, checksum) }
217    }
218
219    #[inline]
220    pub fn nb_group_records(&self) -> u16 {
221        // SAFETY: Pointer arithmetic in bounds of the struct.
222        unsafe { getter_be!(self, nb_group_records, u16) }
223    }
224
225    #[inline]
226    pub fn set_nb_group_records(&mut self, nb_group_records: u16) {
227        // SAFETY: Pointer arithmetic in bounds of the struct.
228        unsafe { setter_be!(self, nb_group_records, nb_group_records) }
229    }
230}