Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set datalink type #24

Open
rossmacarthur opened this issue Sep 14, 2020 · 3 comments
Open

Ability to set datalink type #24

rossmacarthur opened this issue Sep 14, 2020 · 3 comments

Comments

@rossmacarthur
Copy link

No description provided.

dbcfd added a commit that referenced this issue Sep 30, 2020
Add support for setting and getting linktype. Closes #24
@dbcfd dbcfd mentioned this issue Sep 30, 2020
@dbcfd
Copy link
Collaborator

dbcfd commented Sep 30, 2020

@rossmacarthur Are you able to check the branch in #27 to see if it does what you need? I don't have a great way to test this functionality.

@rossmacarthur
Copy link
Author

rossmacarthur commented Oct 16, 2020

@dbcfd Thanks! I tested this out and for some reason when I run this it doesn't work. I modified it to set the datalink after opening the capture and that works.

diff --git a/src/stream.rs b/src/stream.rs
index 18d7a40..7545333 100644
--- a/src/stream.rs
+++ b/src/stream.rs
@@ -32,10 +32,10 @@ impl PacketStream {
                 .set_snaplen(config.snaplen())?
                 .set_promiscuous()?
                 .set_buffer_size(config.buffer_size())?;
+            handle.activate()?;
             if let Some(datalink) = config.datalink() {
                 handle.set_datalink(*datalink)?;
             }
-            handle.activate()?;
             if !config.blocking() {
                 handle.set_non_block()?;
             }

Additionally, I also need to set rfmon to make it work but I think this is macOS specific thing that is required.

diff --git a/src/handle.rs b/src/handle.rs
index de45f2f..e426ae6 100644
--- a/src/handle.rs
+++ b/src/handle.rs
@@ -133,6 +133,14 @@ impl Handle {
         }
     }
 
+    pub fn set_rfmon(&self) -> Result<&Self, Error> {
+        if 0 != unsafe { pcap_sys::pcap_set_rfmon(self.handle, 1) } {
+            Err(pcap_util::convert_libpcap_error(self.handle))
+        } else {
+            Ok(self)
+        }
+    }
+
     pub fn set_snaplen(&self, snaplen: u32) -> Result<&Self, Error> {
         if 0 != unsafe { pcap_sys::pcap_set_snaplen(self.handle, snaplen as _) } {
             Err(pcap_util::convert_libpcap_error(self.handle))
diff --git a/src/stream.rs b/src/stream.rs
index 18d7a40..df9555c 100644
--- a/src/stream.rs
+++ b/src/stream.rs
@@ -30,6 +30,7 @@ impl PacketStream {
         if live_capture {
             handle
                 .set_snaplen(config.snaplen())?
+                .set_rfmon()?
                 .set_promiscuous()?
                 .set_buffer_size(config.buffer_size())?;
             if let Some(datalink) = config.datalink() {

It would be nice if these were more tweakable when creating the packet stream. I am imagining more of an API like the following, and not have the PacketStream new() function set these values.

Handle::live_capture("en0")?
    .set_rfmon()?
    .set_promiscuous()?
	.activate()?
    .set_data_link(127)?
    .into_packet_stream();  // consumes `Handle` and returns a `PacketStream`.

@dbcfd
Copy link
Collaborator

dbcfd commented Dec 10, 2020

@rossmacarthur Adjusted the api for creating handles and streams in #28

Still need to add rfmon support, but let me know if that is a little more usable for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants