feat: added test tcp client

This commit is contained in:
Andrew Rioux
2023-09-19 10:24:51 -04:00
parent e5f6c2aa7e
commit 35bcf5352b
6 changed files with 251 additions and 67 deletions

View File

@@ -1,13 +1,11 @@
use std::net::Ipv4Addr;
use std::{net::Ipv4Addr, sync::Arc};
use anyhow::anyhow;
use nl_sys::{netlink, route};
use packets::EthernetPacket;
struct TcpConnection {
packets_to_ack: Vec<EthernetPacket>,
}
use packets::{
EthernetPacket, IPv4Packet, Layer3Packet, Layer4Packet, TCPPacketBuilder, UDPPacket,
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
@@ -17,7 +15,7 @@ async fn main() -> anyhow::Result<()> {
.ok_or(anyhow!("could not get target IP"))?
.parse::<Ipv4Addr>()?;
let (ifname, src_mac, dst_mac, srcip) = {
let (ifname, srcip, src_mac, dst_mac) = {
let socket = netlink::Socket::new()?;
let routes = socket.get_routes()?;
@@ -25,13 +23,12 @@ async fn main() -> anyhow::Result<()> {
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)
route::get_macs_and_src_for_ip(&addrs, &routes, &neighs, &links, ip)
.ok_or(anyhow!("unable to find a route to the IP"))?
};
dbg!((&ifname, srcip, src_mac, dst_mac));
let mut interface = pcap_sys::Interface::<pcap_sys::DevDisabled>::new(&ifname)?;
interface.set_buffer_size(8192)?;
@@ -39,7 +36,7 @@ async fn main() -> anyhow::Result<()> {
interface.set_promisc(false)?;
interface.set_timeout(10)?;
let mut interface = interface.activate()?;
let interface = Arc::new(interface.activate()?);
let port: u16 = loop {
let port = rand::random();
@@ -48,7 +45,29 @@ async fn main() -> anyhow::Result<()> {
}
};
let packets_to_ack: Vec<EthernetPacket> = vec![];
interface.set_filter(&format!("inbound and tcp port {}", port), true, None)?;
let seq: u32 = rand::random();
let udppacket = UDPPacket::construct(port, 54248, vec![]);
let tcppacket = TCPPacketBuilder::default()
.srcport(port)
.dstport(54248)
.seqnumber(seq)
.acknumber(0)
.syn(true)
.window(65495)
.build(srcip.clone(), ip.clone(), vec![]);
let ippacket = IPv4Packet::construct(srcip, ip, &Layer4Packet::UDP(udppacket));
let ethpacket = EthernetPacket::construct(src_mac, dst_mac, &Layer3Packet::IPv4(ippacket));
println!("sending packet...");
interface.sendpacket(ethpacket.pkt())?;
Ok(())
}