mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
force all UDP packets to respond on the local IP address
smoltcp is written to allow multiple adapters, so 0.0.0.0 as the binding for UDP is illegal. However, in Rust, it seems that 0.0.0.0 is "totally fine" and most OSes will resolve this to a "default" adapter. this patch introduces the behavior of taking 0.0.0.0 and mapping it to a default adapter. In fact, we only allow binding to the default adapter. The reasoning behind this is that if our IP address changes, we would expect that 0.0.0.0 resolves to that. Where this scheme fails is if someone binds to 127.0.0.1 expecting it to not respond to packets coming in from the outside, e.g. they are using UDP as a loopback interface. I think this isn't a common use case for Xous, but if it does become one I suppose we could add a code path to also check for the 127.0.0.1 case and try to handle it...
This commit is contained in:
parent
94cbbddaa6
commit
bac650808d
1 changed files with 52 additions and 2 deletions
|
|
@ -108,10 +108,35 @@ pub(crate) fn std_udp_rx(
|
|||
None
|
||||
};
|
||||
let do_peek = body.offset.is_some();
|
||||
log::trace!("udp rx from fd {}", connection_handle_index);
|
||||
log::debug!("udp rx from fd {}", connection_handle_index);
|
||||
let local_addr = match iface.ipv4_addr() {
|
||||
Some(addr) => addr,
|
||||
None => {
|
||||
std_failure(msg, NetError::Unaddressable);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let socket = iface.get_socket::<UdpSocket>(*handle);
|
||||
let port = socket.endpoint().port;
|
||||
// force the local address to correspond to our (one and only) IP address
|
||||
// the underlying smoltcp library can't handle unspecified source addresses
|
||||
// because the library itself works with multiple interfaces and has no default resolution mechanism
|
||||
if socket.endpoint().addr != IpAddress::Ipv4(local_addr) {
|
||||
if socket.is_open() {
|
||||
socket.close();
|
||||
}
|
||||
if let Err(e) = socket.bind(IpEndpoint{addr: IpAddress::Ipv4(local_addr), port})
|
||||
.map_err(|e| match e {
|
||||
smoltcp::Error::Illegal => NetError::SocketInUse,
|
||||
smoltcp::Error::Unaddressable => NetError::Unaddressable,
|
||||
_ => NetError::LibraryError,
|
||||
}) {
|
||||
std_failure(msg, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if socket.can_recv() {
|
||||
log::trace!("receiving data right away");
|
||||
log::debug!("receiving data right away");
|
||||
if do_peek {
|
||||
// have to duplicate the code because Endpoint on peek is &, but on recv is not. This
|
||||
// difference in types means you can't do a pattern match assign to a common variable.
|
||||
|
|
@ -193,7 +218,32 @@ pub(crate) fn std_udp_tx(
|
|||
let len = u16::from_le_bytes([bytes[19], bytes[20]]);
|
||||
// attempt the tx
|
||||
log::debug!("udp tx to fd {} -> {:?}:{} {:?}", connection_handle_index, address, remote_port, &bytes[21..21 + len as usize]);
|
||||
let local_addr = match iface.ipv4_addr() {
|
||||
Some(addr) => addr,
|
||||
None => {
|
||||
std_failure(msg, NetError::Unaddressable);
|
||||
return;
|
||||
}
|
||||
};
|
||||
let socket = iface.get_socket::<UdpSocket>(*handle);
|
||||
let port = socket.endpoint().port;
|
||||
// force the local address to correspond to our (one and only) IP address
|
||||
// the underlying smoltcp library can't handle unspecified source addresses
|
||||
// because the library itself works with multiple interfaces and has no default resolution mechanism
|
||||
if socket.endpoint().addr != IpAddress::Ipv4(local_addr) {
|
||||
if socket.is_open() {
|
||||
socket.close();
|
||||
}
|
||||
if let Err(e) = socket.bind(IpEndpoint{addr: IpAddress::Ipv4(local_addr), port})
|
||||
.map_err(|e| match e {
|
||||
smoltcp::Error::Illegal => NetError::SocketInUse,
|
||||
smoltcp::Error::Unaddressable => NetError::Unaddressable,
|
||||
_ => NetError::LibraryError,
|
||||
}) {
|
||||
std_failure(msg, e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
match socket.send_slice(&bytes[21..21 + len as usize], IpEndpoint::new(address, remote_port)) {
|
||||
Ok(_) => {
|
||||
body.buf.as_slice_mut()[0] = 0;
|
||||
|
|
|
|||
Loading…
Reference in a new issue