Closed as not planned
Description
Code
// from Network Programming with Rust by Abhishek Chanda
// chapter3/pnet-example/src/main.rs
extern crate pnet;
use pnet::datalink::{self, NetworkInterface};
use pnet::datalink::Channel::Ethernet;
use pnet::packet::ethernet::{EtherTypes, EthernetPacket};
use pnet::packet::ipv4::Ipv4Packet;
use pnet::packet::tcp::TcpPacket;
use pnet::packet::ip::IpNextHeaderProtocols;
use pnet::packet::Packet;
use std::env;
// Handles a single ethernet packet
fn handle_packet(ethernet: &EthernetPacket) {
match ethernet.get_ethertype() {
EtherTypes::Ipv4 => {
let header = Ipv4Packet::new(ethernet.payload());
if let Some(header) = header {
match header.get_next_level_protocol() {
IpNextHeaderProtocols::Tcp => {
let tcp = TcpPacket::new(header.payload());
if let Some(tcp) = tcp {
println!(
"Got a TCP packet {}:{} to {}:{}",
header.get_source(),
tcp.get_source(),
header.get_destination(),
tcp.get_destination()
);
}
}
_ => println!("Ignoring non TCP packet"),
}
}
}
_ => println!("Ignoring non IPv4 packet"),
}
}
fn main() {
let interface_name = env::args().nth(1).unwrap();
// Get all interfaces
let interfaces = datalink::interfaces();
// Filter the list to find the given interface name
let interface = interfaces
.into_iter()
.filter(|iface: &NetworkInterface| iface.name == interface_name)
.next()
.expect("Error getting interface");
let (_tx, mut rx) = match datalink::channel(&interface, Default::default()) {
Ok(Ethernet(tx, rx)) => (tx, rx),
Ok(_) => panic!("Unhandled channel type"),
Err(e) => {
panic!(
"An error occurred when creating the datalink channel:
{}",e
)
}
};
// Loop over packets arriving on the given interface
loop {
match rx.next() {
Ok(packet) => {
let packet = EthernetPacket::new(packet).unwrap();
handle_packet(&packet);
}
Err(e) => {
panic!("An error occurred while reading: {}", e);
}
}
}
}
Meta
rustc --version --verbose
:
rustc 1.49.0 (e1884a8e3 2020-12-29)
binary: rustc
commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
commit-date: 2020-12-29
host: x86_64-pc-windows-msvc
release: 1.49.0
Error output
error: incremental compilation: could not create session directory lock file: Incorrect function. (os error 1)
thread 'rustc' panicked at 'trying to get session directory from `IncrCompSession`: NotInitialized', compiler\rustc_session\src\session.rs:923:48
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: internal compiler error: unexpected panic
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md
note: rustc 1.49.0 (e1884a8e3 2020-12-29) running on x86_64-pc-windows-msvc
note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental --crate-type bin
note: some of the compiler flags provided by cargo are hidden
query stack during panic:
end of query stack
Backtrace
stack backtrace:
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.