1#![doc = "genetlink meta-family that exposes information about all genetlink\nfamilies registered in the kernel (including itself).\n"]
2#![allow(clippy::all)]
3#![allow(unused_imports)]
4#![allow(unused_assignments)]
5#![allow(non_snake_case)]
6#![allow(unused_variables)]
7#![allow(irrefutable_let_patterns)]
8#![allow(unreachable_code)]
9#![allow(unreachable_patterns)]
10use crate::builtin::{PushBuiltinBitfield32, PushBuiltinNfgenmsg, PushDummy, PushNlmsghdr};
11use crate::{
12 consts,
13 traits::{NetlinkRequest, Protocol},
14 utils::*,
15};
16pub const PROTONAME: &CStr = c"nlctrl";
17#[doc = "Flags - defines an integer enumeration, with values for each entry occupying a bit, starting from bit 0, (e.g. 1, 2, 4, 8)"]
18#[derive(Debug, Clone, Copy)]
19pub enum OpFlags {
20 AdminPerm = 1 << 0,
21 CmdCapDo = 1 << 1,
22 CmdCapDump = 1 << 2,
23 CmdCapHaspol = 1 << 3,
24 UnsAdminPerm = 1 << 4,
25}
26impl OpFlags {
27 pub fn from_value(value: u64) -> Option<Self> {
28 Some(match value {
29 n if n == 1 << 0 => Self::AdminPerm,
30 n if n == 1 << 1 => Self::CmdCapDo,
31 n if n == 1 << 2 => Self::CmdCapDump,
32 n if n == 1 << 3 => Self::CmdCapHaspol,
33 n if n == 1 << 4 => Self::UnsAdminPerm,
34 _ => return None,
35 })
36 }
37}
38#[doc = "Enum - defines an integer enumeration, with values for each entry incrementing by 1, (e.g. 0, 1, 2, 3)"]
39#[derive(Debug, Clone, Copy)]
40pub enum AttrType {
41 Invalid = 0,
42 Flag = 1,
43 U8 = 2,
44 U16 = 3,
45 U32 = 4,
46 U64 = 5,
47 S8 = 6,
48 S16 = 7,
49 S32 = 8,
50 S64 = 9,
51 Binary = 10,
52 String = 11,
53 NulString = 12,
54 Nested = 13,
55 NestedArray = 14,
56 Bitfield32 = 15,
57 Sint = 16,
58 Uint = 17,
59}
60impl AttrType {
61 pub fn from_value(value: u64) -> Option<Self> {
62 Some(match value {
63 0 => Self::Invalid,
64 1 => Self::Flag,
65 2 => Self::U8,
66 3 => Self::U16,
67 4 => Self::U32,
68 5 => Self::U64,
69 6 => Self::S8,
70 7 => Self::S16,
71 8 => Self::S32,
72 9 => Self::S64,
73 10 => Self::Binary,
74 11 => Self::String,
75 12 => Self::NulString,
76 13 => Self::Nested,
77 14 => Self::NestedArray,
78 15 => Self::Bitfield32,
79 16 => Self::Sint,
80 17 => Self::Uint,
81 _ => return None,
82 })
83 }
84}
85#[derive(Clone)]
86pub enum CtrlAttrs<'a> {
87 FamilyId(u16),
88 FamilyName(&'a CStr),
89 Version(u32),
90 Hdrsize(u32),
91 Maxattr(u32),
92 Ops(IterableArrayOpAttrs<'a>),
93 McastGroups(IterableArrayMcastGroupAttrs<'a>),
94 Policy(IterablePolicyAttrs<'a>),
95 OpPolicy(IterableOpPolicyAttrs<'a>),
96 Op(u32),
97}
98impl<'a> IterableCtrlAttrs<'a> {
99 pub fn get_family_id(&self) -> Result<u16, ErrorContext> {
100 let mut iter = self.clone();
101 iter.pos = 0;
102 for attr in iter {
103 if let CtrlAttrs::FamilyId(val) = attr? {
104 return Ok(val);
105 }
106 }
107 Err(ErrorContext::new_missing(
108 "CtrlAttrs",
109 "FamilyId",
110 self.orig_loc,
111 self.buf.as_ptr() as usize,
112 ))
113 }
114 pub fn get_family_name(&self) -> Result<&'a CStr, ErrorContext> {
115 let mut iter = self.clone();
116 iter.pos = 0;
117 for attr in iter {
118 if let CtrlAttrs::FamilyName(val) = attr? {
119 return Ok(val);
120 }
121 }
122 Err(ErrorContext::new_missing(
123 "CtrlAttrs",
124 "FamilyName",
125 self.orig_loc,
126 self.buf.as_ptr() as usize,
127 ))
128 }
129 pub fn get_version(&self) -> Result<u32, ErrorContext> {
130 let mut iter = self.clone();
131 iter.pos = 0;
132 for attr in iter {
133 if let CtrlAttrs::Version(val) = attr? {
134 return Ok(val);
135 }
136 }
137 Err(ErrorContext::new_missing(
138 "CtrlAttrs",
139 "Version",
140 self.orig_loc,
141 self.buf.as_ptr() as usize,
142 ))
143 }
144 pub fn get_hdrsize(&self) -> Result<u32, ErrorContext> {
145 let mut iter = self.clone();
146 iter.pos = 0;
147 for attr in iter {
148 if let CtrlAttrs::Hdrsize(val) = attr? {
149 return Ok(val);
150 }
151 }
152 Err(ErrorContext::new_missing(
153 "CtrlAttrs",
154 "Hdrsize",
155 self.orig_loc,
156 self.buf.as_ptr() as usize,
157 ))
158 }
159 pub fn get_maxattr(&self) -> Result<u32, ErrorContext> {
160 let mut iter = self.clone();
161 iter.pos = 0;
162 for attr in iter {
163 if let CtrlAttrs::Maxattr(val) = attr? {
164 return Ok(val);
165 }
166 }
167 Err(ErrorContext::new_missing(
168 "CtrlAttrs",
169 "Maxattr",
170 self.orig_loc,
171 self.buf.as_ptr() as usize,
172 ))
173 }
174 pub fn get_ops(
175 &self,
176 ) -> Result<ArrayIterable<IterableArrayOpAttrs<'a>, IterableOpAttrs<'a>>, ErrorContext> {
177 for attr in self.clone() {
178 if let CtrlAttrs::Ops(val) = attr? {
179 return Ok(ArrayIterable::new(val));
180 }
181 }
182 Err(ErrorContext::new_missing(
183 "CtrlAttrs",
184 "Ops",
185 self.orig_loc,
186 self.buf.as_ptr() as usize,
187 ))
188 }
189 pub fn get_mcast_groups(
190 &self,
191 ) -> Result<
192 ArrayIterable<IterableArrayMcastGroupAttrs<'a>, IterableMcastGroupAttrs<'a>>,
193 ErrorContext,
194 > {
195 for attr in self.clone() {
196 if let CtrlAttrs::McastGroups(val) = attr? {
197 return Ok(ArrayIterable::new(val));
198 }
199 }
200 Err(ErrorContext::new_missing(
201 "CtrlAttrs",
202 "McastGroups",
203 self.orig_loc,
204 self.buf.as_ptr() as usize,
205 ))
206 }
207 pub fn get_policy(&self) -> Result<IterablePolicyAttrs<'a>, ErrorContext> {
208 let mut iter = self.clone();
209 iter.pos = 0;
210 for attr in iter {
211 if let CtrlAttrs::Policy(val) = attr? {
212 return Ok(val);
213 }
214 }
215 Err(ErrorContext::new_missing(
216 "CtrlAttrs",
217 "Policy",
218 self.orig_loc,
219 self.buf.as_ptr() as usize,
220 ))
221 }
222 pub fn get_op_policy(&self) -> Result<IterableOpPolicyAttrs<'a>, ErrorContext> {
223 let mut iter = self.clone();
224 iter.pos = 0;
225 for attr in iter {
226 if let CtrlAttrs::OpPolicy(val) = attr? {
227 return Ok(val);
228 }
229 }
230 Err(ErrorContext::new_missing(
231 "CtrlAttrs",
232 "OpPolicy",
233 self.orig_loc,
234 self.buf.as_ptr() as usize,
235 ))
236 }
237 pub fn get_op(&self) -> Result<u32, ErrorContext> {
238 let mut iter = self.clone();
239 iter.pos = 0;
240 for attr in iter {
241 if let CtrlAttrs::Op(val) = attr? {
242 return Ok(val);
243 }
244 }
245 Err(ErrorContext::new_missing(
246 "CtrlAttrs",
247 "Op",
248 self.orig_loc,
249 self.buf.as_ptr() as usize,
250 ))
251 }
252}
253impl OpAttrs {
254 pub fn new_array(buf: &[u8]) -> IterableArrayOpAttrs<'_> {
255 IterableArrayOpAttrs::with_loc(buf, buf.as_ptr() as usize)
256 }
257}
258#[derive(Clone, Copy, Default)]
259pub struct IterableArrayOpAttrs<'a> {
260 buf: &'a [u8],
261 pos: usize,
262 orig_loc: usize,
263}
264impl<'a> IterableArrayOpAttrs<'a> {
265 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
266 Self {
267 buf,
268 pos: 0,
269 orig_loc,
270 }
271 }
272 pub fn get_buf(&self) -> &'a [u8] {
273 self.buf
274 }
275}
276impl<'a> Iterator for IterableArrayOpAttrs<'a> {
277 type Item = Result<IterableOpAttrs<'a>, ErrorContext>;
278 fn next(&mut self) -> Option<Self::Item> {
279 if self.buf.len() == self.pos {
280 return None;
281 }
282 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
283 {
284 return Some(Ok(IterableOpAttrs::with_loc(next, self.orig_loc)));
285 }
286 }
287 Some(Err(ErrorContext::new(
288 "OpAttrs",
289 None,
290 self.orig_loc,
291 self.buf.as_ptr().wrapping_add(self.pos) as usize,
292 )))
293 }
294}
295impl<'a> McastGroupAttrs<'a> {
296 pub fn new_array(buf: &[u8]) -> IterableArrayMcastGroupAttrs<'_> {
297 IterableArrayMcastGroupAttrs::with_loc(buf, buf.as_ptr() as usize)
298 }
299}
300#[derive(Clone, Copy, Default)]
301pub struct IterableArrayMcastGroupAttrs<'a> {
302 buf: &'a [u8],
303 pos: usize,
304 orig_loc: usize,
305}
306impl<'a> IterableArrayMcastGroupAttrs<'a> {
307 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
308 Self {
309 buf,
310 pos: 0,
311 orig_loc,
312 }
313 }
314 pub fn get_buf(&self) -> &'a [u8] {
315 self.buf
316 }
317}
318impl<'a> Iterator for IterableArrayMcastGroupAttrs<'a> {
319 type Item = Result<IterableMcastGroupAttrs<'a>, ErrorContext>;
320 fn next(&mut self) -> Option<Self::Item> {
321 if self.buf.len() == self.pos {
322 return None;
323 }
324 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
325 {
326 return Some(Ok(IterableMcastGroupAttrs::with_loc(next, self.orig_loc)));
327 }
328 }
329 Some(Err(ErrorContext::new(
330 "McastGroupAttrs",
331 None,
332 self.orig_loc,
333 self.buf.as_ptr().wrapping_add(self.pos) as usize,
334 )))
335 }
336}
337impl<'a> CtrlAttrs<'a> {
338 pub fn new(buf: &'a [u8]) -> IterableCtrlAttrs<'a> {
339 IterableCtrlAttrs::with_loc(buf, buf.as_ptr() as usize)
340 }
341 fn attr_from_type(r#type: u16) -> Option<&'static str> {
342 let res = match r#type {
343 1u16 => "FamilyId",
344 2u16 => "FamilyName",
345 3u16 => "Version",
346 4u16 => "Hdrsize",
347 5u16 => "Maxattr",
348 6u16 => "Ops",
349 7u16 => "McastGroups",
350 8u16 => "Policy",
351 9u16 => "OpPolicy",
352 10u16 => "Op",
353 _ => return None,
354 };
355 Some(res)
356 }
357}
358#[derive(Clone, Copy, Default)]
359pub struct IterableCtrlAttrs<'a> {
360 buf: &'a [u8],
361 pos: usize,
362 orig_loc: usize,
363}
364impl<'a> IterableCtrlAttrs<'a> {
365 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
366 Self {
367 buf,
368 pos: 0,
369 orig_loc,
370 }
371 }
372 pub fn get_buf(&self) -> &'a [u8] {
373 self.buf
374 }
375}
376impl<'a> Iterator for IterableCtrlAttrs<'a> {
377 type Item = Result<CtrlAttrs<'a>, ErrorContext>;
378 fn next(&mut self) -> Option<Self::Item> {
379 if self.buf.len() == self.pos {
380 return None;
381 }
382 let pos = self.pos;
383 let mut r#type = None;
384 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
385 r#type = Some(header.r#type);
386 let res = match header.r#type {
387 1u16 => CtrlAttrs::FamilyId({
388 let res = parse_u16(next);
389 let Some(val) = res else { break };
390 val
391 }),
392 2u16 => CtrlAttrs::FamilyName({
393 let res = CStr::from_bytes_with_nul(next).ok();
394 let Some(val) = res else { break };
395 val
396 }),
397 3u16 => CtrlAttrs::Version({
398 let res = parse_u32(next);
399 let Some(val) = res else { break };
400 val
401 }),
402 4u16 => CtrlAttrs::Hdrsize({
403 let res = parse_u32(next);
404 let Some(val) = res else { break };
405 val
406 }),
407 5u16 => CtrlAttrs::Maxattr({
408 let res = parse_u32(next);
409 let Some(val) = res else { break };
410 val
411 }),
412 6u16 => CtrlAttrs::Ops({
413 let res = Some(IterableArrayOpAttrs::with_loc(next, self.orig_loc));
414 let Some(val) = res else { break };
415 val
416 }),
417 7u16 => CtrlAttrs::McastGroups({
418 let res = Some(IterableArrayMcastGroupAttrs::with_loc(next, self.orig_loc));
419 let Some(val) = res else { break };
420 val
421 }),
422 8u16 => CtrlAttrs::Policy({
423 let res = Some(IterablePolicyAttrs::with_loc(next, self.orig_loc));
424 let Some(val) = res else { break };
425 val
426 }),
427 9u16 => CtrlAttrs::OpPolicy({
428 let res = Some(IterableOpPolicyAttrs::with_loc(next, self.orig_loc));
429 let Some(val) = res else { break };
430 val
431 }),
432 10u16 => CtrlAttrs::Op({
433 let res = parse_u32(next);
434 let Some(val) = res else { break };
435 val
436 }),
437 n => {
438 if cfg!(any(test, feature = "deny-unknown-attrs")) {
439 break;
440 } else {
441 continue;
442 }
443 }
444 };
445 return Some(Ok(res));
446 }
447 Some(Err(ErrorContext::new(
448 "CtrlAttrs",
449 r#type.and_then(|t| CtrlAttrs::attr_from_type(t)),
450 self.orig_loc,
451 self.buf.as_ptr().wrapping_add(pos) as usize,
452 )))
453 }
454}
455impl std::fmt::Debug for IterableArrayOpAttrs<'_> {
456 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
457 fmt.debug_list()
458 .entries(self.clone().map(FlattenErrorContext))
459 .finish()
460 }
461}
462impl std::fmt::Debug for IterableArrayMcastGroupAttrs<'_> {
463 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
464 fmt.debug_list()
465 .entries(self.clone().map(FlattenErrorContext))
466 .finish()
467 }
468}
469impl<'a> std::fmt::Debug for IterableCtrlAttrs<'_> {
470 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
471 let mut fmt = f.debug_struct("CtrlAttrs");
472 for attr in self.clone() {
473 let attr = match attr {
474 Ok(a) => a,
475 Err(err) => {
476 fmt.finish()?;
477 f.write_str("Err(")?;
478 err.fmt(f)?;
479 return f.write_str(")");
480 }
481 };
482 match attr {
483 CtrlAttrs::FamilyId(val) => fmt.field("FamilyId", &val),
484 CtrlAttrs::FamilyName(val) => fmt.field("FamilyName", &val),
485 CtrlAttrs::Version(val) => fmt.field("Version", &val),
486 CtrlAttrs::Hdrsize(val) => fmt.field("Hdrsize", &val),
487 CtrlAttrs::Maxattr(val) => fmt.field("Maxattr", &val),
488 CtrlAttrs::Ops(val) => fmt.field("Ops", &val),
489 CtrlAttrs::McastGroups(val) => fmt.field("McastGroups", &val),
490 CtrlAttrs::Policy(val) => fmt.field("Policy", &val),
491 CtrlAttrs::OpPolicy(val) => fmt.field("OpPolicy", &val),
492 CtrlAttrs::Op(val) => fmt.field("Op", &val),
493 };
494 }
495 fmt.finish()
496 }
497}
498impl IterableCtrlAttrs<'_> {
499 pub fn lookup_attr(
500 &self,
501 offset: usize,
502 missing_type: Option<u16>,
503 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
504 let mut stack = Vec::new();
505 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
506 if cur == offset {
507 stack.push(("CtrlAttrs", offset));
508 return (
509 stack,
510 missing_type.and_then(|t| CtrlAttrs::attr_from_type(t)),
511 );
512 }
513 if cur > offset || cur + self.buf.len() < offset {
514 return (stack, None);
515 }
516 let mut attrs = self.clone();
517 let mut last_off = cur + attrs.pos;
518 let mut missing = None;
519 while let Some(attr) = attrs.next() {
520 let Ok(attr) = attr else { break };
521 match attr {
522 CtrlAttrs::FamilyId(val) => {
523 if last_off == offset {
524 stack.push(("FamilyId", last_off));
525 break;
526 }
527 }
528 CtrlAttrs::FamilyName(val) => {
529 if last_off == offset {
530 stack.push(("FamilyName", last_off));
531 break;
532 }
533 }
534 CtrlAttrs::Version(val) => {
535 if last_off == offset {
536 stack.push(("Version", last_off));
537 break;
538 }
539 }
540 CtrlAttrs::Hdrsize(val) => {
541 if last_off == offset {
542 stack.push(("Hdrsize", last_off));
543 break;
544 }
545 }
546 CtrlAttrs::Maxattr(val) => {
547 if last_off == offset {
548 stack.push(("Maxattr", last_off));
549 break;
550 }
551 }
552 CtrlAttrs::Ops(val) => {
553 for entry in val {
554 let Ok(attr) = entry else { break };
555 (stack, missing) = attr.lookup_attr(offset, missing_type);
556 if !stack.is_empty() {
557 break;
558 }
559 }
560 if !stack.is_empty() {
561 stack.push(("Ops", last_off));
562 break;
563 }
564 }
565 CtrlAttrs::McastGroups(val) => {
566 for entry in val {
567 let Ok(attr) = entry else { break };
568 (stack, missing) = attr.lookup_attr(offset, missing_type);
569 if !stack.is_empty() {
570 break;
571 }
572 }
573 if !stack.is_empty() {
574 stack.push(("McastGroups", last_off));
575 break;
576 }
577 }
578 CtrlAttrs::Policy(val) => {
579 (stack, missing) = val.lookup_attr(offset, missing_type);
580 if !stack.is_empty() {
581 break;
582 }
583 }
584 CtrlAttrs::OpPolicy(val) => {
585 (stack, missing) = val.lookup_attr(offset, missing_type);
586 if !stack.is_empty() {
587 break;
588 }
589 }
590 CtrlAttrs::Op(val) => {
591 if last_off == offset {
592 stack.push(("Op", last_off));
593 break;
594 }
595 }
596 _ => {}
597 };
598 last_off = cur + attrs.pos;
599 }
600 if !stack.is_empty() {
601 stack.push(("CtrlAttrs", cur));
602 }
603 (stack, missing)
604 }
605}
606#[derive(Clone)]
607pub enum McastGroupAttrs<'a> {
608 Name(&'a CStr),
609 Id(u32),
610}
611impl<'a> IterableMcastGroupAttrs<'a> {
612 pub fn get_name(&self) -> Result<&'a CStr, ErrorContext> {
613 let mut iter = self.clone();
614 iter.pos = 0;
615 for attr in iter {
616 if let McastGroupAttrs::Name(val) = attr? {
617 return Ok(val);
618 }
619 }
620 Err(ErrorContext::new_missing(
621 "McastGroupAttrs",
622 "Name",
623 self.orig_loc,
624 self.buf.as_ptr() as usize,
625 ))
626 }
627 pub fn get_id(&self) -> Result<u32, ErrorContext> {
628 let mut iter = self.clone();
629 iter.pos = 0;
630 for attr in iter {
631 if let McastGroupAttrs::Id(val) = attr? {
632 return Ok(val);
633 }
634 }
635 Err(ErrorContext::new_missing(
636 "McastGroupAttrs",
637 "Id",
638 self.orig_loc,
639 self.buf.as_ptr() as usize,
640 ))
641 }
642}
643impl<'a> McastGroupAttrs<'a> {
644 pub fn new(buf: &'a [u8]) -> IterableMcastGroupAttrs<'a> {
645 IterableMcastGroupAttrs::with_loc(buf, buf.as_ptr() as usize)
646 }
647 fn attr_from_type(r#type: u16) -> Option<&'static str> {
648 let res = match r#type {
649 1u16 => "Name",
650 2u16 => "Id",
651 _ => return None,
652 };
653 Some(res)
654 }
655}
656#[derive(Clone, Copy, Default)]
657pub struct IterableMcastGroupAttrs<'a> {
658 buf: &'a [u8],
659 pos: usize,
660 orig_loc: usize,
661}
662impl<'a> IterableMcastGroupAttrs<'a> {
663 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
664 Self {
665 buf,
666 pos: 0,
667 orig_loc,
668 }
669 }
670 pub fn get_buf(&self) -> &'a [u8] {
671 self.buf
672 }
673}
674impl<'a> Iterator for IterableMcastGroupAttrs<'a> {
675 type Item = Result<McastGroupAttrs<'a>, ErrorContext>;
676 fn next(&mut self) -> Option<Self::Item> {
677 if self.buf.len() == self.pos {
678 return None;
679 }
680 let pos = self.pos;
681 let mut r#type = None;
682 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
683 r#type = Some(header.r#type);
684 let res = match header.r#type {
685 1u16 => McastGroupAttrs::Name({
686 let res = CStr::from_bytes_with_nul(next).ok();
687 let Some(val) = res else { break };
688 val
689 }),
690 2u16 => McastGroupAttrs::Id({
691 let res = parse_u32(next);
692 let Some(val) = res else { break };
693 val
694 }),
695 n => {
696 if cfg!(any(test, feature = "deny-unknown-attrs")) {
697 break;
698 } else {
699 continue;
700 }
701 }
702 };
703 return Some(Ok(res));
704 }
705 Some(Err(ErrorContext::new(
706 "McastGroupAttrs",
707 r#type.and_then(|t| McastGroupAttrs::attr_from_type(t)),
708 self.orig_loc,
709 self.buf.as_ptr().wrapping_add(pos) as usize,
710 )))
711 }
712}
713impl<'a> std::fmt::Debug for IterableMcastGroupAttrs<'_> {
714 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
715 let mut fmt = f.debug_struct("McastGroupAttrs");
716 for attr in self.clone() {
717 let attr = match attr {
718 Ok(a) => a,
719 Err(err) => {
720 fmt.finish()?;
721 f.write_str("Err(")?;
722 err.fmt(f)?;
723 return f.write_str(")");
724 }
725 };
726 match attr {
727 McastGroupAttrs::Name(val) => fmt.field("Name", &val),
728 McastGroupAttrs::Id(val) => fmt.field("Id", &val),
729 };
730 }
731 fmt.finish()
732 }
733}
734impl IterableMcastGroupAttrs<'_> {
735 pub fn lookup_attr(
736 &self,
737 offset: usize,
738 missing_type: Option<u16>,
739 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
740 let mut stack = Vec::new();
741 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
742 if cur == offset {
743 stack.push(("McastGroupAttrs", offset));
744 return (
745 stack,
746 missing_type.and_then(|t| McastGroupAttrs::attr_from_type(t)),
747 );
748 }
749 if cur > offset || cur + self.buf.len() < offset {
750 return (stack, None);
751 }
752 let mut attrs = self.clone();
753 let mut last_off = cur + attrs.pos;
754 while let Some(attr) = attrs.next() {
755 let Ok(attr) = attr else { break };
756 match attr {
757 McastGroupAttrs::Name(val) => {
758 if last_off == offset {
759 stack.push(("Name", last_off));
760 break;
761 }
762 }
763 McastGroupAttrs::Id(val) => {
764 if last_off == offset {
765 stack.push(("Id", last_off));
766 break;
767 }
768 }
769 _ => {}
770 };
771 last_off = cur + attrs.pos;
772 }
773 if !stack.is_empty() {
774 stack.push(("McastGroupAttrs", cur));
775 }
776 (stack, None)
777 }
778}
779#[derive(Clone)]
780pub enum OpAttrs {
781 Id(u32),
782 #[doc = "Associated type: \"OpFlags\" (1 bit per enumeration)"]
783 Flags(u32),
784}
785impl<'a> IterableOpAttrs<'a> {
786 pub fn get_id(&self) -> Result<u32, ErrorContext> {
787 let mut iter = self.clone();
788 iter.pos = 0;
789 for attr in iter {
790 if let OpAttrs::Id(val) = attr? {
791 return Ok(val);
792 }
793 }
794 Err(ErrorContext::new_missing(
795 "OpAttrs",
796 "Id",
797 self.orig_loc,
798 self.buf.as_ptr() as usize,
799 ))
800 }
801 #[doc = "Associated type: \"OpFlags\" (1 bit per enumeration)"]
802 pub fn get_flags(&self) -> Result<u32, ErrorContext> {
803 let mut iter = self.clone();
804 iter.pos = 0;
805 for attr in iter {
806 if let OpAttrs::Flags(val) = attr? {
807 return Ok(val);
808 }
809 }
810 Err(ErrorContext::new_missing(
811 "OpAttrs",
812 "Flags",
813 self.orig_loc,
814 self.buf.as_ptr() as usize,
815 ))
816 }
817}
818impl OpAttrs {
819 pub fn new(buf: &'_ [u8]) -> IterableOpAttrs<'_> {
820 IterableOpAttrs::with_loc(buf, buf.as_ptr() as usize)
821 }
822 fn attr_from_type(r#type: u16) -> Option<&'static str> {
823 let res = match r#type {
824 1u16 => "Id",
825 2u16 => "Flags",
826 _ => return None,
827 };
828 Some(res)
829 }
830}
831#[derive(Clone, Copy, Default)]
832pub struct IterableOpAttrs<'a> {
833 buf: &'a [u8],
834 pos: usize,
835 orig_loc: usize,
836}
837impl<'a> IterableOpAttrs<'a> {
838 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
839 Self {
840 buf,
841 pos: 0,
842 orig_loc,
843 }
844 }
845 pub fn get_buf(&self) -> &'a [u8] {
846 self.buf
847 }
848}
849impl<'a> Iterator for IterableOpAttrs<'a> {
850 type Item = Result<OpAttrs, ErrorContext>;
851 fn next(&mut self) -> Option<Self::Item> {
852 if self.buf.len() == self.pos {
853 return None;
854 }
855 let pos = self.pos;
856 let mut r#type = None;
857 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
858 r#type = Some(header.r#type);
859 let res = match header.r#type {
860 1u16 => OpAttrs::Id({
861 let res = parse_u32(next);
862 let Some(val) = res else { break };
863 val
864 }),
865 2u16 => OpAttrs::Flags({
866 let res = parse_u32(next);
867 let Some(val) = res else { break };
868 val
869 }),
870 n => {
871 if cfg!(any(test, feature = "deny-unknown-attrs")) {
872 break;
873 } else {
874 continue;
875 }
876 }
877 };
878 return Some(Ok(res));
879 }
880 Some(Err(ErrorContext::new(
881 "OpAttrs",
882 r#type.and_then(|t| OpAttrs::attr_from_type(t)),
883 self.orig_loc,
884 self.buf.as_ptr().wrapping_add(pos) as usize,
885 )))
886 }
887}
888impl std::fmt::Debug for IterableOpAttrs<'_> {
889 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
890 let mut fmt = f.debug_struct("OpAttrs");
891 for attr in self.clone() {
892 let attr = match attr {
893 Ok(a) => a,
894 Err(err) => {
895 fmt.finish()?;
896 f.write_str("Err(")?;
897 err.fmt(f)?;
898 return f.write_str(")");
899 }
900 };
901 match attr {
902 OpAttrs::Id(val) => fmt.field("Id", &val),
903 OpAttrs::Flags(val) => {
904 fmt.field("Flags", &FormatFlags(val.into(), OpFlags::from_value))
905 }
906 };
907 }
908 fmt.finish()
909 }
910}
911impl IterableOpAttrs<'_> {
912 pub fn lookup_attr(
913 &self,
914 offset: usize,
915 missing_type: Option<u16>,
916 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
917 let mut stack = Vec::new();
918 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
919 if cur == offset {
920 stack.push(("OpAttrs", offset));
921 return (stack, missing_type.and_then(|t| OpAttrs::attr_from_type(t)));
922 }
923 if cur > offset || cur + self.buf.len() < offset {
924 return (stack, None);
925 }
926 let mut attrs = self.clone();
927 let mut last_off = cur + attrs.pos;
928 while let Some(attr) = attrs.next() {
929 let Ok(attr) = attr else { break };
930 match attr {
931 OpAttrs::Id(val) => {
932 if last_off == offset {
933 stack.push(("Id", last_off));
934 break;
935 }
936 }
937 OpAttrs::Flags(val) => {
938 if last_off == offset {
939 stack.push(("Flags", last_off));
940 break;
941 }
942 }
943 _ => {}
944 };
945 last_off = cur + attrs.pos;
946 }
947 if !stack.is_empty() {
948 stack.push(("OpAttrs", cur));
949 }
950 (stack, None)
951 }
952}
953#[derive(Clone)]
954pub enum PolicyAttrs<'a> {
955 #[doc = "Associated type: \"AttrType\" (enum)"]
956 Type(u32),
957 MinValueS(i64),
958 MaxValueS(i64),
959 MinValueU(u64),
960 MaxValueU(u64),
961 MinLength(u32),
962 MaxLength(u32),
963 PolicyIdx(u32),
964 PolicyMaxtype(u32),
965 Bitfield32Mask(u32),
966 Mask(u64),
967 Pad(&'a [u8]),
968}
969impl<'a> IterablePolicyAttrs<'a> {
970 #[doc = "Associated type: \"AttrType\" (enum)"]
971 pub fn get_type(&self) -> Result<u32, ErrorContext> {
972 let mut iter = self.clone();
973 iter.pos = 0;
974 for attr in iter {
975 if let PolicyAttrs::Type(val) = attr? {
976 return Ok(val);
977 }
978 }
979 Err(ErrorContext::new_missing(
980 "PolicyAttrs",
981 "Type",
982 self.orig_loc,
983 self.buf.as_ptr() as usize,
984 ))
985 }
986 pub fn get_min_value_s(&self) -> Result<i64, ErrorContext> {
987 let mut iter = self.clone();
988 iter.pos = 0;
989 for attr in iter {
990 if let PolicyAttrs::MinValueS(val) = attr? {
991 return Ok(val);
992 }
993 }
994 Err(ErrorContext::new_missing(
995 "PolicyAttrs",
996 "MinValueS",
997 self.orig_loc,
998 self.buf.as_ptr() as usize,
999 ))
1000 }
1001 pub fn get_max_value_s(&self) -> Result<i64, ErrorContext> {
1002 let mut iter = self.clone();
1003 iter.pos = 0;
1004 for attr in iter {
1005 if let PolicyAttrs::MaxValueS(val) = attr? {
1006 return Ok(val);
1007 }
1008 }
1009 Err(ErrorContext::new_missing(
1010 "PolicyAttrs",
1011 "MaxValueS",
1012 self.orig_loc,
1013 self.buf.as_ptr() as usize,
1014 ))
1015 }
1016 pub fn get_min_value_u(&self) -> Result<u64, ErrorContext> {
1017 let mut iter = self.clone();
1018 iter.pos = 0;
1019 for attr in iter {
1020 if let PolicyAttrs::MinValueU(val) = attr? {
1021 return Ok(val);
1022 }
1023 }
1024 Err(ErrorContext::new_missing(
1025 "PolicyAttrs",
1026 "MinValueU",
1027 self.orig_loc,
1028 self.buf.as_ptr() as usize,
1029 ))
1030 }
1031 pub fn get_max_value_u(&self) -> Result<u64, ErrorContext> {
1032 let mut iter = self.clone();
1033 iter.pos = 0;
1034 for attr in iter {
1035 if let PolicyAttrs::MaxValueU(val) = attr? {
1036 return Ok(val);
1037 }
1038 }
1039 Err(ErrorContext::new_missing(
1040 "PolicyAttrs",
1041 "MaxValueU",
1042 self.orig_loc,
1043 self.buf.as_ptr() as usize,
1044 ))
1045 }
1046 pub fn get_min_length(&self) -> Result<u32, ErrorContext> {
1047 let mut iter = self.clone();
1048 iter.pos = 0;
1049 for attr in iter {
1050 if let PolicyAttrs::MinLength(val) = attr? {
1051 return Ok(val);
1052 }
1053 }
1054 Err(ErrorContext::new_missing(
1055 "PolicyAttrs",
1056 "MinLength",
1057 self.orig_loc,
1058 self.buf.as_ptr() as usize,
1059 ))
1060 }
1061 pub fn get_max_length(&self) -> Result<u32, ErrorContext> {
1062 let mut iter = self.clone();
1063 iter.pos = 0;
1064 for attr in iter {
1065 if let PolicyAttrs::MaxLength(val) = attr? {
1066 return Ok(val);
1067 }
1068 }
1069 Err(ErrorContext::new_missing(
1070 "PolicyAttrs",
1071 "MaxLength",
1072 self.orig_loc,
1073 self.buf.as_ptr() as usize,
1074 ))
1075 }
1076 pub fn get_policy_idx(&self) -> Result<u32, ErrorContext> {
1077 let mut iter = self.clone();
1078 iter.pos = 0;
1079 for attr in iter {
1080 if let PolicyAttrs::PolicyIdx(val) = attr? {
1081 return Ok(val);
1082 }
1083 }
1084 Err(ErrorContext::new_missing(
1085 "PolicyAttrs",
1086 "PolicyIdx",
1087 self.orig_loc,
1088 self.buf.as_ptr() as usize,
1089 ))
1090 }
1091 pub fn get_policy_maxtype(&self) -> Result<u32, ErrorContext> {
1092 let mut iter = self.clone();
1093 iter.pos = 0;
1094 for attr in iter {
1095 if let PolicyAttrs::PolicyMaxtype(val) = attr? {
1096 return Ok(val);
1097 }
1098 }
1099 Err(ErrorContext::new_missing(
1100 "PolicyAttrs",
1101 "PolicyMaxtype",
1102 self.orig_loc,
1103 self.buf.as_ptr() as usize,
1104 ))
1105 }
1106 pub fn get_bitfield32_mask(&self) -> Result<u32, ErrorContext> {
1107 let mut iter = self.clone();
1108 iter.pos = 0;
1109 for attr in iter {
1110 if let PolicyAttrs::Bitfield32Mask(val) = attr? {
1111 return Ok(val);
1112 }
1113 }
1114 Err(ErrorContext::new_missing(
1115 "PolicyAttrs",
1116 "Bitfield32Mask",
1117 self.orig_loc,
1118 self.buf.as_ptr() as usize,
1119 ))
1120 }
1121 pub fn get_mask(&self) -> Result<u64, ErrorContext> {
1122 let mut iter = self.clone();
1123 iter.pos = 0;
1124 for attr in iter {
1125 if let PolicyAttrs::Mask(val) = attr? {
1126 return Ok(val);
1127 }
1128 }
1129 Err(ErrorContext::new_missing(
1130 "PolicyAttrs",
1131 "Mask",
1132 self.orig_loc,
1133 self.buf.as_ptr() as usize,
1134 ))
1135 }
1136 pub fn get_pad(&self) -> Result<&'a [u8], ErrorContext> {
1137 let mut iter = self.clone();
1138 iter.pos = 0;
1139 for attr in iter {
1140 if let PolicyAttrs::Pad(val) = attr? {
1141 return Ok(val);
1142 }
1143 }
1144 Err(ErrorContext::new_missing(
1145 "PolicyAttrs",
1146 "Pad",
1147 self.orig_loc,
1148 self.buf.as_ptr() as usize,
1149 ))
1150 }
1151}
1152impl<'a> PolicyAttrs<'a> {
1153 pub fn new(buf: &'a [u8]) -> IterablePolicyAttrs<'a> {
1154 IterablePolicyAttrs::with_loc(buf, buf.as_ptr() as usize)
1155 }
1156 fn attr_from_type(r#type: u16) -> Option<&'static str> {
1157 let res = match r#type {
1158 1u16 => "Type",
1159 2u16 => "MinValueS",
1160 3u16 => "MaxValueS",
1161 4u16 => "MinValueU",
1162 5u16 => "MaxValueU",
1163 6u16 => "MinLength",
1164 7u16 => "MaxLength",
1165 8u16 => "PolicyIdx",
1166 9u16 => "PolicyMaxtype",
1167 10u16 => "Bitfield32Mask",
1168 11u16 => "Mask",
1169 12u16 => "Pad",
1170 _ => return None,
1171 };
1172 Some(res)
1173 }
1174}
1175#[derive(Clone, Copy, Default)]
1176pub struct IterablePolicyAttrs<'a> {
1177 buf: &'a [u8],
1178 pos: usize,
1179 orig_loc: usize,
1180}
1181impl<'a> IterablePolicyAttrs<'a> {
1182 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
1183 Self {
1184 buf,
1185 pos: 0,
1186 orig_loc,
1187 }
1188 }
1189 pub fn get_buf(&self) -> &'a [u8] {
1190 self.buf
1191 }
1192}
1193impl<'a> Iterator for IterablePolicyAttrs<'a> {
1194 type Item = Result<PolicyAttrs<'a>, ErrorContext>;
1195 fn next(&mut self) -> Option<Self::Item> {
1196 if self.buf.len() == self.pos {
1197 return None;
1198 }
1199 let pos = self.pos;
1200 let mut r#type = None;
1201 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
1202 r#type = Some(header.r#type);
1203 let res = match header.r#type {
1204 1u16 => PolicyAttrs::Type({
1205 let res = parse_u32(next);
1206 let Some(val) = res else { break };
1207 val
1208 }),
1209 2u16 => PolicyAttrs::MinValueS({
1210 let res = parse_i64(next);
1211 let Some(val) = res else { break };
1212 val
1213 }),
1214 3u16 => PolicyAttrs::MaxValueS({
1215 let res = parse_i64(next);
1216 let Some(val) = res else { break };
1217 val
1218 }),
1219 4u16 => PolicyAttrs::MinValueU({
1220 let res = parse_u64(next);
1221 let Some(val) = res else { break };
1222 val
1223 }),
1224 5u16 => PolicyAttrs::MaxValueU({
1225 let res = parse_u64(next);
1226 let Some(val) = res else { break };
1227 val
1228 }),
1229 6u16 => PolicyAttrs::MinLength({
1230 let res = parse_u32(next);
1231 let Some(val) = res else { break };
1232 val
1233 }),
1234 7u16 => PolicyAttrs::MaxLength({
1235 let res = parse_u32(next);
1236 let Some(val) = res else { break };
1237 val
1238 }),
1239 8u16 => PolicyAttrs::PolicyIdx({
1240 let res = parse_u32(next);
1241 let Some(val) = res else { break };
1242 val
1243 }),
1244 9u16 => PolicyAttrs::PolicyMaxtype({
1245 let res = parse_u32(next);
1246 let Some(val) = res else { break };
1247 val
1248 }),
1249 10u16 => PolicyAttrs::Bitfield32Mask({
1250 let res = parse_u32(next);
1251 let Some(val) = res else { break };
1252 val
1253 }),
1254 11u16 => PolicyAttrs::Mask({
1255 let res = parse_u64(next);
1256 let Some(val) = res else { break };
1257 val
1258 }),
1259 12u16 => PolicyAttrs::Pad({
1260 let res = Some(next);
1261 let Some(val) = res else { break };
1262 val
1263 }),
1264 n => {
1265 if cfg!(any(test, feature = "deny-unknown-attrs")) {
1266 break;
1267 } else {
1268 continue;
1269 }
1270 }
1271 };
1272 return Some(Ok(res));
1273 }
1274 Some(Err(ErrorContext::new(
1275 "PolicyAttrs",
1276 r#type.and_then(|t| PolicyAttrs::attr_from_type(t)),
1277 self.orig_loc,
1278 self.buf.as_ptr().wrapping_add(pos) as usize,
1279 )))
1280 }
1281}
1282impl<'a> std::fmt::Debug for IterablePolicyAttrs<'_> {
1283 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1284 let mut fmt = f.debug_struct("PolicyAttrs");
1285 for attr in self.clone() {
1286 let attr = match attr {
1287 Ok(a) => a,
1288 Err(err) => {
1289 fmt.finish()?;
1290 f.write_str("Err(")?;
1291 err.fmt(f)?;
1292 return f.write_str(")");
1293 }
1294 };
1295 match attr {
1296 PolicyAttrs::Type(val) => {
1297 fmt.field("Type", &FormatEnum(val.into(), AttrType::from_value))
1298 }
1299 PolicyAttrs::MinValueS(val) => fmt.field("MinValueS", &val),
1300 PolicyAttrs::MaxValueS(val) => fmt.field("MaxValueS", &val),
1301 PolicyAttrs::MinValueU(val) => fmt.field("MinValueU", &val),
1302 PolicyAttrs::MaxValueU(val) => fmt.field("MaxValueU", &val),
1303 PolicyAttrs::MinLength(val) => fmt.field("MinLength", &val),
1304 PolicyAttrs::MaxLength(val) => fmt.field("MaxLength", &val),
1305 PolicyAttrs::PolicyIdx(val) => fmt.field("PolicyIdx", &val),
1306 PolicyAttrs::PolicyMaxtype(val) => fmt.field("PolicyMaxtype", &val),
1307 PolicyAttrs::Bitfield32Mask(val) => fmt.field("Bitfield32Mask", &val),
1308 PolicyAttrs::Mask(val) => fmt.field("Mask", &val),
1309 PolicyAttrs::Pad(val) => fmt.field("Pad", &val),
1310 };
1311 }
1312 fmt.finish()
1313 }
1314}
1315impl IterablePolicyAttrs<'_> {
1316 pub fn lookup_attr(
1317 &self,
1318 offset: usize,
1319 missing_type: Option<u16>,
1320 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
1321 let mut stack = Vec::new();
1322 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
1323 if cur == offset {
1324 stack.push(("PolicyAttrs", offset));
1325 return (
1326 stack,
1327 missing_type.and_then(|t| PolicyAttrs::attr_from_type(t)),
1328 );
1329 }
1330 if cur > offset || cur + self.buf.len() < offset {
1331 return (stack, None);
1332 }
1333 let mut attrs = self.clone();
1334 let mut last_off = cur + attrs.pos;
1335 while let Some(attr) = attrs.next() {
1336 let Ok(attr) = attr else { break };
1337 match attr {
1338 PolicyAttrs::Type(val) => {
1339 if last_off == offset {
1340 stack.push(("Type", last_off));
1341 break;
1342 }
1343 }
1344 PolicyAttrs::MinValueS(val) => {
1345 if last_off == offset {
1346 stack.push(("MinValueS", last_off));
1347 break;
1348 }
1349 }
1350 PolicyAttrs::MaxValueS(val) => {
1351 if last_off == offset {
1352 stack.push(("MaxValueS", last_off));
1353 break;
1354 }
1355 }
1356 PolicyAttrs::MinValueU(val) => {
1357 if last_off == offset {
1358 stack.push(("MinValueU", last_off));
1359 break;
1360 }
1361 }
1362 PolicyAttrs::MaxValueU(val) => {
1363 if last_off == offset {
1364 stack.push(("MaxValueU", last_off));
1365 break;
1366 }
1367 }
1368 PolicyAttrs::MinLength(val) => {
1369 if last_off == offset {
1370 stack.push(("MinLength", last_off));
1371 break;
1372 }
1373 }
1374 PolicyAttrs::MaxLength(val) => {
1375 if last_off == offset {
1376 stack.push(("MaxLength", last_off));
1377 break;
1378 }
1379 }
1380 PolicyAttrs::PolicyIdx(val) => {
1381 if last_off == offset {
1382 stack.push(("PolicyIdx", last_off));
1383 break;
1384 }
1385 }
1386 PolicyAttrs::PolicyMaxtype(val) => {
1387 if last_off == offset {
1388 stack.push(("PolicyMaxtype", last_off));
1389 break;
1390 }
1391 }
1392 PolicyAttrs::Bitfield32Mask(val) => {
1393 if last_off == offset {
1394 stack.push(("Bitfield32Mask", last_off));
1395 break;
1396 }
1397 }
1398 PolicyAttrs::Mask(val) => {
1399 if last_off == offset {
1400 stack.push(("Mask", last_off));
1401 break;
1402 }
1403 }
1404 PolicyAttrs::Pad(val) => {
1405 if last_off == offset {
1406 stack.push(("Pad", last_off));
1407 break;
1408 }
1409 }
1410 _ => {}
1411 };
1412 last_off = cur + attrs.pos;
1413 }
1414 if !stack.is_empty() {
1415 stack.push(("PolicyAttrs", cur));
1416 }
1417 (stack, None)
1418 }
1419}
1420#[derive(Clone)]
1421pub enum OpPolicyAttrs {
1422 Do(u32),
1423 Dump(u32),
1424}
1425impl<'a> IterableOpPolicyAttrs<'a> {
1426 pub fn get_do(&self) -> Result<u32, ErrorContext> {
1427 let mut iter = self.clone();
1428 iter.pos = 0;
1429 for attr in iter {
1430 if let OpPolicyAttrs::Do(val) = attr? {
1431 return Ok(val);
1432 }
1433 }
1434 Err(ErrorContext::new_missing(
1435 "OpPolicyAttrs",
1436 "Do",
1437 self.orig_loc,
1438 self.buf.as_ptr() as usize,
1439 ))
1440 }
1441 pub fn get_dump(&self) -> Result<u32, ErrorContext> {
1442 let mut iter = self.clone();
1443 iter.pos = 0;
1444 for attr in iter {
1445 if let OpPolicyAttrs::Dump(val) = attr? {
1446 return Ok(val);
1447 }
1448 }
1449 Err(ErrorContext::new_missing(
1450 "OpPolicyAttrs",
1451 "Dump",
1452 self.orig_loc,
1453 self.buf.as_ptr() as usize,
1454 ))
1455 }
1456}
1457impl OpPolicyAttrs {
1458 pub fn new(buf: &'_ [u8]) -> IterableOpPolicyAttrs<'_> {
1459 IterableOpPolicyAttrs::with_loc(buf, buf.as_ptr() as usize)
1460 }
1461 fn attr_from_type(r#type: u16) -> Option<&'static str> {
1462 let res = match r#type {
1463 1u16 => "Do",
1464 2u16 => "Dump",
1465 _ => return None,
1466 };
1467 Some(res)
1468 }
1469}
1470#[derive(Clone, Copy, Default)]
1471pub struct IterableOpPolicyAttrs<'a> {
1472 buf: &'a [u8],
1473 pos: usize,
1474 orig_loc: usize,
1475}
1476impl<'a> IterableOpPolicyAttrs<'a> {
1477 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
1478 Self {
1479 buf,
1480 pos: 0,
1481 orig_loc,
1482 }
1483 }
1484 pub fn get_buf(&self) -> &'a [u8] {
1485 self.buf
1486 }
1487}
1488impl<'a> Iterator for IterableOpPolicyAttrs<'a> {
1489 type Item = Result<OpPolicyAttrs, ErrorContext>;
1490 fn next(&mut self) -> Option<Self::Item> {
1491 if self.buf.len() == self.pos {
1492 return None;
1493 }
1494 let pos = self.pos;
1495 let mut r#type = None;
1496 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
1497 r#type = Some(header.r#type);
1498 let res = match header.r#type {
1499 1u16 => OpPolicyAttrs::Do({
1500 let res = parse_u32(next);
1501 let Some(val) = res else { break };
1502 val
1503 }),
1504 2u16 => OpPolicyAttrs::Dump({
1505 let res = parse_u32(next);
1506 let Some(val) = res else { break };
1507 val
1508 }),
1509 n => {
1510 if cfg!(any(test, feature = "deny-unknown-attrs")) {
1511 break;
1512 } else {
1513 continue;
1514 }
1515 }
1516 };
1517 return Some(Ok(res));
1518 }
1519 Some(Err(ErrorContext::new(
1520 "OpPolicyAttrs",
1521 r#type.and_then(|t| OpPolicyAttrs::attr_from_type(t)),
1522 self.orig_loc,
1523 self.buf.as_ptr().wrapping_add(pos) as usize,
1524 )))
1525 }
1526}
1527impl std::fmt::Debug for IterableOpPolicyAttrs<'_> {
1528 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1529 let mut fmt = f.debug_struct("OpPolicyAttrs");
1530 for attr in self.clone() {
1531 let attr = match attr {
1532 Ok(a) => a,
1533 Err(err) => {
1534 fmt.finish()?;
1535 f.write_str("Err(")?;
1536 err.fmt(f)?;
1537 return f.write_str(")");
1538 }
1539 };
1540 match attr {
1541 OpPolicyAttrs::Do(val) => fmt.field("Do", &val),
1542 OpPolicyAttrs::Dump(val) => fmt.field("Dump", &val),
1543 };
1544 }
1545 fmt.finish()
1546 }
1547}
1548impl IterableOpPolicyAttrs<'_> {
1549 pub fn lookup_attr(
1550 &self,
1551 offset: usize,
1552 missing_type: Option<u16>,
1553 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
1554 let mut stack = Vec::new();
1555 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
1556 if cur == offset {
1557 stack.push(("OpPolicyAttrs", offset));
1558 return (
1559 stack,
1560 missing_type.and_then(|t| OpPolicyAttrs::attr_from_type(t)),
1561 );
1562 }
1563 if cur > offset || cur + self.buf.len() < offset {
1564 return (stack, None);
1565 }
1566 let mut attrs = self.clone();
1567 let mut last_off = cur + attrs.pos;
1568 while let Some(attr) = attrs.next() {
1569 let Ok(attr) = attr else { break };
1570 match attr {
1571 OpPolicyAttrs::Do(val) => {
1572 if last_off == offset {
1573 stack.push(("Do", last_off));
1574 break;
1575 }
1576 }
1577 OpPolicyAttrs::Dump(val) => {
1578 if last_off == offset {
1579 stack.push(("Dump", last_off));
1580 break;
1581 }
1582 }
1583 _ => {}
1584 };
1585 last_off = cur + attrs.pos;
1586 }
1587 if !stack.is_empty() {
1588 stack.push(("OpPolicyAttrs", cur));
1589 }
1590 (stack, None)
1591 }
1592}
1593pub struct PushCtrlAttrs<Prev: Rec> {
1594 pub(crate) prev: Option<Prev>,
1595 pub(crate) header_offset: Option<usize>,
1596}
1597impl<Prev: Rec> Rec for PushCtrlAttrs<Prev> {
1598 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1599 self.prev.as_mut().unwrap().as_rec_mut()
1600 }
1601}
1602pub struct PushArrayOpAttrs<Prev: Rec> {
1603 pub(crate) prev: Option<Prev>,
1604 pub(crate) header_offset: Option<usize>,
1605 pub(crate) counter: u16,
1606}
1607impl<Prev: Rec> Rec for PushArrayOpAttrs<Prev> {
1608 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1609 self.prev.as_mut().unwrap().as_rec_mut()
1610 }
1611}
1612impl<Prev: Rec> PushArrayOpAttrs<Prev> {
1613 pub fn new(prev: Prev) -> Self {
1614 Self {
1615 prev: Some(prev),
1616 header_offset: None,
1617 counter: 0,
1618 }
1619 }
1620 pub fn end_array(mut self) -> Prev {
1621 let mut prev = self.prev.take().unwrap();
1622 if let Some(header_offset) = &self.header_offset {
1623 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1624 }
1625 prev
1626 }
1627 pub fn entry_nested(mut self) -> PushOpAttrs<Self> {
1628 let index = self.counter;
1629 self.counter += 1;
1630 let header_offset = push_nested_header(self.as_rec_mut(), index);
1631 PushOpAttrs {
1632 prev: Some(self),
1633 header_offset: Some(header_offset),
1634 }
1635 }
1636}
1637impl<Prev: Rec> Drop for PushArrayOpAttrs<Prev> {
1638 fn drop(&mut self) {
1639 if let Some(prev) = &mut self.prev {
1640 if let Some(header_offset) = &self.header_offset {
1641 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1642 }
1643 }
1644 }
1645}
1646pub struct PushArrayMcastGroupAttrs<Prev: Rec> {
1647 pub(crate) prev: Option<Prev>,
1648 pub(crate) header_offset: Option<usize>,
1649 pub(crate) counter: u16,
1650}
1651impl<Prev: Rec> Rec for PushArrayMcastGroupAttrs<Prev> {
1652 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1653 self.prev.as_mut().unwrap().as_rec_mut()
1654 }
1655}
1656impl<Prev: Rec> PushArrayMcastGroupAttrs<Prev> {
1657 pub fn new(prev: Prev) -> Self {
1658 Self {
1659 prev: Some(prev),
1660 header_offset: None,
1661 counter: 0,
1662 }
1663 }
1664 pub fn end_array(mut self) -> Prev {
1665 let mut prev = self.prev.take().unwrap();
1666 if let Some(header_offset) = &self.header_offset {
1667 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1668 }
1669 prev
1670 }
1671 pub fn entry_nested(mut self) -> PushMcastGroupAttrs<Self> {
1672 let index = self.counter;
1673 self.counter += 1;
1674 let header_offset = push_nested_header(self.as_rec_mut(), index);
1675 PushMcastGroupAttrs {
1676 prev: Some(self),
1677 header_offset: Some(header_offset),
1678 }
1679 }
1680}
1681impl<Prev: Rec> Drop for PushArrayMcastGroupAttrs<Prev> {
1682 fn drop(&mut self) {
1683 if let Some(prev) = &mut self.prev {
1684 if let Some(header_offset) = &self.header_offset {
1685 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1686 }
1687 }
1688 }
1689}
1690impl<Prev: Rec> PushCtrlAttrs<Prev> {
1691 pub fn new(prev: Prev) -> Self {
1692 Self {
1693 prev: Some(prev),
1694 header_offset: None,
1695 }
1696 }
1697 pub fn end_nested(mut self) -> Prev {
1698 let mut prev = self.prev.take().unwrap();
1699 if let Some(header_offset) = &self.header_offset {
1700 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1701 }
1702 prev
1703 }
1704 pub fn push_family_id(mut self, value: u16) -> Self {
1705 push_header(self.as_rec_mut(), 1u16, 2 as u16);
1706 self.as_rec_mut().extend(value.to_ne_bytes());
1707 self
1708 }
1709 pub fn push_family_name(mut self, value: &CStr) -> Self {
1710 push_header(
1711 self.as_rec_mut(),
1712 2u16,
1713 value.to_bytes_with_nul().len() as u16,
1714 );
1715 self.as_rec_mut().extend(value.to_bytes_with_nul());
1716 self
1717 }
1718 pub fn push_family_name_bytes(mut self, value: &[u8]) -> Self {
1719 push_header(self.as_rec_mut(), 2u16, (value.len() + 1) as u16);
1720 self.as_rec_mut().extend(value);
1721 self.as_rec_mut().push(0);
1722 self
1723 }
1724 pub fn push_version(mut self, value: u32) -> Self {
1725 push_header(self.as_rec_mut(), 3u16, 4 as u16);
1726 self.as_rec_mut().extend(value.to_ne_bytes());
1727 self
1728 }
1729 pub fn push_hdrsize(mut self, value: u32) -> Self {
1730 push_header(self.as_rec_mut(), 4u16, 4 as u16);
1731 self.as_rec_mut().extend(value.to_ne_bytes());
1732 self
1733 }
1734 pub fn push_maxattr(mut self, value: u32) -> Self {
1735 push_header(self.as_rec_mut(), 5u16, 4 as u16);
1736 self.as_rec_mut().extend(value.to_ne_bytes());
1737 self
1738 }
1739 pub fn array_ops(mut self) -> PushArrayOpAttrs<Self> {
1740 let header_offset = push_nested_header(self.as_rec_mut(), 6u16);
1741 PushArrayOpAttrs {
1742 prev: Some(self),
1743 header_offset: Some(header_offset),
1744 counter: 0,
1745 }
1746 }
1747 pub fn array_mcast_groups(mut self) -> PushArrayMcastGroupAttrs<Self> {
1748 let header_offset = push_nested_header(self.as_rec_mut(), 7u16);
1749 PushArrayMcastGroupAttrs {
1750 prev: Some(self),
1751 header_offset: Some(header_offset),
1752 counter: 0,
1753 }
1754 }
1755 pub fn nested_policy(mut self) -> PushPolicyAttrs<Self> {
1756 let header_offset = push_nested_header(self.as_rec_mut(), 8u16);
1757 PushPolicyAttrs {
1758 prev: Some(self),
1759 header_offset: Some(header_offset),
1760 }
1761 }
1762 pub fn nested_op_policy(mut self) -> PushOpPolicyAttrs<Self> {
1763 let header_offset = push_nested_header(self.as_rec_mut(), 9u16);
1764 PushOpPolicyAttrs {
1765 prev: Some(self),
1766 header_offset: Some(header_offset),
1767 }
1768 }
1769 pub fn push_op(mut self, value: u32) -> Self {
1770 push_header(self.as_rec_mut(), 10u16, 4 as u16);
1771 self.as_rec_mut().extend(value.to_ne_bytes());
1772 self
1773 }
1774}
1775impl<Prev: Rec> Drop for PushCtrlAttrs<Prev> {
1776 fn drop(&mut self) {
1777 if let Some(prev) = &mut self.prev {
1778 if let Some(header_offset) = &self.header_offset {
1779 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1780 }
1781 }
1782 }
1783}
1784pub struct PushMcastGroupAttrs<Prev: Rec> {
1785 pub(crate) prev: Option<Prev>,
1786 pub(crate) header_offset: Option<usize>,
1787}
1788impl<Prev: Rec> Rec for PushMcastGroupAttrs<Prev> {
1789 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1790 self.prev.as_mut().unwrap().as_rec_mut()
1791 }
1792}
1793impl<Prev: Rec> PushMcastGroupAttrs<Prev> {
1794 pub fn new(prev: Prev) -> Self {
1795 Self {
1796 prev: Some(prev),
1797 header_offset: None,
1798 }
1799 }
1800 pub fn end_nested(mut self) -> Prev {
1801 let mut prev = self.prev.take().unwrap();
1802 if let Some(header_offset) = &self.header_offset {
1803 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1804 }
1805 prev
1806 }
1807 pub fn push_name(mut self, value: &CStr) -> Self {
1808 push_header(
1809 self.as_rec_mut(),
1810 1u16,
1811 value.to_bytes_with_nul().len() as u16,
1812 );
1813 self.as_rec_mut().extend(value.to_bytes_with_nul());
1814 self
1815 }
1816 pub fn push_name_bytes(mut self, value: &[u8]) -> Self {
1817 push_header(self.as_rec_mut(), 1u16, (value.len() + 1) as u16);
1818 self.as_rec_mut().extend(value);
1819 self.as_rec_mut().push(0);
1820 self
1821 }
1822 pub fn push_id(mut self, value: u32) -> Self {
1823 push_header(self.as_rec_mut(), 2u16, 4 as u16);
1824 self.as_rec_mut().extend(value.to_ne_bytes());
1825 self
1826 }
1827}
1828impl<Prev: Rec> Drop for PushMcastGroupAttrs<Prev> {
1829 fn drop(&mut self) {
1830 if let Some(prev) = &mut self.prev {
1831 if let Some(header_offset) = &self.header_offset {
1832 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1833 }
1834 }
1835 }
1836}
1837pub struct PushOpAttrs<Prev: Rec> {
1838 pub(crate) prev: Option<Prev>,
1839 pub(crate) header_offset: Option<usize>,
1840}
1841impl<Prev: Rec> Rec for PushOpAttrs<Prev> {
1842 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1843 self.prev.as_mut().unwrap().as_rec_mut()
1844 }
1845}
1846impl<Prev: Rec> PushOpAttrs<Prev> {
1847 pub fn new(prev: Prev) -> Self {
1848 Self {
1849 prev: Some(prev),
1850 header_offset: None,
1851 }
1852 }
1853 pub fn end_nested(mut self) -> Prev {
1854 let mut prev = self.prev.take().unwrap();
1855 if let Some(header_offset) = &self.header_offset {
1856 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1857 }
1858 prev
1859 }
1860 pub fn push_id(mut self, value: u32) -> Self {
1861 push_header(self.as_rec_mut(), 1u16, 4 as u16);
1862 self.as_rec_mut().extend(value.to_ne_bytes());
1863 self
1864 }
1865 #[doc = "Associated type: \"OpFlags\" (1 bit per enumeration)"]
1866 pub fn push_flags(mut self, value: u32) -> Self {
1867 push_header(self.as_rec_mut(), 2u16, 4 as u16);
1868 self.as_rec_mut().extend(value.to_ne_bytes());
1869 self
1870 }
1871}
1872impl<Prev: Rec> Drop for PushOpAttrs<Prev> {
1873 fn drop(&mut self) {
1874 if let Some(prev) = &mut self.prev {
1875 if let Some(header_offset) = &self.header_offset {
1876 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1877 }
1878 }
1879 }
1880}
1881pub struct PushPolicyAttrs<Prev: Rec> {
1882 pub(crate) prev: Option<Prev>,
1883 pub(crate) header_offset: Option<usize>,
1884}
1885impl<Prev: Rec> Rec for PushPolicyAttrs<Prev> {
1886 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1887 self.prev.as_mut().unwrap().as_rec_mut()
1888 }
1889}
1890impl<Prev: Rec> PushPolicyAttrs<Prev> {
1891 pub fn new(prev: Prev) -> Self {
1892 Self {
1893 prev: Some(prev),
1894 header_offset: None,
1895 }
1896 }
1897 pub fn end_nested(mut self) -> Prev {
1898 let mut prev = self.prev.take().unwrap();
1899 if let Some(header_offset) = &self.header_offset {
1900 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1901 }
1902 prev
1903 }
1904 #[doc = "Associated type: \"AttrType\" (enum)"]
1905 pub fn push_type(mut self, value: u32) -> Self {
1906 push_header(self.as_rec_mut(), 1u16, 4 as u16);
1907 self.as_rec_mut().extend(value.to_ne_bytes());
1908 self
1909 }
1910 pub fn push_min_value_s(mut self, value: i64) -> Self {
1911 push_header(self.as_rec_mut(), 2u16, 8 as u16);
1912 self.as_rec_mut().extend(value.to_ne_bytes());
1913 self
1914 }
1915 pub fn push_max_value_s(mut self, value: i64) -> Self {
1916 push_header(self.as_rec_mut(), 3u16, 8 as u16);
1917 self.as_rec_mut().extend(value.to_ne_bytes());
1918 self
1919 }
1920 pub fn push_min_value_u(mut self, value: u64) -> Self {
1921 push_header(self.as_rec_mut(), 4u16, 8 as u16);
1922 self.as_rec_mut().extend(value.to_ne_bytes());
1923 self
1924 }
1925 pub fn push_max_value_u(mut self, value: u64) -> Self {
1926 push_header(self.as_rec_mut(), 5u16, 8 as u16);
1927 self.as_rec_mut().extend(value.to_ne_bytes());
1928 self
1929 }
1930 pub fn push_min_length(mut self, value: u32) -> Self {
1931 push_header(self.as_rec_mut(), 6u16, 4 as u16);
1932 self.as_rec_mut().extend(value.to_ne_bytes());
1933 self
1934 }
1935 pub fn push_max_length(mut self, value: u32) -> Self {
1936 push_header(self.as_rec_mut(), 7u16, 4 as u16);
1937 self.as_rec_mut().extend(value.to_ne_bytes());
1938 self
1939 }
1940 pub fn push_policy_idx(mut self, value: u32) -> Self {
1941 push_header(self.as_rec_mut(), 8u16, 4 as u16);
1942 self.as_rec_mut().extend(value.to_ne_bytes());
1943 self
1944 }
1945 pub fn push_policy_maxtype(mut self, value: u32) -> Self {
1946 push_header(self.as_rec_mut(), 9u16, 4 as u16);
1947 self.as_rec_mut().extend(value.to_ne_bytes());
1948 self
1949 }
1950 pub fn push_bitfield32_mask(mut self, value: u32) -> Self {
1951 push_header(self.as_rec_mut(), 10u16, 4 as u16);
1952 self.as_rec_mut().extend(value.to_ne_bytes());
1953 self
1954 }
1955 pub fn push_mask(mut self, value: u64) -> Self {
1956 push_header(self.as_rec_mut(), 11u16, 8 as u16);
1957 self.as_rec_mut().extend(value.to_ne_bytes());
1958 self
1959 }
1960 pub fn push_pad(mut self, value: &[u8]) -> Self {
1961 push_header(self.as_rec_mut(), 12u16, value.len() as u16);
1962 self.as_rec_mut().extend(value);
1963 self
1964 }
1965}
1966impl<Prev: Rec> Drop for PushPolicyAttrs<Prev> {
1967 fn drop(&mut self) {
1968 if let Some(prev) = &mut self.prev {
1969 if let Some(header_offset) = &self.header_offset {
1970 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1971 }
1972 }
1973 }
1974}
1975pub struct PushOpPolicyAttrs<Prev: Rec> {
1976 pub(crate) prev: Option<Prev>,
1977 pub(crate) header_offset: Option<usize>,
1978}
1979impl<Prev: Rec> Rec for PushOpPolicyAttrs<Prev> {
1980 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
1981 self.prev.as_mut().unwrap().as_rec_mut()
1982 }
1983}
1984impl<Prev: Rec> PushOpPolicyAttrs<Prev> {
1985 pub fn new(prev: Prev) -> Self {
1986 Self {
1987 prev: Some(prev),
1988 header_offset: None,
1989 }
1990 }
1991 pub fn end_nested(mut self) -> Prev {
1992 let mut prev = self.prev.take().unwrap();
1993 if let Some(header_offset) = &self.header_offset {
1994 finalize_nested_header(prev.as_rec_mut(), *header_offset);
1995 }
1996 prev
1997 }
1998 pub fn push_do(mut self, value: u32) -> Self {
1999 push_header(self.as_rec_mut(), 1u16, 4 as u16);
2000 self.as_rec_mut().extend(value.to_ne_bytes());
2001 self
2002 }
2003 pub fn push_dump(mut self, value: u32) -> Self {
2004 push_header(self.as_rec_mut(), 2u16, 4 as u16);
2005 self.as_rec_mut().extend(value.to_ne_bytes());
2006 self
2007 }
2008}
2009impl<Prev: Rec> Drop for PushOpPolicyAttrs<Prev> {
2010 fn drop(&mut self) {
2011 if let Some(prev) = &mut self.prev {
2012 if let Some(header_offset) = &self.header_offset {
2013 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2014 }
2015 }
2016 }
2017}
2018#[doc = "Get / dump genetlink families"]
2019pub struct PushOpGetfamilyDumpRequest<Prev: Rec> {
2020 pub(crate) prev: Option<Prev>,
2021 pub(crate) header_offset: Option<usize>,
2022}
2023impl<Prev: Rec> Rec for PushOpGetfamilyDumpRequest<Prev> {
2024 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
2025 self.prev.as_mut().unwrap().as_rec_mut()
2026 }
2027}
2028impl<Prev: Rec> PushOpGetfamilyDumpRequest<Prev> {
2029 pub fn new(mut prev: Prev) -> Self {
2030 Self::write_header(&mut prev);
2031 Self::new_without_header(prev)
2032 }
2033 fn new_without_header(prev: Prev) -> Self {
2034 Self {
2035 prev: Some(prev),
2036 header_offset: None,
2037 }
2038 }
2039 fn write_header(prev: &mut Prev) {
2040 let mut header = PushBuiltinNfgenmsg::new();
2041 header.set_cmd(3u8);
2042 header.set_version(1u8);
2043 prev.as_rec_mut().extend(header.as_slice());
2044 }
2045 pub fn end_nested(mut self) -> Prev {
2046 let mut prev = self.prev.take().unwrap();
2047 if let Some(header_offset) = &self.header_offset {
2048 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2049 }
2050 prev
2051 }
2052}
2053impl<Prev: Rec> Drop for PushOpGetfamilyDumpRequest<Prev> {
2054 fn drop(&mut self) {
2055 if let Some(prev) = &mut self.prev {
2056 if let Some(header_offset) = &self.header_offset {
2057 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2058 }
2059 }
2060 }
2061}
2062#[doc = "Get / dump genetlink families"]
2063#[derive(Clone)]
2064pub enum OpGetfamilyDumpRequest {}
2065impl<'a> IterableOpGetfamilyDumpRequest<'a> {}
2066impl OpGetfamilyDumpRequest {
2067 pub fn new(buf: &'_ [u8]) -> IterableOpGetfamilyDumpRequest<'_> {
2068 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
2069 IterableOpGetfamilyDumpRequest::with_loc(attrs, buf.as_ptr() as usize)
2070 }
2071 fn attr_from_type(r#type: u16) -> Option<&'static str> {
2072 CtrlAttrs::attr_from_type(r#type)
2073 }
2074}
2075#[derive(Clone, Copy, Default)]
2076pub struct IterableOpGetfamilyDumpRequest<'a> {
2077 buf: &'a [u8],
2078 pos: usize,
2079 orig_loc: usize,
2080}
2081impl<'a> IterableOpGetfamilyDumpRequest<'a> {
2082 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
2083 Self {
2084 buf,
2085 pos: 0,
2086 orig_loc,
2087 }
2088 }
2089 pub fn get_buf(&self) -> &'a [u8] {
2090 self.buf
2091 }
2092}
2093impl<'a> Iterator for IterableOpGetfamilyDumpRequest<'a> {
2094 type Item = Result<OpGetfamilyDumpRequest, ErrorContext>;
2095 fn next(&mut self) -> Option<Self::Item> {
2096 if self.buf.len() == self.pos {
2097 return None;
2098 }
2099 let pos = self.pos;
2100 let mut r#type = None;
2101 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
2102 r#type = Some(header.r#type);
2103 let res = match header.r#type {
2104 n => {
2105 if cfg!(any(test, feature = "deny-unknown-attrs")) {
2106 break;
2107 } else {
2108 continue;
2109 }
2110 }
2111 };
2112 return Some(Ok(res));
2113 }
2114 Some(Err(ErrorContext::new(
2115 "OpGetfamilyDumpRequest",
2116 r#type.and_then(|t| OpGetfamilyDumpRequest::attr_from_type(t)),
2117 self.orig_loc,
2118 self.buf.as_ptr().wrapping_add(pos) as usize,
2119 )))
2120 }
2121}
2122impl std::fmt::Debug for IterableOpGetfamilyDumpRequest<'_> {
2123 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2124 let mut fmt = f.debug_struct("OpGetfamilyDumpRequest");
2125 for attr in self.clone() {
2126 let attr = match attr {
2127 Ok(a) => a,
2128 Err(err) => {
2129 fmt.finish()?;
2130 f.write_str("Err(")?;
2131 err.fmt(f)?;
2132 return f.write_str(")");
2133 }
2134 };
2135 match attr {};
2136 }
2137 fmt.finish()
2138 }
2139}
2140impl IterableOpGetfamilyDumpRequest<'_> {
2141 pub fn lookup_attr(
2142 &self,
2143 offset: usize,
2144 missing_type: Option<u16>,
2145 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
2146 let mut stack = Vec::new();
2147 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
2148 if cur == offset + PushBuiltinNfgenmsg::len() {
2149 stack.push(("OpGetfamilyDumpRequest", offset));
2150 return (
2151 stack,
2152 missing_type.and_then(|t| OpGetfamilyDumpRequest::attr_from_type(t)),
2153 );
2154 }
2155 (stack, None)
2156 }
2157}
2158#[doc = "Get / dump genetlink families"]
2159pub struct PushOpGetfamilyDumpReply<Prev: Rec> {
2160 pub(crate) prev: Option<Prev>,
2161 pub(crate) header_offset: Option<usize>,
2162}
2163impl<Prev: Rec> Rec for PushOpGetfamilyDumpReply<Prev> {
2164 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
2165 self.prev.as_mut().unwrap().as_rec_mut()
2166 }
2167}
2168impl<Prev: Rec> PushOpGetfamilyDumpReply<Prev> {
2169 pub fn new(mut prev: Prev) -> Self {
2170 Self::write_header(&mut prev);
2171 Self::new_without_header(prev)
2172 }
2173 fn new_without_header(prev: Prev) -> Self {
2174 Self {
2175 prev: Some(prev),
2176 header_offset: None,
2177 }
2178 }
2179 fn write_header(prev: &mut Prev) {
2180 let mut header = PushBuiltinNfgenmsg::new();
2181 header.set_cmd(1u8);
2182 header.set_version(1u8);
2183 prev.as_rec_mut().extend(header.as_slice());
2184 }
2185 pub fn end_nested(mut self) -> Prev {
2186 let mut prev = self.prev.take().unwrap();
2187 if let Some(header_offset) = &self.header_offset {
2188 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2189 }
2190 prev
2191 }
2192 pub fn push_family_id(mut self, value: u16) -> Self {
2193 push_header(self.as_rec_mut(), 1u16, 2 as u16);
2194 self.as_rec_mut().extend(value.to_ne_bytes());
2195 self
2196 }
2197 pub fn push_family_name(mut self, value: &CStr) -> Self {
2198 push_header(
2199 self.as_rec_mut(),
2200 2u16,
2201 value.to_bytes_with_nul().len() as u16,
2202 );
2203 self.as_rec_mut().extend(value.to_bytes_with_nul());
2204 self
2205 }
2206 pub fn push_family_name_bytes(mut self, value: &[u8]) -> Self {
2207 push_header(self.as_rec_mut(), 2u16, (value.len() + 1) as u16);
2208 self.as_rec_mut().extend(value);
2209 self.as_rec_mut().push(0);
2210 self
2211 }
2212 pub fn push_version(mut self, value: u32) -> Self {
2213 push_header(self.as_rec_mut(), 3u16, 4 as u16);
2214 self.as_rec_mut().extend(value.to_ne_bytes());
2215 self
2216 }
2217 pub fn push_hdrsize(mut self, value: u32) -> Self {
2218 push_header(self.as_rec_mut(), 4u16, 4 as u16);
2219 self.as_rec_mut().extend(value.to_ne_bytes());
2220 self
2221 }
2222 pub fn push_maxattr(mut self, value: u32) -> Self {
2223 push_header(self.as_rec_mut(), 5u16, 4 as u16);
2224 self.as_rec_mut().extend(value.to_ne_bytes());
2225 self
2226 }
2227 pub fn array_ops(mut self) -> PushArrayOpAttrs<Self> {
2228 let header_offset = push_nested_header(self.as_rec_mut(), 6u16);
2229 PushArrayOpAttrs {
2230 prev: Some(self),
2231 header_offset: Some(header_offset),
2232 counter: 0,
2233 }
2234 }
2235 pub fn array_mcast_groups(mut self) -> PushArrayMcastGroupAttrs<Self> {
2236 let header_offset = push_nested_header(self.as_rec_mut(), 7u16);
2237 PushArrayMcastGroupAttrs {
2238 prev: Some(self),
2239 header_offset: Some(header_offset),
2240 counter: 0,
2241 }
2242 }
2243}
2244impl<Prev: Rec> Drop for PushOpGetfamilyDumpReply<Prev> {
2245 fn drop(&mut self) {
2246 if let Some(prev) = &mut self.prev {
2247 if let Some(header_offset) = &self.header_offset {
2248 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2249 }
2250 }
2251 }
2252}
2253#[doc = "Get / dump genetlink families"]
2254#[derive(Clone)]
2255pub enum OpGetfamilyDumpReply<'a> {
2256 FamilyId(u16),
2257 FamilyName(&'a CStr),
2258 Version(u32),
2259 Hdrsize(u32),
2260 Maxattr(u32),
2261 Ops(IterableArrayOpAttrs<'a>),
2262 McastGroups(IterableArrayMcastGroupAttrs<'a>),
2263}
2264impl<'a> IterableOpGetfamilyDumpReply<'a> {
2265 pub fn get_family_id(&self) -> Result<u16, ErrorContext> {
2266 let mut iter = self.clone();
2267 iter.pos = 0;
2268 for attr in iter {
2269 if let OpGetfamilyDumpReply::FamilyId(val) = attr? {
2270 return Ok(val);
2271 }
2272 }
2273 Err(ErrorContext::new_missing(
2274 "OpGetfamilyDumpReply",
2275 "FamilyId",
2276 self.orig_loc,
2277 self.buf.as_ptr() as usize,
2278 ))
2279 }
2280 pub fn get_family_name(&self) -> Result<&'a CStr, ErrorContext> {
2281 let mut iter = self.clone();
2282 iter.pos = 0;
2283 for attr in iter {
2284 if let OpGetfamilyDumpReply::FamilyName(val) = attr? {
2285 return Ok(val);
2286 }
2287 }
2288 Err(ErrorContext::new_missing(
2289 "OpGetfamilyDumpReply",
2290 "FamilyName",
2291 self.orig_loc,
2292 self.buf.as_ptr() as usize,
2293 ))
2294 }
2295 pub fn get_version(&self) -> Result<u32, ErrorContext> {
2296 let mut iter = self.clone();
2297 iter.pos = 0;
2298 for attr in iter {
2299 if let OpGetfamilyDumpReply::Version(val) = attr? {
2300 return Ok(val);
2301 }
2302 }
2303 Err(ErrorContext::new_missing(
2304 "OpGetfamilyDumpReply",
2305 "Version",
2306 self.orig_loc,
2307 self.buf.as_ptr() as usize,
2308 ))
2309 }
2310 pub fn get_hdrsize(&self) -> Result<u32, ErrorContext> {
2311 let mut iter = self.clone();
2312 iter.pos = 0;
2313 for attr in iter {
2314 if let OpGetfamilyDumpReply::Hdrsize(val) = attr? {
2315 return Ok(val);
2316 }
2317 }
2318 Err(ErrorContext::new_missing(
2319 "OpGetfamilyDumpReply",
2320 "Hdrsize",
2321 self.orig_loc,
2322 self.buf.as_ptr() as usize,
2323 ))
2324 }
2325 pub fn get_maxattr(&self) -> Result<u32, ErrorContext> {
2326 let mut iter = self.clone();
2327 iter.pos = 0;
2328 for attr in iter {
2329 if let OpGetfamilyDumpReply::Maxattr(val) = attr? {
2330 return Ok(val);
2331 }
2332 }
2333 Err(ErrorContext::new_missing(
2334 "OpGetfamilyDumpReply",
2335 "Maxattr",
2336 self.orig_loc,
2337 self.buf.as_ptr() as usize,
2338 ))
2339 }
2340 pub fn get_ops(
2341 &self,
2342 ) -> Result<ArrayIterable<IterableArrayOpAttrs<'a>, IterableOpAttrs<'a>>, ErrorContext> {
2343 for attr in self.clone() {
2344 if let OpGetfamilyDumpReply::Ops(val) = attr? {
2345 return Ok(ArrayIterable::new(val));
2346 }
2347 }
2348 Err(ErrorContext::new_missing(
2349 "OpGetfamilyDumpReply",
2350 "Ops",
2351 self.orig_loc,
2352 self.buf.as_ptr() as usize,
2353 ))
2354 }
2355 pub fn get_mcast_groups(
2356 &self,
2357 ) -> Result<
2358 ArrayIterable<IterableArrayMcastGroupAttrs<'a>, IterableMcastGroupAttrs<'a>>,
2359 ErrorContext,
2360 > {
2361 for attr in self.clone() {
2362 if let OpGetfamilyDumpReply::McastGroups(val) = attr? {
2363 return Ok(ArrayIterable::new(val));
2364 }
2365 }
2366 Err(ErrorContext::new_missing(
2367 "OpGetfamilyDumpReply",
2368 "McastGroups",
2369 self.orig_loc,
2370 self.buf.as_ptr() as usize,
2371 ))
2372 }
2373}
2374impl<'a> OpGetfamilyDumpReply<'a> {
2375 pub fn new(buf: &'a [u8]) -> IterableOpGetfamilyDumpReply<'a> {
2376 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
2377 IterableOpGetfamilyDumpReply::with_loc(attrs, buf.as_ptr() as usize)
2378 }
2379 fn attr_from_type(r#type: u16) -> Option<&'static str> {
2380 CtrlAttrs::attr_from_type(r#type)
2381 }
2382}
2383#[derive(Clone, Copy, Default)]
2384pub struct IterableOpGetfamilyDumpReply<'a> {
2385 buf: &'a [u8],
2386 pos: usize,
2387 orig_loc: usize,
2388}
2389impl<'a> IterableOpGetfamilyDumpReply<'a> {
2390 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
2391 Self {
2392 buf,
2393 pos: 0,
2394 orig_loc,
2395 }
2396 }
2397 pub fn get_buf(&self) -> &'a [u8] {
2398 self.buf
2399 }
2400}
2401impl<'a> Iterator for IterableOpGetfamilyDumpReply<'a> {
2402 type Item = Result<OpGetfamilyDumpReply<'a>, ErrorContext>;
2403 fn next(&mut self) -> Option<Self::Item> {
2404 if self.buf.len() == self.pos {
2405 return None;
2406 }
2407 let pos = self.pos;
2408 let mut r#type = None;
2409 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
2410 r#type = Some(header.r#type);
2411 let res = match header.r#type {
2412 1u16 => OpGetfamilyDumpReply::FamilyId({
2413 let res = parse_u16(next);
2414 let Some(val) = res else { break };
2415 val
2416 }),
2417 2u16 => OpGetfamilyDumpReply::FamilyName({
2418 let res = CStr::from_bytes_with_nul(next).ok();
2419 let Some(val) = res else { break };
2420 val
2421 }),
2422 3u16 => OpGetfamilyDumpReply::Version({
2423 let res = parse_u32(next);
2424 let Some(val) = res else { break };
2425 val
2426 }),
2427 4u16 => OpGetfamilyDumpReply::Hdrsize({
2428 let res = parse_u32(next);
2429 let Some(val) = res else { break };
2430 val
2431 }),
2432 5u16 => OpGetfamilyDumpReply::Maxattr({
2433 let res = parse_u32(next);
2434 let Some(val) = res else { break };
2435 val
2436 }),
2437 6u16 => OpGetfamilyDumpReply::Ops({
2438 let res = Some(IterableArrayOpAttrs::with_loc(next, self.orig_loc));
2439 let Some(val) = res else { break };
2440 val
2441 }),
2442 7u16 => OpGetfamilyDumpReply::McastGroups({
2443 let res = Some(IterableArrayMcastGroupAttrs::with_loc(next, self.orig_loc));
2444 let Some(val) = res else { break };
2445 val
2446 }),
2447 n => {
2448 if cfg!(any(test, feature = "deny-unknown-attrs")) {
2449 break;
2450 } else {
2451 continue;
2452 }
2453 }
2454 };
2455 return Some(Ok(res));
2456 }
2457 Some(Err(ErrorContext::new(
2458 "OpGetfamilyDumpReply",
2459 r#type.and_then(|t| OpGetfamilyDumpReply::attr_from_type(t)),
2460 self.orig_loc,
2461 self.buf.as_ptr().wrapping_add(pos) as usize,
2462 )))
2463 }
2464}
2465impl<'a> std::fmt::Debug for IterableOpGetfamilyDumpReply<'_> {
2466 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2467 let mut fmt = f.debug_struct("OpGetfamilyDumpReply");
2468 for attr in self.clone() {
2469 let attr = match attr {
2470 Ok(a) => a,
2471 Err(err) => {
2472 fmt.finish()?;
2473 f.write_str("Err(")?;
2474 err.fmt(f)?;
2475 return f.write_str(")");
2476 }
2477 };
2478 match attr {
2479 OpGetfamilyDumpReply::FamilyId(val) => fmt.field("FamilyId", &val),
2480 OpGetfamilyDumpReply::FamilyName(val) => fmt.field("FamilyName", &val),
2481 OpGetfamilyDumpReply::Version(val) => fmt.field("Version", &val),
2482 OpGetfamilyDumpReply::Hdrsize(val) => fmt.field("Hdrsize", &val),
2483 OpGetfamilyDumpReply::Maxattr(val) => fmt.field("Maxattr", &val),
2484 OpGetfamilyDumpReply::Ops(val) => fmt.field("Ops", &val),
2485 OpGetfamilyDumpReply::McastGroups(val) => fmt.field("McastGroups", &val),
2486 };
2487 }
2488 fmt.finish()
2489 }
2490}
2491impl IterableOpGetfamilyDumpReply<'_> {
2492 pub fn lookup_attr(
2493 &self,
2494 offset: usize,
2495 missing_type: Option<u16>,
2496 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
2497 let mut stack = Vec::new();
2498 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
2499 if cur == offset + PushBuiltinNfgenmsg::len() {
2500 stack.push(("OpGetfamilyDumpReply", offset));
2501 return (
2502 stack,
2503 missing_type.and_then(|t| OpGetfamilyDumpReply::attr_from_type(t)),
2504 );
2505 }
2506 if cur > offset || cur + self.buf.len() < offset {
2507 return (stack, None);
2508 }
2509 let mut attrs = self.clone();
2510 let mut last_off = cur + attrs.pos;
2511 let mut missing = None;
2512 while let Some(attr) = attrs.next() {
2513 let Ok(attr) = attr else { break };
2514 match attr {
2515 OpGetfamilyDumpReply::FamilyId(val) => {
2516 if last_off == offset {
2517 stack.push(("FamilyId", last_off));
2518 break;
2519 }
2520 }
2521 OpGetfamilyDumpReply::FamilyName(val) => {
2522 if last_off == offset {
2523 stack.push(("FamilyName", last_off));
2524 break;
2525 }
2526 }
2527 OpGetfamilyDumpReply::Version(val) => {
2528 if last_off == offset {
2529 stack.push(("Version", last_off));
2530 break;
2531 }
2532 }
2533 OpGetfamilyDumpReply::Hdrsize(val) => {
2534 if last_off == offset {
2535 stack.push(("Hdrsize", last_off));
2536 break;
2537 }
2538 }
2539 OpGetfamilyDumpReply::Maxattr(val) => {
2540 if last_off == offset {
2541 stack.push(("Maxattr", last_off));
2542 break;
2543 }
2544 }
2545 OpGetfamilyDumpReply::Ops(val) => {
2546 for entry in val {
2547 let Ok(attr) = entry else { break };
2548 (stack, missing) = attr.lookup_attr(offset, missing_type);
2549 if !stack.is_empty() {
2550 break;
2551 }
2552 }
2553 if !stack.is_empty() {
2554 stack.push(("Ops", last_off));
2555 break;
2556 }
2557 }
2558 OpGetfamilyDumpReply::McastGroups(val) => {
2559 for entry in val {
2560 let Ok(attr) = entry else { break };
2561 (stack, missing) = attr.lookup_attr(offset, missing_type);
2562 if !stack.is_empty() {
2563 break;
2564 }
2565 }
2566 if !stack.is_empty() {
2567 stack.push(("McastGroups", last_off));
2568 break;
2569 }
2570 }
2571 _ => {}
2572 };
2573 last_off = cur + attrs.pos;
2574 }
2575 if !stack.is_empty() {
2576 stack.push(("OpGetfamilyDumpReply", cur));
2577 }
2578 (stack, missing)
2579 }
2580}
2581#[derive(Debug)]
2582pub struct RequestOpGetfamilyDumpRequest<'r> {
2583 request: Request<'r>,
2584}
2585impl<'r> RequestOpGetfamilyDumpRequest<'r> {
2586 pub fn new(mut request: Request<'r>) -> Self {
2587 PushOpGetfamilyDumpRequest::write_header(&mut request.buf_mut());
2588 Self {
2589 request: request.set_dump(),
2590 }
2591 }
2592 pub fn encode(&mut self) -> PushOpGetfamilyDumpRequest<&mut Vec<u8>> {
2593 PushOpGetfamilyDumpRequest::new_without_header(self.request.buf_mut())
2594 }
2595 pub fn into_encoder(self) -> PushOpGetfamilyDumpRequest<RequestBuf<'r>> {
2596 PushOpGetfamilyDumpRequest::new_without_header(self.request.buf)
2597 }
2598}
2599impl NetlinkRequest for RequestOpGetfamilyDumpRequest<'_> {
2600 type ReplyType<'buf> = IterableOpGetfamilyDumpReply<'buf>;
2601 fn protocol(&self) -> Protocol {
2602 Protocol::Raw {
2603 protonum: 0x10,
2604 request_type: 0x10,
2605 }
2606 }
2607 fn flags(&self) -> u16 {
2608 self.request.flags
2609 }
2610 fn payload(&self) -> &[u8] {
2611 self.request.buf()
2612 }
2613 fn decode_reply<'buf>(buf: &'buf [u8]) -> Self::ReplyType<'buf> {
2614 OpGetfamilyDumpReply::new(buf)
2615 }
2616 fn lookup(
2617 buf: &[u8],
2618 offset: usize,
2619 missing_type: Option<u16>,
2620 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
2621 OpGetfamilyDumpRequest::new(buf).lookup_attr(offset, missing_type)
2622 }
2623}
2624#[doc = "Get / dump genetlink families"]
2625pub struct PushOpGetfamilyDoRequest<Prev: Rec> {
2626 pub(crate) prev: Option<Prev>,
2627 pub(crate) header_offset: Option<usize>,
2628}
2629impl<Prev: Rec> Rec for PushOpGetfamilyDoRequest<Prev> {
2630 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
2631 self.prev.as_mut().unwrap().as_rec_mut()
2632 }
2633}
2634impl<Prev: Rec> PushOpGetfamilyDoRequest<Prev> {
2635 pub fn new(mut prev: Prev) -> Self {
2636 Self::write_header(&mut prev);
2637 Self::new_without_header(prev)
2638 }
2639 fn new_without_header(prev: Prev) -> Self {
2640 Self {
2641 prev: Some(prev),
2642 header_offset: None,
2643 }
2644 }
2645 fn write_header(prev: &mut Prev) {
2646 let mut header = PushBuiltinNfgenmsg::new();
2647 header.set_cmd(3u8);
2648 header.set_version(1u8);
2649 prev.as_rec_mut().extend(header.as_slice());
2650 }
2651 pub fn end_nested(mut self) -> Prev {
2652 let mut prev = self.prev.take().unwrap();
2653 if let Some(header_offset) = &self.header_offset {
2654 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2655 }
2656 prev
2657 }
2658 pub fn push_family_name(mut self, value: &CStr) -> Self {
2659 push_header(
2660 self.as_rec_mut(),
2661 2u16,
2662 value.to_bytes_with_nul().len() as u16,
2663 );
2664 self.as_rec_mut().extend(value.to_bytes_with_nul());
2665 self
2666 }
2667 pub fn push_family_name_bytes(mut self, value: &[u8]) -> Self {
2668 push_header(self.as_rec_mut(), 2u16, (value.len() + 1) as u16);
2669 self.as_rec_mut().extend(value);
2670 self.as_rec_mut().push(0);
2671 self
2672 }
2673}
2674impl<Prev: Rec> Drop for PushOpGetfamilyDoRequest<Prev> {
2675 fn drop(&mut self) {
2676 if let Some(prev) = &mut self.prev {
2677 if let Some(header_offset) = &self.header_offset {
2678 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2679 }
2680 }
2681 }
2682}
2683#[doc = "Get / dump genetlink families"]
2684#[derive(Clone)]
2685pub enum OpGetfamilyDoRequest<'a> {
2686 FamilyName(&'a CStr),
2687}
2688impl<'a> IterableOpGetfamilyDoRequest<'a> {
2689 pub fn get_family_name(&self) -> Result<&'a CStr, ErrorContext> {
2690 let mut iter = self.clone();
2691 iter.pos = 0;
2692 for attr in iter {
2693 if let OpGetfamilyDoRequest::FamilyName(val) = attr? {
2694 return Ok(val);
2695 }
2696 }
2697 Err(ErrorContext::new_missing(
2698 "OpGetfamilyDoRequest",
2699 "FamilyName",
2700 self.orig_loc,
2701 self.buf.as_ptr() as usize,
2702 ))
2703 }
2704}
2705impl<'a> OpGetfamilyDoRequest<'a> {
2706 pub fn new(buf: &'a [u8]) -> IterableOpGetfamilyDoRequest<'a> {
2707 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
2708 IterableOpGetfamilyDoRequest::with_loc(attrs, buf.as_ptr() as usize)
2709 }
2710 fn attr_from_type(r#type: u16) -> Option<&'static str> {
2711 CtrlAttrs::attr_from_type(r#type)
2712 }
2713}
2714#[derive(Clone, Copy, Default)]
2715pub struct IterableOpGetfamilyDoRequest<'a> {
2716 buf: &'a [u8],
2717 pos: usize,
2718 orig_loc: usize,
2719}
2720impl<'a> IterableOpGetfamilyDoRequest<'a> {
2721 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
2722 Self {
2723 buf,
2724 pos: 0,
2725 orig_loc,
2726 }
2727 }
2728 pub fn get_buf(&self) -> &'a [u8] {
2729 self.buf
2730 }
2731}
2732impl<'a> Iterator for IterableOpGetfamilyDoRequest<'a> {
2733 type Item = Result<OpGetfamilyDoRequest<'a>, ErrorContext>;
2734 fn next(&mut self) -> Option<Self::Item> {
2735 if self.buf.len() == self.pos {
2736 return None;
2737 }
2738 let pos = self.pos;
2739 let mut r#type = None;
2740 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
2741 r#type = Some(header.r#type);
2742 let res = match header.r#type {
2743 2u16 => OpGetfamilyDoRequest::FamilyName({
2744 let res = CStr::from_bytes_with_nul(next).ok();
2745 let Some(val) = res else { break };
2746 val
2747 }),
2748 n => {
2749 if cfg!(any(test, feature = "deny-unknown-attrs")) {
2750 break;
2751 } else {
2752 continue;
2753 }
2754 }
2755 };
2756 return Some(Ok(res));
2757 }
2758 Some(Err(ErrorContext::new(
2759 "OpGetfamilyDoRequest",
2760 r#type.and_then(|t| OpGetfamilyDoRequest::attr_from_type(t)),
2761 self.orig_loc,
2762 self.buf.as_ptr().wrapping_add(pos) as usize,
2763 )))
2764 }
2765}
2766impl<'a> std::fmt::Debug for IterableOpGetfamilyDoRequest<'_> {
2767 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2768 let mut fmt = f.debug_struct("OpGetfamilyDoRequest");
2769 for attr in self.clone() {
2770 let attr = match attr {
2771 Ok(a) => a,
2772 Err(err) => {
2773 fmt.finish()?;
2774 f.write_str("Err(")?;
2775 err.fmt(f)?;
2776 return f.write_str(")");
2777 }
2778 };
2779 match attr {
2780 OpGetfamilyDoRequest::FamilyName(val) => fmt.field("FamilyName", &val),
2781 };
2782 }
2783 fmt.finish()
2784 }
2785}
2786impl IterableOpGetfamilyDoRequest<'_> {
2787 pub fn lookup_attr(
2788 &self,
2789 offset: usize,
2790 missing_type: Option<u16>,
2791 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
2792 let mut stack = Vec::new();
2793 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
2794 if cur == offset + PushBuiltinNfgenmsg::len() {
2795 stack.push(("OpGetfamilyDoRequest", offset));
2796 return (
2797 stack,
2798 missing_type.and_then(|t| OpGetfamilyDoRequest::attr_from_type(t)),
2799 );
2800 }
2801 if cur > offset || cur + self.buf.len() < offset {
2802 return (stack, None);
2803 }
2804 let mut attrs = self.clone();
2805 let mut last_off = cur + attrs.pos;
2806 while let Some(attr) = attrs.next() {
2807 let Ok(attr) = attr else { break };
2808 match attr {
2809 OpGetfamilyDoRequest::FamilyName(val) => {
2810 if last_off == offset {
2811 stack.push(("FamilyName", last_off));
2812 break;
2813 }
2814 }
2815 _ => {}
2816 };
2817 last_off = cur + attrs.pos;
2818 }
2819 if !stack.is_empty() {
2820 stack.push(("OpGetfamilyDoRequest", cur));
2821 }
2822 (stack, None)
2823 }
2824}
2825#[doc = "Get / dump genetlink families"]
2826pub struct PushOpGetfamilyDoReply<Prev: Rec> {
2827 pub(crate) prev: Option<Prev>,
2828 pub(crate) header_offset: Option<usize>,
2829}
2830impl<Prev: Rec> Rec for PushOpGetfamilyDoReply<Prev> {
2831 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
2832 self.prev.as_mut().unwrap().as_rec_mut()
2833 }
2834}
2835impl<Prev: Rec> PushOpGetfamilyDoReply<Prev> {
2836 pub fn new(mut prev: Prev) -> Self {
2837 Self::write_header(&mut prev);
2838 Self::new_without_header(prev)
2839 }
2840 fn new_without_header(prev: Prev) -> Self {
2841 Self {
2842 prev: Some(prev),
2843 header_offset: None,
2844 }
2845 }
2846 fn write_header(prev: &mut Prev) {
2847 let mut header = PushBuiltinNfgenmsg::new();
2848 header.set_cmd(1u8);
2849 header.set_version(1u8);
2850 prev.as_rec_mut().extend(header.as_slice());
2851 }
2852 pub fn end_nested(mut self) -> Prev {
2853 let mut prev = self.prev.take().unwrap();
2854 if let Some(header_offset) = &self.header_offset {
2855 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2856 }
2857 prev
2858 }
2859 pub fn push_family_id(mut self, value: u16) -> Self {
2860 push_header(self.as_rec_mut(), 1u16, 2 as u16);
2861 self.as_rec_mut().extend(value.to_ne_bytes());
2862 self
2863 }
2864 pub fn push_family_name(mut self, value: &CStr) -> Self {
2865 push_header(
2866 self.as_rec_mut(),
2867 2u16,
2868 value.to_bytes_with_nul().len() as u16,
2869 );
2870 self.as_rec_mut().extend(value.to_bytes_with_nul());
2871 self
2872 }
2873 pub fn push_family_name_bytes(mut self, value: &[u8]) -> Self {
2874 push_header(self.as_rec_mut(), 2u16, (value.len() + 1) as u16);
2875 self.as_rec_mut().extend(value);
2876 self.as_rec_mut().push(0);
2877 self
2878 }
2879 pub fn push_version(mut self, value: u32) -> Self {
2880 push_header(self.as_rec_mut(), 3u16, 4 as u16);
2881 self.as_rec_mut().extend(value.to_ne_bytes());
2882 self
2883 }
2884 pub fn push_hdrsize(mut self, value: u32) -> Self {
2885 push_header(self.as_rec_mut(), 4u16, 4 as u16);
2886 self.as_rec_mut().extend(value.to_ne_bytes());
2887 self
2888 }
2889 pub fn push_maxattr(mut self, value: u32) -> Self {
2890 push_header(self.as_rec_mut(), 5u16, 4 as u16);
2891 self.as_rec_mut().extend(value.to_ne_bytes());
2892 self
2893 }
2894 pub fn array_ops(mut self) -> PushArrayOpAttrs<Self> {
2895 let header_offset = push_nested_header(self.as_rec_mut(), 6u16);
2896 PushArrayOpAttrs {
2897 prev: Some(self),
2898 header_offset: Some(header_offset),
2899 counter: 0,
2900 }
2901 }
2902 pub fn array_mcast_groups(mut self) -> PushArrayMcastGroupAttrs<Self> {
2903 let header_offset = push_nested_header(self.as_rec_mut(), 7u16);
2904 PushArrayMcastGroupAttrs {
2905 prev: Some(self),
2906 header_offset: Some(header_offset),
2907 counter: 0,
2908 }
2909 }
2910}
2911impl<Prev: Rec> Drop for PushOpGetfamilyDoReply<Prev> {
2912 fn drop(&mut self) {
2913 if let Some(prev) = &mut self.prev {
2914 if let Some(header_offset) = &self.header_offset {
2915 finalize_nested_header(prev.as_rec_mut(), *header_offset);
2916 }
2917 }
2918 }
2919}
2920#[doc = "Get / dump genetlink families"]
2921#[derive(Clone)]
2922pub enum OpGetfamilyDoReply<'a> {
2923 FamilyId(u16),
2924 FamilyName(&'a CStr),
2925 Version(u32),
2926 Hdrsize(u32),
2927 Maxattr(u32),
2928 Ops(IterableArrayOpAttrs<'a>),
2929 McastGroups(IterableArrayMcastGroupAttrs<'a>),
2930}
2931impl<'a> IterableOpGetfamilyDoReply<'a> {
2932 pub fn get_family_id(&self) -> Result<u16, ErrorContext> {
2933 let mut iter = self.clone();
2934 iter.pos = 0;
2935 for attr in iter {
2936 if let OpGetfamilyDoReply::FamilyId(val) = attr? {
2937 return Ok(val);
2938 }
2939 }
2940 Err(ErrorContext::new_missing(
2941 "OpGetfamilyDoReply",
2942 "FamilyId",
2943 self.orig_loc,
2944 self.buf.as_ptr() as usize,
2945 ))
2946 }
2947 pub fn get_family_name(&self) -> Result<&'a CStr, ErrorContext> {
2948 let mut iter = self.clone();
2949 iter.pos = 0;
2950 for attr in iter {
2951 if let OpGetfamilyDoReply::FamilyName(val) = attr? {
2952 return Ok(val);
2953 }
2954 }
2955 Err(ErrorContext::new_missing(
2956 "OpGetfamilyDoReply",
2957 "FamilyName",
2958 self.orig_loc,
2959 self.buf.as_ptr() as usize,
2960 ))
2961 }
2962 pub fn get_version(&self) -> Result<u32, ErrorContext> {
2963 let mut iter = self.clone();
2964 iter.pos = 0;
2965 for attr in iter {
2966 if let OpGetfamilyDoReply::Version(val) = attr? {
2967 return Ok(val);
2968 }
2969 }
2970 Err(ErrorContext::new_missing(
2971 "OpGetfamilyDoReply",
2972 "Version",
2973 self.orig_loc,
2974 self.buf.as_ptr() as usize,
2975 ))
2976 }
2977 pub fn get_hdrsize(&self) -> Result<u32, ErrorContext> {
2978 let mut iter = self.clone();
2979 iter.pos = 0;
2980 for attr in iter {
2981 if let OpGetfamilyDoReply::Hdrsize(val) = attr? {
2982 return Ok(val);
2983 }
2984 }
2985 Err(ErrorContext::new_missing(
2986 "OpGetfamilyDoReply",
2987 "Hdrsize",
2988 self.orig_loc,
2989 self.buf.as_ptr() as usize,
2990 ))
2991 }
2992 pub fn get_maxattr(&self) -> Result<u32, ErrorContext> {
2993 let mut iter = self.clone();
2994 iter.pos = 0;
2995 for attr in iter {
2996 if let OpGetfamilyDoReply::Maxattr(val) = attr? {
2997 return Ok(val);
2998 }
2999 }
3000 Err(ErrorContext::new_missing(
3001 "OpGetfamilyDoReply",
3002 "Maxattr",
3003 self.orig_loc,
3004 self.buf.as_ptr() as usize,
3005 ))
3006 }
3007 pub fn get_ops(
3008 &self,
3009 ) -> Result<ArrayIterable<IterableArrayOpAttrs<'a>, IterableOpAttrs<'a>>, ErrorContext> {
3010 for attr in self.clone() {
3011 if let OpGetfamilyDoReply::Ops(val) = attr? {
3012 return Ok(ArrayIterable::new(val));
3013 }
3014 }
3015 Err(ErrorContext::new_missing(
3016 "OpGetfamilyDoReply",
3017 "Ops",
3018 self.orig_loc,
3019 self.buf.as_ptr() as usize,
3020 ))
3021 }
3022 pub fn get_mcast_groups(
3023 &self,
3024 ) -> Result<
3025 ArrayIterable<IterableArrayMcastGroupAttrs<'a>, IterableMcastGroupAttrs<'a>>,
3026 ErrorContext,
3027 > {
3028 for attr in self.clone() {
3029 if let OpGetfamilyDoReply::McastGroups(val) = attr? {
3030 return Ok(ArrayIterable::new(val));
3031 }
3032 }
3033 Err(ErrorContext::new_missing(
3034 "OpGetfamilyDoReply",
3035 "McastGroups",
3036 self.orig_loc,
3037 self.buf.as_ptr() as usize,
3038 ))
3039 }
3040}
3041impl<'a> OpGetfamilyDoReply<'a> {
3042 pub fn new(buf: &'a [u8]) -> IterableOpGetfamilyDoReply<'a> {
3043 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
3044 IterableOpGetfamilyDoReply::with_loc(attrs, buf.as_ptr() as usize)
3045 }
3046 fn attr_from_type(r#type: u16) -> Option<&'static str> {
3047 CtrlAttrs::attr_from_type(r#type)
3048 }
3049}
3050#[derive(Clone, Copy, Default)]
3051pub struct IterableOpGetfamilyDoReply<'a> {
3052 buf: &'a [u8],
3053 pos: usize,
3054 orig_loc: usize,
3055}
3056impl<'a> IterableOpGetfamilyDoReply<'a> {
3057 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
3058 Self {
3059 buf,
3060 pos: 0,
3061 orig_loc,
3062 }
3063 }
3064 pub fn get_buf(&self) -> &'a [u8] {
3065 self.buf
3066 }
3067}
3068impl<'a> Iterator for IterableOpGetfamilyDoReply<'a> {
3069 type Item = Result<OpGetfamilyDoReply<'a>, ErrorContext>;
3070 fn next(&mut self) -> Option<Self::Item> {
3071 if self.buf.len() == self.pos {
3072 return None;
3073 }
3074 let pos = self.pos;
3075 let mut r#type = None;
3076 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
3077 r#type = Some(header.r#type);
3078 let res = match header.r#type {
3079 1u16 => OpGetfamilyDoReply::FamilyId({
3080 let res = parse_u16(next);
3081 let Some(val) = res else { break };
3082 val
3083 }),
3084 2u16 => OpGetfamilyDoReply::FamilyName({
3085 let res = CStr::from_bytes_with_nul(next).ok();
3086 let Some(val) = res else { break };
3087 val
3088 }),
3089 3u16 => OpGetfamilyDoReply::Version({
3090 let res = parse_u32(next);
3091 let Some(val) = res else { break };
3092 val
3093 }),
3094 4u16 => OpGetfamilyDoReply::Hdrsize({
3095 let res = parse_u32(next);
3096 let Some(val) = res else { break };
3097 val
3098 }),
3099 5u16 => OpGetfamilyDoReply::Maxattr({
3100 let res = parse_u32(next);
3101 let Some(val) = res else { break };
3102 val
3103 }),
3104 6u16 => OpGetfamilyDoReply::Ops({
3105 let res = Some(IterableArrayOpAttrs::with_loc(next, self.orig_loc));
3106 let Some(val) = res else { break };
3107 val
3108 }),
3109 7u16 => OpGetfamilyDoReply::McastGroups({
3110 let res = Some(IterableArrayMcastGroupAttrs::with_loc(next, self.orig_loc));
3111 let Some(val) = res else { break };
3112 val
3113 }),
3114 n => {
3115 if cfg!(any(test, feature = "deny-unknown-attrs")) {
3116 break;
3117 } else {
3118 continue;
3119 }
3120 }
3121 };
3122 return Some(Ok(res));
3123 }
3124 Some(Err(ErrorContext::new(
3125 "OpGetfamilyDoReply",
3126 r#type.and_then(|t| OpGetfamilyDoReply::attr_from_type(t)),
3127 self.orig_loc,
3128 self.buf.as_ptr().wrapping_add(pos) as usize,
3129 )))
3130 }
3131}
3132impl<'a> std::fmt::Debug for IterableOpGetfamilyDoReply<'_> {
3133 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3134 let mut fmt = f.debug_struct("OpGetfamilyDoReply");
3135 for attr in self.clone() {
3136 let attr = match attr {
3137 Ok(a) => a,
3138 Err(err) => {
3139 fmt.finish()?;
3140 f.write_str("Err(")?;
3141 err.fmt(f)?;
3142 return f.write_str(")");
3143 }
3144 };
3145 match attr {
3146 OpGetfamilyDoReply::FamilyId(val) => fmt.field("FamilyId", &val),
3147 OpGetfamilyDoReply::FamilyName(val) => fmt.field("FamilyName", &val),
3148 OpGetfamilyDoReply::Version(val) => fmt.field("Version", &val),
3149 OpGetfamilyDoReply::Hdrsize(val) => fmt.field("Hdrsize", &val),
3150 OpGetfamilyDoReply::Maxattr(val) => fmt.field("Maxattr", &val),
3151 OpGetfamilyDoReply::Ops(val) => fmt.field("Ops", &val),
3152 OpGetfamilyDoReply::McastGroups(val) => fmt.field("McastGroups", &val),
3153 };
3154 }
3155 fmt.finish()
3156 }
3157}
3158impl IterableOpGetfamilyDoReply<'_> {
3159 pub fn lookup_attr(
3160 &self,
3161 offset: usize,
3162 missing_type: Option<u16>,
3163 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
3164 let mut stack = Vec::new();
3165 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
3166 if cur == offset + PushBuiltinNfgenmsg::len() {
3167 stack.push(("OpGetfamilyDoReply", offset));
3168 return (
3169 stack,
3170 missing_type.and_then(|t| OpGetfamilyDoReply::attr_from_type(t)),
3171 );
3172 }
3173 if cur > offset || cur + self.buf.len() < offset {
3174 return (stack, None);
3175 }
3176 let mut attrs = self.clone();
3177 let mut last_off = cur + attrs.pos;
3178 let mut missing = None;
3179 while let Some(attr) = attrs.next() {
3180 let Ok(attr) = attr else { break };
3181 match attr {
3182 OpGetfamilyDoReply::FamilyId(val) => {
3183 if last_off == offset {
3184 stack.push(("FamilyId", last_off));
3185 break;
3186 }
3187 }
3188 OpGetfamilyDoReply::FamilyName(val) => {
3189 if last_off == offset {
3190 stack.push(("FamilyName", last_off));
3191 break;
3192 }
3193 }
3194 OpGetfamilyDoReply::Version(val) => {
3195 if last_off == offset {
3196 stack.push(("Version", last_off));
3197 break;
3198 }
3199 }
3200 OpGetfamilyDoReply::Hdrsize(val) => {
3201 if last_off == offset {
3202 stack.push(("Hdrsize", last_off));
3203 break;
3204 }
3205 }
3206 OpGetfamilyDoReply::Maxattr(val) => {
3207 if last_off == offset {
3208 stack.push(("Maxattr", last_off));
3209 break;
3210 }
3211 }
3212 OpGetfamilyDoReply::Ops(val) => {
3213 for entry in val {
3214 let Ok(attr) = entry else { break };
3215 (stack, missing) = attr.lookup_attr(offset, missing_type);
3216 if !stack.is_empty() {
3217 break;
3218 }
3219 }
3220 if !stack.is_empty() {
3221 stack.push(("Ops", last_off));
3222 break;
3223 }
3224 }
3225 OpGetfamilyDoReply::McastGroups(val) => {
3226 for entry in val {
3227 let Ok(attr) = entry else { break };
3228 (stack, missing) = attr.lookup_attr(offset, missing_type);
3229 if !stack.is_empty() {
3230 break;
3231 }
3232 }
3233 if !stack.is_empty() {
3234 stack.push(("McastGroups", last_off));
3235 break;
3236 }
3237 }
3238 _ => {}
3239 };
3240 last_off = cur + attrs.pos;
3241 }
3242 if !stack.is_empty() {
3243 stack.push(("OpGetfamilyDoReply", cur));
3244 }
3245 (stack, missing)
3246 }
3247}
3248#[derive(Debug)]
3249pub struct RequestOpGetfamilyDoRequest<'r> {
3250 request: Request<'r>,
3251}
3252impl<'r> RequestOpGetfamilyDoRequest<'r> {
3253 pub fn new(mut request: Request<'r>) -> Self {
3254 PushOpGetfamilyDoRequest::write_header(&mut request.buf_mut());
3255 Self { request: request }
3256 }
3257 pub fn encode(&mut self) -> PushOpGetfamilyDoRequest<&mut Vec<u8>> {
3258 PushOpGetfamilyDoRequest::new_without_header(self.request.buf_mut())
3259 }
3260 pub fn into_encoder(self) -> PushOpGetfamilyDoRequest<RequestBuf<'r>> {
3261 PushOpGetfamilyDoRequest::new_without_header(self.request.buf)
3262 }
3263}
3264impl NetlinkRequest for RequestOpGetfamilyDoRequest<'_> {
3265 type ReplyType<'buf> = IterableOpGetfamilyDoReply<'buf>;
3266 fn protocol(&self) -> Protocol {
3267 Protocol::Raw {
3268 protonum: 0x10,
3269 request_type: 0x10,
3270 }
3271 }
3272 fn flags(&self) -> u16 {
3273 self.request.flags
3274 }
3275 fn payload(&self) -> &[u8] {
3276 self.request.buf()
3277 }
3278 fn decode_reply<'buf>(buf: &'buf [u8]) -> Self::ReplyType<'buf> {
3279 OpGetfamilyDoReply::new(buf)
3280 }
3281 fn lookup(
3282 buf: &[u8],
3283 offset: usize,
3284 missing_type: Option<u16>,
3285 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
3286 OpGetfamilyDoRequest::new(buf).lookup_attr(offset, missing_type)
3287 }
3288}
3289#[doc = "Get / dump genetlink policies"]
3290pub struct PushOpGetpolicyDumpRequest<Prev: Rec> {
3291 pub(crate) prev: Option<Prev>,
3292 pub(crate) header_offset: Option<usize>,
3293}
3294impl<Prev: Rec> Rec for PushOpGetpolicyDumpRequest<Prev> {
3295 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
3296 self.prev.as_mut().unwrap().as_rec_mut()
3297 }
3298}
3299impl<Prev: Rec> PushOpGetpolicyDumpRequest<Prev> {
3300 pub fn new(mut prev: Prev) -> Self {
3301 Self::write_header(&mut prev);
3302 Self::new_without_header(prev)
3303 }
3304 fn new_without_header(prev: Prev) -> Self {
3305 Self {
3306 prev: Some(prev),
3307 header_offset: None,
3308 }
3309 }
3310 fn write_header(prev: &mut Prev) {
3311 let mut header = PushBuiltinNfgenmsg::new();
3312 header.set_cmd(10u8);
3313 header.set_version(1u8);
3314 prev.as_rec_mut().extend(header.as_slice());
3315 }
3316 pub fn end_nested(mut self) -> Prev {
3317 let mut prev = self.prev.take().unwrap();
3318 if let Some(header_offset) = &self.header_offset {
3319 finalize_nested_header(prev.as_rec_mut(), *header_offset);
3320 }
3321 prev
3322 }
3323 pub fn push_family_id(mut self, value: u16) -> Self {
3324 push_header(self.as_rec_mut(), 1u16, 2 as u16);
3325 self.as_rec_mut().extend(value.to_ne_bytes());
3326 self
3327 }
3328 pub fn push_family_name(mut self, value: &CStr) -> Self {
3329 push_header(
3330 self.as_rec_mut(),
3331 2u16,
3332 value.to_bytes_with_nul().len() as u16,
3333 );
3334 self.as_rec_mut().extend(value.to_bytes_with_nul());
3335 self
3336 }
3337 pub fn push_family_name_bytes(mut self, value: &[u8]) -> Self {
3338 push_header(self.as_rec_mut(), 2u16, (value.len() + 1) as u16);
3339 self.as_rec_mut().extend(value);
3340 self.as_rec_mut().push(0);
3341 self
3342 }
3343 pub fn push_op(mut self, value: u32) -> Self {
3344 push_header(self.as_rec_mut(), 10u16, 4 as u16);
3345 self.as_rec_mut().extend(value.to_ne_bytes());
3346 self
3347 }
3348}
3349impl<Prev: Rec> Drop for PushOpGetpolicyDumpRequest<Prev> {
3350 fn drop(&mut self) {
3351 if let Some(prev) = &mut self.prev {
3352 if let Some(header_offset) = &self.header_offset {
3353 finalize_nested_header(prev.as_rec_mut(), *header_offset);
3354 }
3355 }
3356 }
3357}
3358#[doc = "Get / dump genetlink policies"]
3359#[derive(Clone)]
3360pub enum OpGetpolicyDumpRequest<'a> {
3361 FamilyId(u16),
3362 FamilyName(&'a CStr),
3363 Op(u32),
3364}
3365impl<'a> IterableOpGetpolicyDumpRequest<'a> {
3366 pub fn get_family_id(&self) -> Result<u16, ErrorContext> {
3367 let mut iter = self.clone();
3368 iter.pos = 0;
3369 for attr in iter {
3370 if let OpGetpolicyDumpRequest::FamilyId(val) = attr? {
3371 return Ok(val);
3372 }
3373 }
3374 Err(ErrorContext::new_missing(
3375 "OpGetpolicyDumpRequest",
3376 "FamilyId",
3377 self.orig_loc,
3378 self.buf.as_ptr() as usize,
3379 ))
3380 }
3381 pub fn get_family_name(&self) -> Result<&'a CStr, ErrorContext> {
3382 let mut iter = self.clone();
3383 iter.pos = 0;
3384 for attr in iter {
3385 if let OpGetpolicyDumpRequest::FamilyName(val) = attr? {
3386 return Ok(val);
3387 }
3388 }
3389 Err(ErrorContext::new_missing(
3390 "OpGetpolicyDumpRequest",
3391 "FamilyName",
3392 self.orig_loc,
3393 self.buf.as_ptr() as usize,
3394 ))
3395 }
3396 pub fn get_op(&self) -> Result<u32, ErrorContext> {
3397 let mut iter = self.clone();
3398 iter.pos = 0;
3399 for attr in iter {
3400 if let OpGetpolicyDumpRequest::Op(val) = attr? {
3401 return Ok(val);
3402 }
3403 }
3404 Err(ErrorContext::new_missing(
3405 "OpGetpolicyDumpRequest",
3406 "Op",
3407 self.orig_loc,
3408 self.buf.as_ptr() as usize,
3409 ))
3410 }
3411}
3412impl<'a> OpGetpolicyDumpRequest<'a> {
3413 pub fn new(buf: &'a [u8]) -> IterableOpGetpolicyDumpRequest<'a> {
3414 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
3415 IterableOpGetpolicyDumpRequest::with_loc(attrs, buf.as_ptr() as usize)
3416 }
3417 fn attr_from_type(r#type: u16) -> Option<&'static str> {
3418 CtrlAttrs::attr_from_type(r#type)
3419 }
3420}
3421#[derive(Clone, Copy, Default)]
3422pub struct IterableOpGetpolicyDumpRequest<'a> {
3423 buf: &'a [u8],
3424 pos: usize,
3425 orig_loc: usize,
3426}
3427impl<'a> IterableOpGetpolicyDumpRequest<'a> {
3428 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
3429 Self {
3430 buf,
3431 pos: 0,
3432 orig_loc,
3433 }
3434 }
3435 pub fn get_buf(&self) -> &'a [u8] {
3436 self.buf
3437 }
3438}
3439impl<'a> Iterator for IterableOpGetpolicyDumpRequest<'a> {
3440 type Item = Result<OpGetpolicyDumpRequest<'a>, ErrorContext>;
3441 fn next(&mut self) -> Option<Self::Item> {
3442 if self.buf.len() == self.pos {
3443 return None;
3444 }
3445 let pos = self.pos;
3446 let mut r#type = None;
3447 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
3448 r#type = Some(header.r#type);
3449 let res = match header.r#type {
3450 1u16 => OpGetpolicyDumpRequest::FamilyId({
3451 let res = parse_u16(next);
3452 let Some(val) = res else { break };
3453 val
3454 }),
3455 2u16 => OpGetpolicyDumpRequest::FamilyName({
3456 let res = CStr::from_bytes_with_nul(next).ok();
3457 let Some(val) = res else { break };
3458 val
3459 }),
3460 10u16 => OpGetpolicyDumpRequest::Op({
3461 let res = parse_u32(next);
3462 let Some(val) = res else { break };
3463 val
3464 }),
3465 n => {
3466 if cfg!(any(test, feature = "deny-unknown-attrs")) {
3467 break;
3468 } else {
3469 continue;
3470 }
3471 }
3472 };
3473 return Some(Ok(res));
3474 }
3475 Some(Err(ErrorContext::new(
3476 "OpGetpolicyDumpRequest",
3477 r#type.and_then(|t| OpGetpolicyDumpRequest::attr_from_type(t)),
3478 self.orig_loc,
3479 self.buf.as_ptr().wrapping_add(pos) as usize,
3480 )))
3481 }
3482}
3483impl<'a> std::fmt::Debug for IterableOpGetpolicyDumpRequest<'_> {
3484 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3485 let mut fmt = f.debug_struct("OpGetpolicyDumpRequest");
3486 for attr in self.clone() {
3487 let attr = match attr {
3488 Ok(a) => a,
3489 Err(err) => {
3490 fmt.finish()?;
3491 f.write_str("Err(")?;
3492 err.fmt(f)?;
3493 return f.write_str(")");
3494 }
3495 };
3496 match attr {
3497 OpGetpolicyDumpRequest::FamilyId(val) => fmt.field("FamilyId", &val),
3498 OpGetpolicyDumpRequest::FamilyName(val) => fmt.field("FamilyName", &val),
3499 OpGetpolicyDumpRequest::Op(val) => fmt.field("Op", &val),
3500 };
3501 }
3502 fmt.finish()
3503 }
3504}
3505impl IterableOpGetpolicyDumpRequest<'_> {
3506 pub fn lookup_attr(
3507 &self,
3508 offset: usize,
3509 missing_type: Option<u16>,
3510 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
3511 let mut stack = Vec::new();
3512 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
3513 if cur == offset + PushBuiltinNfgenmsg::len() {
3514 stack.push(("OpGetpolicyDumpRequest", offset));
3515 return (
3516 stack,
3517 missing_type.and_then(|t| OpGetpolicyDumpRequest::attr_from_type(t)),
3518 );
3519 }
3520 if cur > offset || cur + self.buf.len() < offset {
3521 return (stack, None);
3522 }
3523 let mut attrs = self.clone();
3524 let mut last_off = cur + attrs.pos;
3525 while let Some(attr) = attrs.next() {
3526 let Ok(attr) = attr else { break };
3527 match attr {
3528 OpGetpolicyDumpRequest::FamilyId(val) => {
3529 if last_off == offset {
3530 stack.push(("FamilyId", last_off));
3531 break;
3532 }
3533 }
3534 OpGetpolicyDumpRequest::FamilyName(val) => {
3535 if last_off == offset {
3536 stack.push(("FamilyName", last_off));
3537 break;
3538 }
3539 }
3540 OpGetpolicyDumpRequest::Op(val) => {
3541 if last_off == offset {
3542 stack.push(("Op", last_off));
3543 break;
3544 }
3545 }
3546 _ => {}
3547 };
3548 last_off = cur + attrs.pos;
3549 }
3550 if !stack.is_empty() {
3551 stack.push(("OpGetpolicyDumpRequest", cur));
3552 }
3553 (stack, None)
3554 }
3555}
3556#[doc = "Get / dump genetlink policies"]
3557pub struct PushOpGetpolicyDumpReply<Prev: Rec> {
3558 pub(crate) prev: Option<Prev>,
3559 pub(crate) header_offset: Option<usize>,
3560}
3561impl<Prev: Rec> Rec for PushOpGetpolicyDumpReply<Prev> {
3562 fn as_rec_mut(&mut self) -> &mut Vec<u8> {
3563 self.prev.as_mut().unwrap().as_rec_mut()
3564 }
3565}
3566impl<Prev: Rec> PushOpGetpolicyDumpReply<Prev> {
3567 pub fn new(mut prev: Prev) -> Self {
3568 Self::write_header(&mut prev);
3569 Self::new_without_header(prev)
3570 }
3571 fn new_without_header(prev: Prev) -> Self {
3572 Self {
3573 prev: Some(prev),
3574 header_offset: None,
3575 }
3576 }
3577 fn write_header(prev: &mut Prev) {
3578 let mut header = PushBuiltinNfgenmsg::new();
3579 header.set_cmd(10u8);
3580 header.set_version(1u8);
3581 prev.as_rec_mut().extend(header.as_slice());
3582 }
3583 pub fn end_nested(mut self) -> Prev {
3584 let mut prev = self.prev.take().unwrap();
3585 if let Some(header_offset) = &self.header_offset {
3586 finalize_nested_header(prev.as_rec_mut(), *header_offset);
3587 }
3588 prev
3589 }
3590 pub fn push_family_id(mut self, value: u16) -> Self {
3591 push_header(self.as_rec_mut(), 1u16, 2 as u16);
3592 self.as_rec_mut().extend(value.to_ne_bytes());
3593 self
3594 }
3595 pub fn nested_policy(mut self) -> PushPolicyAttrs<Self> {
3596 let header_offset = push_nested_header(self.as_rec_mut(), 8u16);
3597 PushPolicyAttrs {
3598 prev: Some(self),
3599 header_offset: Some(header_offset),
3600 }
3601 }
3602 pub fn nested_op_policy(mut self) -> PushOpPolicyAttrs<Self> {
3603 let header_offset = push_nested_header(self.as_rec_mut(), 9u16);
3604 PushOpPolicyAttrs {
3605 prev: Some(self),
3606 header_offset: Some(header_offset),
3607 }
3608 }
3609}
3610impl<Prev: Rec> Drop for PushOpGetpolicyDumpReply<Prev> {
3611 fn drop(&mut self) {
3612 if let Some(prev) = &mut self.prev {
3613 if let Some(header_offset) = &self.header_offset {
3614 finalize_nested_header(prev.as_rec_mut(), *header_offset);
3615 }
3616 }
3617 }
3618}
3619#[doc = "Get / dump genetlink policies"]
3620#[derive(Clone)]
3621pub enum OpGetpolicyDumpReply<'a> {
3622 FamilyId(u16),
3623 Policy(IterablePolicyAttrs<'a>),
3624 OpPolicy(IterableOpPolicyAttrs<'a>),
3625}
3626impl<'a> IterableOpGetpolicyDumpReply<'a> {
3627 pub fn get_family_id(&self) -> Result<u16, ErrorContext> {
3628 let mut iter = self.clone();
3629 iter.pos = 0;
3630 for attr in iter {
3631 if let OpGetpolicyDumpReply::FamilyId(val) = attr? {
3632 return Ok(val);
3633 }
3634 }
3635 Err(ErrorContext::new_missing(
3636 "OpGetpolicyDumpReply",
3637 "FamilyId",
3638 self.orig_loc,
3639 self.buf.as_ptr() as usize,
3640 ))
3641 }
3642 pub fn get_policy(&self) -> Result<IterablePolicyAttrs<'a>, ErrorContext> {
3643 let mut iter = self.clone();
3644 iter.pos = 0;
3645 for attr in iter {
3646 if let OpGetpolicyDumpReply::Policy(val) = attr? {
3647 return Ok(val);
3648 }
3649 }
3650 Err(ErrorContext::new_missing(
3651 "OpGetpolicyDumpReply",
3652 "Policy",
3653 self.orig_loc,
3654 self.buf.as_ptr() as usize,
3655 ))
3656 }
3657 pub fn get_op_policy(&self) -> Result<IterableOpPolicyAttrs<'a>, ErrorContext> {
3658 let mut iter = self.clone();
3659 iter.pos = 0;
3660 for attr in iter {
3661 if let OpGetpolicyDumpReply::OpPolicy(val) = attr? {
3662 return Ok(val);
3663 }
3664 }
3665 Err(ErrorContext::new_missing(
3666 "OpGetpolicyDumpReply",
3667 "OpPolicy",
3668 self.orig_loc,
3669 self.buf.as_ptr() as usize,
3670 ))
3671 }
3672}
3673impl<'a> OpGetpolicyDumpReply<'a> {
3674 pub fn new(buf: &'a [u8]) -> IterableOpGetpolicyDumpReply<'a> {
3675 let (_header, attrs) = buf.split_at(buf.len().min(PushBuiltinNfgenmsg::len()));
3676 IterableOpGetpolicyDumpReply::with_loc(attrs, buf.as_ptr() as usize)
3677 }
3678 fn attr_from_type(r#type: u16) -> Option<&'static str> {
3679 CtrlAttrs::attr_from_type(r#type)
3680 }
3681}
3682#[derive(Clone, Copy, Default)]
3683pub struct IterableOpGetpolicyDumpReply<'a> {
3684 buf: &'a [u8],
3685 pos: usize,
3686 orig_loc: usize,
3687}
3688impl<'a> IterableOpGetpolicyDumpReply<'a> {
3689 fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
3690 Self {
3691 buf,
3692 pos: 0,
3693 orig_loc,
3694 }
3695 }
3696 pub fn get_buf(&self) -> &'a [u8] {
3697 self.buf
3698 }
3699}
3700impl<'a> Iterator for IterableOpGetpolicyDumpReply<'a> {
3701 type Item = Result<OpGetpolicyDumpReply<'a>, ErrorContext>;
3702 fn next(&mut self) -> Option<Self::Item> {
3703 if self.buf.len() == self.pos {
3704 return None;
3705 }
3706 let pos = self.pos;
3707 let mut r#type = None;
3708 while let Some((header, next)) = chop_header(self.buf, &mut self.pos) {
3709 r#type = Some(header.r#type);
3710 let res = match header.r#type {
3711 1u16 => OpGetpolicyDumpReply::FamilyId({
3712 let res = parse_u16(next);
3713 let Some(val) = res else { break };
3714 val
3715 }),
3716 8u16 => OpGetpolicyDumpReply::Policy({
3717 let res = Some(IterablePolicyAttrs::with_loc(next, self.orig_loc));
3718 let Some(val) = res else { break };
3719 val
3720 }),
3721 9u16 => OpGetpolicyDumpReply::OpPolicy({
3722 let res = Some(IterableOpPolicyAttrs::with_loc(next, self.orig_loc));
3723 let Some(val) = res else { break };
3724 val
3725 }),
3726 n => {
3727 if cfg!(any(test, feature = "deny-unknown-attrs")) {
3728 break;
3729 } else {
3730 continue;
3731 }
3732 }
3733 };
3734 return Some(Ok(res));
3735 }
3736 Some(Err(ErrorContext::new(
3737 "OpGetpolicyDumpReply",
3738 r#type.and_then(|t| OpGetpolicyDumpReply::attr_from_type(t)),
3739 self.orig_loc,
3740 self.buf.as_ptr().wrapping_add(pos) as usize,
3741 )))
3742 }
3743}
3744impl<'a> std::fmt::Debug for IterableOpGetpolicyDumpReply<'_> {
3745 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3746 let mut fmt = f.debug_struct("OpGetpolicyDumpReply");
3747 for attr in self.clone() {
3748 let attr = match attr {
3749 Ok(a) => a,
3750 Err(err) => {
3751 fmt.finish()?;
3752 f.write_str("Err(")?;
3753 err.fmt(f)?;
3754 return f.write_str(")");
3755 }
3756 };
3757 match attr {
3758 OpGetpolicyDumpReply::FamilyId(val) => fmt.field("FamilyId", &val),
3759 OpGetpolicyDumpReply::Policy(val) => fmt.field("Policy", &val),
3760 OpGetpolicyDumpReply::OpPolicy(val) => fmt.field("OpPolicy", &val),
3761 };
3762 }
3763 fmt.finish()
3764 }
3765}
3766impl IterableOpGetpolicyDumpReply<'_> {
3767 pub fn lookup_attr(
3768 &self,
3769 offset: usize,
3770 missing_type: Option<u16>,
3771 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
3772 let mut stack = Vec::new();
3773 let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
3774 if cur == offset + PushBuiltinNfgenmsg::len() {
3775 stack.push(("OpGetpolicyDumpReply", offset));
3776 return (
3777 stack,
3778 missing_type.and_then(|t| OpGetpolicyDumpReply::attr_from_type(t)),
3779 );
3780 }
3781 if cur > offset || cur + self.buf.len() < offset {
3782 return (stack, None);
3783 }
3784 let mut attrs = self.clone();
3785 let mut last_off = cur + attrs.pos;
3786 let mut missing = None;
3787 while let Some(attr) = attrs.next() {
3788 let Ok(attr) = attr else { break };
3789 match attr {
3790 OpGetpolicyDumpReply::FamilyId(val) => {
3791 if last_off == offset {
3792 stack.push(("FamilyId", last_off));
3793 break;
3794 }
3795 }
3796 OpGetpolicyDumpReply::Policy(val) => {
3797 (stack, missing) = val.lookup_attr(offset, missing_type);
3798 if !stack.is_empty() {
3799 break;
3800 }
3801 }
3802 OpGetpolicyDumpReply::OpPolicy(val) => {
3803 (stack, missing) = val.lookup_attr(offset, missing_type);
3804 if !stack.is_empty() {
3805 break;
3806 }
3807 }
3808 _ => {}
3809 };
3810 last_off = cur + attrs.pos;
3811 }
3812 if !stack.is_empty() {
3813 stack.push(("OpGetpolicyDumpReply", cur));
3814 }
3815 (stack, missing)
3816 }
3817}
3818#[derive(Debug)]
3819pub struct RequestOpGetpolicyDumpRequest<'r> {
3820 request: Request<'r>,
3821}
3822impl<'r> RequestOpGetpolicyDumpRequest<'r> {
3823 pub fn new(mut request: Request<'r>) -> Self {
3824 PushOpGetpolicyDumpRequest::write_header(&mut request.buf_mut());
3825 Self {
3826 request: request.set_dump(),
3827 }
3828 }
3829 pub fn encode(&mut self) -> PushOpGetpolicyDumpRequest<&mut Vec<u8>> {
3830 PushOpGetpolicyDumpRequest::new_without_header(self.request.buf_mut())
3831 }
3832 pub fn into_encoder(self) -> PushOpGetpolicyDumpRequest<RequestBuf<'r>> {
3833 PushOpGetpolicyDumpRequest::new_without_header(self.request.buf)
3834 }
3835}
3836impl NetlinkRequest for RequestOpGetpolicyDumpRequest<'_> {
3837 type ReplyType<'buf> = IterableOpGetpolicyDumpReply<'buf>;
3838 fn protocol(&self) -> Protocol {
3839 Protocol::Raw {
3840 protonum: 0x10,
3841 request_type: 0x10,
3842 }
3843 }
3844 fn flags(&self) -> u16 {
3845 self.request.flags
3846 }
3847 fn payload(&self) -> &[u8] {
3848 self.request.buf()
3849 }
3850 fn decode_reply<'buf>(buf: &'buf [u8]) -> Self::ReplyType<'buf> {
3851 OpGetpolicyDumpReply::new(buf)
3852 }
3853 fn lookup(
3854 buf: &[u8],
3855 offset: usize,
3856 missing_type: Option<u16>,
3857 ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
3858 OpGetpolicyDumpRequest::new(buf).lookup_attr(offset, missing_type)
3859 }
3860}
3861use crate::traits::LookupFn;
3862use crate::utils::RequestBuf;
3863#[derive(Debug)]
3864pub struct Request<'buf> {
3865 buf: RequestBuf<'buf>,
3866 flags: u16,
3867 writeback: Option<&'buf mut Option<RequestInfo>>,
3868}
3869#[allow(unused)]
3870#[derive(Debug, Clone)]
3871pub struct RequestInfo {
3872 protocol: Protocol,
3873 flags: u16,
3874 name: &'static str,
3875 lookup: LookupFn,
3876}
3877impl Request<'static> {
3878 pub fn new() -> Self {
3879 Self::new_from_buf(Vec::new())
3880 }
3881 pub fn new_from_buf(buf: Vec<u8>) -> Self {
3882 Self {
3883 flags: 0,
3884 buf: RequestBuf::Own(buf),
3885 writeback: None,
3886 }
3887 }
3888 pub fn into_buf(self) -> Vec<u8> {
3889 match self.buf {
3890 RequestBuf::Own(buf) => buf,
3891 _ => unreachable!(),
3892 }
3893 }
3894}
3895impl<'buf> Request<'buf> {
3896 pub fn new_with_buf(buf: &'buf mut Vec<u8>) -> Self {
3897 buf.clear();
3898 Self::new_extend(buf)
3899 }
3900 pub fn new_extend(buf: &'buf mut Vec<u8>) -> Self {
3901 Self {
3902 flags: 0,
3903 buf: RequestBuf::Ref(buf),
3904 writeback: None,
3905 }
3906 }
3907 fn do_writeback(&mut self, protocol: Protocol, name: &'static str, lookup: LookupFn) {
3908 let Some(writeback) = &mut self.writeback else {
3909 return;
3910 };
3911 **writeback = Some(RequestInfo {
3912 protocol,
3913 flags: self.flags,
3914 name,
3915 lookup,
3916 })
3917 }
3918 pub fn buf(&self) -> &Vec<u8> {
3919 self.buf.buf()
3920 }
3921 pub fn buf_mut(&mut self) -> &mut Vec<u8> {
3922 self.buf.buf_mut()
3923 }
3924 #[doc = "Set `NLM_F_CREATE` flag"]
3925 pub fn set_create(mut self) -> Self {
3926 self.flags |= consts::NLM_F_CREATE as u16;
3927 self
3928 }
3929 #[doc = "Set `NLM_F_EXCL` flag"]
3930 pub fn set_excl(mut self) -> Self {
3931 self.flags |= consts::NLM_F_EXCL as u16;
3932 self
3933 }
3934 #[doc = "Set `NLM_F_REPLACE` flag"]
3935 pub fn set_replace(mut self) -> Self {
3936 self.flags |= consts::NLM_F_REPLACE as u16;
3937 self
3938 }
3939 #[doc = "Set `NLM_F_CREATE` and `NLM_F_REPLACE` flag"]
3940 pub fn set_change(self) -> Self {
3941 self.set_create().set_replace()
3942 }
3943 #[doc = "Set `NLM_F_APPEND` flag"]
3944 pub fn set_append(mut self) -> Self {
3945 self.flags |= consts::NLM_F_APPEND as u16;
3946 self
3947 }
3948 #[doc = "Set `NLM_F_DUMP` flag"]
3949 fn set_dump(mut self) -> Self {
3950 self.flags |= consts::NLM_F_DUMP as u16;
3951 self
3952 }
3953 pub fn op_getfamily_dump_request(self) -> RequestOpGetfamilyDumpRequest<'buf> {
3954 let mut res = RequestOpGetfamilyDumpRequest::new(self);
3955 res.request.do_writeback(
3956 res.protocol(),
3957 "op-getfamily-dump-request",
3958 RequestOpGetfamilyDumpRequest::lookup,
3959 );
3960 res
3961 }
3962 pub fn op_getfamily_do_request(self) -> RequestOpGetfamilyDoRequest<'buf> {
3963 let mut res = RequestOpGetfamilyDoRequest::new(self);
3964 res.request.do_writeback(
3965 res.protocol(),
3966 "op-getfamily-do-request",
3967 RequestOpGetfamilyDoRequest::lookup,
3968 );
3969 res
3970 }
3971 pub fn op_getpolicy_dump_request(self) -> RequestOpGetpolicyDumpRequest<'buf> {
3972 let mut res = RequestOpGetpolicyDumpRequest::new(self);
3973 res.request.do_writeback(
3974 res.protocol(),
3975 "op-getpolicy-dump-request",
3976 RequestOpGetpolicyDumpRequest::lookup,
3977 );
3978 res
3979 }
3980}