Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce a new net::server module #482

Draft
wants to merge 25 commits into
base: new-base
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b9c0810
Add module 'new_net'
bal-e Jan 14, 2025
436f7fc
[new_net] Add module 'server'
bal-e Jan 14, 2025
2618d4e
[new_net/server] Set up a service layering architecture
bal-e Jan 27, 2025
dbe8ee7
[new_net/server/impls] Avoid MSRV 1.80 IntoIterator
bal-e Jan 28, 2025
bb3490b
Merge branch 'new-base' into new-net-server
bal-e Jan 28, 2025
f3909e4
[new_net/server] Overhaul service traits for async support
bal-e Jan 28, 2025
73e135a
[new_net/server/request] Implement construction
bal-e Jan 28, 2025
15e6b10
[new_net/server/request] Rewrite init a bit more cleanly
bal-e Jan 29, 2025
e0745c8
[new_net/server] Impl 'RequestMessage::sole_question()'
bal-e Jan 29, 2025
64ea348
[new_net/server/request] Add the most important getters
bal-e Jan 29, 2025
133c8e9
[new_net/server] Fix broken doc links
bal-e Jan 29, 2025
44125fc
[new_net/server/request] Make method docs clearer
bal-e Jan 29, 2025
5aadd4b
[new_net/server] Define a simple UDP transport
bal-e Jan 30, 2025
6fe81d9
Move 'new_net::server' to 'new_server'
bal-e Jan 30, 2025
7a4fa11
[new_server] Overhaul API for more flexibility
bal-e Feb 3, 2025
feb68af
[new_server/exchange] Make 'new()' non-const
bal-e Feb 3, 2025
4f97176
[new_server/exchange] Enhance documentation
bal-e Feb 3, 2025
cdf18b7
[new_server/exchange] Fix broken doc links
bal-e Feb 4, 2025
0cd88ab
[new_base] Fix various minor bugs
bal-e Feb 4, 2025
2c13124
[new_server] Implement cookie middleware
bal-e Feb 6, 2025
121efe0
Merge branch 'new-base' into new-net-server
bal-e Feb 7, 2025
2219209
[new_edns/cookie] Import 'AsBytes' unconditionally
bal-e Feb 7, 2025
0be40c4
[new_edns] Remove leftover debugging
bal-e Feb 7, 2025
54a4296
[new_server] Use 'log'
bal-e Feb 7, 2025
1c55f04
Add example program 'new-server'
bal-e Feb 7, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ license = "BSD-3-Clause"
domain-macros = { path = "./macros", version = "0.10.3" }

arbitrary = { version = "1.4.1", optional = true, features = ["derive"] }
bumpalo = { version = "3.12", optional = true }
octseq = { version = "0.5.2", default-features = false }
time = { version = "0.3.1", default-features = false }
rand = { version = "0.8", optional = true }
Expand Down Expand Up @@ -74,7 +75,7 @@ zonefile = ["bytes", "serde", "std"]

# Unstable features
unstable-client-transport = ["moka", "net", "tracing"]
unstable-server-transport = ["arc-swap", "chrono/clock", "libc", "net", "siphasher", "tracing"]
unstable-server-transport = ["dep:bumpalo", "arc-swap", "chrono/clock", "dep:log", "libc", "net", "rand", "siphasher", "tracing"]
unstable-sign = ["std", "dep:secrecy", "unstable-validate", "time/formatting"]
unstable-stelline = ["tokio/test-util", "tracing", "tracing-subscriber", "tsig", "unstable-client-transport", "unstable-server-transport", "zonefile"]
unstable-validate = ["bytes", "std", "ring"]
Expand All @@ -99,6 +100,7 @@ tokio-rustls = { version = "0.26", default-features = false, features = [
tokio-test = "0.4"
tokio-tfo = { version = "0.2.0" }
webpki-roots = { version = "0.26" }
env_logger = { version = "0.11" }

# For the "mysql-zone" example
#sqlx = { version = "0.6", features = [ "runtime-tokio-native-tls", "mysql" ] }
Expand All @@ -118,6 +120,10 @@ required-features = ["resolv"]
name = "lookup"
required-features = ["resolv"]

[[example]]
name = "new-server"
required-features = ["net", "unstable-server-transport", "unstable-client-transport"]

[[example]]
name = "resolv-sync"
required-features = ["resolv-sync"]
Expand Down
92 changes: 92 additions & 0 deletions examples/new-server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::ops::ControlFlow;

use log::trace;

use domain::new_server::{
exchange::{OutgoingResponse, ResponseCode},
layers::{
cookie::{CookieMetadata, CookiePolicy, CookieSecrets},
CookieLayer,
},
transport, Exchange, LocalService, LocalServiceLayer, Service,
ServiceLayer,
};

pub struct MyService;

impl Service for MyService {
async fn respond(&self, exchange: &mut Exchange<'_>) {
let cookie = exchange
.metadata
.iter()
.find_map(|m| m.try_as::<CookieMetadata>());

if let Some(CookieMetadata::ServerCookie { .. }) = cookie {
trace!(target: "MyService", "Request had a valid cookie");
} else {
trace!(target: "MyService", "Request did not have a valid cookie");
}

exchange.respond(ResponseCode::Success);

// Copy all questions from the request to the response.
exchange
.response
.questions
.append(&mut exchange.request.questions);
}
}

impl LocalService for MyService {
async fn respond_local(&self, exchange: &mut Exchange<'_>) {
self.respond(exchange).await
}
}

pub struct MyLayer;

impl ServiceLayer for MyLayer {
async fn process_incoming(
&self,
exchange: &mut Exchange<'_>,
) -> ControlFlow<()> {
trace!(target: "MyLayer",
"Incoming request (message ID {})",
exchange.request.id);
ControlFlow::Continue(())
}

async fn process_outgoing(&self, response: OutgoingResponse<'_, '_>) {
trace!(target: "MyLayer",
"Outgoing response (message ID {})",
response.response.id);
}
}

impl LocalServiceLayer for MyLayer {
async fn process_local_incoming(
&self,
exchange: &mut Exchange<'_>,
) -> ControlFlow<()> {
self.process_incoming(exchange).await
}

async fn process_local_outgoing(
&self,
response: OutgoingResponse<'_, '_>,
) {
self.process_outgoing(response).await
}
}

#[tokio::main]
async fn main() {
env_logger::init();

let addr = "127.0.0.1:8080".parse().unwrap();
let cookie_layer =
CookieLayer::new(CookiePolicy::default(), CookieSecrets::generate());
let service = (MyLayer, cookie_layer, MyService);
let result = transport::serve_udp(addr, service).await;
println!("Ended on result {result:?}");
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,5 @@ pub mod zonetree;
pub mod new_base;
pub mod new_edns;
pub mod new_rdata;

pub mod new_server;
Loading