From e0b05c8585166334212284b37faf3643c61e915d Mon Sep 17 00:00:00 2001 From: Wiktor Mazur Date: Wed, 31 Jul 2019 20:34:13 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 + Cargo.lock | 330 ++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ lib/packet-sniffer/Cargo.toml | 10 ++ lib/packet-sniffer/src/lib.rs | 146 +++++++++++++++ src/main.rs | 16 ++ 6 files changed, 514 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 lib/packet-sniffer/Cargo.toml create mode 100644 lib/packet-sniffer/src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..236b9cf --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,330 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "aho-corasick" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "damage-meter" +version = "0.1.0" +dependencies = [ + "packet_sniffer 0.1.0", +] + +[[package]] +name = "glob" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ipnetwork" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "lazy_static" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "libc" +version = "0.2.60" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "log" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "log" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "memchr" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "packet_sniffer" +version = "0.1.0" +dependencies = [ + "pnet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ipnetwork 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_datalink 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_packet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_transport 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_base" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pnet_datalink" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ipnetwork 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_macros" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.42.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_macros_support" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_packet" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_macros 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_macros_support 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex 0.42.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_sys" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pnet_transport" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_packet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pnet_sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde" +version = "1.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syntex" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syntex_errors" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syntex_pos" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syntex_syntax" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "syntex_pos 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)", + "term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "term" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "ucd-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "utf8-ranges" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" +"checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23" +"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" +"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" +"checksum ipnetwork 0.14.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b3d862c86f7867f19b693ec86765e0252d82e53d4240b9b629815675a0714ad1" +"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" +"checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" +"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +"checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" +"checksum pnet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "63d693c84430248366146e3181ff9d330243464fa9e6146c372b2f3eb2e2d8e7" +"checksum pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4df28acf2fcc77436dd2b91a9a0c2bb617f9ca5f2acefee1a4135058b9f9801f" +"checksum pnet_datalink 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b34f8ca857599d05b6b082e9baff8d27c54cb9c26568cf3c0993a5755816966" +"checksum pnet_macros 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2f16d1fa7fd0edebc36055587b8b5af8a109bbc29a55fb484a37e2029b971a7" +"checksum pnet_macros_support 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "84684f2cddefc37a06f2fe2ca4dcc3457fc3b282734b5246507d8ee75d2780ae" +"checksum pnet_packet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08a6cdcdaddc5174f18286298842a4e31cd3cc018933d42af51434b1fa07dcbe" +"checksum pnet_sys 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "682b2eca84cc440bce8336813f78eb6d3cb0fed89fe0e87ae22acfca8363f176" +"checksum pnet_transport 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5faa55dcf725487a699adcff88dfea8f17ea34fa2640528866d9acbb4e3a104f" +"checksum regex 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467" +"checksum regex-syntax 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5485bf1523a9ed51c4964273f22f63f24e31632adb5dad134f488f86a3875c" +"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" +"checksum serde 1.0.98 (registry+https://github.com/rust-lang/crates.io-index)" = "7fe5626ac617da2f2d9c48af5515a21d5a480dbd151e01bb1c355e26a3e68113" +"checksum syntex 0.42.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0a30b08a6b383a22e5f6edc127d169670d48f905bb00ca79a00ea3e442ebe317" +"checksum syntex_errors 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "04c48f32867b6114449155b2a82114b86d4b09e1bddb21c47ff104ab9172b646" +"checksum syntex_pos 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3fd49988e52451813c61fecbe9abb5cfd4e1b7bb6cdbb980a6fbcbab859171a6" +"checksum syntex_syntax 0.42.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7628a0506e8f9666fdabb5f265d0059b059edac9a3f810bda077abb5d826bd8d" +"checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum ucd-util 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa9b3b49edd3468c0e6565d85783f51af95212b6fa3986a5500954f00b460874" +"checksum unicode-xid 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb" +"checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde" +"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" +"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..107f927 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "damage-meter" +version = "0.1.0" +authors = ["Wiktor Mazur "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +packet_sniffer = { path = "./lib/packet-sniffer" } diff --git a/lib/packet-sniffer/Cargo.toml b/lib/packet-sniffer/Cargo.toml new file mode 100644 index 0000000..b832f5d --- /dev/null +++ b/lib/packet-sniffer/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "packet_sniffer" +version = "0.1.0" +authors = ["Wiktor Mazur "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pnet = "0.22.0" \ No newline at end of file diff --git a/lib/packet-sniffer/src/lib.rs b/lib/packet-sniffer/src/lib.rs new file mode 100644 index 0000000..dfd11b5 --- /dev/null +++ b/lib/packet-sniffer/src/lib.rs @@ -0,0 +1,146 @@ +extern crate pnet; + +use std::net::IpAddr; +use std::sync::mpsc::Sender; +use std::thread; + +use pnet::datalink::{self, NetworkInterface}; +use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket}; +use pnet::packet::ip::{IpNextHeaderProtocol, IpNextHeaderProtocols}; +use pnet::packet::ipv4::Ipv4Packet; +use pnet::packet::ipv6::Ipv6Packet; +use pnet::packet::udp; +use pnet::packet::Packet; +use pnet::util::MacAddr; + +#[derive(Clone, Debug)] +pub struct UdpPacket { + pub interface_name: String, + pub source_address: IpAddr, + pub source_port: u16, + pub destination_address: IpAddr, + pub destination_port: u16, + pub length: u16, +} + +pub fn receive(tx: Sender) { + use pnet::datalink::Channel::Ethernet; + let interfaces = datalink::interfaces(); + let interface = interfaces + .into_iter() + .filter(|i| !i.is_loopback()) + .next() + .unwrap(); + + // Create a channel to receive on + let (_, mut rx) = match datalink::channel(&interface, Default::default()) { + Ok(Ethernet(tx, rx)) => (tx, rx), + Ok(_) => panic!("packetdump: unhandled channel type: {}"), + Err(e) => panic!("packetdump: unable to create channel: {}", e), + }; + + let child = thread::spawn(move || { + loop { + let mut buf: [u8; 1600] = [0u8; 1600]; + let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..]).unwrap(); + match rx.next() { + Ok(packet) => { + if cfg!(target_os = "macos") + && interface.is_up() + && !interface.is_broadcast() + && !interface.is_loopback() + && interface.is_point_to_point() + { + // Maybe is TUN interface + let version = Ipv4Packet::new(&packet).unwrap().get_version(); + if version == 4 { + fake_ethernet_frame.set_destination(MacAddr(0, 0, 0, 0, 0, 0)); + fake_ethernet_frame.set_source(MacAddr(0, 0, 0, 0, 0, 0)); + fake_ethernet_frame.set_ethertype(EtherTypes::Ipv4); + fake_ethernet_frame.set_payload(&packet); + handle_ethernet_frame(&interface, &fake_ethernet_frame.to_immutable(), &tx); + continue; + } else if version == 6 { + fake_ethernet_frame.set_destination(MacAddr(0, 0, 0, 0, 0, 0)); + fake_ethernet_frame.set_source(MacAddr(0, 0, 0, 0, 0, 0)); + fake_ethernet_frame.set_ethertype(EtherTypes::Ipv6); + fake_ethernet_frame.set_payload(&packet); + handle_ethernet_frame(&interface, &fake_ethernet_frame.to_immutable(), &tx); + continue; + } + } + handle_ethernet_frame(&interface, &EthernetPacket::new(packet).unwrap(), &tx); + } + Err(e) => panic!("packetdump: unable to receive packet: {}", e), + } + } + }); +} + +fn handle_ethernet_frame(interface: &NetworkInterface, ethernet: &EthernetPacket, tx: &Sender) { + let interface_name = &interface.name[..]; + match ethernet.get_ethertype() { + EtherTypes::Ipv4 => handle_ipv4_packet(interface_name, ethernet, &tx), + EtherTypes::Ipv6 => handle_ipv6_packet(interface_name, ethernet, &tx), + _ => {} + } +} + +fn handle_ipv4_packet(interface_name: &str, ethernet: &EthernetPacket, tx: &Sender) { + let header = Ipv4Packet::new(ethernet.payload()); + if let Some(header) = header { + handle_transport_protocol( + interface_name, + IpAddr::V4(header.get_source()), + IpAddr::V4(header.get_destination()), + header.get_next_level_protocol(), + header.payload(), + &tx + ); + } +} + +fn handle_ipv6_packet(interface_name: &str, ethernet: &EthernetPacket, tx: &Sender) { + let header = Ipv6Packet::new(ethernet.payload()); + if let Some(header) = header { + handle_transport_protocol( + interface_name, + IpAddr::V6(header.get_source()), + IpAddr::V6(header.get_destination()), + header.get_next_header(), + header.payload(), + &tx + ); + } +} + +fn handle_transport_protocol( + interface_name: &str, + source: IpAddr, + destination: IpAddr, + protocol: IpNextHeaderProtocol, + packet: &[u8], + tx: &Sender +) { + match protocol { + IpNextHeaderProtocols::Udp => { + handle_udp_packet(interface_name, source, destination, packet, &tx) + } + _ => {} + } +} + +fn handle_udp_packet(interface_name: &str, source: IpAddr, destination: IpAddr, packet: &[u8], tx: &Sender) { + let udp = udp::UdpPacket::new(packet); + + if let Some(udp) = udp { + tx.send(UdpPacket{ + interface_name: String::from(interface_name), + source_address: source, + source_port: udp.get_source(), + destination_address: destination, + destination_port: udp.get_destination(), + length: udp.get_length() + }).unwrap(); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9223aab --- /dev/null +++ b/src/main.rs @@ -0,0 +1,16 @@ +extern crate packet_sniffer; + +use std::sync::mpsc::{Sender, Receiver}; +use std::sync::mpsc; + +use packet_sniffer::UdpPacket; + +fn main() { + let (tx, rx): (Sender, Receiver) = mpsc::channel(); + + packet_sniffer::receive(tx); + + loop { + println!("{:?}", rx.recv()); + } +}