Class DNSAddressRequest


  • public class DNSAddressRequest
    extends Object
    
     The DNSAddressRequest holds a DNS request to lookup the IP address of a host -
     provides for transmitting and receiving the response for this lookup.
    
     NOTES: A DNS request and response has the following fileds header questions
     answers authorities additional information
    
     The header has the following format: id - unique id sent by the client and
     returned by the server in its response 16 bits of flags -
     Query(0)/response(1) flag opcode - that has type of query AA - set if the
     response is an authoritative answer TC - set if response is truncated RD -
     set if recursion is desired RA - set if recursion is available Z - reserved
     bits RCODE - response code
    
     This class checks only for the received response to have the answer(which
     will hold the IP address) - ignores the authorities and additional info
    
     
    Author:
    Sowmya
    • Field Detail

      • CLASS_IN

        public static final int CLASS_IN

        Defines the class internet in the domain name system.

        See Also:
        Constant Field Values
      • SHIFT_QUERY

        public static final int SHIFT_QUERY

        The offset of the query bit in the header.

        See Also:
        Constant Field Values
      • SHIFT_OPCODE

        public static final int SHIFT_OPCODE

        The offset of the opcode bits in the header.

        See Also:
        Constant Field Values
      • SHIFT_AUTHORITATIVE

        public static final int SHIFT_AUTHORITATIVE

        The offset of the authoritative bit in the header.

        See Also:
        Constant Field Values
      • SHIFT_TRUNCATED

        public static final int SHIFT_TRUNCATED

        The offset of the truncated bit in the header.

        See Also:
        Constant Field Values
      • SHIFT_RECURSE_PLEASE

        public static final int SHIFT_RECURSE_PLEASE

        The offset of the recurse req bit in the header.

        See Also:
        Constant Field Values
      • SHIFT_RECURSE_AVAILABLE

        public static final int SHIFT_RECURSE_AVAILABLE

        The offset of the requrse avail bit in the header.

        See Also:
        Constant Field Values
      • SHIFT_RESERVED

        public static final int SHIFT_RESERVED

        The offset of the reserved bits in the header.

        See Also:
        Constant Field Values
      • SHIFT_RESPONSE_CODE

        public static final int SHIFT_RESPONSE_CODE

        The offset of the response code bits in the header.

        See Also:
        Constant Field Values
      • OPCODE_QUERY

        public static final int OPCODE_QUERY

        The op code for a query in the header.

        See Also:
        Constant Field Values
      • m_reqHost

        public String m_reqHost

        The host to request information from. This would be the nameserver if it supports DNS.

      • m_reqID

        public int m_reqID

        The id used to seralize the request. This allows the client (us) and the server (host) to match exchanges.

      • m_authoritative

        public boolean m_authoritative

        True if the answer is authoratitve.

      • m_truncated

        public boolean m_truncated

        True if the message is truncated.

      • m_recursive

        public boolean m_recursive

        True if the message is recursive.

    • Constructor Detail

      • DNSAddressRequest

        public DNSAddressRequest​(String host)

        Constructs a DNSAddressRequest for ths hostname passed. The host string that is passed to the address string should be a hostname in "x.y.z" where x, y, and z are strings. This is not suppose to be a dotted decimal address.

        Parameters:
        host - hostname for which address is to be constructed
    • Method Detail

      • buildRequest

        public byte[] buildRequest()
                            throws IOException

        Builds the address request.

        Returns:
        A byte array containing the request.
        Throws:
        IOException - if any.
      • receiveResponse

        public void receiveResponse​(byte[] data,
                                    int length)
                             throws IOException

        Extracts the response from the bytearray.

        Parameters:
        data - The byte array containing the response.
        length - The length of the byte array.
        Throws:
        IOException - Thrown if there is an error while reading the recieved packet
        IOException - if any.
      • verifyResponse

        public void verifyResponse​(byte[] data,
                                   int length)
                            throws IOException

        This method only goes so far as to decode the flags in the response byte array to verify that a DNS server sent the response.

        NOTE: This is really a hack to get around the fact that the receiveResponse() method is not robust enough to handle all possible DNS server responses.

        Parameters:
        data - The byte array containing the response.
        length - The length of the byte array.
        Throws:
        IOException - Thrown if there is an error while reading the recieved packet
        IOException - if any.
      • getAnswers

        public List<DNSAddressRR> getAnswers()

        Return an enumeration of the recieved answers.

        Returns:
        The list of received answers.
      • getRequestID

        public int getRequestID()

        The request id for this particular instance.

        Returns:
        a int.
      • getHost

        public String getHost()

        The hostname that will be request from the DNS box.

        Returns:
        a String object.
      • isTruncated

        public boolean isTruncated()

        Returns true if the answer is truncated.

        Returns:
        a boolean.
      • isRecursive

        public boolean isRecursive()

        Returns true if the answer is recursive.

        Returns:
        a boolean.
      • isAuthoritative

        public boolean isAuthoritative()

        Returns true if the answer is authoritative.

        Returns:
        a boolean.
      • codeName

        public static String codeName​(int code)

        Returns the code string for the error code recieved.

        Parameters:
        code - The error code.
        Returns:
        The error string corresponding to the error code