Skip to content

Commit 89be741

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

File tree

4 files changed

+134
-32
lines changed

4 files changed

+134
-32
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

+24-10
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() {
@@ -80,12 +94,12 @@ pub fn copy_to_rust(tvb: *mut wireshark_sys::tvbuff_t) -> Vec<u8> {
8094
buffer
8195
}
8296

83-
unsafe extern "C" fn dissect_heur_udp(
97+
unsafe extern "C" fn dissect_heur_udp<Ret: From<bool>>(
8498
tvb: *mut wireshark_sys::tvbuff_t,
8599
mut pinfo: *mut wireshark_sys::_packet_info,
86100
proto: *mut wireshark_sys::_proto_node,
87101
_: *mut std::ffi::c_void,
88-
) -> i32 {
102+
) -> Ret {
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

134-
unsafe extern "C" fn dissect_heur_tcp(
148+
unsafe extern "C" fn dissect_heur_tcp<Ret: From<bool>>(
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+
) -> Ret {
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

+96-16
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,46 @@ 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 {
51-
fn run(self, sh: &Shell) -> Result {
53+
fn run(mut self, sh: &Shell) -> Result {
54+
self.wireshark_version.load(sh);
55+
5256
let target = if let Some(target) = self.target.as_ref() {
5357
let _ = cmd!(sh, "rustup target add {target}").run();
5458
vec!["--target", target]
5559
} else {
5660
vec![]
5761
};
5862
let profile = self.profile.as_deref().unwrap_or("release");
63+
64+
let _env = sh.push_env(
65+
"PLUGIN_MAJOR_VERSION",
66+
self.wireshark_version.major_version(),
67+
);
68+
let _env = sh.push_env(
69+
"PLUGIN_MINOR_VERSION",
70+
self.wireshark_version.minor_version(),
71+
);
72+
5973
cmd!(sh, "cargo build --profile {profile} {target...}").run()?;
6074
Ok(())
6175
}
6276
}
6377

6478
#[derive(Debug, Parser)]
65-
struct Test {}
79+
struct Test {
80+
#[command(flatten)]
81+
wireshark_version: WiresharkVersion,
82+
}
6683

6784
impl Test {
68-
fn run(self, sh: &Shell) -> Result {
85+
fn run(mut self, sh: &Shell) -> Result {
6986
cmd!(sh, "cargo test").run()?;
70-
let plugin_dir = plugin_dir();
87+
let plugin_dir = self.wireshark_version.plugin_dir(sh);
7188

7289
sh.create_dir(format!("target/wireshark/{plugin_dir}"))?;
7390
sh.create_dir("target/pcaps")?;
@@ -76,6 +93,14 @@ impl Test {
7693
let plugin_name = "dcQUIC__DEV";
7794
let plugin_name_lower = &plugin_name.to_lowercase();
7895
let _env = sh.push_env("PLUGIN_NAME", plugin_name);
96+
let _env = sh.push_env(
97+
"PLUGIN_MAJOR_VERSION",
98+
self.wireshark_version.major_version(),
99+
);
100+
let _env = sh.push_env(
101+
"PLUGIN_MINOR_VERSION",
102+
self.wireshark_version.minor_version(),
103+
);
79104

80105
let profile = "release-test";
81106

@@ -176,26 +201,27 @@ fn so() -> &'static str {
176201
}
177202
}
178203

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-
187204
#[derive(Debug, Parser)]
188-
struct Install {}
205+
struct Install {
206+
#[command(flatten)]
207+
wireshark_version: WiresharkVersion,
208+
}
189209

190210
impl Install {
191-
fn run(self, sh: &Shell) -> Result {
192-
Build::default().run(sh)?;
211+
fn run(mut self, sh: &Shell) -> Result {
212+
let plugin_dir = self.wireshark_version.plugin_dir(sh);
213+
214+
Build {
215+
wireshark_version: self.wireshark_version.clone(),
216+
..Default::default()
217+
}
218+
.run(sh)?;
193219

194220
let dir = if cfg!(unix) {
195221
homedir::get_my_home()?
196222
.expect("missing home dir")
197223
.join(".local/lib/wireshark")
198-
.join(plugin_dir())
224+
.join(plugin_dir)
199225
} else {
200226
todo!("OS is currently unsupported")
201227
};
@@ -212,6 +238,60 @@ impl Install {
212238
}
213239
}
214240

241+
#[derive(Clone, Debug, Default, Parser)]
242+
struct WiresharkVersion {
243+
#[arg(long, default_value = "DYNAMIC")]
244+
wireshark_version: String,
245+
}
246+
247+
impl WiresharkVersion {
248+
fn plugin_dir(&mut self, sh: &Shell) -> String {
249+
self.load(sh);
250+
251+
let value = &self.wireshark_version;
252+
if cfg!(target_os = "macos") {
253+
format!("plugins/{}/epan", value.replace('.', "-"))
254+
} else {
255+
format!("plugins/{value}/epan")
256+
}
257+
}
258+
259+
fn load(&mut self, sh: &Shell) {
260+
if !(self.wireshark_version.is_empty() || self.wireshark_version == "DYNAMIC") {
261+
return;
262+
}
263+
264+
let tshark = tshark(sh).unwrap();
265+
let output = cmd!(sh, "{tshark} --version").read().unwrap();
266+
let version = output.lines().next().unwrap();
267+
let version = version.trim_start_matches(|v: char| !v.is_digit(10));
268+
let (version, _) = version
269+
.split_once(char::is_whitespace)
270+
.unwrap_or_else(|| panic!("invalid version line: {version}"));
271+
272+
match version.split('.').count() {
273+
2 => {
274+
self.wireshark_version = version.to_string();
275+
}
276+
3 => {
277+
let (version, _) = version.rsplit_once('.').unwrap();
278+
self.wireshark_version = version.to_string();
279+
}
280+
_ => panic!("invalid tshark version: {version}"),
281+
}
282+
}
283+
284+
fn major_version(&self) -> &str {
285+
let (version, _) = self.wireshark_version.split_once('.').unwrap();
286+
version
287+
}
288+
289+
fn minor_version(&self) -> &str {
290+
let (_, version) = self.wireshark_version.split_once('.').unwrap();
291+
version
292+
}
293+
}
294+
215295
fn main() {
216296
Args::parse().run();
217297
}

0 commit comments

Comments
 (0)