Skip to content

Commit 9e0fb1d

Browse files
committed
24-linktype
Add support for setting and getting linktype. Closes #24
1 parent 0de85e2 commit 9e0fb1d

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub struct Config {
55
max_packets_read: usize,
66
snaplen: u32,
77
buffer_size: u32,
8+
datalink: Option<i32>,
89
bpf: Option<String>,
910
buffer_for: std::time::Duration,
1011
blocking: bool,
@@ -29,6 +30,15 @@ impl Config {
2930
self
3031
}
3132

33+
pub fn datalink(&self) -> &Option<i32> {
34+
&self.datalink
35+
}
36+
37+
pub fn with_datalink_type(&mut self, datalink: i32) -> &mut Self {
38+
self.datalink = Some(datalink);
39+
self
40+
}
41+
3242
pub fn buffer_size(&self) -> u32 {
3343
self.buffer_size
3444
}
@@ -69,6 +79,7 @@ impl Config {
6979
max_packets_read: usize,
7080
snaplen: u32,
7181
buffer_size: u32,
82+
datalink: Option<i32>,
7283
bpf: Option<String>,
7384
buffer_for: std::time::Duration,
7485
blocking: bool,
@@ -77,6 +88,7 @@ impl Config {
7788
max_packets_read,
7889
snaplen,
7990
buffer_size,
91+
datalink,
8092
bpf,
8193
buffer_for,
8294
blocking,
@@ -90,6 +102,7 @@ impl Default for Config {
90102
max_packets_read: 1000,
91103
snaplen: 65535,
92104
buffer_size: 16777216,
105+
datalink: None,
93106
bpf: None,
94107
buffer_for: std::time::Duration::from_millis(100),
95108
blocking: false,

src/handle.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,23 @@ impl Handle {
157157
}
158158
}
159159

160+
pub fn set_datalink(&self, datalink: i32) -> Result<&Self, Error> {
161+
if 0 != unsafe { pcap_sys::pcap_set_datalink(self.handle, datalink as _) } {
162+
Err(pcap_util::convert_libpcap_error(self.handle))
163+
} else {
164+
Ok(self)
165+
}
166+
}
167+
168+
pub fn get_datalink(&self) -> Result<i32, Error> {
169+
let r = unsafe { pcap_sys::pcap_datalink(self.handle) };
170+
if r < 0 {
171+
Err(pcap_util::convert_libpcap_error(self.handle))
172+
} else {
173+
Ok(r)
174+
}
175+
}
176+
160177
pub fn compile_bpf(&self, bpf: &str) -> Result<Bpf, Error> {
161178
let mut bpf_program = pcap_sys::bpf_program {
162179
bf_len: 0,
@@ -297,6 +314,7 @@ mod tests {
297314

298315
assert!(handle.is_ok());
299316
}
317+
300318
#[test]
301319
fn open_dead() {
302320
let _ = env_logger::try_init();
@@ -305,6 +323,22 @@ mod tests {
305323

306324
assert!(handle.is_ok());
307325
}
326+
327+
#[test]
328+
fn set_datalink() {
329+
let _ = env_logger::try_init();
330+
331+
let handle = Handle::dead(0, 0).unwrap();
332+
333+
assert_eq!(handle.get_datalink().unwrap(), 0);
334+
335+
let r = handle.set_datalink(108);
336+
337+
assert!(r.is_err());
338+
339+
assert!(format!("{:?}", r.err().unwrap()).contains("not one of the DLTs supported"));
340+
}
341+
308342
#[test]
309343
fn bpf_compile() {
310344
let _ = env_logger::try_init();

src/stream.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ impl PacketStream {
2828
let live_capture = handle.is_live_capture();
2929

3030
if live_capture {
31-
let h = handle
31+
handle
3232
.set_snaplen(config.snaplen())?
3333
.set_promiscuous()?
34-
.set_buffer_size(config.buffer_size())?
35-
.activate()?;
34+
.set_buffer_size(config.buffer_size())?;
35+
if let Some(datalink) = config.datalink() {
36+
handle.set_datalink(*datalink)?;
37+
}
38+
handle.activate()?;
3639
if !config.blocking() {
37-
h.set_non_block()?;
40+
handle.set_non_block()?;
3841
}
3942

4043
if let Some(bpf) = config.bpf() {

0 commit comments

Comments
 (0)