Skip to content

Commit 517adc6

Browse files
committed
refactor(net): simplify IPv4 CIDR block creation
1 parent 281469d commit 517adc6

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/executor/device.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use smoltcp::socket::dns;
1414
use smoltcp::time::Instant;
1515
use smoltcp::wire::{EthernetAddress, HardwareAddress};
1616
#[cfg(not(feature = "dhcpv4"))]
17-
use smoltcp::wire::{IpCidr, Ipv4Address};
17+
use smoltcp::wire::{Ipv4Address, Ipv4Cidr};
1818

1919
use super::network::{NetworkInterface, NetworkState};
2020
use crate::arch;
@@ -126,14 +126,9 @@ impl<'a> NetworkInterface<'a> {
126126
#[cfg(feature = "dns")]
127127
let mydns2 = Ipv4Address::from_str(hermit_var_or!("HERMIT_DNS2", "1.1.1.1")).unwrap();
128128

129-
// calculate the netmask length
130-
// => count the number of contiguous 1 bits
131-
let prefix_len = mymask.to_bits().leading_ones();
132-
let prefix_len = u8::try_from(prefix_len).unwrap();
133-
134129
let ethernet_addr = EthernetAddress(mac);
135130
let hardware_addr = HardwareAddress::Ethernet(ethernet_addr);
136-
let ip_addrs = [IpCidr::new(myip.into(), prefix_len)];
131+
let ip_addrs = [Ipv4Cidr::from_netmask(myip, mymask).unwrap()];
137132

138133
info!("MAC address {hardware_addr}");
139134
info!("Configure network interface with address {}", ip_addrs[0]);
@@ -150,7 +145,9 @@ impl<'a> NetworkInterface<'a> {
150145

151146
let mut iface = Interface::new(config, &mut device, crate::executor::network::now());
152147
iface.update_ip_addrs(|ip_addrs| {
153-
ip_addrs.push(IpCidr::new(myip.into(), prefix_len)).unwrap();
148+
ip_addrs
149+
.push(Ipv4Cidr::from_netmask(myip, mymask).unwrap().into())
150+
.unwrap();
154151
});
155152
iface.routes_mut().add_default_ipv4_route(mygw).unwrap();
156153

0 commit comments

Comments
 (0)