Skip to content

Commit 59b7e7d

Browse files
authored
Merge pull request #1803 from hermit-os/rm-octets
refactor(net): simplify non-DHCP address creation
2 parents 760384c + 1bcd276 commit 59b7e7d

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

src/executor/device.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ use smoltcp::socket::dhcpv4;
1212
#[cfg(all(feature = "dns", not(feature = "dhcpv4")))]
1313
use smoltcp::socket::dns;
1414
use smoltcp::time::Instant;
15-
#[cfg(not(feature = "dhcpv4"))]
16-
use smoltcp::wire::Ipv4Address;
1715
use smoltcp::wire::{EthernetAddress, HardwareAddress};
1816
#[cfg(not(feature = "dhcpv4"))]
19-
use smoltcp::wire::{IpAddress, IpCidr};
17+
use smoltcp::wire::{IpCidr, Ipv4Address, Ipv4Cidr};
2018

2119
use super::network::{NetworkInterface, NetworkState};
2220
use crate::arch;
@@ -128,34 +126,12 @@ impl<'a> NetworkInterface<'a> {
128126
#[cfg(feature = "dns")]
129127
let mydns2 = Ipv4Address::from_str(hermit_var_or!("HERMIT_DNS2", "1.1.1.1")).unwrap();
130128

131-
// calculate the netmask length
132-
// => count the number of contiguous 1 bits,
133-
// starting at the most significant bit in the first octet
134-
let mut prefix_len = (!mymask.octets()[0]).trailing_zeros();
135-
if prefix_len == 8 {
136-
prefix_len += (!mymask.octets()[1]).trailing_zeros();
137-
}
138-
if prefix_len == 16 {
139-
prefix_len += (!mymask.octets()[2]).trailing_zeros();
140-
}
141-
if prefix_len == 24 {
142-
prefix_len += (!mymask.octets()[3]).trailing_zeros();
143-
}
144-
145-
let ethernet_addr = EthernetAddress([mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]]);
129+
let ethernet_addr = EthernetAddress(mac);
146130
let hardware_addr = HardwareAddress::Ethernet(ethernet_addr);
147-
let ip_addrs = [IpCidr::new(
148-
IpAddress::v4(
149-
myip.octets()[0],
150-
myip.octets()[1],
151-
myip.octets()[2],
152-
myip.octets()[3],
153-
),
154-
prefix_len.try_into().unwrap(),
155-
)];
131+
let ip_addr = IpCidr::from(Ipv4Cidr::from_netmask(myip, mymask).unwrap());
156132

157133
info!("MAC address {hardware_addr}");
158-
info!("Configure network interface with address {}", ip_addrs[0]);
134+
info!("Configure network interface with address {ip_addr}");
159135
info!("Configure gateway with address {mygw}");
160136
info!("{checksums:?}");
161137
info!("MTU: {mtu} bytes");
@@ -169,17 +145,7 @@ impl<'a> NetworkInterface<'a> {
169145

170146
let mut iface = Interface::new(config, &mut device, crate::executor::network::now());
171147
iface.update_ip_addrs(|ip_addrs| {
172-
ip_addrs
173-
.push(IpCidr::new(
174-
IpAddress::v4(
175-
myip.octets()[0],
176-
myip.octets()[1],
177-
myip.octets()[2],
178-
myip.octets()[3],
179-
),
180-
prefix_len.try_into().unwrap(),
181-
))
182-
.unwrap();
148+
ip_addrs.push(ip_addr).unwrap();
183149
});
184150
iface.routes_mut().add_default_ipv4_route(mygw).unwrap();
185151

0 commit comments

Comments
 (0)