-
-
Notifications
You must be signed in to change notification settings - Fork 299
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
Refactor OpenSockets to provide cross OS compatibility #180
Changes from all commits
6333b33
f09ad10
c9e748f
56d47ea
8c07e77
d6bdcae
9f0f0d9
20f7c9c
1772bdb
f7d9a65
743150f
ae5d79d
336fde4
3fda7ef
db1966a
9acb8a3
77704d2
2c7edfb
e40e88a
9688d80
785b8aa
db98c8a
5c02531
e199a79
2e17c4b
1a35f1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
fn main() { | ||
#[cfg(target_os = "windows")] | ||
download_winpcap_sdk() | ||
} | ||
|
||
#[cfg(target_os = "windows")] | ||
fn download_winpcap_sdk() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand this correctly, this happens every time we build bandwhich, right? Even if the file already exists. Not terrible, but might make development a little slow? What do you think about using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea, thanks. I thought that this flag was for C sources only, gotta check it out. |
||
use http_req::request; | ||
use std::env; | ||
use std::fs::File; | ||
use std::io::prelude::*; | ||
|
||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
let out_dir = env::var("OUT_DIR").unwrap(); | ||
|
||
let mut reader = Vec::new(); | ||
let _res = request::get( | ||
"https://nmap.org/npcap/dist/npcap-sdk-1.05.zip", | ||
&mut reader, | ||
) | ||
.unwrap(); | ||
|
||
let mut pcapzip = File::create(format!("{}{}", out_dir, "/npcap.zip")).unwrap(); | ||
pcapzip.write_all(reader.as_slice()).unwrap(); | ||
pcapzip.flush().unwrap(); | ||
|
||
pcapzip = File::open(format!("{}{}", out_dir, "/npcap.zip")).unwrap(); | ||
|
||
let lib_name = "Packet.lib"; | ||
#[cfg(target_arch = "x86_64")] | ||
let lib_dir = "Lib/x64"; | ||
#[cfg(target_arch = "x86")] | ||
let lib_dir = "Lib"; | ||
|
||
let lib_path = format!("{}/{}", lib_dir, lib_name); | ||
let mut zip_archive = zip::ZipArchive::new(pcapzip).unwrap(); | ||
let mut pcaplib = match zip_archive.by_name(lib_path.as_str()) { | ||
Ok(pcaplib) => pcaplib, | ||
Err(err) => { | ||
panic!(err); | ||
} | ||
}; | ||
|
||
let mut pcaplib_bytes = Vec::new(); | ||
pcaplib.read_to_end(&mut pcaplib_bytes).unwrap(); | ||
|
||
std::fs::create_dir_all(format!("{}/{}", out_dir, lib_dir)).unwrap(); | ||
let mut pcaplib_file = File::create(format!("{}/{}", out_dir, lib_path)).unwrap(); | ||
pcaplib_file.write_all(pcaplib_bytes.as_slice()).unwrap(); | ||
pcaplib_file.flush().unwrap(); | ||
|
||
println!("cargo:rustc-link-search=native={}/{}", out_dir, lib_dir); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,16 @@ use ::tokio::runtime::Runtime; | |
use ::std::time; | ||
|
||
use crate::os::errors::GetInterfaceErrorKind; | ||
#[cfg(not(target_os = "windows"))] | ||
use signal_hook::iterator::Signals; | ||
|
||
#[cfg(target_os = "linux")] | ||
use crate::os::linux::get_open_sockets; | ||
#[cfg(any(target_os = "macos", target_os = "freebsd"))] | ||
use crate::os::lsof::get_open_sockets; | ||
#[cfg(target_os = "windows")] | ||
use crate::os::windows::get_open_sockets; | ||
|
||
use crate::{network::dns, OsInputOutput}; | ||
|
||
pub type OnSigWinch = dyn Fn(Box<dyn Fn()>) + Send; | ||
|
@@ -63,6 +67,7 @@ fn get_interface(interface_name: &str) -> Option<NetworkInterface> { | |
.find(|iface| iface.name == interface_name) | ||
} | ||
|
||
#[cfg(not(target_os = "windows"))] | ||
fn sigwinch() -> (Box<OnSigWinch>, Box<SigCleanup>) { | ||
let signals = Signals::new(&[signal_hook::SIGWINCH]).unwrap(); | ||
let on_winch = { | ||
|
@@ -82,6 +87,15 @@ fn sigwinch() -> (Box<OnSigWinch>, Box<SigCleanup>) { | |
(Box::new(on_winch), Box::new(cleanup)) | ||
} | ||
|
||
#[cfg(any(target_os = "windows"))] | ||
fn sigwinch() -> (Box<OnSigWinch>, Box<SigCleanup>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a better way to return sigwinch stub on Windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, are we not handling window size changes on windows? Or did I miss it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Windows does not provide SIGWINCH, and its not that easy to get the console resize event. https://stackoverflow.com/questions/10856926/sigwinch-equivalent-on-windows I've just checked and yes,
that is how it behaves, however I noticed that only after you described that. |
||
let on_winch = { move |_cb: Box<dyn Fn()>| {} }; | ||
let cleanup = move || { | ||
println!("Fake signal cleanup"); | ||
}; | ||
(Box::new(on_winch), Box::new(cleanup)) | ||
} | ||
|
||
fn create_write_to_stdout() -> Box<dyn FnMut(String) + Send> { | ||
Box::new({ | ||
let mut stdout = io::stdout(); | ||
|
@@ -180,6 +194,12 @@ pub fn get_input( | |
datalink::interfaces() | ||
}; | ||
|
||
#[cfg(any(target_os = "windows"))] | ||
let network_frames = network_interfaces | ||
.iter() | ||
.filter(|iface| !iface.ips.is_empty()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a regression, but I had to do that as on Windows libpnet does not read interface flags which causes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a bug in libpnet? Are the interface flags there, or is it not a thing in windows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, its a TODO in libpnet, I guess conditional is a way to go for now.
|
||
.map(|iface| (iface, get_datalink_channel(iface))); | ||
#[cfg(not(target_os = "windows"))] | ||
let network_frames = network_interfaces | ||
.iter() | ||
.filter(|iface| iface.is_up() && !iface.ips.is_empty()) | ||
|
@@ -257,3 +277,9 @@ fn eperm_message() -> &'static str { | |
`cap_sys_ptrace,cap_dac_read_search,cap_net_raw,cap_net_admin+ep` | ||
"# | ||
} | ||
|
||
#[inline] | ||
#[cfg(any(target_os = "windows"))] | ||
fn eperm_message() -> &'static str { | ||
"Insufficient permissions to listen on network interface(s). Try running with administrator rights." | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it correct, those crates should not be downloaded on unixes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think this is correct. They were not downloaded locally on my linux.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imsnif actually that's not the case: I had to uncomment this line to make sure that
http_req
doesn't get downloaded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oi, I'm sorry about that! You are very right and I managed to miss this. :) I'm fixing this now and will push a patch. Thanks bringing this up (and catching it so quickly)!