Skip to content

Commit 1255c11

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

File tree

4 files changed

+123
-31
lines changed

4 files changed

+123
-31
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

+85-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 {
@@ -56,18 +58,31 @@ impl Build {
5658
vec![]
5759
};
5860
let profile = self.profile.as_deref().unwrap_or("release");
61+
62+
let _env = sh.push_env(
63+
"PLUGIN_MAJOR_VERSION",
64+
self.wireshark_version.major_version(),
65+
);
66+
let _env = sh.push_env(
67+
"PLUGIN_MINOR_VERSION",
68+
self.wireshark_version.minor_version(),
69+
);
70+
5971
cmd!(sh, "cargo build --profile {profile} {target...}").run()?;
6072
Ok(())
6173
}
6274
}
6375

6476
#[derive(Debug, Parser)]
65-
struct Test {}
77+
struct Test {
78+
#[command(flatten)]
79+
wireshark_version: WiresharkVersion,
80+
}
6681

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

7287
sh.create_dir(format!("target/wireshark/{plugin_dir}"))?;
7388
sh.create_dir("target/pcaps")?;
@@ -76,6 +91,14 @@ impl Test {
7691
let plugin_name = "dcQUIC__DEV";
7792
let plugin_name_lower = &plugin_name.to_lowercase();
7893
let _env = sh.push_env("PLUGIN_NAME", plugin_name);
94+
let _env = sh.push_env(
95+
"PLUGIN_MAJOR_VERSION",
96+
self.wireshark_version.major_version(),
97+
);
98+
let _env = sh.push_env(
99+
"PLUGIN_MINOR_VERSION",
100+
self.wireshark_version.minor_version(),
101+
);
79102

80103
let profile = "release-test";
81104

@@ -176,26 +199,27 @@ fn so() -> &'static str {
176199
}
177200
}
178201

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-
187202
#[derive(Debug, Parser)]
188-
struct Install {}
203+
struct Install {
204+
#[command(flatten)]
205+
wireshark_version: WiresharkVersion,
206+
}
189207

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

194218
let dir = if cfg!(unix) {
195219
homedir::get_my_home()?
196220
.expect("missing home dir")
197221
.join(".local/lib/wireshark")
198-
.join(plugin_dir())
222+
.join(plugin_dir)
199223
} else {
200224
todo!("OS is currently unsupported")
201225
};
@@ -212,6 +236,52 @@ impl Install {
212236
}
213237
}
214238

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

0 commit comments

Comments
 (0)