Skip to content

Commit

Permalink
Modularize: split out misc.rs for general stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
omoerbeek committed Feb 11, 2025
1 parent 1639e31 commit ce42b61
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 183 deletions.
7 changes: 0 additions & 7 deletions pdns/recursordist/rec-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,6 @@ int RecThreadInfo::runThreads(Logr::log_t log)
}

if (::arg().mustDo("webserver")) {
extern void serveRustWeb();
cerr << "CALL serveRustWeb" << endl;
serveRustWeb();
}

Expand Down Expand Up @@ -358,13 +356,8 @@ int RecThreadInfo::runThreads(Logr::log_t log)
info.start(currentThreadId, "web+stat", cpusMap, log);

if (::arg().mustDo("webserver")) {
extern void serveRustWeb();
cerr << "WS is CALLED " << endl;
serveRustWeb();
}
else {
cerr << "WS is FALSE " << endl;
}
for (auto& tInfo : RecThreadInfo::infos()) {
tInfo.thread.join();
if (tInfo.exitCode != 0) {
Expand Down
3 changes: 3 additions & 0 deletions pdns/recursordist/rec-web-stubs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ namespace pdns::rust::web::rec
#define WRAPPER(A) \
void A(const Request& /* unused */, Response& /* unused */) {}

class Request;
class Response;

WRAPPER(apiDiscovery)
WRAPPER(apiDiscoveryV1)
WRAPPER(apiServer)
Expand Down
44 changes: 23 additions & 21 deletions pdns/recursordist/settings/cxxsupport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "iputils.hh"
#include "bridge.hh"
#include "settings/rust/web.rs.h"
#include "settings/rust/misc.rs.h"

::rust::Vec<::rust::String> pdns::settings::rec::getStrings(const std::string& name)
{
Expand Down Expand Up @@ -1441,24 +1442,7 @@ pdns::settings::rec::YamlSettingsStatus pdns::settings::rec::tryReadYAML(const s
return yamlstatus;
}

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;
}
}

namespace pdns::rust::web::rec
namespace pdns::rust::misc
{

template <typename M>
Expand All @@ -1478,7 +1462,25 @@ template <typename M>

template class Wrapper<::NetmaskGroup>;
template class Wrapper<::ComboAddress>;
//template class Wrapper<std::shared_ptr<::Logr::Logger>>;

uint16_t qTypeStringToCode(::rust::Str str)
{
std::string tmp(str.data(), str.length());
return QType::chartocode(tmp.c_str());
}

bool isValidHostname(::rust::Str str)
{
try {
auto name = DNSName(string(str));
return name.isHostname();
}
catch (...) {
return false;
}
}

void findBetterSolution(const std::unique_ptr<CredentialsHolder>& /* x */){};

std::unique_ptr<ComboAddress> comboaddress(::rust::Str str)
{
Expand All @@ -1490,7 +1492,7 @@ bool matches(const std::unique_ptr<NetmaskGroup>& nmg, const std::unique_ptr<Com
return nmg->get().match(address->get());
}

void log(const std::shared_ptr<Logger>& logger, pdns::rust::web::rec::Priority log_level, ::rust::Str msg, const ::rust::Vec<KeyValue>& values)
void log(const std::shared_ptr<Logger>& logger, pdns::rust::misc::Priority log_level, ::rust::Str msg, const ::rust::Vec<KeyValue>& values)
{
auto log = logger;
for (const auto& [key, value] : values) {
Expand All @@ -1499,7 +1501,7 @@ void log(const std::shared_ptr<Logger>& logger, pdns::rust::web::rec::Priority l
log->info(static_cast<Logr::Priority>(log_level), std::string(msg));
}

void error(const std::shared_ptr<Logger>& logger, pdns::rust::web::rec::Priority log_level, ::rust::Str error, ::rust::Str msg, const ::rust::Vec<KeyValue>& values)
void error(const std::shared_ptr<Logger>& logger, pdns::rust::misc::Priority log_level, ::rust::Str error, ::rust::Str msg, const ::rust::Vec<KeyValue>& values)
{
auto log = logger;
for (const auto& [key, value] : values) {
Expand Down
9 changes: 2 additions & 7 deletions pdns/recursordist/settings/rust-bridge-in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ pub struct IncomingTLS {
certificate: String,
#[serde(default, skip_serializing_if = "crate::is_default")]
key: String,
#[serde(default, skip_serializing_if = "crate::is_default")]
password: String,
// #[serde(default, skip_serializing_if = "crate::is_default")]
// password: String, Not currently supported, as rusttls does not support this out of the box
}
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[serde(deny_unknown_fields)]
Expand Down Expand Up @@ -410,8 +410,3 @@ extern "Rust" {
fn api_delete_zones(file: &str) -> Result<()>;
}

unsafe extern "C++" {
include!("bridge.hh");
fn qTypeStringToCode(name: &str) -> u16;
fn isValidHostname(name: &str) -> bool;
}
2 changes: 2 additions & 0 deletions pdns/recursordist/settings/rust-preamble-in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use helpers::*;
mod bridge;
use bridge::*;

mod misc;

mod web; // leaving this out causes link issues

// Suppresses "Deserialize unused" warning
Expand Down
1 change: 1 addition & 0 deletions pdns/recursordist/settings/rust/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/cxx.h
/lib.rs.h
/web.rs.h
/misc.rs.h
src/lib.rs
.dir-locals.el
1 change: 0 additions & 1 deletion pdns/recursordist/settings/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pdns/recursordist/settings/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ hyper-util = { version = "0.1", features = ["tokio"]}
bytes = "1.8"
form_urlencoded = "1.2"
hyper-rustls = { version = "0.27", default-features = false }
rustls = { version = "0.23", default-features = false, features = ["ring"] }
rustls = { version = "0.23", default-features = false, features = [] }
rustls-pemfile = "2.2"
pki-types = { package = "rustls-pki-types", version = "1.10" }
tokio-rustls = { version = "0.26", default-features = false }
uuid = { version = "1.12.1", features = ["v4"] }

[build-dependencies]
cxx-build = "1.0"

16 changes: 13 additions & 3 deletions pdns/recursordist/settings/rust/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ CARGO ?= cargo
all install: libsettings.a

EXTRA_DIST = \
Cargo.toml \
Cargo.lock \
Cargo.toml \
build.rs \
src/bridge.rs \
src/helpers.rs \
src/misc.rs \
src/web.rs

# should actually end up in a target specific dir...
libsettings.a lib.rs.h web.rs.h: src/web.rs src/bridge.rs src/lib.rs src/helpers.rs Cargo.toml Cargo.lock build.rs
libsettings.a lib.rs.h web.rs.h misc.rs.h: \
Cargo.lock \
Cargo.toml \
build.rs \
src/bridge.rs \
src/helpers.rs \
src/lib.rs \
src/misc.rs \
src/web.rs
SYSCONFDIR=$(sysconfdir) NODCACHEDIRNOD=$(localstatedir)/nod NODCACHEDIRUDR=$(localstatedir)/udr $(CARGO) build --release $(RUST_TARGET) --target-dir=$(builddir)/target --manifest-path ${srcdir}/Cargo.toml
cp target/$(RUSTC_TARGET_ARCH)/release/libsettings.a libsettings.a
cp target/$(RUSTC_TARGET_ARCH)/cxxbridge/settings/src/lib.rs.h lib.rs.h
cp target/$(RUSTC_TARGET_ARCH)/cxxbridge/settings/src/web.rs.h web.rs.h
cp target/$(RUSTC_TARGET_ARCH)/cxxbridge/settings/src/misc.rs.h misc.rs.h
cp target/$(RUSTC_TARGET_ARCH)/cxxbridge/rust/cxx.h cxx.h

clean-local:
rm -rf libsettings.a src/lib.rs lib.rs.h web.rs.h cxx.h target
rm -rf libsettings.a src/lib.rs lib.rs.h web.rs.h cxx.h misc.rs.h target
6 changes: 5 additions & 1 deletion pdns/recursordist/settings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
fn main() {
let sources = vec!["src/lib.rs", "src/web.rs"];
let sources = vec!["src/lib.rs", "src/web.rs", "src/misc.rs"];
cxx_build::bridges(sources)
// .file("src/source.cc") Code callable from Rust is in ../cxxsupport.cc
.flag_if_supported("-std=c++17")
.flag("-Isrc")
.flag("-I../../..")
.compile("settings");

// lib.rs is genertated an take carte of by parent Makefile
println!("cargo:rerun-if-changed=src/misc.rs");
println!("cargo:rerun-if-changed=src/web.rs");
}
3 changes: 2 additions & 1 deletion pdns/recursordist/settings/rust/build_settings
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

$CARGO build --release $RUST_TARGET --target-dir=$builddir/target --manifest-path $srcdir/Cargo.toml


cp -vp target/$RUSTC_TARGET_ARCH/release/libsettings.a $builddir/settings/rust/libsettings.a
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/lib.rs.h $srcdir/lib.rs.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/lib.rs.h $builddir/settings/rust/lib.rs.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/rust/cxx.h $srcdir/cxx.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/rust/cxx.h $builddir/settings/rust/cxx.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/web.rs.h $srcdir/web.rs.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/web.rs.h $builddir/settings/rust/web.rs.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/misc.rs.h $srcdir/misc.rs.h
cp -vp target/$RUSTC_TARGET_ARCH/cxxbridge/settings/src/misc.rs.h $builddir/settings/rust/misc.rs.h
44 changes: 23 additions & 21 deletions pdns/recursordist/settings/rust/src/bridge.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,18 @@
#include "rust/cxx.h"
#include "credentials.hh"

namespace pdns::rust::settings::rec
{
uint16_t qTypeStringToCode(::rust::Str str);
bool isValidHostname(::rust::Str str);
void setThreadName(::rust::Str str);
}

class NetmaskGroup;
union ComboAddress;
namespace Logr
{
class Logger;
}


namespace pdns::rust::web::rec
{
using CredentialsHolder = ::CredentialsHolder;
struct KeyValue;
struct Request;
struct Response;
struct IncomingWSConfig;
namespace pdns::rust::misc {
enum class Priority : uint8_t;
enum class LogLevel : uint8_t;
using Logger = ::Logr::Logger;
struct KeyValue;

template <typename A>
class Wrapper
Expand All @@ -68,9 +57,27 @@ public:
private:
std::unique_ptr<A> d_ptr;
};

using NetmaskGroup = Wrapper<::NetmaskGroup>;
using ComboAddress = Wrapper<::ComboAddress>;
using Logger = ::Logr::Logger;

uint16_t qTypeStringToCode(::rust::Str str);
bool isValidHostname(::rust::Str str);
std::unique_ptr<pdns::rust::misc::ComboAddress> comboaddress(::rust::Str str);
bool matches(const std::unique_ptr<NetmaskGroup>& nmg, const std::unique_ptr<ComboAddress>& address);
std::shared_ptr<Logger> withValue(const std::shared_ptr<Logger>& logger, ::rust::Str key, ::rust::Str val);
void log(const std::shared_ptr<Logger>& logger, Priority log_level, ::rust::Str msg, const ::rust::Vec<KeyValue>& values);
void error(const std::shared_ptr<Logger>& logger, Priority log_level, ::rust::Str err, ::rust::Str msg, const ::rust::Vec<KeyValue>& values);
}


namespace pdns::rust::web::rec
{
using CredentialsHolder = ::CredentialsHolder;
struct KeyValue;
struct Request;
struct Response;
struct IncomingWSConfig;

void apiServer(const Request& rustRequest, Response& rustResponse);
void apiDiscovery(const Request& rustRequest, Response& rustResponse);
Expand All @@ -93,9 +100,4 @@ void apiServerSearchData(const Request& rustRequest, Response& rustResponse);
void apiServerZoneDetailGET(const Request& rustRequest, Response& rustResponse);
void apiServerZoneDetailPUT(const Request& rustRequest, Response& rustResponse);
void apiServerZoneDetailDELETE(const Request& rustRequest, Response& rustResponse);
std::unique_ptr<ComboAddress> comboaddress(::rust::Str str);
bool matches(const std::unique_ptr<NetmaskGroup>& nmg, const std::unique_ptr<ComboAddress>& address);
std::shared_ptr<Logger> withValue(const std::shared_ptr<Logger>& logger, ::rust::Str key, ::rust::Str val);
void log(const std::shared_ptr<Logger>& logger, Priority log_level, ::rust::Str msg, const ::rust::Vec<KeyValue>& values);
void error(const std::shared_ptr<Logger>& logger, Priority log_level, ::rust::Str err, ::rust::Str msg, const ::rust::Vec<KeyValue>& values);
}
7 changes: 4 additions & 3 deletions pdns/recursordist/settings/rust/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::sync::Mutex;
use crate::helpers::OVERRIDE_TAG;
use crate::recsettings::{self, *};
use crate::{Merge, ValidationError};
use crate::misc::rustmisc;

impl Default for ForwardZone {
fn default() -> Self {
Expand Down Expand Up @@ -123,9 +124,9 @@ 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() && !isValidHostname(val) {
if sa.is_err() && !rustmisc::isValidHostname(val) {
let parts: Vec<&str> = val.split(':').collect();
if parts.len() != 2 || !isValidHostname(parts[0]) || !is_port_number(parts[1]) {
if parts.len() != 2 || !rustmisc::isValidHostname(parts[0]) || !is_port_number(parts[1]) {
let msg = format!(
"{}: value `{}' is not an IP, IP:port, name or name:port combination",
field, val
Expand All @@ -137,7 +138,7 @@ pub fn validate_socket_address_or_name(field: &str, val: &String) -> Result<(),
}

fn validate_qtype(field: &str, val: &String) -> Result<(), ValidationError> {
let code = qTypeStringToCode(val);
let code = rustmisc::qTypeStringToCode(val);
if code == 0 {
let msg = format!("{}: value `{}' is not a qtype", field, val);
return Err(ValidationError { msg });
Expand Down
47 changes: 47 additions & 0 deletions pdns/recursordist/settings/rust/src/misc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[cxx::bridge(namespace = "pdns::rust::misc")]
pub mod rustmisc {

pub enum LogLevel {
None,
Normal,
Detailed,
}
enum Priority {
Absent = 0,
Alert = 1,
Critical = 2,
Error = 3,
Warning = 4,
Notice = 5,
Info = 6,
Debug = 7,
}
struct KeyValue {
key: String,
value: String,
}

extern "C++" {
type NetmaskGroup;
type ComboAddress;
type Logger;
}

unsafe extern "C++" {
include!("bridge.hh");
fn qTypeStringToCode(name: &str) -> u16;
fn isValidHostname(name: &str) -> bool;
fn comboaddress(address: &str) -> UniquePtr<ComboAddress>;
fn matches(nmg: &UniquePtr<NetmaskGroup>, address: &UniquePtr<ComboAddress>) -> bool; // match is a keyword
fn withValue(logger: &SharedPtr<Logger>, key: &str, val: &str) -> SharedPtr<Logger>;
fn log(logger: &SharedPtr<Logger>, prio: Priority, msg: &str, values: &Vec<KeyValue>);
fn error(
logger: &SharedPtr<Logger>,
prio: Priority,
err: &str,
msg: &str,
values: &Vec<KeyValue>,
);
}
}

Loading

0 comments on commit ce42b61

Please sign in to comment.