Skip to content

Commit

Permalink
fixed bug no stats in DoQ
Browse files Browse the repository at this point in the history
  • Loading branch information
dandyvica committed Jan 14, 2025
1 parent bd075da commit 693f109
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 34 deletions.
5 changes: 4 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ Project home page: https://github.com/dandyvica/dqy"#,
let usage = format!(
r#"dqy [TYPES] [DOMAIN] [@RESOLVER] [OPTIONS]
Caveat: all options starting with a dash (-) should be placed after optional [TYPES] [DOMAIN] [@RESOLVER].
Caveats:
- all options starting with a dash (-) should be placed after optional [TYPES] [DOMAIN] [@RESOLVER].
- whenever you enter a domain name, it must ends with the root (.). E.g.: fr. or mx.
Supported query types: {}
"#,
Expand Down
23 changes: 15 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod transport;
use transport::{
https::HttpsProtocol,
network::{Messenger, Protocol},
quic::QuicProtocol,
root_servers::init_root_map,
tcp::TcpProtocol,
tls::TlsProtocol,
Expand All @@ -54,15 +55,15 @@ use lua::LuaDisplay;
const BUFFER_SIZE: usize = 8192;

//───────────────────────────────────────────────────────────────────────────────────
// get list of messages using transport
// get list of messages using transport: sync mode
//───────────────────────────────────────────────────────────────────────────────────
fn get_messages_using_transport<T: Messenger>(
fn get_messages_using_sync_transport<T: Messenger>(
info: Option<&mut QueryInfo>,
transport: &mut T,
options: &CliOptions,
) -> error::Result<MessageList> {
// BUFFER_SIZE is the size of the buffer used to received data
let messages = DnsProtocol::process_request(options, transport, BUFFER_SIZE)?;
let messages = DnsProtocol::sync_process_request(options, transport, BUFFER_SIZE)?;

// we want run info
if let Some(info) = info {
Expand All @@ -83,19 +84,19 @@ pub fn get_messages(info: Option<&mut QueryInfo>, options: &CliOptions) -> error
match options.transport.transport_mode {
Protocol::Udp => {
let mut transport = UdpProtocol::new(&options.transport)?;
get_messages_using_transport(info, &mut transport, options)
get_messages_using_sync_transport(info, &mut transport, options)
}
Protocol::Tcp => {
let mut transport = TcpProtocol::new(&options.transport)?;
get_messages_using_transport(info, &mut transport, options)
get_messages_using_sync_transport(info, &mut transport, options)
}
Protocol::DoT => {
let mut transport = TlsProtocol::new(&options.transport)?;
get_messages_using_transport(info, &mut transport, options)
get_messages_using_sync_transport(info, &mut transport, options)
}
Protocol::DoH => {
let mut transport = HttpsProtocol::new(&options.transport)?;
get_messages_using_transport(info, &mut transport, options)
get_messages_using_sync_transport(info, &mut transport, options)
}
Protocol::DoQ => {
// quinn crate doesn't provide blocking
Expand All @@ -105,7 +106,13 @@ pub fn get_messages(info: Option<&mut QueryInfo>, options: &CliOptions) -> error
.map_err(Error::Tokio)?;

rt.block_on(async {
let messages = DnsProtocol::quic_process_request(options, BUFFER_SIZE).await?;
let mut transport = QuicProtocol::new(&options.transport).await?;
let messages = DnsProtocol::async_process_request(options, &mut transport, BUFFER_SIZE).await?;

// we want run info
if let Some(info) = info {
info.netinfo = *transport.network_info();
}
Ok(messages)
})
}
Expand Down
18 changes: 8 additions & 10 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::dns::{
};
use crate::error::{self};
use crate::transport::network::{Messenger, Protocol};
use crate::transport::quic::QuicProtocol;
use crate::transport::tcp::TcpProtocol;
use crate::{args::CliOptions, cli_options::FromOptions};

Expand Down Expand Up @@ -93,9 +92,9 @@ impl DnsProtocol {
}

//───────────────────────────────────────────────────────────────────────────────────
// this sends and receives queries using a transport
// this sends and receives queries using a sync transport
//───────────────────────────────────────────────────────────────────────────────────
pub(crate) fn process_request<T: Messenger>(
pub(crate) fn sync_process_request<T: Messenger>(
options: &CliOptions,
trp: &mut T,
buffer_size: usize,
Expand Down Expand Up @@ -132,25 +131,24 @@ impl DnsProtocol {
}

//───────────────────────────────────────────────────────────────────────────────────
// specific to QUIC
// this sends and receives queries using an async transport
//───────────────────────────────────────────────────────────────────────────────────
pub(crate) async fn quic_process_request(
pub(crate) async fn async_process_request<T: Messenger>(
options: &CliOptions,
trp: &mut T,
buffer_size: usize,
) -> crate::error::Result<MessageList> {
// we'll have the same number of messages than the number of types to query
let mut messages = Vec::with_capacity(options.protocol.qtype.len());
let mut buffer = vec![0u8; buffer_size];

let mut trp = QuicProtocol::new(&options.transport).await?;

for qtype in options.protocol.qtype.iter() {
// for QUIC, we need a specific stream for each query as stated in https://www.rfc-editor.org/rfc/rfc9250.html
trp.connect().await?;
trp.aconnect().await?;

// send query, response is depending on TC flag if UDP
let query = Self::asend_query(options, qtype, &mut trp).await?;
let response = Self::areceive_response(&mut trp, &mut buffer, &options.dump.write_response).await?;
let query = Self::asend_query(options, qtype, trp).await?;
let response = Self::areceive_response(trp, &mut buffer, &options.dump.write_response).await?;

// struct Message is a convenient way to gather both query and response
let msg = Message { query, response };
Expand Down
4 changes: 4 additions & 0 deletions src/transport/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ impl<'a> Messenger for HttpsProtocol<'a> {
Ok(0)
}

async fn aconnect(&mut self) -> error::Result<()> {
Ok(())
}

fn send(&mut self, buffer: &[u8]) -> crate::error::Result<usize> {
self.netinfo.sent = buffer.len();

Expand Down
3 changes: 3 additions & 0 deletions src/transport/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub trait Messenger {
// async version
async fn arecv(&mut self, buffer: &mut [u8]) -> error::Result<usize>;

// async connect used by QUIC
async fn aconnect(&mut self) -> error::Result<()>;

// true if transporter uses Tcp. This is required for TCP transport to have 2 bytes
// for the message length prepended in the query
fn uses_leading_length(&self) -> bool;
Expand Down
36 changes: 21 additions & 15 deletions src/transport/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,21 @@ impl QuicProtocol {
.map_err(|e| Error::Quic(QuicError::Connection(e)))?;
debug!("conn: {:?}", conn);

let addr = conn.remote_address();

Ok(Self {
handle: QuicConn {
conn,
send: None,
recv: None,
},
netinfo: NetworkInfo::default(),
netinfo: NetworkInfo {
sent: 0,
received: 0,
peer: Some(addr),
},
})
}

pub async fn connect(&mut self) -> Result<()> {
let (send, recv) = self
.handle
.conn
.open_bi()
.await
.map_err(|e| Error::Quic(QuicError::Connection(e)))?;

self.handle.send = Some(send);
self.handle.recv = Some(recv);

Ok(())
}
}

impl Messenger for QuicProtocol {
Expand Down Expand Up @@ -130,6 +122,20 @@ impl Messenger for QuicProtocol {
Ok(length)
}

async fn aconnect(&mut self) -> Result<()> {
let (send, recv) = self
.handle
.conn
.open_bi()
.await
.map_err(|e| Error::Quic(QuicError::Connection(e)))?;

self.handle.send = Some(send);
self.handle.recv = Some(recv);

Ok(())
}

fn uses_leading_length(&self) -> bool {
true
}
Expand Down
4 changes: 4 additions & 0 deletions src/transport/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ impl Messenger for TcpProtocol {
Ok(0)
}

async fn aconnect(&mut self) -> error::Result<()> {
Ok(())
}

fn send(&mut self, buffer: &[u8]) -> Result<usize> {
self.netinfo.sent = self.handle.write(buffer).map_err(crate::error::Error::Buffer)?;
self.handle.flush().map_err(crate::error::Error::Buffer)?;
Expand Down
4 changes: 4 additions & 0 deletions src/transport/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl Messenger for TlsProtocol {
Ok(0)
}

async fn aconnect(&mut self) -> error::Result<()> {
Ok(())
}

fn send(&mut self, buffer: &[u8]) -> Result<usize> {
self.netinfo.sent = self
.handle
Expand Down
4 changes: 4 additions & 0 deletions src/transport/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Messenger for UdpProtocol {
Ok(0)
}

async fn aconnect(&mut self) -> error::Result<()> {
Ok(())
}

fn send(&mut self, buffer: &[u8]) -> Result<usize> {
self.netinfo.sent = self.handle.send(buffer).map_err(|e| Error::Network(e, Network::Send))?;
debug!("sent {} bytes", self.netinfo.sent);
Expand Down

0 comments on commit 693f109

Please sign in to comment.