feat: add pcap
This commit is contained in:
15
__network/pcap/examples/easylisten.rs
Normal file
15
__network/pcap/examples/easylisten.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
fn main() {
|
||||
// get the default Device
|
||||
let device = pcap::Device::lookup().unwrap();
|
||||
println!("Using device {}", device.name);
|
||||
|
||||
// Setup Capture
|
||||
let mut cap = pcap::Capture::from_device(device)
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// get a packet and print its bytes
|
||||
println!("{:?}", cap.next());
|
||||
}
|
||||
18
__network/pcap/examples/getdevices.rs
Normal file
18
__network/pcap/examples/getdevices.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
fn main() {
|
||||
// list all of the devices pcap tells us are available
|
||||
for device in pcap::Device::list().unwrap() {
|
||||
println!("Found device! {:?}", device);
|
||||
|
||||
// now you can create a Capture with this Device if you want.
|
||||
let mut cap = pcap::Capture::from_device(device)
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// get a packet from this capture
|
||||
let packet = cap.next();
|
||||
|
||||
println!("got a packet! {:?}", packet);
|
||||
}
|
||||
}
|
||||
22
__network/pcap/examples/getstatistics.rs
Normal file
22
__network/pcap/examples/getstatistics.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
fn main() {
|
||||
// get the default Device
|
||||
let device = pcap::Device::lookup().unwrap();
|
||||
println!("Using device {}", device.name);
|
||||
|
||||
// Setup Capture
|
||||
let mut cap = pcap::Capture::from_device(device)
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// get 10 packets
|
||||
for _ in 0..10 {
|
||||
cap.next().ok();
|
||||
}
|
||||
let stats = cap.stats().unwrap();
|
||||
println!(
|
||||
"Received: {}, dropped: {}, if_dropped: {}",
|
||||
stats.received, stats.dropped, stats.if_dropped
|
||||
);
|
||||
}
|
||||
16
__network/pcap/examples/listenlocalhost.rs
Normal file
16
__network/pcap/examples/listenlocalhost.rs
Normal file
@@ -0,0 +1,16 @@
|
||||
fn main() {
|
||||
// listen on the device named "any", which is only available on Linux. This is only for
|
||||
// demonstration purposes.
|
||||
let mut cap = pcap::Capture::from_device("any")
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// filter out all packets that don't have 127.0.0.1 as a source or destination.
|
||||
cap.filter("host 127.0.0.1").unwrap();
|
||||
|
||||
while let Ok(packet) = cap.next() {
|
||||
println!("got packet! {:?}", packet);
|
||||
}
|
||||
}
|
||||
37
__network/pcap/examples/savefile.rs
Normal file
37
__network/pcap/examples/savefile.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use pcap::*;
|
||||
|
||||
fn main() {
|
||||
{
|
||||
// open capture from default device
|
||||
let device = Device::lookup().unwrap();
|
||||
println!("Using device {}", device.name);
|
||||
|
||||
// Setup Capture
|
||||
let mut cap = Capture::from_device(device)
|
||||
.unwrap()
|
||||
.immediate_mode(true)
|
||||
.open()
|
||||
.unwrap();
|
||||
|
||||
// open savefile using the capture
|
||||
let mut savefile = cap.savefile("test.pcap").unwrap();
|
||||
|
||||
// get a packet from the interface
|
||||
let p = cap.next().unwrap();
|
||||
|
||||
// print the packet out
|
||||
println!("packet received on network: {:?}", p);
|
||||
|
||||
// write the packet to the savefile
|
||||
savefile.write(&p);
|
||||
}
|
||||
|
||||
// open a new capture from the test.pcap file we wrote to above
|
||||
let mut cap = Capture::from_file("test.pcap").unwrap();
|
||||
|
||||
// get a packet
|
||||
let p = cap.next().unwrap();
|
||||
|
||||
// print that packet out -- it should be the same as the one we printed above
|
||||
println!("packet obtained from file: {:?}", p);
|
||||
}
|
||||
47
__network/pcap/examples/streamlisten.rs
Normal file
47
__network/pcap/examples/streamlisten.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
use futures::StreamExt;
|
||||
use pcap::stream::{PacketCodec, PacketStream};
|
||||
use pcap::{Active, Capture, Device, Error, Packet};
|
||||
|
||||
pub struct SimpleDumpCodec;
|
||||
|
||||
impl PacketCodec for SimpleDumpCodec {
|
||||
type Type = String;
|
||||
|
||||
fn decode<'p>(&mut self, packet: Packet<'p>) -> Result<Self::Type, Error> {
|
||||
Ok(format!("{:?}", packet))
|
||||
}
|
||||
}
|
||||
|
||||
fn new_stream() -> Result<PacketStream<Active, SimpleDumpCodec>, Error> {
|
||||
// get the default Device
|
||||
let device = Device::lookup()?;
|
||||
println!("Using device {}", device.name);
|
||||
|
||||
let cap = Capture::from_device(device)?
|
||||
.immediate_mode(true)
|
||||
.open()?
|
||||
.setnonblock()?;
|
||||
cap.stream(SimpleDumpCodec {})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rt = tokio::runtime::Builder::new()
|
||||
.enable_io()
|
||||
.basic_scheduler()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let stream = rt.enter(|| match new_stream() {
|
||||
Ok(stream) => stream,
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
let fut = stream.for_each(move |s| {
|
||||
println!("{:?}", s);
|
||||
futures::future::ready(())
|
||||
});
|
||||
rt.block_on(fut);
|
||||
}
|
||||
Reference in New Issue
Block a user