doh_client/
run.rs

1use crate::config::Config;
2use crate::error::Result as DohResult;
3use crate::handler::request_handler;
4use bytes::Bytes;
5use dns_message_parser::MAXIMUM_DNS_PACKET_SIZE;
6use std::sync::Arc;
7use tokio::spawn;
8
9/// Run the `doh-client` with a specific configuration.
10pub async fn run(config: Config) -> DohResult<()> {
11    let (recv, context) = config.into().await?;
12
13    let context = Arc::new(context);
14
15    let mut buffer: [u8; MAXIMUM_DNS_PACKET_SIZE] = [0; MAXIMUM_DNS_PACKET_SIZE];
16    loop {
17        let (n, addr) = recv.recv_from(&mut buffer[..]).await?;
18        let msg = Bytes::copy_from_slice(&buffer[..n]);
19        debug!("Receive UDP packet: {:?}", msg);
20        let c = context.clone();
21        spawn(async move {
22            if let Err(e) = request_handler(msg, addr, c.as_ref()).await {
23                error!("Could not handle request: {}", e);
24            }
25        });
26    }
27}