com.lavantech.net.dns
Class DNSLookup

java.lang.Object
  extended bycom.lavantech.net.dns.DNSLookup

public class DNSLookup
extends java.lang.Object

DNSLookup class looks up a record on a given DNS Server. If you want to do simple DNS lookup, take a look at SimpleDNSLookup class. The lookup is performed when the class is constructed. Here is an example where the class is used to get all IP Address (A Record) for www.hotmail.com.

 
       // QTYPE_A asks for the ARecord(Address Record).
       // QCLASS_IN is for Internet class.
       // null is passed for server name. If null is passed the name server setting of the system is used. 
       DNSLookup dnsLookup = new DNSLookup("www.hotmail.com", DNSLookup.QTYPE_A,
              DNSLookup.QCLASS_IN, 1000, null);

       // Get all Address Records.
       ResourceRecord[] ansRecords = dnsLookup.getAnswerRecords();

       // Print all the Address Records.
       for(int i=0; i < ansRecords.length; i++)
       {
          if(ansRecords[i] instanceof ARecord)
              System.out.println(((ARecord)ansRecords[i]).getInetAddress().getHostAddress());
       }
 
 

See Also:
SimpleDNSLookup

Field Summary
static int INTERNAL_BUFFER
          Size of internal buffer used to field the response from the DNS server.
static int QCLASS_ANY
          Query class for any scheme of record system.
static int QCLASS_CHAOS
          Query class for Chaos scheme of record system.
static int QCLASS_HESIOD
          Query class for Hesiod scheme of record system.
static int QCLASS_IN
          Query class for Internet scheme of record system.
static int QTYPE_A
          Query type value for Address Record.
static int QTYPE_A6
          Query type value for IPV6 Address Record(Newer version of A6).
static int QTYPE_AAAA
          Query type value for IPv6 Address Record.
static int QTYPE_ANY
          Query type value for All Records.
static int QTYPE_CNAME
          Query type value for Canonical Name Record.
static int QTYPE_MX
          Query type value for Mail Exchange Record.
static int QTYPE_NS
          Query type value for Name Server Record.
static int QTYPE_PTR
          Query type value for Pointer Record.
static int QTYPE_SOA
          Query type value for Start Of Authority Record.
static int QTYPE_TXT
          Query type value for Text Record.
static int QTYPE_WKS
          Query type value for Well Known Services Record.
 
Constructor Summary
DNSLookup(java.lang.String query, int qtype, int qclass, int timeout, java.lang.String server)
          DNSLookup Constructor looks up a requested record on a given server.
 
Method Summary
 A6Record[] getA6Records()
          Returns all A6 records received in the response.
 AAAARecord[] getAAAARecords()
          Returns all AAAA records received in the response.
 ResourceRecord[] getAdditionalRecords()
          Returns Resource Records extracted from the Additional Section of the response.
 ResourceRecord[] getAnswerRecords()
          Returns Resource Records extracted from the Answer Section of the response.
 ARecord[] getARecords()
          Returns all A records received in the response.
 ResourceRecord[] getAuthorityRecords()
          Returns Resource Records extracted from the Authority Section of the response.
 CNAMERecord[] getCNAMERecords()
          Returns all CNAME records received in the response.
 MXRecord[] getMXRecords()
          Returns all MX records received in the response.
 NSRecord[] getNSRecords()
          Returns all NS records received in the response.
 PTRRecord[] getPTRRecords()
          Returns all PTR records received in the response.
 QuestionRecord getQuestionRecord()
          Returns the Question Record that is sent back in the response.
 SOARecord[] getSOARecords()
          Returns all SOA records received in the response.
 TXTRecord[] getTXTRecords()
          Returns all TXT records received in the response.
 WKSRecord[] getWKSRecords()
          Returns all WKS records received in the response.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

QTYPE_A

public static final int QTYPE_A
Query type value for Address Record. Address record provides the IP address for a hostname.

See Also:
Constant Field Values

QTYPE_NS

public static final int QTYPE_NS
Query type value for Name Server Record. Name Server records provides the Authoritative Name Server for a domain.

See Also:
Constant Field Values

QTYPE_CNAME

public static final int QTYPE_CNAME
Query type value for Canonical Name Record. Alias Hostname can be created for some other hostname in possibly a different domain. The CNAME record provides the real hostname for a given hostname.

See Also:
Constant Field Values

QTYPE_SOA

public static final int QTYPE_SOA
Query type value for Start Of Authority Record. SOA Record contains information about a zone.

See Also:
Constant Field Values

QTYPE_WKS

public static final int QTYPE_WKS
Query type value for Well Known Services Record. WKS record provides information about services that are available on a server. The services are HTTP, POP, IMAP... The record provides a list of ports on which the services are available.

See Also:
Constant Field Values

QTYPE_PTR

public static final int QTYPE_PTR
Query type value for Pointer Record. PTR Record provide a mapping from IP address back to host name.

See Also:
Constant Field Values

QTYPE_MX

public static final int QTYPE_MX
Query type value for Mail Exchange Record. MX Record indicates servers that are responsible for handling mail for the domain.

See Also:
Constant Field Values

QTYPE_TXT

public static final int QTYPE_TXT
Query type value for Text Record. Text Record has text information. TXT record is sometimes used for SPF(Sender Policy Framework) record.

See Also:
Constant Field Values

QTYPE_AAAA

public static final int QTYPE_AAAA
Query type value for IPv6 Address Record. AAAA Record provides the IPv6 address for a hostname.

See Also:
Constant Field Values

QTYPE_A6

public static final int QTYPE_A6
Query type value for IPV6 Address Record(Newer version of A6). AAAA is still widely used.

See Also:
Constant Field Values

QTYPE_ANY

public static final int QTYPE_ANY
Query type value for All Records. This query will return all available records for a host/domain.

See Also:
Constant Field Values

QCLASS_IN

public static final int QCLASS_IN
Query class for Internet scheme of record system. This is the most widely used class

See Also:
Constant Field Values

QCLASS_CHAOS

public static final int QCLASS_CHAOS
Query class for Chaos scheme of record system. This scheme is almost extinct.

See Also:
Constant Field Values

QCLASS_HESIOD

public static final int QCLASS_HESIOD
Query class for Hesiod scheme of record system. This scheme is only used at M.I.T.

See Also:
Constant Field Values

QCLASS_ANY

public static final int QCLASS_ANY
Query class for any scheme of record system.

See Also:
Constant Field Values

INTERNAL_BUFFER

public static int INTERNAL_BUFFER
Size of internal buffer used to field the response from the DNS server. If lot of records are expected in the response, this number should be increased. The default value is 5024.

Constructor Detail

DNSLookup

public DNSLookup(java.lang.String query,
                 int qtype,
                 int qclass,
                 int timeout,
                 java.lang.String server)
          throws java.lang.Exception
DNSLookup Constructor looks up a requested record on a given server. The lookup is done in the constructor itself. If there is any error, an exception is throw.

Parameters:
query - The string indicating the hostname/domainname/ipaddress for which the record lookup will be done.
qtype - The Record type that needs to be looked up. QTYPE_A is for address, QTYPE_NS if for Name server, QTYPE_MX for Mail Exchange....
qclass - The Record scheme on which the lookup should be performed. The most widely used scheme is Internet ( QCLASS_IN ).
timeout - The Timeout in milliseconds to wait for the response from the server. If reponse is not recived with the timeout period, a SocketTimeoutException is thrown.
server - The Domain Name Server on which the lookup has to be performed. This could be a hostname or a IP address. If null can be passed, the DNS setting from the system would be used. If a system property dns.server is set, this would be used. The system property can be set while starting the Java VM with the option -Ddns.server="DNS name"
Throws:
java.net.SocketTimeoutException - If a reponse is not received within the timeout period.
java.lang.Exception - If the Name Server can't be determined or any other internal problem occurs.
Method Detail

getQuestionRecord

public QuestionRecord getQuestionRecord()
Returns the Question Record that is sent back in the response. The QuestionRecord has the original query that caused the response.


getAnswerRecords

public ResourceRecord[] getAnswerRecords()
Returns Resource Records extracted from the Answer Section of the response. The return array has instances of subclasses of Resource Records. Each element in the array can be checked for the type and typecast.

Returns:
An array of resource records extracted from Answer section of the response.

getAuthorityRecords

public ResourceRecord[] getAuthorityRecords()
Returns Resource Records extracted from the Authority Section of the response. The return array has instances of subclasses of Resource Records. Each element in the array can be checked for the type and typecast.

Returns:
An array of resource records extracted from Authority section of the response.

getAdditionalRecords

public ResourceRecord[] getAdditionalRecords()
Returns Resource Records extracted from the Additional Section of the response. The return array has instances of subclasses of Resource Records. Each element in the array can be checked for the type and typecast.

Returns:
An array of resource records extracted from Additional section of the response.

getA6Records

public A6Record[] getA6Records()
Returns all A6 records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one A6 record for a hostname.

Returns:
An array of A6 records.

getAAAARecords

public AAAARecord[] getAAAARecords()
Returns all AAAA records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one AAAA record for a hostname.

Returns:
An array of AAAA records.

getARecords

public ARecord[] getARecords()
Returns all A records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one A record for a hostname.

Returns:
An array of A records.

getCNAMERecords

public CNAMERecord[] getCNAMERecords()
Returns all CNAME records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one CNAME record for a hostname.

Returns:
An array of CNAME records.

getMXRecords

public MXRecord[] getMXRecords()
Returns all MX records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one MX record for a hostname.

Returns:
An array of MX records.

getNSRecords

public NSRecord[] getNSRecords()
Returns all NS records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one NS record for a hostname.

Returns:
An array of NS records.

getPTRRecords

public PTRRecord[] getPTRRecords()
Returns all PTR records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response. There can be more than one PTR record for a hostname.

Returns:
An array of PTR records.

getSOARecords

public SOARecord[] getSOARecords()
Returns all SOA records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response.

Returns:
An array of SOA records.

getWKSRecords

public WKSRecord[] getWKSRecords()
Returns all WKS records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response.

Returns:
An array of WKS records.

getTXTRecords

public TXTRecord[] getTXTRecords()
Returns all TXT records received in the response. These records are extracted from all three sections(Answer, Authority and Additional) of the response.

Returns:
An array of TXT records.