Make dual-stack work in embassy-net

This commit is contained in:
bjoernQ 2023-07-18 10:39:29 +02:00
parent 6b5df4523a
commit f581831b86

View File

@ -531,11 +531,14 @@ impl<D: Driver + 'static> Inner<D> {
debug!(" IP address: {}", config.address); debug!(" IP address: {}", config.address);
s.iface.update_ip_addrs(|addrs| { s.iface.update_ip_addrs(|addrs| {
if addrs.is_empty() { if let Some((index, _)) = addrs
addrs.push(IpCidr::Ipv4(config.address)).unwrap(); .iter()
} else { .enumerate()
addrs[0] = IpCidr::Ipv4(config.address); .find(|(_, &addr)| matches!(addr, IpCidr::Ipv4(_)))
{
addrs.remove(index);
} }
addrs.push(IpCidr::Ipv4(config.address)).unwrap();
}); });
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
@ -570,11 +573,14 @@ impl<D: Driver + 'static> Inner<D> {
debug!(" IP address: {}", config.address); debug!(" IP address: {}", config.address);
s.iface.update_ip_addrs(|addrs| { s.iface.update_ip_addrs(|addrs| {
if addrs.is_empty() { if let Some((index, _)) = addrs
addrs.push(IpCidr::Ipv6(config.address)).unwrap(); .iter()
} else { .enumerate()
addrs[0] = IpCidr::Ipv6(config.address); .find(|(_, &addr)| matches!(addr, IpCidr::Ipv6(_)))
{
addrs.remove(index);
} }
addrs.push(IpCidr::Ipv6(config.address)).unwrap();
}); });
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
@ -643,12 +649,19 @@ impl<D: Driver + 'static> Inner<D> {
} }
#[allow(unused)] // used only with dhcp #[allow(unused)] // used only with dhcp
fn unapply_config(&mut self, s: &mut SocketStack) { fn unapply_config_v4(&mut self, s: &mut SocketStack) {
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
let medium = self.device.capabilities().medium; let medium = self.device.capabilities().medium;
debug!("Lost IP configuration"); debug!("Lost IP configuration");
s.iface.update_ip_addrs(|ip_addrs| ip_addrs.clear()); s.iface.update_ip_addrs(|ip_addrs| {
if let Some((index, _)) = ip_addrs
.iter()
.enumerate()
.find(|(index, &addr)| matches!(addr, IpCidr::Ipv4(_)))
{
ip_addrs.remove(index);
}
});
#[cfg(feature = "medium-ethernet")] #[cfg(feature = "medium-ethernet")]
if medium == Medium::Ethernet { if medium == Medium::Ethernet {
#[cfg(feature = "proto-ipv4")] #[cfg(feature = "proto-ipv4")]
@ -695,7 +708,7 @@ impl<D: Driver + 'static> Inner<D> {
if self.link_up { if self.link_up {
match socket.poll() { match socket.poll() {
None => {} None => {}
Some(dhcpv4::Event::Deconfigured) => self.unapply_config(s), Some(dhcpv4::Event::Deconfigured) => self.unapply_config_v4(s),
Some(dhcpv4::Event::Configured(config)) => { Some(dhcpv4::Event::Configured(config)) => {
let config = StaticConfigV4 { let config = StaticConfigV4 {
address: config.address, address: config.address,
@ -707,7 +720,7 @@ impl<D: Driver + 'static> Inner<D> {
} }
} else if old_link_up { } else if old_link_up {
socket.reset(); socket.reset();
self.unapply_config(s); self.unapply_config_v4(s);
} }
} }
//if old_link_up || self.link_up { //if old_link_up || self.link_up {