[pve-devel] [PATCH proxmox 2/2] network-types: add hostname type
Maximiliano Sandoval
m.sandoval at proxmox.com
Tue Apr 1 15:28:41 CEST 2025
Stefan Hanreich <s.hanreich at proxmox.com> writes:
> Add a type for representing Linux hostnames. These are the same
> constraints as the installer enforces [1]. Lowercasing is fine as
> well, since practically everything treats hostnames case-insensitively
> as RFC 952 stipulates:
>
>> No distinction is made between upper and lower case.
>
> [1] https://git.proxmox.com/?p=pve-installer.git;a=blob;f=Proxmox/Sys/Net.pm;h=81cb15f0042b195461324fffeca53d732133629e;hb=HEAD#l11
> [2] https://www.rfc-editor.org/rfc/rfc952.txt
>
> Signed-off-by: Stefan Hanreich <s.hanreich at proxmox.com>
> ---
>
> Notes:
> sending this separately because this contains the new types, that
> haven't been a part of proxmox-ve-rs before.
>
> proxmox-network-types/src/hostname.rs | 75 +++++++++++++++++++++++++++
> proxmox-network-types/src/lib.rs | 1 +
> 2 files changed, 76 insertions(+)
> create mode 100644 proxmox-network-types/src/hostname.rs
>
> diff --git a/proxmox-network-types/src/hostname.rs b/proxmox-network-types/src/hostname.rs
> new file mode 100644
> index 00000000..f183aecb
> --- /dev/null
> +++ b/proxmox-network-types/src/hostname.rs
> @@ -0,0 +1,75 @@
> +use std::fmt::Display;
> +
> +use serde::{Deserialize, Serialize};
> +use thiserror::Error;
> +
> +#[derive(Error, Debug)]
> +pub enum HostnameError {
> + #[error("the hostname must be from 1 to 63 characters long")]
> + InvalidLength,
> + #[error("the hostname contains invalid symbols")]
> + InvalidSymbols,
> +}
> +
> +/// Hostname of a Linux machine
> +///
> +/// A hostname is at most 63 characters long and must only contain lowercase alphanumeric
> +/// characters as well as hyphens. It must not start or end with a hyphen.
> +#[derive(Debug, Deserialize, Serialize, Clone, Eq, Hash, PartialOrd, Ord, PartialEq)]
> +pub struct Hostname(String);
> +
> +impl std::str::FromStr for Hostname {
> + type Err = HostnameError;
> +
> + fn from_str(hostname: &str) -> Result<Self, Self::Err> {
> + Self::new(hostname)
> + }
> +}
> +
> +impl AsRef<str> for Hostname {
> + fn as_ref(&self) -> &str {
> + &self.0
> + }
> +}
> +
> +impl Display for Hostname {
> + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
> + self.0.fmt(f)
> + }
> +}
> +
> +impl Hostname {
> + /// Constructs a new hostname from a string
> + ///
> + /// This function accepts characters in any case, but the resulting hostname will be
> + /// lowercased.
> + pub fn new(name: impl AsRef<str>) -> Result<Self, HostnameError> {
> + let name_ref = name.as_ref();
> +
> + if !(1..63).contains(&name_ref.len()) {
> + return Err(HostnameError::InvalidLength);
> + }
> +
> + let host_name = name_ref.to_lowercase();
> +
> + let mut characters = host_name.chars();
> +
> + // first character must not be a hyphen
> + // SAFETY: ok because of length check
> + if !characters.next().unwrap().is_alphanumeric() {
> + return Err(HostnameError::InvalidSymbols);
> + }
> +
> + if !characters.all(|c| c.is_alphanumeric() || c == '-') {
> + return Err(HostnameError::InvalidSymbols);
> + }
> +
> + // last character must not be a hyphen
> + // SAFETY: ok because of length check
> + if !host_name.chars().next_back().unwrap().is_alphanumeric() {
> + return Err(HostnameError::InvalidSymbols);
> + }
Why not use !name.starts_with(char::is_alphanumeric) ||
!name.ends_with(char::is_alphanumeric) { return Err(..) };`? The code
above is fine, but it took me some time to process and e.g. one has to
double check that the iterator user at the end is not the same used in
the middle.
> +
> + Ok(Self(host_name))
> + }
> +}
> diff --git a/proxmox-network-types/src/lib.rs b/proxmox-network-types/src/lib.rs
> index b952d71c..f4812146 100644
> --- a/proxmox-network-types/src/lib.rs
> +++ b/proxmox-network-types/src/lib.rs
> @@ -1,2 +1,3 @@
> +pub mod hostname;
> pub mod ip_address;
> pub mod mac_address;
More information about the pve-devel
mailing list