SK.gnome.dwarf.utils.dns
Class DNSClient

java.lang.Object
  extended bySK.gnome.dwarf.utils.dns.DNSClient

public class DNSClient
extends java.lang.Object

This is a client for the DNS name lookups.

The name lookups consists of creating a Question instance and passing it to the query(Question, InetAddress, int, boolean, Service) method. Alternatively, the query(String[], Service) or main(String[]) method may be used, too. The returned Message object represents an answer from the remote DNS server.

The following example shows a simple use of the query method:

 Question question = new Question("one.foo.bar", Types.A, class.IN);
 InetAddress server = InetAddress.getByName("ns.internic.net");
 Message message = DNSClient.query(question, server, 60000, false, null);

 List answers = message.getAnswers();
 System.out.println("Found " + answers.size() + " answers:");
 for (Iterator it = answers.iterator(); it.hasNext(); )
   System.out.println(it.next());
 


Field Summary
static int DEFAULT_LOOKUP_TIMEOUT
          The default query timeout in seconds.
 
Constructor Summary
DNSClient()
           
 
Method Summary
static void main(java.lang.String[] args)
          Main static method for performing a DNS query.
static Message query(Question question, java.net.InetAddress server, int timeout, boolean stream, Service logger)
          Performs a DNS query.
static Message query(java.lang.String[] args, Service logger)
          Peforms a DNS query command.
static PTRResource reverseQuery(java.lang.String addr, java.net.InetAddress server, int timeout, boolean stream, Service logger)
          Performs a reverse DNS query.
static void setServer(java.net.InetAddress server)
          Sets the default server for the name lookups.
static void setTimeout(int timeout)
          Sets the default name lookup timeout.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LOOKUP_TIMEOUT

public static final int DEFAULT_LOOKUP_TIMEOUT
The default query timeout in seconds.

Value: 30

See Also:
Constant Field Values
Constructor Detail

DNSClient

public DNSClient()
Method Detail

setServer

public static void setServer(java.net.InetAddress server)
Sets the default server for the name lookups.

The default server is set to "localhost" an may be changed via this method.

Parameters:
server - the DNS server address

setTimeout

public static void setTimeout(int timeout)
Sets the default name lookup timeout.

The defaul value is set to DEFAULT_LOOKUP_TIMEOUT and may be changed via this method.

Parameters:
timeout - the timeout in seconds

query

public static Message query(Question question,
                            java.net.InetAddress server,
                            int timeout,
                            boolean stream,
                            Service logger)
                     throws java.io.IOException
Performs a DNS query.

A new TCP connection will be established for each query if the stream argument is true.

Parameters:
question - the question
server - the DNS server
timeout - the lookup timeout in milliseconds
stream - whether to use the TCP or UDP protocol
logger - the logging service or null
Returns:
the DNS response message
Throws:
java.io.IOException - if an error occured

reverseQuery

public static PTRResource reverseQuery(java.lang.String addr,
                                       java.net.InetAddress server,
                                       int timeout,
                                       boolean stream,
                                       Service logger)
                                throws java.io.IOException
Performs a reverse DNS query.

This method converts a valid IP address to the full specified hostname represented by a PTRResource pointer.

A new TCP connection will be established for each query if the stream argument is true.

Parameters:
addr - the IP address
server - the DNS server
timeout - the lookup timeout in milliseconds
stream - whether to use the TCP or UDP protocol
logger - the logging service or null
Returns:
the PTR resource record or null if not found
Throws:
java.io.IOException - if an error occured

query

public static Message query(java.lang.String[] args,
                            Service logger)
                     throws java.io.IOException
Peforms a DNS query command.

The following arguments may be specified via the string array:

 [-server hostname] [-type type] [-class class] [-timeout seconds] [-stream] hostname
 
 server   - the domain name or IP address of the DNS server
 type     - the resource type (A, CNAME, PTR, MX, ...)
 class    - the protocol family (IN)
 timeout  - the query timeout in seconds
 stream   - whether to use TCP instead of the UDP protocol
 hostname - the query subject
 

This method parses the arguments, converts them to their corresponding Java types and calls the query(Question, InetAddress, int, boolean, Service) method.

Parameters:
args - the query arguments
logger - the loging service or null
Returns:
the DNS message
Throws:
java.io.IOException - if an error occured

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException,
                        ServiceException
Main static method for performing a DNS query.

It directly calls the query(String[], Service) method. The output is redirected to the standard system output.

Parameters:
args - the query arguments
Throws:
java.io.IOException - if an error occured
ServiceException - if an error occured


Copyright (c) 1999-2005, Gnome Ltd. All rights reserved.