From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <pve-devel-bounces@lists.proxmox.com> Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 820491FF172 for <inbox@lore.proxmox.com>; Tue, 1 Apr 2025 15:36:59 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 854A53330E; Tue, 1 Apr 2025 15:36:49 +0200 (CEST) From: Stefan Hanreich <s.hanreich@proxmox.com> To: pve-devel@lists.proxmox.com Date: Tue, 1 Apr 2025 15:36:14 +0200 Message-Id: <20250401133616.331046-2-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250401133616.331046-1-s.hanreich@proxmox.com> References: <20250401133616.331046-1-s.hanreich@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.225 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [rfc-editor.org, proxmox.com, lib.rs, hostname.rs] Subject: [pve-devel] [PATCH proxmox v2 2/2] network-types: add hostname type X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com> List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe> List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/> List-Post: <mailto:pve-devel@lists.proxmox.com> List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help> List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe> Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com> Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com> 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@proxmox.com> --- Notes: sending this separately because this contains the new types, that haven't been a part of proxmox-ve-rs before. Changes from v1: * added unit tests proxmox-network-types/src/hostname.rs | 94 +++++++++++++++++++++++++++ proxmox-network-types/src/lib.rs | 1 + 2 files changed, 95 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..ebe79986 --- /dev/null +++ b/proxmox-network-types/src/hostname.rs @@ -0,0 +1,94 @@ +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); + } + + Ok(Self(host_name)) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_parse_hostname() { + for valid_hostname in ["debian", "0host", "some-host-123", "123"] { + Hostname::new(valid_hostname).expect("valid hostname"); + } + + for invalid_hostname in ["-debian", "0host-", "some/host", "", "thisisawaywaywaytoolonghostnameheremustnotbemorethan63characters"] { + Hostname::new(invalid_hostname).expect_err("invalid hostname"); + } + + let uppercased_hostname = Hostname::new("UPPERCASE").expect("valid hostname"); + assert_eq!(uppercased_hostname.as_ref(), "uppercase"); + } +} 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; -- 2.39.5 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel