Skip to content

Commit 2eb0b6d

Browse files
committed
fix(dc): support updated wireshark definitions
1 parent 17171ec commit 2eb0b6d

File tree

4 files changed

+92
-29
lines changed

4 files changed

+92
-29
lines changed

dc/wireshark/build.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
fn main() {
5-
let plugin_name = option_env("PLUGIN_NAME").unwrap_or_else(|| "dcQUIC".to_string());
6-
println!("cargo:rustc-env=PLUGIN_NAME={plugin_name}");
5+
let plugin_name = fwd("PLUGIN_NAME", "dcQUIC");
6+
let _ = fwd("PLUGIN_MAJOR_VERSION", "4");
7+
let _ = fwd("PLUGIN_MINOR_VERSION", "2");
78
println!(
89
"cargo:rustc-env=PLUGIN_NAME_LOWER={}",
910
plugin_name.to_lowercase()
@@ -18,6 +19,13 @@ fn main() {
1819
}
1920
}
2021

22+
fn fwd<N: AsRef<str>, D: AsRef<str>>(name: N, default: D) -> String {
23+
let name = name.as_ref();
24+
let value = option_env(name).unwrap_or_else(|| default.as_ref().to_string());
25+
println!("cargo:rustc-env={name}={value}");
26+
value
27+
}
28+
2129
fn env<N: AsRef<str>>(name: N) -> String {
2230
let name = name.as_ref();
2331
option_env(name).unwrap_or_else(|| panic!("missing env {name}"))

dc/wireshark/src/plugin.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,29 @@ use std::{ffi::CStr, sync::OnceLock};
88
#[used]
99
static plugin_version: [std::ffi::c_char; 4] = [b'0' as _, b'.' as _, b'1' as _, b'\0' as _];
1010

11+
macro_rules! env_version {
12+
($name:literal) => {{
13+
let v = env!($name);
14+
if v.len() != 1 {
15+
panic!("unexpected version");
16+
}
17+
let v = v.as_bytes()[0] as char;
18+
match v.to_digit(10) {
19+
Some(v) => v as _,
20+
None => panic!("unexpected version"),
21+
}
22+
}}
23+
}
24+
1125
// When bumping, make sure that the bindgen bindings are updated to the new version.
1226
#[no_mangle]
1327
#[used]
14-
static plugin_want_major: std::ffi::c_int = 4;
28+
static plugin_want_major: std::ffi::c_int = env_version!("PLUGIN_MAJOR_VERSION");
1529

1630
// When bumping, make sure that the bindgen bindings are updated to the new version.
1731
#[no_mangle]
1832
#[used]
19-
static plugin_want_minor: std::ffi::c_int = 2;
33+
static plugin_want_minor: std::ffi::c_int = env_version!("PLUGIN_MINOR_VERSION");
2034

2135
#[no_mangle]
2236
pub extern "C" fn plugin_register() {
@@ -85,7 +99,7 @@ unsafe extern "C" fn dissect_heur_udp(
8599
mut pinfo: *mut wireshark_sys::_packet_info,
86100
proto: *mut wireshark_sys::_proto_node,
87101
_: *mut std::ffi::c_void,
88-
) -> i32 {
102+
) -> wireshark_sys::gboolean {
89103
let fields = field::get();
90104

91105
let packet = copy_to_rust(tvb);
@@ -116,7 +130,7 @@ unsafe extern "C" fn dissect_heur_udp(
116130

117131
// Didn't look like a dcQUIC packet.
118132
if accepted_offset == 0 {
119-
return 0;
133+
return false.into();
120134
}
121135

122136
if !info.is_empty() {
@@ -128,15 +142,15 @@ unsafe extern "C" fn dissect_heur_udp(
128142

129143
set_protocol(pinfo, c"dcQUIC");
130144

131-
accepted_offset as _
145+
(accepted_offset != 0).into()
132146
}
133147

134148
unsafe extern "C" fn dissect_heur_tcp(
135149
tvb: *mut wireshark_sys::tvbuff_t,
136150
mut pinfo: *mut wireshark_sys::_packet_info,
137151
proto: *mut wireshark_sys::_proto_node,
138152
_: *mut std::ffi::c_void,
139-
) -> i32 {
153+
) -> wireshark_sys::gboolean {
140154
let fields = field::get();
141155

142156
let packet = copy_to_rust(tvb);
@@ -179,7 +193,7 @@ unsafe extern "C" fn dissect_heur_tcp(
179193

180194
// Didn't look like a dcQUIC segment.
181195
if accepted_offset == 0 {
182-
return 0;
196+
return false.into();
183197
}
184198

185199
if !info.is_empty() {
@@ -191,7 +205,7 @@ unsafe extern "C" fn dissect_heur_tcp(
191205

192206
set_protocol(pinfo, c"TCP/dcQUIC");
193207

194-
accepted_offset as _
208+
(accepted_offset != 0).into()
195209
}
196210

197211
unsafe fn register_root_node(

dc/wireshark/src/wireshark.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ mod wireshark_sys_impl {
168168
buffer.tvb,
169169
parsed.offset as _,
170170
parsed.len as _,
171-
parsed.value as u32,
171+
parsed.value as _,
172172
)
173173
}
174174
}
@@ -186,7 +186,7 @@ mod wireshark_sys_impl {
186186
buffer.tvb,
187187
parsed.offset as _,
188188
parsed.len as _,
189-
parsed.value.into() as u32,
189+
parsed.value.into() as _,
190190
)
191191
}
192192
}
@@ -237,8 +237,8 @@ mod wireshark_sys_impl {
237237
parsed: Parsed<Duration>,
238238
) -> Self::AddedItem {
239239
let time = wireshark_sys::nstime_t {
240-
secs: parsed.value.as_secs() as i64,
241-
nsecs: parsed.value.subsec_nanos() as i32,
240+
secs: parsed.value.as_secs() as _,
241+
nsecs: parsed.value.subsec_nanos() as _,
242242
};
243243
unsafe {
244244
wireshark_sys::proto_tree_add_time(

dc/wireshark/xtask/src/main.rs

+56-15
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ struct Build {
4545
profile: Option<String>,
4646
#[arg(long)]
4747
target: Option<String>,
48+
#[command(flatten)]
49+
wireshark_version: WiresharkVersion,
4850
}
4951

5052
impl Build {
@@ -62,12 +64,15 @@ impl Build {
6264
}
6365

6466
#[derive(Debug, Parser)]
65-
struct Test {}
67+
struct Test {
68+
#[command(flatten)]
69+
wireshark_version: WiresharkVersion,
70+
}
6671

6772
impl Test {
68-
fn run(self, sh: &Shell) -> Result {
73+
fn run(mut self, sh: &Shell) -> Result {
6974
cmd!(sh, "cargo test").run()?;
70-
let plugin_dir = plugin_dir();
75+
let plugin_dir = self.wireshark_version.plugin_dir(sh);
7176

7277
sh.create_dir(format!("target/wireshark/{plugin_dir}"))?;
7378
sh.create_dir("target/pcaps")?;
@@ -176,26 +181,26 @@ fn so() -> &'static str {
176181
}
177182
}
178183

179-
fn plugin_dir() -> &'static str {
180-
if cfg!(target_os = "macos") {
181-
"plugins/4-2/epan"
182-
} else {
183-
"plugins/4.2/epan"
184-
}
185-
}
186-
187184
#[derive(Debug, Parser)]
188-
struct Install {}
185+
struct Install {
186+
#[command(flatten)]
187+
wireshark_version: WiresharkVersion,
188+
}
189189

190190
impl Install {
191-
fn run(self, sh: &Shell) -> Result {
192-
Build::default().run(sh)?;
191+
fn run(mut self, sh: &Shell) -> Result {
192+
let plugin_dir = self.wireshark_version.plugin_dir(sh);
193+
194+
Build {
195+
wireshark_version: self.wireshark_version.clone(),
196+
..Default::default()
197+
}.run(sh)?;
193198

194199
let dir = if cfg!(unix) {
195200
homedir::get_my_home()?
196201
.expect("missing home dir")
197202
.join(".local/lib/wireshark")
198-
.join(plugin_dir())
203+
.join(plugin_dir)
199204
} else {
200205
todo!("OS is currently unsupported")
201206
};
@@ -212,6 +217,42 @@ impl Install {
212217
}
213218
}
214219

220+
#[derive(Clone, Debug, Default, Parser)]
221+
struct WiresharkVersion {
222+
#[arg(long, default_value = "DYNAMIC")]
223+
wireshark_version: String,
224+
}
225+
226+
impl WiresharkVersion {
227+
fn plugin_dir(&mut self, sh: &Shell) -> String {
228+
if self.wireshark_version.is_empty() || self.wireshark_version == "DYNAMIC" {
229+
let tshark = tshark(sh).unwrap();
230+
let output = cmd!(sh, "{tshark} --version").read().unwrap();
231+
let version = output.lines().next().unwrap();
232+
let version = version.trim_start_matches(|v: char| !v.is_digit(10));
233+
let (version, _) = version.split_once(char::is_whitespace).unwrap();
234+
235+
match version.split('.').count() {
236+
2 => {
237+
self.wireshark_version = version.to_string();
238+
}
239+
3 => {
240+
let (version, _) = version.rsplit_once('.').unwrap();
241+
self.wireshark_version = version.to_string();
242+
}
243+
_ => panic!("invalid tshark version: {version}"),
244+
}
245+
}
246+
247+
let value = &self.wireshark_version;
248+
if cfg!(target_os = "macos") {
249+
format!("plugins/{}/epan", value.replace('.', "-"))
250+
} else {
251+
format!("plugins/{value}/epan")
252+
}
253+
}
254+
}
255+
215256
fn main() {
216257
Args::parse().run();
217258
}

0 commit comments

Comments
 (0)