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

rewrite ::phy::raw_socket #136

Closed
wants to merge 1 commit 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: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ license = "0BSD"
managed = { version = "0.5", default-features = false, features = ["map"] }
byteorder = { version = "1.0", default-features = false }
log = { version = "0.3", default-features = false, optional = true }
libc = { version = "0.2.18", optional = true }
libc = { version = "0.2.36", optional = true }
cfg-if = "0.1"

[dev-dependencies]
env_logger = "0.4"
Expand Down Expand Up @@ -76,4 +77,4 @@ name = "benchmark"
required-features = ["std", "phy-tap_interface", "proto-ipv4", "socket-tcp"]

[profile.release]
debug = 2
debug = 2
68 changes: 57 additions & 11 deletions examples/tcpdump.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
extern crate smoltcp;

use smoltcp::wire;
use smoltcp::phy::{self, LinkLayer, Device, RxToken, RawSocket};


use std::env;
use std::os::unix::io::AsRawFd;
use smoltcp::phy::wait as phy_wait;
use smoltcp::phy::{Device, RxToken, RawSocket};
use smoltcp::wire::{PrettyPrinter, EthernetFrame};


fn handle_ip_packet(packet: &[u8]) {
match wire::IpVersion::of_packet(&packet) {
Ok(version) => match version {
wire::IpVersion::Ipv4 => {
println!("{}", &wire::PrettyPrinter::<wire::Ipv4Packet<&[u8]>>::new("", &packet));
},
wire::IpVersion::Ipv6 => {
println!("{}", &wire::PrettyPrinter::<wire::Ipv6Packet<&[u8]>>::new("", &packet));
},
_ => { }
},
Err(_) => { }
}
}

fn handle_ethernet_frame(packet: &[u8]) {
println!("{}", &wire::PrettyPrinter::<wire::EthernetFrame<&[u8]>>::new("", &packet));
}

fn handle_packet(link_layer: &LinkLayer, packet: &[u8]) {
match link_layer {
&LinkLayer::Null => {
handle_ip_packet(&packet[4..]);
},
&LinkLayer::Eth => {
handle_ethernet_frame(&packet[..]);
},
&LinkLayer::Ip => {
handle_ip_packet(&packet[..]);
}
}
}

fn main() {
let ifname = env::args().nth(1).unwrap();
let mut socket = RawSocket::new(ifname.as_ref()).unwrap();
let mut raw_socket = RawSocket::with_ifname(ifname.as_ref()).unwrap();

let link_layer = raw_socket.link_layer();

println!("Interface: {}, Data Link Type: {:?}\n", ifname, link_layer);
let fd = raw_socket.as_raw_fd();

loop {
phy_wait(socket.as_raw_fd(), None).unwrap();
let (rx_token, _) = socket.receive().unwrap();
rx_token.consume(/*timestamp = */ 0, |buffer| {
println!("{}", PrettyPrinter::<EthernetFrame<&[u8]>>::new("", &buffer));
Ok(())
}).unwrap();
phy::wait(fd, None).unwrap();

match raw_socket.receive() {
Some((rx_token, _)) => {
rx_token.consume(/*timestamp = */ 0, |buffer| {
handle_packet(&link_layer, &buffer[..]);
Ok(())
}).unwrap();
}
None => { }
};
}
}
}
2 changes: 1 addition & 1 deletion examples/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,4 @@ pub fn millis_since(startup_time: Instant) -> u64 {
let duration_ms = (duration.as_secs() * 1000) +
(duration.subsec_nanos() / 1000000) as u64;
duration_ms
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ extern crate managed;
#[cfg(any(test, feature = "std"))]
#[macro_use]
extern crate std;
#[macro_use]
extern crate cfg_if;
#[cfg(any(feature = "phy-raw_socket", feature = "phy-tap_interface"))]
extern crate libc;
#[cfg(feature = "alloc")]
Expand Down
40 changes: 40 additions & 0 deletions src/phy/linklayer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use core::fmt;

#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
pub enum LinkLayer {
/// macOS loopback or utun, Linux tun without `IFF_NO_PI` flag
Null,
/// Ethernet Frame
Eth,
/// Raw IP Packet (IPv4 Packet / IPv6 Packet)
Ip,
}

impl fmt::Display for LinkLayer {
#[cfg(target_os = "linux")]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LinkLayer::Null => write!(f, "tun"),
LinkLayer::Eth => write!(f, "loopback/ethernet"),
LinkLayer::Ip => write!(f, "ipv4/ipv6"),
}
}

#[cfg(target_os = "macos")]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LinkLayer::Null => write!(f, "loopback/utun"),
LinkLayer::Eth => write!(f, "ethernet"),
LinkLayer::Ip => write!(f, "ipv4/ipv6"),
}
}

#[cfg(not(any(target_os = "linux", target_os = "macos")))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LinkLayer::Null => write!(f, "null"),
LinkLayer::Eth => write!(f, "ethernet"),
LinkLayer::Ip => write!(f, "ipv4/ipv6"),
}
}
}
53 changes: 36 additions & 17 deletions src/phy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,31 +85,50 @@ impl<'a> phy::TxToken for StmPhyTxToken<'a> {

use Result;

#[cfg(any(feature = "phy-raw_socket", feature = "phy-tap_interface"))]
mod sys;
mod linklayer;
pub use self::linklayer::LinkLayer;

cfg_if! {
if #[cfg(any(all(feature = "phy-raw_socket",
any(target_os = "macos", all(target_os = "linux", target_env = "gnu"))),
all(feature = "phy-tap_interface",
all(target_os = "linux", target_env = "gnu"))))] {
mod sys;
pub use self::sys::wait;
}
}

cfg_if! {
if #[cfg(all(feature = "phy-raw_socket",
any(target_os = "macos",
all(target_os = "linux", target_env = "gnu"))))] {
mod raw_socket;
pub use self::raw_socket::RawSocket;
}
}

cfg_if! {
if #[cfg(all(feature = "phy-tap_interface",
all(target_os = "linux", target_env = "gnu")))] {
mod tap_interface;
pub use self::tap_interface::TapInterface;
}
}

cfg_if! {
if #[cfg(any(feature = "std", feature = "alloc"))] {
mod loopback;
pub use self::loopback::Loopback;
}
}

mod tracer;
mod fault_injector;
mod pcap_writer;
#[cfg(any(feature = "std", feature = "alloc"))]
mod loopback;
#[cfg(all(feature = "phy-raw_socket", target_os = "linux"))]
mod raw_socket;
#[cfg(all(feature = "phy-tap_interface", target_os = "linux"))]
mod tap_interface;

#[cfg(any(feature = "phy-raw_socket", feature = "phy-tap_interface"))]
pub use self::sys::wait;

pub use self::tracer::Tracer;
pub use self::fault_injector::FaultInjector;
pub use self::pcap_writer::{PcapLinkType, PcapMode, PcapSink, PcapWriter};
#[cfg(any(feature = "std", feature = "alloc"))]
pub use self::loopback::Loopback;
#[cfg(all(feature = "phy-raw_socket", target_os = "linux"))]
pub use self::raw_socket::RawSocket;
#[cfg(all(feature = "phy-tap_interface", target_os = "linux"))]
pub use self::tap_interface::TapInterface;

/// A tracer device for Ethernet frames.
pub type EthernetTracer<T> = Tracer<T, super::wire::EthernetFrame<&'static [u8]>>;
Expand Down
Loading