use std::net::SocketAddr;
use crate::{
net::{BufDnsStreamHandle, DnsStreamHandle, NetError, xfer::Protocol},
proto::{op::SerialMessage, rr::Record},
server::ResponseInfo,
zone_handler::MessageResponse,
};
#[async_trait::async_trait]
pub trait ResponseHandler: Clone + Send + Sync + Unpin + 'static {
async fn send_response<'a>(
&mut self,
response: MessageResponse<
'_,
'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
>,
) -> Result<ResponseInfo, NetError>;
}
#[derive(Clone)]
pub struct ResponseHandle {
dst: SocketAddr,
stream_handle: BufDnsStreamHandle,
protocol: Protocol,
}
impl ResponseHandle {
pub fn new(dst: SocketAddr, stream_handle: BufDnsStreamHandle, protocol: Protocol) -> Self {
Self {
dst,
stream_handle,
protocol,
}
}
}
#[async_trait::async_trait]
impl ResponseHandler for ResponseHandle {
async fn send_response<'a>(
&mut self,
response: MessageResponse<
'_,
'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
impl Iterator<Item = &'a Record> + Send + 'a,
>,
) -> Result<ResponseInfo, NetError> {
let (info, buffer) = response.encode(self.protocol)?;
self.stream_handle
.send(SerialMessage::new(buffer, self.dst))?;
Ok(info)
}
}