Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add support for DHCP option 15 (domain name) #3244

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion embassy-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ dns = ["smoltcp/socket-dns", "smoltcp/proto-dns"]
dhcpv4 = ["proto-ipv4", "medium-ethernet", "smoltcp/socket-dhcpv4"]
## Enable DHCPv4 support with hostname
dhcpv4-hostname = ["dhcpv4"]
## Enable DHCP option 15 (domain-name) for use in DNS queries
dhcpv4-domainname = ["dhcpv4"]
## Enable IPv4 support
proto-ipv4 = ["smoltcp/proto-ipv4"]
## Enable IPv6 support
Expand All @@ -66,7 +68,7 @@ igmp = ["smoltcp/proto-igmp"]
defmt = { version = "0.3", optional = true }
log = { version = "0.4.14", optional = true }

smoltcp = { version = "0.11.0", default-features = false, features = [
smoltcp = { git = "https:/korken89/smoltcp.git", branch = "dhcp-option-15", default-features = false, features = [
"socket",
"async",
] }
Expand All @@ -80,3 +82,4 @@ managed = { version = "0.8.0", default-features = false, features = [ "map" ] }
heapless = { version = "0.8", default-features = false }
embedded-nal-async = { version = "0.7.1" }
document-features = "0.2.7"

2 changes: 1 addition & 1 deletion embassy-net/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ where
{
fn consume<R, F>(self, f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
F: FnOnce(&[u8]) -> R,
{
self.0.consume(|buf| {
#[cfg(feature = "packet-trace")]
Expand Down
39 changes: 37 additions & 2 deletions embassy-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use embassy_net_driver::{Driver, LinkState};
use embassy_sync::waitqueue::WakerRegistration;
use embassy_time::{Instant, Timer};
#[allow(unused_imports)]
use heapless::Vec;
use heapless::{String, Vec};
#[cfg(feature = "dns")]
pub use smoltcp::config::DNS_MAX_SERVER_COUNT;
#[cfg(feature = "igmp")]
Expand Down Expand Up @@ -66,6 +66,8 @@ const LOCAL_PORT_MAX: u16 = 65535;
const MAX_QUERIES: usize = 4;
#[cfg(feature = "dhcpv4-hostname")]
const MAX_HOSTNAME_LEN: usize = 32;
#[cfg(all(feature = "dhcpv4-domainname", feature = "dns"))]
const MAX_DNS_QUERY_LEN: usize = 128;

/// Memory resources needed for a network stack.
pub struct StackResources<const SOCK: usize> {
Expand Down Expand Up @@ -110,6 +112,9 @@ pub struct StaticConfigV4 {
pub gateway: Option<Ipv4Address>,
/// DNS servers.
pub dns_servers: Vec<Ipv4Address, 3>,
/// Domain name.
#[cfg(feature = "dhcpv4-domainname")]
pub domain_name: Option<String<{ smoltcp::config::DHCP_MAX_DOMAIN_NAME_SIZE }>>,
}

/// Static IPv6 address configuration
Expand Down Expand Up @@ -146,7 +151,7 @@ pub struct DhcpConfig {
pub client_port: u16,
/// Our hostname. This will be sent to the DHCP server as Option 12.
#[cfg(feature = "dhcpv4-hostname")]
pub hostname: Option<heapless::String<MAX_HOSTNAME_LEN>>,
pub hostname: Option<String<MAX_HOSTNAME_LEN>>,
}

#[cfg(feature = "dhcpv4")]
Expand Down Expand Up @@ -527,6 +532,15 @@ impl<D: Driver> Stack<D> {
_ => {}
}

#[cfg(feature = "dhcpv4-domainname")]
let name = if name.contains(".") {
// Already a FQDN.
name
} else {
&self.create_fqdn(name)?
};
debug!("Performing DNS lookup of: {}", name);

let query = poll_fn(|cx| {
self.with_mut(|s, i| {
let socket = s.sockets.get_mut::<dns::Socket>(i.dns_socket);
Expand Down Expand Up @@ -602,6 +616,23 @@ impl<D: Driver> Stack<D> {

res
}

#[cfg(feature = "dhcpv4-domainname")]
fn create_fqdn(&self, name: &str) -> Result<String<MAX_DNS_QUERY_LEN>, dns::Error> {
use core::str::FromStr;

// Form name together with domain name.
let mut name = String::<MAX_DNS_QUERY_LEN>::from_str(name).map_err(|_| dns::Error::NameTooLong)?;

if let Some(Some(domain_name)) = &self.inner.borrow().static_v4.as_ref().map(|c| &c.domain_name) {
if !domain_name.is_empty() {
name.push('.').map_err(|_| dns::Error::NameTooLong)?;
name.push_str(&domain_name).map_err(|_| dns::Error::NameTooLong)?;
}
}

Ok(name)
}
}

#[cfg(feature = "igmp")]
Expand Down Expand Up @@ -788,6 +819,8 @@ impl<D: Driver> Inner<D> {
debug!(" DNS server: {:?}", s);
unwrap!(dns_servers.push(s.clone().into()).ok());
}
#[cfg(feature = "dhcpv4-domainname")]
debug!(" Domain name: {:?}", config.domain_name);
} else {
info!("IPv4: DOWN");
}
Expand Down Expand Up @@ -902,6 +935,8 @@ impl<D: Driver> Inner<D> {
address: config.address,
gateway: config.router,
dns_servers: config.dns_servers,
#[cfg(feature = "dhcpv4-domainname")]
domain_name: config.domain_name,
});
apply_config = true;
}
Expand Down