mirror of
https://github.com/GyulyVGC/sniffnet.git
synced 2026-10-25 08:31:35 +00:00
mappa, display e output su file **INCOMPLETO, MANCA UN TRATTO DISPLAY, NON TESTATO**
This commit is contained in:
parent
b91a5eb7c8
commit
f2b69d4b68
5 changed files with 69 additions and 28 deletions
|
|
@ -14,6 +14,7 @@ impl AddressPort {
|
|||
port1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_ip(&self) -> String { address1 }
|
||||
pub fn get_port(&self) -> u16 { port1 }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,15 +3,18 @@ mod report_info;
|
|||
|
||||
use std::borrow::BorrowMut;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::ops::Add;
|
||||
use etherparse::{IpHeader, PacketHeaders, TransportHeader};
|
||||
use pcap::{Device, Capture};
|
||||
use std::fs::File;
|
||||
use crate::address_port::{AddressPort};
|
||||
use crate::report_info::ReportInfo;
|
||||
use crate::report_info::{ReportInfo, TransProtocol};
|
||||
|
||||
fn main() {
|
||||
|
||||
let device = Device::lookup().unwrap();
|
||||
let output = File::create("report.txt")?;
|
||||
println!("{:?}", device);
|
||||
|
||||
println!("Waiting for packets........");
|
||||
|
|
@ -31,7 +34,8 @@ fn main() {
|
|||
let mut address2 = String::new();
|
||||
let mut port1= 0;
|
||||
let mut port2= 0;
|
||||
let mut transmitted_bytes;
|
||||
let mut transmitted_bytes: u32;
|
||||
let mut protocol = TransProtocol::Other;
|
||||
|
||||
match value.ip.unwrap() {
|
||||
IpHeader::Version4(ipv4header, _) => {
|
||||
|
|
@ -45,30 +49,32 @@ fn main() {
|
|||
.replace("]","")
|
||||
.replace(",",".")
|
||||
.replace(" ","");
|
||||
transmitted_bytes = ipv4header.payload_len;
|
||||
transmitted_bytes = ipv4header.payload_len as u32;
|
||||
}
|
||||
IpHeader::Version6(ipv6header, _) => {
|
||||
address1 = format!("{:?}", ipv6header.source)
|
||||
.replace("[","")
|
||||
.replace("]","")
|
||||
.replace(",",".")
|
||||
.replace(" ","");
|
||||
.replace("[", "")
|
||||
.replace("]", "")
|
||||
.replace(",", ".")
|
||||
.replace(" ", "");
|
||||
address2 = format!("{:?}", ipv6header.destination)
|
||||
.replace("[","")
|
||||
.replace("]","")
|
||||
.replace(",",".")
|
||||
.replace(" ","");
|
||||
transmitted_bytes = ipv6header.payload_length;
|
||||
.replace("[", "")
|
||||
.replace("]", "")
|
||||
.replace(",", ".")
|
||||
.replace(" ", "");
|
||||
transmitted_bytes = ipv6header.payload_length as u32;
|
||||
}
|
||||
}
|
||||
|
||||
match value.transport.unwrap() {
|
||||
TransportHeader::Udp(udpheader) => {
|
||||
port1 = udpheader.source_port;
|
||||
protocol = TransProtocol::UDP;
|
||||
port2 = udpheader.destination_port
|
||||
}
|
||||
TransportHeader::Tcp(tcpheader) => {
|
||||
port1 = tcpheader.source_port;
|
||||
protocol = TransProtocol::TCP;
|
||||
port2 = tcpheader.destination_port
|
||||
}
|
||||
TransportHeader::Icmpv4(_) => {}
|
||||
|
|
@ -76,16 +82,24 @@ fn main() {
|
|||
}
|
||||
|
||||
println!("----------------------------------");
|
||||
println!("addresses: {:?} {:?}", address1, address2);
|
||||
println!("ports: {:?} {:?}", port1, port2);
|
||||
println!("ip payload length: {:?}", transmitted_bytes);
|
||||
//println!("addresses: {:?} {:?}", address1, address2);
|
||||
//println!("ports: {:?} {:?}", port1, port2);
|
||||
//println!("ip payload length: {:?}", transmitted_bytes);
|
||||
|
||||
let key1: AddressPort = AddressPort::new(address1,port1);
|
||||
let key2: AddressPort = AddressPort::new(address2,port2);
|
||||
//let key2: AddressPort = AddressPort::new(address2,port2);
|
||||
|
||||
map.insert(key1,ReportInfo::new());
|
||||
map.insert(key2,ReportInfo::new());
|
||||
println!("map: {:?}",map);
|
||||
map.entry(key1).and_modify(|info| { info.transmitted_bytes += transmitted_bytes; // TODO: Timestamp
|
||||
info.trans_protocols.insert(protocol); }
|
||||
).or_insert(ReportInfo {transmitted_bytes, initial_timestamp: "".to_string(), final_timestamp: "".to_string(), trans_protocols: HashSet::from([protocol])});
|
||||
map.entry(key2).and_modify(|info| { info.trans_protocols.insert(protocol);
|
||||
// TODO: Timestamp
|
||||
}
|
||||
).or_insert(ReportInfo {transmitted_bytes: 0, initial_timestamp: "".to_string(), final_timestamp: "".to_string(), trans_protocols: HashSet::from([protocol])});
|
||||
|
||||
for (key, val) in map.iter() {
|
||||
write!(output, "Address: {}:{}\n{}", key.get_ip(), key.get_port(), val).expect("File output error");
|
||||
}
|
||||
println!("----------------------------------");
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
#[derive(Debug)]
|
||||
use std::collections::HashSet;
|
||||
use std::fmt;
|
||||
|
||||
pub struct ReportInfo {
|
||||
transmitted_bytes: u16,
|
||||
initial_timestamp: String,
|
||||
final_timestamp: String,
|
||||
pub transmitted_bytes: u32,
|
||||
// pub received_bytes: u32 ???
|
||||
pub initial_timestamp: String,
|
||||
pub final_timestamp: String,
|
||||
pub trans_protocols: HashSet<TransProtocol>,
|
||||
}
|
||||
|
||||
impl ReportInfo {
|
||||
|
|
@ -11,8 +15,30 @@ impl ReportInfo {
|
|||
ReportInfo {
|
||||
transmitted_bytes: 0,
|
||||
initial_timestamp: "".to_string(),
|
||||
final_timestamp: "".to_string()
|
||||
final_timestamp: "".to_string(),
|
||||
trans_protocols: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ReportInfo {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let mut multiple = "".to_string();
|
||||
let mut n = self.transmitted_bytes as f32;
|
||||
|
||||
match transmitted_bytes {
|
||||
0 .. 1000 => {},
|
||||
1000 .. 1000000 => {n /= 1000; multiple.push('k'); },
|
||||
1000000 .. 1000000000 => {n /= 1000000; multiple.push('M');},
|
||||
_ => {n /= 1000000000; multiple.push('G'); }
|
||||
}
|
||||
write!(f, "Transmitted: {:.2} {}B\n\
|
||||
Initial Timestamp: {}\n\
|
||||
Final Timestamp: {}\n\
|
||||
trans_protocols: {}\n", n, multiple, self.initial_timestamp, self.final_timestamp, self.trans_protocols)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Display, Copy)]
|
||||
pub enum TransProtocol { Other, TCP, UDP, }
|
||||
|
|
@ -1 +1 @@
|
|||
{"rustc_fingerprint":2971462514782156308,"outputs":{"931469667778813386":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n/Users/giulianobellini/.rustup/toolchains/stable-x86_64-apple-darwin\ndebug_assertions\nproc_macro\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"\"\ntarget_family=\"unix\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_feature=\"sse3\"\ntarget_feature=\"ssse3\"\ntarget_os=\"macos\"\ntarget_pointer_width=\"64\"\ntarget_vendor=\"apple\"\nunix\n","stderr":""},"17598535894874457435":{"success":true,"status":"","code":0,"stdout":"rustc 1.59.0 (9d1b2106e 2022-02-23)\nbinary: rustc\ncommit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a\ncommit-date: 2022-02-23\nhost: x86_64-apple-darwin\nrelease: 1.59.0\nLLVM version: 13.0.0\n","stderr":""},"5309432699494263626":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""},"2797684049618456168":{"success":true,"status":"","code":0,"stdout":"___\nlib___.rlib\nlib___.dylib\nlib___.dylib\nlib___.a\nlib___.dylib\n","stderr":""}},"successes":{}}
|
||||
{"rustc_fingerprint":16955251817357809681,"outputs":{"15697416045686424142":{"success":false,"status":"exit code: 1","code":1,"stdout":"","stderr":"error: `-Csplit-debuginfo` is unstable on this platform\n\n"},"4614504638168534921":{"success":true,"status":"","code":0,"stdout":"rustc 1.62.1 (e092d0b6b 2022-07-16)\nbinary: rustc\ncommit-hash: e092d0b6b43f2de967af0887873151bb1c0b18d3\ncommit-date: 2022-07-16\nhost: x86_64-pc-windows-msvc\nrelease: 1.62.1\nLLVM version: 14.0.5\n","stderr":""},"10376369925670944939":{"success":true,"status":"","code":0,"stdout":"___.exe\nlib___.rlib\n___.dll\n___.dll\n___.lib\n___.dll\nC:\\Users\\canep\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\ndebug_assertions\npanic=\"unwind\"\nproc_macro\ntarget_abi=\"\"\ntarget_arch=\"x86_64\"\ntarget_endian=\"little\"\ntarget_env=\"msvc\"\ntarget_family=\"windows\"\ntarget_feature=\"fxsr\"\ntarget_feature=\"llvm14-builtins-abi\"\ntarget_feature=\"sse\"\ntarget_feature=\"sse2\"\ntarget_has_atomic=\"16\"\ntarget_has_atomic=\"32\"\ntarget_has_atomic=\"64\"\ntarget_has_atomic=\"8\"\ntarget_has_atomic=\"ptr\"\ntarget_has_atomic_equal_alignment=\"16\"\ntarget_has_atomic_equal_alignment=\"32\"\ntarget_has_atomic_equal_alignment=\"64\"\ntarget_has_atomic_equal_alignment=\"8\"\ntarget_has_atomic_equal_alignment=\"ptr\"\ntarget_has_atomic_load_store=\"16\"\ntarget_has_atomic_load_store=\"32\"\ntarget_has_atomic_load_store=\"64\"\ntarget_has_atomic_load_store=\"8\"\ntarget_has_atomic_load_store=\"ptr\"\ntarget_os=\"windows\"\ntarget_pointer_width=\"64\"\ntarget_thread_local\ntarget_vendor=\"pc\"\nwindows\n","stderr":""}},"successes":{}}
|
||||
|
|
@ -1 +1 @@
|
|||
/Users/giulianobellini/Desktop/PoliTO/Courses/PDS/project/pdsproject/packet_sniffer/target/debug/packet_sniffer: /Users/giulianobellini/Desktop/PoliTO/Courses/PDS/project/pdsproject/packet_sniffer/src/address_port.rs /Users/giulianobellini/Desktop/PoliTO/Courses/PDS/project/pdsproject/packet_sniffer/src/main.rs /Users/giulianobellini/Desktop/PoliTO/Courses/PDS/project/pdsproject/packet_sniffer/src/report_info.rs
|
||||
C:\Users\canep\CLionProjects\pdsproject\packet_sniffer\target\debug\packet_sniffer.exe: C:\Users\canep\CLionProjects\pdsproject\packet_sniffer\src\address_port.rs C:\Users\canep\CLionProjects\pdsproject\packet_sniffer\src\main.rs C:\Users\canep\CLionProjects\pdsproject\packet_sniffer\src\report_info.rs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue