From 6b02a200285baebaf61704877a478df263eb13b1 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 28 Nov 2016 14:45:30 -0700 Subject: [PATCH] Fix loopback --- schemes/ipd/src/interface/ethernet.rs | 4 ++++ schemes/ipd/src/interface/loopback.rs | 8 +++++--- schemes/ipd/src/interface/mod.rs | 1 + schemes/ipd/src/main.rs | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/schemes/ipd/src/interface/ethernet.rs b/schemes/ipd/src/interface/ethernet.rs index 4c0bb78..d914a78 100644 --- a/schemes/ipd/src/interface/ethernet.rs +++ b/schemes/ipd/src/interface/ethernet.rs @@ -37,6 +37,10 @@ impl Interface for EthernetInterface { self.ip } + fn routable(&self, dst: Ipv4Addr) -> bool { + dst != Ipv4Addr::LOOPBACK + } + fn arp_event(&mut self) -> Result<()> { loop { let mut bytes = [0; 65536]; diff --git a/schemes/ipd/src/interface/loopback.rs b/schemes/ipd/src/interface/loopback.rs index 61c7e8c..a957ef8 100644 --- a/schemes/ipd/src/interface/loopback.rs +++ b/schemes/ipd/src/interface/loopback.rs @@ -4,14 +4,12 @@ use std::io::Result; use interface::Interface; pub struct LoopbackInterface { - ip: Ipv4Addr, packets: Vec } impl LoopbackInterface { pub fn new() -> Self { LoopbackInterface { - ip: Ipv4Addr::LOOPBACK, packets: Vec::new() } } @@ -19,7 +17,11 @@ impl LoopbackInterface { impl Interface for LoopbackInterface { fn ip(&self) -> Ipv4Addr { - self.ip + Ipv4Addr::LOOPBACK + } + + fn routable(&self, dst: Ipv4Addr) -> bool { + dst == Ipv4Addr::LOOPBACK } fn recv(&mut self) -> Result> { diff --git a/schemes/ipd/src/interface/mod.rs b/schemes/ipd/src/interface/mod.rs index 279e2be..2fa89d9 100644 --- a/schemes/ipd/src/interface/mod.rs +++ b/schemes/ipd/src/interface/mod.rs @@ -9,6 +9,7 @@ mod loopback; pub trait Interface { fn ip(&self) -> Ipv4Addr; + fn routable(&self, dst: Ipv4Addr) -> bool; fn recv(&mut self) -> Result>; fn send(&mut self, ip: Ipv4) -> Result; diff --git a/schemes/ipd/src/main.rs b/schemes/ipd/src/main.rs index 7c4fcaf..52ae0cb 100644 --- a/schemes/ipd/src/main.rs +++ b/schemes/ipd/src/main.rs @@ -194,7 +194,7 @@ impl SchemeMut for Ipd { if let Some(mut ip) = Ipv4::from_bytes(buf) { for mut interface in self.interfaces.iter_mut() { let if_ip = interface.ip(); - if ip.header.src == if_ip || ip.header.src == Ipv4Addr::NULL { + if ip.header.src == if_ip || (ip.header.src == Ipv4Addr::NULL && interface.routable(ip.header.dst)) { ip.header.src = if_ip; ip.header.proto = handle.proto;