From 7541d39fd62b7620cf4c31e5aa67877ec4f7212f Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 2 Aug 2022 16:45:17 +0800 Subject: [PATCH] add a static variable to contain our IPV4 address this is used to do address binding checking from library calls, so we can reject binding to address that are not our actual IP address. --- services/net/src/main.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 8bdf24d21..7adc8a6f5 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -18,6 +18,11 @@ use num_traits::*; mod connection_manager; mod device; +#[cfg(test)] +mod tests; +#[cfg(feature="btest")] +mod btests; + use std::collections::{BTreeMap, HashMap, BTreeSet}; use std::convert::TryInto; use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message, CID, SID}; @@ -43,6 +48,9 @@ use std::sync::mpsc::{channel, Sender}; use std::thread; use std::cmp::Ordering as CmpOrdering; +// 0 indicates no address is currently assigned +pub static IPV4_ADDRESS: AtomicU32 = AtomicU32::new(0); + const PING_DEFAULT_TIMEOUT_MS: u32 = 10_000; const MAX_DELAY_THREADS: u32 = 9; const PING_IDENT: u16 = 0x22b; @@ -1074,6 +1082,8 @@ fn main() -> ! { std::net::IpAddr::from(config.addr), xous::BOOKEND_END); net_config = Some(config); + // update a static variable that tracks this, useful for e.g. UDP bind address checking + IPV4_ADDRESS.store(u32::from_be_bytes(config.addr), Ordering::SeqCst); // note: ARP cache is stale. Maybe that's ok? @@ -1868,6 +1878,8 @@ fn main() -> ! { } }), Some(Opcode::Reset) => { + // reset the DHCP address + IPV4_ADDRESS.store(0, Ordering::SeqCst); // ack any pending ints com_int_list.clear(); com.ints_get_active(&mut com_int_list).ok();