From 93754e2cbae124f1a65f7018bd7a5d5b010714b5 Mon Sep 17 00:00:00 2001 From: Andrew Rioux Date: Thu, 8 Feb 2024 19:03:28 -0500 Subject: [PATCH] fix: Destination ports used in network traffic --- .gitignore | 1 + sparse-05/sparse-05-client/src/commands/connect/mod.rs | 4 ++-- sparse-05/sparse-05-server/src/connection.rs | 4 +++- sparse-05/sparse-05-server/src/main.rs | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 63e45bb..35c3873 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ core **/core .direnv result +sparse-public diff --git a/sparse-05/sparse-05-client/src/commands/connect/mod.rs b/sparse-05/sparse-05-client/src/commands/connect/mod.rs index c9ce847..465d7d7 100644 --- a/sparse-05/sparse-05-client/src/commands/connect/mod.rs +++ b/sparse-05/sparse-05-client/src/commands/connect/mod.rs @@ -110,8 +110,8 @@ pub async fn connect(config: PathBuf, ip: SocketAddr) -> anyhow::Result<()> { Ok(packet) => { let (count, addr) = packet?; - if addr != ip { - bail!("received response from wrong source"); + if addr.ip() != ip.ip() { + bail!("received response from wrong source (expected {ip:?}; got {addr:?})"); } let data = &buf[..count]; diff --git a/sparse-05/sparse-05-server/src/connection.rs b/sparse-05/sparse-05-server/src/connection.rs index f91dbc9..966507e 100644 --- a/sparse-05/sparse-05-server/src/connection.rs +++ b/sparse-05/sparse-05-server/src/connection.rs @@ -40,6 +40,7 @@ struct ConnectionInformation { srcip: Ipv4Addr, dstip: Ipv4Addr, srcport: u16, + listen_port: u16, packet_sender: Sender, } @@ -47,7 +48,7 @@ impl ConnectionInformation { fn format_udp_packet(&self, data: &[u8]) -> EthernetPacket { use packets::*; - let udp_packet = UDPPacket::construct(54248, self.srcport, data); + let udp_packet = UDPPacket::construct(self.listen_port, self.srcport, data); let ip_packet = IPv4Packet::construct(self.dstip, self.srcip, &Layer4Packet::UDP(udp_packet)); EthernetPacket::construct(self.dstmac, self.srcmac, &Layer3Packet::IPv4(ip_packet)) @@ -147,6 +148,7 @@ pub fn spawn_connection_handler( srcip: ip_pkt.source_ip(), dstip: ip_pkt.dest_ip(), srcport: udp_pkt.srcport(), + listen_port: udp_pkt.dstport(), packet_sender, } }; diff --git a/sparse-05/sparse-05-server/src/main.rs b/sparse-05/sparse-05-server/src/main.rs index 285ae40..c302d89 100644 --- a/sparse-05/sparse-05-server/src/main.rs +++ b/sparse-05/sparse-05-server/src/main.rs @@ -2,7 +2,7 @@ use std::{ collections::HashMap, net::Ipv4Addr, sync::{mpsc::channel, Arc, Mutex}, - thread, ffi::OsString, + thread, }; use anyhow::{bail, Context};