From: Stefan Hanreich <s.hanreich@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox v4 2/2] network-types: add hostname type
Date: Fri, 4 Apr 2025 15:55:20 +0200 [thread overview]
Message-ID: <20250404135522.2603272-2-s.hanreich@proxmox.com> (raw)
In-Reply-To: <20250404135522.2603272-1-s.hanreich@proxmox.com>
Add a type for representing Linux hostnames. It enforces the same
constraints as the installer enforces [1], which are in turn taken
from the requirements Debian has for hostnames.
[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 v3 (thanks @Wolfgang):
* improved validation logic
* moved hostname to a specific debian module
* added additional TryFrom implementations
* added a unit test with a non-ASCII alphanumeric character
Changes from v2:
* improved hostname validation (thanks @Maximiliano @Christoph)
* added additional unit tests
Changes from v1:
* added unit tests
proxmox-network-types/src/debian.rs | 113 ++++++++++++++++++++++++++++
proxmox-network-types/src/lib.rs | 1 +
2 files changed, 114 insertions(+)
create mode 100644 proxmox-network-types/src/debian.rs
diff --git a/proxmox-network-types/src/debian.rs b/proxmox-network-types/src/debian.rs
new file mode 100644
index 00000000..7acd55ca
--- /dev/null
+++ b/proxmox-network-types/src/debian.rs
@@ -0,0 +1,113 @@
+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 has an invalid format")]
+ InvalidFormat,
+}
+
+/// Hostname of a Debian system
+///
+/// It checks for the following conditions:
+/// * At most 63 characters long.
+/// * It must not start or end with a hyphen.
+/// * Must only contain ASCII alphanumeric characters as well as hyphens.
+/// * It must not be purely numerical.
+#[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.to_string())
+ }
+}
+
+impl AsRef<str> for Hostname {
+ fn as_ref(&self) -> &str {
+ &self.0
+ }
+}
+
+impl TryFrom<&str> for Hostname {
+ type Error = HostnameError;
+
+ fn try_from(value: &str) -> Result<Self, Self::Error> {
+ value.parse()
+ }
+
+}
+
+impl TryFrom<String> for Hostname {
+ type Error = HostnameError;
+
+ fn try_from(value: String) -> Result<Self, Self::Error> {
+ Hostname::new(value)
+ }
+
+}
+
+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
+ pub fn new(name: String) -> Result<Self, HostnameError> {
+ if name.is_empty() || name.len() > 63 {
+ return Err(HostnameError::InvalidLength);
+ }
+
+ if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
+ return Err(HostnameError::InvalidFormat);
+ }
+
+ if name.starts_with('-') || name.ends_with('-') {
+ return Err(HostnameError::InvalidFormat);
+ }
+
+ if name.chars().all(|c| c.is_ascii_digit()) {
+ return Err(HostnameError::InvalidFormat);
+ }
+
+ Ok(Self(name))
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_parse_hostname() {
+ for valid_hostname in [
+ "debian",
+ "0host",
+ "some-host-123",
+ "63characterlonghostnamexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ ] {
+ Hostname::new(valid_hostname.to_string()).expect("valid hostname");
+ }
+
+ for invalid_hostname in [
+ "-debian",
+ "0host-",
+ "some/host",
+ "",
+ "123",
+ "64characterlonghostnamexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
+ "🆒",
+ "Ɖ",
+ ] {
+ Hostname::new(invalid_hostname.to_string()).expect_err("invalid hostname");
+ }
+ }
+}
diff --git a/proxmox-network-types/src/lib.rs b/proxmox-network-types/src/lib.rs
index b952d71c..9151348a 100644
--- a/proxmox-network-types/src/lib.rs
+++ b/proxmox-network-types/src/lib.rs
@@ -1,2 +1,3 @@
+pub mod debian;
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
next prev parent reply other threads:[~2025-04-04 13:56 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-04 13:55 [pve-devel] [PATCH proxmox v4 1/2] network-types: initial commit Stefan Hanreich
2025-04-04 13:55 ` Stefan Hanreich [this message]
2025-04-04 13:55 ` [pve-devel] [PATCH proxmox-ve-rs v4 1/1] ve-config: move types to proxmox-network-types Stefan Hanreich
2025-04-15 10:51 ` Christoph Heiss
2025-04-04 13:55 ` [pve-devel] [PATCH proxmox-firewall v4 1/1] firewall: nftables: migrate " Stefan Hanreich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250404135522.2603272-2-s.hanreich@proxmox.com \
--to=s.hanreich@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal