1use pcap_file::DataLink;
2
3use crate::arg::IfArg;
4use crate::print_opt_value;
5
6#[derive(Default)]
8pub struct IFace<'a> {
9 interface: String,
10 descr: Option<String>,
11 dlt: u32,
12 dltname: Option<String>,
13 dltdescr: Option<String>,
14 args: Vec<IfArg<'a>>,
15 debug: bool,
16}
17
18impl<'a> IFace<'a> {
19 pub fn new(interface: &str) -> Self {
21 Self {
22 interface: interface.to_owned(),
23 dlt: DataLink::USER0.into(), ..Default::default()
25 }
26 }
27
28 pub fn get_interface(&self) -> &str {
30 &self.interface
31 }
32
33 pub fn description(mut self, descr: &str) -> Self {
35 self.descr = Some(descr.to_owned());
36 self
37 }
38
39 pub fn dlt(mut self, dlt: u32) -> Self {
41 self.dlt = dlt;
42 self
43 }
44
45 pub fn dltname(mut self, dltname: &str) -> Self {
47 self.dltname = Some(dltname.to_owned());
48 self
49 }
50
51 pub fn dltdescription(mut self, dltdescr: &str) -> Self {
53 self.dltdescr = Some(dltdescr.to_owned());
54 self
55 }
56
57 pub fn add_arg(&mut self, mut arg: IfArg<'a>) {
59 arg.set_number(self.args.len());
60 self.args.push(arg);
61 }
62
63 pub(crate) fn get_args(&self) -> &[IfArg<'a>] {
64 &self.args
65 }
66
67 pub(crate) fn get_arg_idx(&self, arg: &str) -> Option<usize> {
68 self.args.iter().position(|x| x.get_name() == arg)
69 }
70
71 pub(crate) fn get_arg(&self, aidx: usize) -> &IfArg {
72 &self.args[aidx]
73 }
74
75 pub(crate) fn get_arg_mut(&mut self, aidx: usize) -> &mut IfArg<'a> {
76 &mut self.args[aidx]
77 }
78
79 pub(crate) fn has_reloadable_arg(&self) -> bool {
80 self.args.iter().any(IfArg::has_reload)
81 }
82
83 pub(crate) fn has_debug(&self) -> bool {
84 self.debug
85 }
86
87 pub fn config_debug(&mut self) {
89 if self.debug {
90 return;
91 }
92 self.debug = true;
93 self.add_arg(
94 IfArg::new_boolflag("debug")
95 .display("Run in debug mode")
96 .default(&"false")
97 .tooltip("Print debug messages")
98 .group("Debug"),
99 );
100 self.add_arg(
101 IfArg::new_string("debug-file")
102 .display("Use a file for debug")
103 .tooltip("Set a file where the debug messages are written")
104 .group("Debug"),
105 );
106 }
107
108 pub(crate) fn print_iface(&self) {
109 print!("interface {{value={}}}", self.interface);
110 print_opt_value("display", &self.descr);
111 println!();
112 }
113
114 pub(crate) fn print_dlt_list(&self) {
115 print!(
116 "dlt {{number={}}}{{name={}}}",
117 self.dlt,
118 self.dltname.as_ref().unwrap_or(&self.interface)
119 );
120 print_opt_value("display", &self.dltdescr);
121 println!();
122 }
123
124 pub(crate) fn print_arg_list(&self) {
125 self.args.iter().for_each(IfArg::print_arg);
126 }
127}