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
/// A metadata belongs to one [Layer]. It describes one particular information about a [Packet] (example: IP source address).
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Metadata {
/// Name displayed by TShark
name: String,
/// Value displayed by TShark, in a human readable format
/// It uses pyshark-like algorithm to display the best 'value' :
/// it looks for "show" first, then "value", finally "showname"
value: String,
/// Value displayed by TShark, if different from human readable format
raw_value: Option<String>,
/// Both name and value, as displayed by thshark
display: Option<String>,
/// Size of this data extracted from packet header protocol, in bytes
size: Option<u32>,
/// Offset of this data in the packet, in bytes
position: Option<u32>,
}
/// This is one metadata from a given layer of the packet returned by TShark application.
impl Metadata {
/// Creates a new metadata. This function is useless for most applications.
pub fn new(
name: String,
value: String,
display: Option<String>,
size: Option<u32>,
position: Option<u32>,
) -> Metadata {
Metadata {
name,
value,
raw_value: None,
display,
size,
position,
}
}
/// Get the name of this metadata. The name is returned by TShark.
///
/// # Examples
///
/// ```
/// let ip_src = rtshark::Metadata::new("ip.src".to_string(), "127.0.0.1".to_string(), None, None, None);
/// assert_eq!(ip_src.name(), "ip.src")
/// ```
pub fn name(&self) -> &str {
self.name.as_str()
}
/// Value for this metadata, displayed by TShark, in a human readable format.
/// It uses pyshark-like algorithm to display the best 'value' :
/// it looks for "show" first, then "value", finally "showname".
///
/// # Examples
///
/// ```
/// let ip_src = rtshark::Metadata::new("ip.src".to_string(), "127.0.0.1".to_string(), None, None, None);
/// assert_eq!(ip_src.value(), "127.0.0.1")
/// ```
pub fn value(&self) -> &str {
self.value.as_str()
}
/// Raw value for this metadata, displayed by TShark.
///
/// This value is not set when using metadata whitelist filtering.
///
/// When `value` is set to "show" instead of "value", "value" can still
/// be retrieved from `raw_value`.
pub fn raw_value(&self) -> &str {
self.raw_value.as_ref().unwrap_or(&self.value).as_str()
}
/// Set "raw value", from by TShark output.
pub(crate) fn raw_value_mut(&mut self) -> &mut Option<String> {
&mut self.raw_value
}
/// Both name and value, as displayed by TShark
///
/// This value is not set when using metadata whitelist filtering.
///
/// # Examples
///
/// ```
/// let ip_src = rtshark::Metadata::new("ip.src".to_string(), "127.0.0.1".to_string(), Some("Source: 127.0.0.1".to_string()), None, None);
/// assert_eq!(ip_src.display(), Some("Source: 127.0.0.1"))
/// ```
pub fn display(&self) -> Option<&str> {
self.display.as_deref()
}
/// Set "display", from by TShark output.
pub(crate) fn display_mut(&mut self) -> &mut Option<String> {
&mut self.display
}
/// Size of this data extracted from packet header protocol, in bytes
///
/// This value is not set when using metadata whitelist filtering.
///
/// # Examples
///
/// ```
/// let ip_src = rtshark::Metadata::new("ip.src".to_string(), "127.0.0.1".to_string(), Some("Source: 127.0.0.1".to_string()), Some(4), Some(12));
/// assert_eq!(ip_src.size(), Some(4))
/// ```
pub fn size(&self) -> Option<u32> {
self.size
}
/// Set "size", from by TShark output.
pub(crate) fn size_mut(&mut self) -> &mut Option<u32> {
&mut self.size
}
/// Offset of this data in the packet, in bytes
///
/// This value is not set when using metadata whitelist filtering.
///
/// # Examples
///
/// ```
/// let ip_src = rtshark::Metadata::new("ip.src".to_string(), "127.0.0.1".to_string(), Some("Source: 127.0.0.1".to_string()), Some(4), Some(12));
/// assert_eq!(ip_src.position(), Some(12))
/// ```
pub fn position(&self) -> Option<u32> {
self.position
}
/// Set "position", from by TShark output.
pub(crate) fn position_mut(&mut self) -> &mut Option<u32> {
&mut self.position
}
}
#[cfg(test)]
mod tests {}