hickory_server/server/
response_handler.rs1use std::net::SocketAddr;
9
10use crate::{
11 net::{BufDnsStreamHandle, DnsStreamHandle, NetError, xfer::Protocol},
12 proto::{op::SerialMessage, rr::Record},
13 server::ResponseInfo,
14 zone_handler::MessageResponse,
15};
16
17#[async_trait::async_trait]
19pub trait ResponseHandler: Clone + Send + Sync + Unpin + 'static {
20 async fn send_response<'a>(
25 &mut self,
26 response: MessageResponse<
27 '_,
28 'a,
29 impl Iterator<Item = &'a Record> + Send + 'a,
30 impl Iterator<Item = &'a Record> + Send + 'a,
31 impl Iterator<Item = &'a Record> + Send + 'a,
32 impl Iterator<Item = &'a Record> + Send + 'a,
33 >,
34 ) -> Result<ResponseInfo, NetError>;
35}
36
37#[derive(Clone)]
40pub struct ResponseHandle {
41 dst: SocketAddr,
42 stream_handle: BufDnsStreamHandle,
43 protocol: Protocol,
44}
45
46impl ResponseHandle {
47 pub fn new(dst: SocketAddr, stream_handle: BufDnsStreamHandle, protocol: Protocol) -> Self {
49 Self {
50 dst,
51 stream_handle,
52 protocol,
53 }
54 }
55}
56
57#[async_trait::async_trait]
58impl ResponseHandler for ResponseHandle {
59 async fn send_response<'a>(
61 &mut self,
62 response: MessageResponse<
63 '_,
64 'a,
65 impl Iterator<Item = &'a Record> + Send + 'a,
66 impl Iterator<Item = &'a Record> + Send + 'a,
67 impl Iterator<Item = &'a Record> + Send + 'a,
68 impl Iterator<Item = &'a Record> + Send + 'a,
69 >,
70 ) -> Result<ResponseInfo, NetError> {
71 let (info, buffer) = response.encode(self.protocol)?;
72 self.stream_handle
73 .send(SerialMessage::new(buffer, self.dst))?;
74
75 Ok(info)
76 }
77}