/// Send a DNS query using UDP transport
///
/// This will construct a query packet, and fire it off to the specified server.
/// The query is sent from the callee thread, but responses are read on a
/// worker thread, and returned to this thread through a channel. Thus this
/// method is thread safe, and can be used from any number of threads in
/// parallell.
Send a DNS query using UDP transport
This will construct a query packet, and fire it off to the specified server.
The query is sent from the callee thread, but responses are read on a
worker thread, and returned to this thread through a channel. Thus this
method is thread safe, and can be used from any number of threads in
parallell.
Send a DNS query using UDP transport This will construct a query packet and fire it off to the specified server . The query is sent from the callee thread but responses are read on a worker thread and returned to this thread through a channel . Thus this method is thread safe and can be used from any number of threads in parallell .
pub async fn send_udp_query(
        &self,
        qname: &str,
        qtype: QueryType,
        server: (&str, u16),
        recursive: bool,
    ) -> Result<DnsPacket> {
        let _ = self.total_sent.fetch_add(1, Ordering::Release);
        // Prepare request
        let mut packet = DnsPacket::new();
        packet.header.id = self.seq.fetch_add(1, Ordering::SeqCst) as u16;
        if packet.header.id + 1 == 0xFFFF {
            self.seq.compare_and_swap(0xFFFF, 0, Ordering::SeqCst);
        }
        packet.header.questions = 1;
        packet.header.recursion_desired = recursive;
        packet
            .questions
            .push(DnsQuestion::new(qname.to_string(), qtype));
        // Send query
        let mut req_buffer = BytePacketBuffer::new();
        packet.write(&mut req_buffer, 512)?;
        self.socket
            .send_to(&req_buffer.buf[0..req_buffer.pos], server)
            .await?;
        // Read data into a buffer
        let mut res_buffer = BytePacketBuffer::new();
        let (size, _src) = self.socket.recv_from(&mut res_buffer.buf).await?;
        assert!(res_buffer.buf.len() > size);
        // Construct a DnsPacket from buffer, skipping the packet if parsing
        // failed
        // Wait for response
        if let Ok(res) = DnsPacket::from_buffer(&mut res_buffer) {
            Ok(res)
        } else {
            // Otherwise, fail
            let _ = self.total_failed.fetch_add(1, Ordering::Release);
            Err(Error::new(ErrorKind::InvalidInput, "Lookup failed"))
        }
    }
pub async fn send_udp_query ( & self , qname : & str , qtype : QueryType , server : ( & str , u16 ) , recursive : bool , ) -> Result < DnsPacket > { let _ = self . total_sent . fetch_add ( 1 , Ordering :: Release ) ; // Prepare request let mut packet = DnsPacket :: new ( ) ; packet . header . id = self . seq . fetch_add ( 1 , Ordering :: SeqCst ) as u16 ; if packet . header . id + 1 == 0xFFFF { self . seq . compare_and_swap ( 0xFFFF , 0 , Ordering :: SeqCst ) ; } packet . header . questions = 1 ; packet . header . recursion_desired = recursive ; packet . questions . push ( DnsQuestion :: new ( qname . to_string ( ) , qtype ) ) ; // Send query let mut req_buffer = BytePacketBuffer :: new ( ) ; packet . write ( & mut req_buffer , 512 ) ? ; self . socket . send_to ( & req_buffer . buf [ 0 .. req_buffer . pos ] , server ) . await ? ; // Read data into a buffer let mut res_buffer = BytePacketBuffer :: new ( ) ; let ( size , _src ) = self . socket . recv_from ( & mut res_buffer . buf ) . await ? ; assert ! ( res_buffer buf len ( ) size ) ; // Construct a DnsPacket from buffer, skipping the packet if parsing // failed // Wait for response if let Ok ( res ) = DnsPacket :: from_buffer ( & mut res_buffer ) { Ok ( res ) } else { // Otherwise, fail let _ = self . total_failed . fetch_add ( 1 , Ordering :: Release ) ; Err ( Error :: new ( ErrorKind :: InvalidInput , " " ) ) } }
Send a DNS query using UDP transport
This will construct a query packet, and fire it off to the specified server.
Send a DNS query using UDP transport This will construct a query packet and fire it off to the specified server .
&self
qname
&str
qtype
QueryType
server
(&str, u16)
recursive
bool