From: Otto Moerbeek Date: Tue, 26 Mar 2024 08:29:22 +0000 (+0100) Subject: Make a isValidHostname() callable from Rust that calls into DNSName::is_hostname() X-Git-Tag: rec-5.1.0-alpha1~9^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7bbf885af3ecb13d4e8bc8196d4ea14fd497e3fc;p=thirdparty%2Fpdns.git Make a isValidHostname() callable from Rust that calls into DNSName::is_hostname() --- diff --git a/pdns/recursordist/settings/cxxsupport.cc b/pdns/recursordist/settings/cxxsupport.cc index d34cbca4e5..71f76b1fa4 100644 --- a/pdns/recursordist/settings/cxxsupport.cc +++ b/pdns/recursordist/settings/cxxsupport.cc @@ -1376,3 +1376,14 @@ uint16_t pdns::rust::settings::rec::qTypeStringToCode(::rust::Str str) std::string tmp(str.data(), str.length()); return QType::chartocode(tmp.c_str()); } + +bool pdns::rust::settings::rec::isValidHostname(::rust::Str str) +{ + try { + auto name = DNSName(string(str)); + return name.isHostname(); + } + catch (...) { + return false; + } +} diff --git a/pdns/recursordist/settings/rust-bridge-in.rs b/pdns/recursordist/settings/rust-bridge-in.rs index bf155c2b0e..427da15c74 100644 --- a/pdns/recursordist/settings/rust-bridge-in.rs +++ b/pdns/recursordist/settings/rust-bridge-in.rs @@ -347,4 +347,5 @@ extern "Rust" { unsafe extern "C++" { include!("bridge.hh"); fn qTypeStringToCode(name: &str) -> u16; + fn isValidHostname(name: &str) -> bool; } diff --git a/pdns/recursordist/settings/rust/Cargo.lock b/pdns/recursordist/settings/rust/Cargo.lock index 48d4f54571..25fa9b107b 100644 --- a/pdns/recursordist/settings/rust/Cargo.lock +++ b/pdns/recursordist/settings/rust/Cargo.lock @@ -83,12 +83,6 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" -[[package]] -name = "hostname-validator" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f558a64ac9af88b5ba400d99b579451af0d39c6d360980045b91aac966d705e2" - [[package]] name = "indexmap" version = "2.1.0" @@ -202,7 +196,6 @@ dependencies = [ "base64", "cxx", "cxx-build", - "hostname-validator", "ipnet", "once_cell", "serde", diff --git a/pdns/recursordist/settings/rust/Cargo.toml b/pdns/recursordist/settings/rust/Cargo.toml index 3dd157c50d..6179bd863b 100644 --- a/pdns/recursordist/settings/rust/Cargo.toml +++ b/pdns/recursordist/settings/rust/Cargo.toml @@ -13,7 +13,6 @@ serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9" ipnet = "2.8" once_cell = "1.18.0" -hostname-validator = "1.1.1" # This is temporary. PR 13819 has the infra to call C++ from, so we can arrange for DNSName::ishostname() to be called instead of importing another crate after that one is merged. base64 = "0.21" [build-dependencies] diff --git a/pdns/recursordist/settings/rust/build.rs b/pdns/recursordist/settings/rust/build.rs index 256f9981bf..ddddf7f292 100644 --- a/pdns/recursordist/settings/rust/build.rs +++ b/pdns/recursordist/settings/rust/build.rs @@ -1,6 +1,6 @@ fn main() { cxx_build::bridge("src/lib.rs") - // .file("src/source.cc") at the moment no C++ code callable from Rust + // .file("src/source.cc") Code callable from Rust is in ../cxxsupport.cc .flag_if_supported("-std=c++17") .flag("-Isrc") .compile("settings"); diff --git a/pdns/recursordist/settings/rust/src/bridge.hh b/pdns/recursordist/settings/rust/src/bridge.hh index 02f8beb733..6a6fd2b41d 100644 --- a/pdns/recursordist/settings/rust/src/bridge.hh +++ b/pdns/recursordist/settings/rust/src/bridge.hh @@ -26,4 +26,5 @@ namespace pdns::rust::settings::rec { uint16_t qTypeStringToCode(::rust::Str str); +bool isValidHostname(::rust::Str str); } diff --git a/pdns/recursordist/settings/rust/src/bridge.rs b/pdns/recursordist/settings/rust/src/bridge.rs index cbb9ef04ee..24998e2d1d 100644 --- a/pdns/recursordist/settings/rust/src/bridge.rs +++ b/pdns/recursordist/settings/rust/src/bridge.rs @@ -89,10 +89,10 @@ fn is_port_number(str: &str) -> bool { pub fn validate_socket_address_or_name(field: &str, val: &String) -> Result<(), ValidationError> { let sa = validate_socket_address(field, val); if sa.is_err() { - if !hostname_validator::is_valid(val) { + if !isValidHostname(val) { let parts: Vec<&str> = val.split(':').collect(); if parts.len() != 2 - || !hostname_validator::is_valid(parts[0]) + || !isValidHostname(parts[0]) || !is_port_number(parts[1]) { let msg = format!(