feat: starting the TCP client proof of concept

This commit is contained in:
Andrew Rioux
2023-09-17 14:07:31 -04:00
parent 0ef459bcfe
commit 25948a17f4
8 changed files with 271 additions and 8 deletions

View File

@@ -1,4 +1,44 @@
use std::net::Ipv4Addr;
use anyhow::anyhow;
use nl_sys::{netlink, route};
#[tokio::main]
async fn main() {
println!("Hello, world!");
async fn main() -> anyhow::Result<()> {
let ip = std::env::args()
.skip(1)
.next()
.ok_or(anyhow!("could not get target IP"))?
.parse::<Ipv4Addr>()?;
let (ifname, src_mac, dst_mac, srcip) = {
let socket = netlink::Socket::new()?;
let routes = socket.get_routes()?;
let neighs = socket.get_neigh()?;
let links = socket.get_links()?;
let addrs = socket.get_addrs()?;
let (ifname, srcip, srcmac, dstmac) =
route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, ip)
.ok_or(anyhow!("unable to find a route to the IP"))?;
(ifname, srcip, srcmac, dstmac)
};
let mut interface = pcap_sys::new_aggregate_interface(false)?;
interface.set_buffer_size(8192)?;
interface.set_non_blocking(true)?;
interface.set_promisc(false)?;
interface.set_timeout(10)?;
let mut interface = interface.activate()?;
interface.set_filter("inbound and tcp port 54249", true, None)?;
interface.prune(|_, interface| interface.datalink() != pcap_sys::consts::DLT_EN10MB);
Ok(())
}