Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::io;
- use std::collections::HashMap;
- ust std::net::Ipv4Addr;
- mod tcp
- ;
- struct Quad {
- src: (Ipv4Addr, u16),
- dst: (Ipv4Addr, u16)
- }
- fn main() -> io::Result<()> {
- let mut connections: HashMap<Quad, tcp::State> = Default::default();
- let nic = tun_tap::Iface::new("tun0", tun_tap::Mode::Tun)?;
- let mut buf = [0u8; 1504];
- loop {
- let nbytes = nic.recv(&mut buf[..])?;
- let eth_flags = u16::from_be_bytes([buf[0], buf[1]]);
- let eth_protocol = u16::from_be_bytes([buf[2], buf[3]]);
- if eth_protocol != 0x0800 {
- println!("Got non-ipv4 protocol");
- continue; // ethernet protocol is not ipv4
- }
- match etherparse::Ipv4HeaderSlice::from_slice(&buf[4..nbytes]) {
- Ok(ip_header) => {
- let src = ip_header.source_addr();
- let dst = ip_header.destination_addr();
- if ip_header.protocol() != 0x06 { // Protocol has to be TCP
- continue;
- }
- match etherparse::TcpHeaderSlice::from_slice(&buf[4+ip_header.slice().len()..]) {
- Ok(tcp_header) => {
- let data_i = 4 + ip_header.slice().len() + tcp_header.slice().len();
- connections.entry(Quad {
- src: (src, p.source_port()),
- dst: (dst, p.destination_port()),
- }).or_default().on_packet(ip_header, tcp_header, &buf[data_i..]);
- // (srcip, srcport, dstip, dstport)
- eprintln!("{} -> {} - {} bytes of TCP to port {}", src, dst, tcp_header.slice().len(), tcp_header.destination_port());
- },
- Err(e) => {
- eprintln!("Ignoring TCP packet {:?}", e);
- }
- }
- },
- Err(e) => {
- eprintln!("Ignoring weird packet {:?}", e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement