feat: add pcap

This commit is contained in:
2020-11-07 11:19:46 +08:00
parent 47eb944308
commit e126c953a0
4 changed files with 180 additions and 0 deletions

109
__network/n_pcap/Cargo.lock generated Normal file
View File

@@ -0,0 +1,109 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
[[package]]
name = "libloading"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1090080fe06ec2648d0da3881d9453d97e71a45f00eb179af7fdd7e3f686fdb0"
dependencies = [
"cfg-if",
"winapi",
]
[[package]]
name = "memchr"
version = "2.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525"
[[package]]
name = "n_pcap"
version = "0.1.0"
dependencies = [
"pcap",
]
[[package]]
name = "pcap"
version = "0.7.0"
dependencies = [
"libc",
"libloading",
"regex",
]
[[package]]
name = "regex"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
dependencies = [
"lazy_static",
]
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@@ -0,0 +1,10 @@
[package]
name = "n_pcap"
version = "0.1.0"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
pcap = { path = "../pcap" }

View File

@@ -0,0 +1,60 @@
use std::env;
use pcap::*;
fn main() {
let mut args = env::args();
args.next();
match args.next() {
None => println!("No arg!"),
Some(arg1) => match arg1.as_str() {
"d" | "dump" => dump(),
"p" | "pcap" => pcap(),
_ => println!("Unknown arg: {}", arg1),
}
}
}
fn pcap() {
let device = Device::lookup().unwrap();
println!("Using device {}", device.name);
let mut cap = Capture::from_device(device)
.unwrap()
.immediate_mode(true)
.open()
.unwrap();
let mut savefile = cap.savefile("test.pcap").unwrap();
for _ in 0..10 {
let p = cap.next().unwrap();
println!("packet received on network: {:?}", p);
savefile.write(&p);
}
// let mut cap = Capture::from_file("test.pcap").unwrap();
// let p = cap.next().unwrap();
// println!("packet obtained from file: {:?}", p);
}
fn dump() {
let device = Device::lookup().unwrap();
println!("Using device {}", device.name);
let mut cap = Capture::from_device(device)
.unwrap()
.immediate_mode(true)
.open()
.unwrap();
loop {
if let Ok(c) = cap.next() {
println!("{:?}", c.header);
println!("{:?}", c.data);
}
}
// let stats = cap.stats().unwrap();
// println!(
// "Received: {}, dropped: {}, if_dropped: {}",
// stats.received, stats.dropped, stats.if_dropped
// );
}

1
__network/pcap Submodule

Submodule __network/pcap added at 4ba95f687e