Skip to content

Commit

Permalink
Add support for domain name to generate FQDNs
Browse files Browse the repository at this point in the history
  • Loading branch information
korken89 committed Aug 11, 2024
1 parent b55726c commit 3318d21
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 5 additions & 2 deletions embassy-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ target = "thumbv7em-none-eabi"
features = ["defmt", "tcp", "udp", "raw", "dns", "dhcpv4", "proto-ipv6", "medium-ethernet", "medium-ip", "medium-ieee802154", "igmp", "dhcpv4-hostname"]

[features]
default = []
default = ["dns", "dhcpv4-hostname", "dhcpv4-domainname"]
std = []

## Enable defmt
Expand All @@ -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://github.com/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
27 changes: 25 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,22 @@ impl<D: Driver> Stack<D> {
_ => {}
}

// Form name together with domain name.
#[cfg(feature = "dhcpv4-domainname")]
let name = &{
use core::str::FromStr;

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)?;
}
}
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 @@ -902,6 +923,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

0 comments on commit 3318d21

Please sign in to comment.