Skip to content

Commit

Permalink
Redo examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip-NLnetLabs committed Sep 22, 2023
1 parent fac9883 commit c46627e
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 122 deletions.
7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,5 @@ name = "client"
required-features = ["std", "rand"]

[[example]]
name = "tcp-client"
name = "client-transports"
required-features = ["net"]

[[example]]
name = "tls-client"
required-features = ["net"]

130 changes: 130 additions & 0 deletions examples/client-transports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use domain::base::Dname;
use domain::base::Rtype::Aaaa;
use domain::base::{MessageBuilder, StaticCompressor, StreamTarget};
use domain::net::client::multi_stream;
use domain::net::client::query::QueryMessage2;
use domain::net::client::redundant;
use domain::net::client::tcp_factory::TcpConnFactory;
use domain::net::client::tls_factory::TlsConnFactory;
use domain::net::client::udp_tcp;
use std::net::{IpAddr, SocketAddr};
use std::str::FromStr;
use std::sync::Arc;
use tokio_rustls::rustls::{ClientConfig, OwnedTrustAnchor, RootCertStore};

#[tokio::main]
async fn main() {
// Create DNS request message
// Create a message builder wrapping a compressor wrapping a stream
// target.
let mut msg =
MessageBuilder::from_target(StaticCompressor::new(StreamTarget::new_vec())).unwrap();
msg.header_mut().set_rd(true);
let mut msg = msg.question();
msg.push((Dname::<Vec<u8>>::vec_from_str("example.com").unwrap(), Aaaa))
.unwrap();
let mut msg = msg.as_builder_mut().clone();

// Destination for UDP and TCP
let server_addr = SocketAddr::new(IpAddr::from_str("::1").unwrap(), 53);

// Create a new UDP+TCP transport connection. Pass the destination address
// and port as parameter.
let udptcp_conn = udp_tcp::Connection::new(server_addr).unwrap();

// Create a clone for the run function. Start the run function on a
// separate task.
let conn_run = udptcp_conn.clone();
tokio::spawn(async move {
conn_run.run().await;
});

// Send a query message.
let mut query = udptcp_conn.query(&mut msg).await.unwrap();

// Get the reply
let reply = query.get_result().await;
println!("UDP+TCP reply: {:?}", reply);

// Create a factory of TCP connections. Pass the destination address and
// port as parameter.
let tcp_factory = TcpConnFactory::new(server_addr);

// A muli_stream transport connection sets up new TCP connections when
// needed.
let tcp_conn = multi_stream::Connection::<Vec<u8>>::new().unwrap();

// Start the run function as a separate task. The run function receives
// the factory as a parameter.
let conn_run = tcp_conn.clone();
tokio::spawn(async move {
conn_run.run(tcp_factory).await;
});

// Send a query message.
let mut query = tcp_conn.query(&mut msg).await.unwrap();

// Get the reply
let reply = query.get_result().await;
println!("TCP reply: {:?}", reply);

// Some TLS boiler plate for the root certificates.
let mut root_store = RootCertStore::empty();
root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
)
}));

// TLS config
let client_config = Arc::new(
ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth(),
);

// Currently the only support TLS connections are the ones that have a
// valid certificate. Use a well known public resolver.
let server_addr = SocketAddr::new(IpAddr::from_str("8.8.8.8").unwrap(), 853);

// Create a new TLS connection factory. We pass the TLS config, the name of
// the remote server and the destination address and port.
let tls_factory = TlsConnFactory::new(client_config, "dns.google", server_addr);

// Again create a multi_stream transport connection.
let tls_conn = multi_stream::Connection::new().unwrap();

// Can start the run function.
let conn_run = tls_conn.clone();
tokio::spawn(async move {
conn_run.run(tls_factory).await;
});

let mut query = tls_conn.query(&mut msg).await.unwrap();
let reply = query.get_result().await;
println!("TLS reply: {:?}", reply);

// Create a transport connection for redundant connections.
let redun = redundant::Connection::new().unwrap();

// Start the run function on a separate task.
let redun_run = redun.clone();
tokio::spawn(async move {
redun_run.run().await;
});

// Add the previously created transports.
redun.add(Box::new(udptcp_conn)).await;
redun.add(Box::new(tcp_conn)).await;
redun.add(Box::new(tls_conn)).await;

// Start a few queries.
for _i in 1..10 {
let mut query = redun.query(&mut msg).await.unwrap();
let reply = query.get_result().await;
println!("redundant connection reply: {:?}", reply);
}
}
44 changes: 0 additions & 44 deletions examples/tcp-client.rs

This file was deleted.

64 changes: 0 additions & 64 deletions examples/tls-client.rs

This file was deleted.

5 changes: 5 additions & 0 deletions src/net/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#![cfg(feature = "net")]
#![cfg_attr(docsrs, doc(cfg(feature = "net")))]

//! # Example with various transport connections
//! ```
#![doc = include_str!("../../../examples/client-transports.rs")]
//! ```

pub mod error;
pub mod factory;
pub mod multi_stream;
Expand Down
8 changes: 0 additions & 8 deletions src/net/client/octet_stream.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
//! A DNS over octet stream transport
//! # Example with TCP connection to port 53
//! ```
#![doc = include_str!("../../../examples/tcp-client.rs")]
//! ```
//! # Example with TLS connection to port 853
//! ```
#![doc = include_str!("../../../examples/tls-client.rs")]
//! ```

#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
Expand Down

0 comments on commit c46627e

Please sign in to comment.