netlink-bindings 0.3.2

Type-safe Rust bindings for Netlink generated from YAML specifications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#![doc = "Binder interface over generic netlink\n"]
#![allow(clippy::all)]
#![allow(unused_imports)]
#![allow(unused_assignments)]
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(irrefutable_let_patterns)]
#![allow(unreachable_code)]
#![allow(unreachable_patterns)]
use crate::builtin::{BuiltinBitfield32, BuiltinNfgenmsg, Nlmsghdr, PushDummy};
use crate::{
    consts,
    traits::{NetlinkRequest, Protocol},
    utils::*,
};
pub const PROTONAME: &str = "binder";
pub const PROTONAME_CSTR: &CStr = c"binder";
#[derive(Clone)]
pub enum Report<'a> {
    #[doc = "The enum binder_driver_return_protocol returned to the sender.\n"]
    Error(u32),
    #[doc = "The binder context where the transaction occurred.\n"]
    Context(&'a CStr),
    #[doc = "The PID of the sender process.\n"]
    FromPid(u32),
    #[doc = "The TID of the sender thread.\n"]
    FromTid(u32),
    #[doc = "The PID of the recipient process. This attribute may not be present if\nthe target could not be determined.\n"]
    ToPid(u32),
    #[doc = "The TID of the recipient thread. This attribute may not be present if\nthe target could not be determined.\n"]
    ToTid(u32),
    #[doc = "When present, indicates the failed transaction is a reply.\n"]
    IsReply(()),
    #[doc = "The bitmask of enum transaction_flags from the transaction.\n"]
    Flags(u32),
    #[doc = "The application-defined code from the transaction.\n"]
    Code(u32),
    #[doc = "The transaction payload size in bytes.\n"]
    DataSize(u32),
}
impl<'a> IterableReport<'a> {
    #[doc = "The enum binder_driver_return_protocol returned to the sender.\n"]
    pub fn get_error(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::Error(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "Error",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The binder context where the transaction occurred.\n"]
    pub fn get_context(&self) -> Result<&'a CStr, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::Context(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "Context",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The PID of the sender process.\n"]
    pub fn get_from_pid(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::FromPid(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "FromPid",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The TID of the sender thread.\n"]
    pub fn get_from_tid(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::FromTid(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "FromTid",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The PID of the recipient process. This attribute may not be present if\nthe target could not be determined.\n"]
    pub fn get_to_pid(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::ToPid(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "ToPid",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The TID of the recipient thread. This attribute may not be present if\nthe target could not be determined.\n"]
    pub fn get_to_tid(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::ToTid(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "ToTid",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "When present, indicates the failed transaction is a reply.\n"]
    pub fn get_is_reply(&self) -> Result<(), ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::IsReply(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "IsReply",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The bitmask of enum transaction_flags from the transaction.\n"]
    pub fn get_flags(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::Flags(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "Flags",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The application-defined code from the transaction.\n"]
    pub fn get_code(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::Code(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "Code",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
    #[doc = "The transaction payload size in bytes.\n"]
    pub fn get_data_size(&self) -> Result<u32, ErrorContext> {
        let mut iter = self.clone();
        iter.pos = 0;
        for attr in iter {
            if let Ok(Report::DataSize(val)) = attr {
                return Ok(val);
            }
        }
        Err(ErrorContext::new_missing(
            "Report",
            "DataSize",
            self.orig_loc,
            self.buf.as_ptr() as usize,
        ))
    }
}
impl Report<'_> {
    pub fn new<'a>(buf: &'a [u8]) -> IterableReport<'a> {
        IterableReport::with_loc(buf, buf.as_ptr() as usize)
    }
    fn attr_from_type(r#type: u16) -> Option<&'static str> {
        let res = match r#type {
            1u16 => "Error",
            2u16 => "Context",
            3u16 => "FromPid",
            4u16 => "FromTid",
            5u16 => "ToPid",
            6u16 => "ToTid",
            7u16 => "IsReply",
            8u16 => "Flags",
            9u16 => "Code",
            10u16 => "DataSize",
            _ => return None,
        };
        Some(res)
    }
}
#[derive(Clone, Copy, Default)]
pub struct IterableReport<'a> {
    buf: &'a [u8],
    pos: usize,
    orig_loc: usize,
}
impl<'a> IterableReport<'a> {
    fn with_loc(buf: &'a [u8], orig_loc: usize) -> Self {
        Self {
            buf,
            pos: 0,
            orig_loc,
        }
    }
    pub fn get_buf(&self) -> &'a [u8] {
        self.buf
    }
}
impl<'a> Iterator for IterableReport<'a> {
    type Item = Result<Report<'a>, ErrorContext>;
    fn next(&mut self) -> Option<Self::Item> {
        let mut pos;
        let mut r#type;
        loop {
            pos = self.pos;
            r#type = None;
            if self.buf.len() == self.pos {
                return None;
            }
            let Some((header, next)) = chop_header(self.buf, &mut self.pos) else {
                self.pos = self.buf.len();
                break;
            };
            r#type = Some(header.r#type);
            let res = match header.r#type {
                1u16 => Report::Error({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                2u16 => Report::Context({
                    let res = CStr::from_bytes_with_nul(next).ok();
                    let Some(val) = res else { break };
                    val
                }),
                3u16 => Report::FromPid({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                4u16 => Report::FromTid({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                5u16 => Report::ToPid({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                6u16 => Report::ToTid({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                7u16 => Report::IsReply(()),
                8u16 => Report::Flags({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                9u16 => Report::Code({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                10u16 => Report::DataSize({
                    let res = parse_u32(next);
                    let Some(val) = res else { break };
                    val
                }),
                n if cfg!(any(test, feature = "deny-unknown-attrs")) => break,
                n => continue,
            };
            return Some(Ok(res));
        }
        Some(Err(ErrorContext::new(
            "Report",
            r#type.and_then(|t| Report::attr_from_type(t)),
            self.orig_loc,
            self.buf.as_ptr().wrapping_add(pos) as usize,
        )))
    }
}
impl<'a> std::fmt::Debug for IterableReport<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut fmt = f.debug_struct("Report");
        for attr in self.clone() {
            let attr = match attr {
                Ok(a) => a,
                Err(err) => {
                    fmt.finish()?;
                    f.write_str("Err(")?;
                    err.fmt(f)?;
                    return f.write_str(")");
                }
            };
            match attr {
                Report::Error(val) => fmt.field("Error", &val),
                Report::Context(val) => fmt.field("Context", &val),
                Report::FromPid(val) => fmt.field("FromPid", &val),
                Report::FromTid(val) => fmt.field("FromTid", &val),
                Report::ToPid(val) => fmt.field("ToPid", &val),
                Report::ToTid(val) => fmt.field("ToTid", &val),
                Report::IsReply(val) => fmt.field("IsReply", &val),
                Report::Flags(val) => fmt.field("Flags", &val),
                Report::Code(val) => fmt.field("Code", &val),
                Report::DataSize(val) => fmt.field("DataSize", &val),
            };
        }
        fmt.finish()
    }
}
impl IterableReport<'_> {
    pub fn lookup_attr(
        &self,
        offset: usize,
        missing_type: Option<u16>,
    ) -> (Vec<(&'static str, usize)>, Option<&'static str>) {
        let mut stack = Vec::new();
        let cur = ErrorContext::calc_offset(self.orig_loc, self.buf.as_ptr() as usize);
        if missing_type.is_some() && cur == offset {
            stack.push(("Report", offset));
            return (stack, missing_type.and_then(|t| Report::attr_from_type(t)));
        }
        if cur > offset || cur + self.buf.len() < offset {
            return (stack, None);
        }
        let mut attrs = self.clone();
        let mut last_off = cur + attrs.pos;
        while let Some(attr) = attrs.next() {
            let Ok(attr) = attr else { break };
            match attr {
                Report::Error(val) => {
                    if last_off == offset {
                        stack.push(("Error", last_off));
                        break;
                    }
                }
                Report::Context(val) => {
                    if last_off == offset {
                        stack.push(("Context", last_off));
                        break;
                    }
                }
                Report::FromPid(val) => {
                    if last_off == offset {
                        stack.push(("FromPid", last_off));
                        break;
                    }
                }
                Report::FromTid(val) => {
                    if last_off == offset {
                        stack.push(("FromTid", last_off));
                        break;
                    }
                }
                Report::ToPid(val) => {
                    if last_off == offset {
                        stack.push(("ToPid", last_off));
                        break;
                    }
                }
                Report::ToTid(val) => {
                    if last_off == offset {
                        stack.push(("ToTid", last_off));
                        break;
                    }
                }
                Report::IsReply(val) => {
                    if last_off == offset {
                        stack.push(("IsReply", last_off));
                        break;
                    }
                }
                Report::Flags(val) => {
                    if last_off == offset {
                        stack.push(("Flags", last_off));
                        break;
                    }
                }
                Report::Code(val) => {
                    if last_off == offset {
                        stack.push(("Code", last_off));
                        break;
                    }
                }
                Report::DataSize(val) => {
                    if last_off == offset {
                        stack.push(("DataSize", last_off));
                        break;
                    }
                }
                _ => {}
            };
            last_off = cur + attrs.pos;
        }
        if !stack.is_empty() {
            stack.push(("Report", cur));
        }
        (stack, None)
    }
}
pub struct PushReport<Prev: Pusher> {
    pub(crate) prev: Option<Prev>,
    pub(crate) header_offset: Option<usize>,
}
impl<Prev: Pusher> Pusher for PushReport<Prev> {
    fn as_vec_mut(&mut self) -> &mut Vec<u8> {
        self.prev.as_mut().unwrap().as_vec_mut()
    }
    fn as_vec(&self) -> &Vec<u8> {
        self.prev.as_ref().unwrap().as_vec()
    }
}
impl<Prev: Pusher> PushReport<Prev> {
    pub fn new(prev: Prev) -> Self {
        Self {
            prev: Some(prev),
            header_offset: None,
        }
    }
    pub fn end_nested(mut self) -> Prev {
        let mut prev = self.prev.take().unwrap();
        if let Some(header_offset) = &self.header_offset {
            finalize_nested_header(prev.as_vec_mut(), *header_offset);
        }
        prev
    }
    #[doc = "The enum binder_driver_return_protocol returned to the sender.\n"]
    pub fn push_error(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 1u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The binder context where the transaction occurred.\n"]
    pub fn push_context(mut self, value: &CStr) -> Self {
        push_header(
            self.as_vec_mut(),
            2u16,
            value.to_bytes_with_nul().len() as u16,
        );
        self.as_vec_mut().extend(value.to_bytes_with_nul());
        self
    }
    #[doc = "The binder context where the transaction occurred.\n"]
    pub fn push_context_bytes(mut self, value: &[u8]) -> Self {
        push_header(self.as_vec_mut(), 2u16, (value.len() + 1) as u16);
        self.as_vec_mut().extend(value);
        self.as_vec_mut().push(0);
        self
    }
    #[doc = "The PID of the sender process.\n"]
    pub fn push_from_pid(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 3u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The TID of the sender thread.\n"]
    pub fn push_from_tid(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 4u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The PID of the recipient process. This attribute may not be present if\nthe target could not be determined.\n"]
    pub fn push_to_pid(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 5u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The TID of the recipient thread. This attribute may not be present if\nthe target could not be determined.\n"]
    pub fn push_to_tid(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 6u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "When present, indicates the failed transaction is a reply.\n"]
    pub fn push_is_reply(mut self, value: ()) -> Self {
        push_header(self.as_vec_mut(), 7u16, 0 as u16);
        self
    }
    #[doc = "The bitmask of enum transaction_flags from the transaction.\n"]
    pub fn push_flags(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 8u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The application-defined code from the transaction.\n"]
    pub fn push_code(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 9u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
    #[doc = "The transaction payload size in bytes.\n"]
    pub fn push_data_size(mut self, value: u32) -> Self {
        push_header(self.as_vec_mut(), 10u16, 4 as u16);
        self.as_vec_mut().extend(value.to_ne_bytes());
        self
    }
}
impl<Prev: Pusher> Drop for PushReport<Prev> {
    fn drop(&mut self) {
        if let Some(prev) = &mut self.prev {
            if let Some(header_offset) = &self.header_offset {
                finalize_nested_header(prev.as_vec_mut(), *header_offset);
            }
        }
    }
}
#[doc = "Notify attributes:\n- [`.get_error()`](IterableReport::get_error)\n- [`.get_context()`](IterableReport::get_context)\n- [`.get_from_pid()`](IterableReport::get_from_pid)\n- [`.get_from_tid()`](IterableReport::get_from_tid)\n- [`.get_to_pid()`](IterableReport::get_to_pid)\n- [`.get_to_tid()`](IterableReport::get_to_tid)\n- [`.get_is_reply()`](IterableReport::get_is_reply)\n- [`.get_flags()`](IterableReport::get_flags)\n- [`.get_code()`](IterableReport::get_code)\n- [`.get_data_size()`](IterableReport::get_data_size)\n"]
#[derive(Debug)]
pub struct OpReportNotif;
impl OpReportNotif {
    pub const CMD: u8 = 1u8;
    pub fn decode_notif<'a>(buf: &'a [u8]) -> IterableReport<'a> {
        let (_header, attrs) = buf.split_at(buf.len().min(BuiltinNfgenmsg::len()));
        IterableReport::with_loc(attrs, buf.as_ptr() as usize)
    }
}
pub struct NotifGroup;
impl NotifGroup {
    #[doc = "Notifications:\n- [`OpReportNotif`]\n"]
    pub const REPORT: &str = "report";
    #[doc = "Notifications:\n- [`OpReportNotif`]\n"]
    pub const REPORT_CSTR: &CStr = c"report";
}
#[cfg(test)]
mod generated_tests {
    use super::*;
    #[test]
    fn tests() {
        let _ = IterableReport::get_code;
        let _ = IterableReport::get_context;
        let _ = IterableReport::get_data_size;
        let _ = IterableReport::get_error;
        let _ = IterableReport::get_flags;
        let _ = IterableReport::get_from_pid;
        let _ = IterableReport::get_from_tid;
        let _ = IterableReport::get_is_reply;
        let _ = IterableReport::get_to_pid;
        let _ = IterableReport::get_to_tid;
        let _ = OpReportNotif;
    }
}