diff --git a/ci.sh b/ci.sh index 46a48f6b2..9038a0660 100755 --- a/ci.sh +++ b/ci.sh @@ -36,7 +36,7 @@ cargo batch \ --- build --release --manifest-path embassy-time/Cargo.toml --target thumbv6m-none-eabi --features nightly,defmt,defmt-timestamp-uptime,tick-hz-32_768,generic-queue-8 \ --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,proto-ipv4,medium-ethernet \ --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,dhcpv4,medium-ethernet \ - --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,dhcpv4,medium-ethernet,nightly \ + --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,dhcpv4,medium-ethernet,nightly,dhcpv4-hostname \ --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,proto-ipv6,medium-ethernet \ --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,proto-ipv6,medium-ieee802154 \ --- build --release --manifest-path embassy-net/Cargo.toml --target thumbv7em-none-eabi --features defmt,tcp,udp,dns,proto-ipv6,medium-ethernet,medium-ieee802154 \ diff --git a/embassy-net/Cargo.toml b/embassy-net/Cargo.toml index 89582deee..c2fffba84 100644 --- a/embassy-net/Cargo.toml +++ b/embassy-net/Cargo.toml @@ -33,6 +33,7 @@ udp = ["smoltcp/socket-udp"] tcp = ["smoltcp/socket-tcp"] dns = ["smoltcp/socket-dns", "smoltcp/proto-dns"] dhcpv4 = ["proto-ipv4", "medium-ethernet", "smoltcp/socket-dhcpv4"] +dhcpv4-hostname = ["dhcpv4"] proto-ipv4 = ["smoltcp/proto-ipv4"] proto-ipv6 = ["smoltcp/proto-ipv6"] medium-ethernet = ["smoltcp/medium-ethernet"] diff --git a/embassy-net/src/lib.rs b/embassy-net/src/lib.rs index 297f04679..ef67935e1 100644 --- a/embassy-net/src/lib.rs +++ b/embassy-net/src/lib.rs @@ -56,12 +56,22 @@ const LOCAL_PORT_MIN: u16 = 1025; const LOCAL_PORT_MAX: u16 = 65535; #[cfg(feature = "dns")] const MAX_QUERIES: usize = 4; +#[cfg(feature = "dhcpv4-hostname")] +const MAX_HOSTNAME_LEN: usize = 32; /// Memory resources needed for a network stack. pub struct StackResources { sockets: [SocketStorage<'static>; SOCK], #[cfg(feature = "dns")] queries: [Option; MAX_QUERIES], + #[cfg(feature = "dhcpv4-hostname")] + hostname: core::cell::UnsafeCell, +} + +#[cfg(feature = "dhcpv4-hostname")] +struct HostnameResources { + option: smoltcp::wire::DhcpOption<'static>, + data: [u8; MAX_HOSTNAME_LEN], } impl StackResources { @@ -73,6 +83,11 @@ impl StackResources { sockets: [SocketStorage::EMPTY; SOCK], #[cfg(feature = "dns")] queries: [INIT; MAX_QUERIES], + #[cfg(feature = "dhcpv4-hostname")] + hostname: core::cell::UnsafeCell::new(HostnameResources { + option: smoltcp::wire::DhcpOption { kind: 0, data: &[] }, + data: [0; MAX_HOSTNAME_LEN], + }), } } } @@ -104,6 +119,7 @@ pub struct StaticConfigV6 { /// DHCP configuration. #[cfg(feature = "dhcpv4")] #[derive(Debug, Clone, PartialEq, Eq)] +#[non_exhaustive] pub struct DhcpConfig { /// Maximum lease duration. /// @@ -120,6 +136,9 @@ pub struct DhcpConfig { pub server_port: u16, /// Client port. This is almost always 68. Do not change unless you know what you're doing. pub client_port: u16, + /// Our hostname. This will be sent to the DHCP server as Option 12. + #[cfg(feature = "dhcpv4-hostname")] + pub hostname: Option>, } #[cfg(feature = "dhcpv4")] @@ -131,6 +150,8 @@ impl Default for DhcpConfig { ignore_naks: Default::default(), server_port: smoltcp::wire::DHCP_SERVER_PORT, client_port: smoltcp::wire::DHCP_CLIENT_PORT, + #[cfg(feature = "dhcpv4-hostname")] + hostname: None, } } } @@ -232,6 +253,8 @@ struct Inner { dns_socket: SocketHandle, #[cfg(feature = "dns")] dns_waker: WakerRegistration, + #[cfg(feature = "dhcpv4-hostname")] + hostname: &'static mut core::cell::UnsafeCell, } pub(crate) struct SocketStack { @@ -307,6 +330,8 @@ impl Stack { )), #[cfg(feature = "dns")] dns_waker: WakerRegistration::new(), + #[cfg(feature = "dhcpv4-hostname")] + hostname: &mut resources.hostname, }; #[cfg(feature = "proto-ipv4")] @@ -673,6 +698,25 @@ impl Inner { socket.set_max_lease_duration(c.max_lease_duration.map(crate::time::duration_to_smoltcp)); socket.set_ports(c.server_port, c.client_port); socket.set_retry_config(c.retry_config); + + socket.set_outgoing_options(&[]); + #[cfg(feature = "dhcpv4-hostname")] + if let Some(h) = c.hostname { + // safety: we just did set_outgoing_options([]) so we know the socket is no longer holding a reference. + let hostname = unsafe { &mut *self.hostname.get() }; + + // create data + // safety: we know the buffer lives forever, new borrows the StackResources for 'static. + // also we won't modify it until next call to this function. + hostname.data[..h.len()].copy_from_slice(h.as_bytes()); + let data: &[u8] = &hostname.data[..h.len()]; + let data: &'static [u8] = unsafe { core::mem::transmute(data) }; + + // set the option. + hostname.option = smoltcp::wire::DhcpOption { data, kind: 12 }; + socket.set_outgoing_options(core::slice::from_ref(&hostname.option)); + } + socket.reset(); } _ => {