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

Adding --add-payload-offset flag (macos only) [and 1.48 updates] #192

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/display/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ where
let mut terminal = Terminal::new(terminal_backend).unwrap();
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
let state = UIState {
cumulative_mode: opts.total_utilization,
..Default::default()
};
let state = UIState::with_cumulative_mode(opts.total_utilization);
Ui {
terminal,
state,
Expand Down
7 changes: 7 additions & 0 deletions src/display/ui_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ pub struct UIState {
}

impl UIState {
pub fn with_cumulative_mode(cumulative_mode: bool) -> Self {
UIState {
cumulative_mode,
..Default::default()
}
}

fn get_proc_name<'a>(
connections_to_procs: &'a HashMap<LocalSocket, String>,
local_socket: &LocalSocket,
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ pub struct Opt {
#[structopt(short, long)]
/// A dns server ip to use instead of the system default
dns_server: Option<Ipv4Addr>,
#[cfg_attr(not(target_os = "macos"), structopt(skip = false))]
#[cfg_attr(target_os = "macos", structopt(short = "o", long))]
/// Add a 14-byte payload offset needed for some utun (Point-to-Point) interfaces
add_payload_offset: bool,
}

#[derive(StructOpt, Debug, Copy, Clone)]
Expand Down Expand Up @@ -289,12 +293,13 @@ where
let name = format!("sniffing_handler_{}", iface.name);
let running = running.clone();
let show_dns = opts.show_dns;
let with_payload_offset = opts.add_payload_offset;
let network_utilization = network_utilization.clone();

thread::Builder::new()
.name(name)
.spawn(move || {
let mut sniffer = Sniffer::new(iface, frames, show_dns);
let mut sniffer = Sniffer::new(iface, frames, show_dns, with_payload_offset);

while running.load(Ordering::Acquire) {
if let Some(segment) = sniffer.next() {
Expand Down
32 changes: 19 additions & 13 deletions src/network/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,34 @@ pub struct Sniffer {
network_interface: NetworkInterface,
network_frames: Box<dyn DataLinkReceiver>,
dns_shown: bool,
payload_offset: usize,
}

impl Sniffer {
pub fn new(
network_interface: NetworkInterface,
network_frames: Box<dyn DataLinkReceiver>,
dns_shown: bool,
with_payload_offset: bool,
) -> Self {
let payload_offset = {
// See https://github.com/libpnet/libpnet/blob/master/examples/packetdump.rs
// The pnet code for BPF loopback adds a zero'd out Ethernet header
if cfg!(target_os = "macos")
&& (network_interface.is_loopback()
// utun interfaces shouldn't need the offset, but user can add with a flag
|| (with_payload_offset && network_interface.is_point_to_point()))
{
14
} else {
0
}
};
Sniffer {
network_interface,
network_frames,
dns_shown,
payload_offset,
}
}
pub fn next(&mut self) -> Option<Segment> {
Expand All @@ -116,24 +132,14 @@ impl Sniffer {
}
},
};
// See https://github.com/libpnet/libpnet/blob/master/examples/packetdump.rs
// VPN interfaces (such as utun0, utun1, etc) have POINT_TO_POINT bit set to 1
let payload_offset = if (self.network_interface.is_loopback()
|| self.network_interface.is_point_to_point())
&& cfg!(target_os = "macos")
{
// The pnet code for BPF loopback adds a zero'd out Ethernet header
14
} else {
0
};
let ip_packet = Ipv4Packet::new(&bytes[payload_offset..])?;

let ip_packet = Ipv4Packet::new(&bytes[self.payload_offset..])?;
let version = ip_packet.get_version();

match version {
4 => Self::handle_v4(ip_packet, &self.network_interface, self.dns_shown),
6 => Self::handle_v6(
Ipv6Packet::new(&bytes[payload_offset..])?,
Ipv6Packet::new(&bytes[self.payload_offset..])?,
&self.network_interface,
),
_ => {
Expand Down
1 change: 1 addition & 0 deletions src/tests/cases/raw_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ fn no_resolve_mode() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};
start(backend, os_input, opts);
let stdout = Arc::try_unwrap(stdout).unwrap().into_inner().unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/tests/cases/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ fn opts_factory(raw: bool) -> Opt {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
}
}
type BackendWithStreams = (
Expand Down
9 changes: 9 additions & 0 deletions src/tests/cases/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ fn basic_only_processes() {
processes: true,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down Expand Up @@ -208,6 +209,7 @@ fn basic_processes_with_dns_queries() {
processes: true,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down Expand Up @@ -235,6 +237,7 @@ fn basic_only_connections() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down Expand Up @@ -262,6 +265,7 @@ fn basic_only_addresses() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand All @@ -287,6 +291,7 @@ fn two_packets_only_processes() {
processes: true,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand All @@ -313,6 +318,7 @@ fn two_packets_only_connections() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand All @@ -339,6 +345,7 @@ fn two_packets_only_addresses() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down Expand Up @@ -367,6 +374,7 @@ fn two_windows_split_horizontally() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down Expand Up @@ -394,6 +402,7 @@ fn two_windows_split_vertically() {
processes: false,
total_utilization: false,
},
add_payload_offset: false,
};

start(backend, os_input, opts);
Expand Down