net.sf.ivmaidns.dns
Class DNSConnection

java.lang.Object
  extended by net.sf.ivmaidns.dns.DNSConnection

public final class DNSConnection
extends java.lang.Object

Class for DNS TCP connection (client/server-side).

Version:
3.0
Author:
Ivan Maidanski

Field Summary
protected  java.io.BufferedInputStream in
          NOTE: These are input and output streams.
protected  byte[] lenBuf
          NOTE: Buffer for writing message length.
protected static java.net.ServerSocket listener
          NOTE: If listener == null then no listening is performed.
static int MAX_MSG_LEN
          NOTE: The maximum length of DNS message.
protected  byte[] msgBytes
           
protected  int msgLen
           
protected  java.io.OutputStream out
           
static int PORT
          NOTE: Standard 'domain' service TCP/UDP port.
protected  java.net.Socket socket
          NOTE: If socket == null then connection is closed.
 
Constructor Summary
DNSConnection()
          NOTE: socket is closed initially.
 
Method Summary
 void close()
          NOTE: Must be synchronized outside.
static DNSRecord[] decode(byte[] msgBytes)
          NOTE: msgBytes must be !
static byte[] encode(DNSMsgHeader header, DNSRecord[] records)
          NOTE: header must be !
 java.net.InetAddress getInetAddress()
          NOTE: Result !
static void listen()
          NOTE: Start listening (if not already) on DNS port for incoming TCP connections.
 void open(java.net.InetAddress server)
          NOTE: old connection should be closed. server must be !
 void openIncoming()
          NOTE: old connection should be closed.
 byte[] receive(boolean wait)
          NOTE: If !
 void send(byte[] msgBytes)
          NOTE: msgBytes must be !
static void stopListening()
          NOTE: Stop listening on DNS port for incoming TCP connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PORT

public static final int PORT
NOTE: Standard 'domain' service TCP/UDP port.

See Also:
Constant Field Values

MAX_MSG_LEN

public static final int MAX_MSG_LEN
NOTE: The maximum length of DNS message.

Since:
3.0
See Also:
Constant Field Values

listener

protected static java.net.ServerSocket listener
NOTE: If listener == null then no listening is performed. listener is shared among all instances of this class.

Since:
3.0

socket

protected java.net.Socket socket
NOTE: If socket == null then connection is closed.


msgBytes

protected byte[] msgBytes

msgLen

protected int msgLen

in

protected java.io.BufferedInputStream in
NOTE: These are input and output streams.

Since:
3.0

out

protected java.io.OutputStream out

lenBuf

protected final byte[] lenBuf
NOTE: Buffer for writing message length.

Since:
3.0
Constructor Detail

DNSConnection

public DNSConnection()
NOTE: socket is closed initially.

Since:
2.2
Method Detail

listen

public static void listen()
                   throws java.io.IOException
NOTE: Start listening (if not already) on DNS port for incoming TCP connections. If this port is already busy then BindException (subclass of SocketException) is thrown. If SecurityException is caught then SocketException is thrown.

Throws:
java.io.IOException
Since:
3.0

stopListening

public static void stopListening()
NOTE: Stop listening on DNS port for incoming TCP connections.

Since:
3.0

openIncoming

public void openIncoming()
                  throws java.io.IOException
NOTE: old connection should be closed. Wait for any incoming connection and accept it. If listening is not active or if SecurityException is caught then SocketException is thrown. If waiting fails then InterruptedIOException is thrown. Must be synchronized outside.

Throws:
java.io.IOException
Since:
3.0

open

public void open(java.net.InetAddress server)
          throws java.lang.NullPointerException,
                 java.io.IOException
NOTE: old connection should be closed. server must be != null. If server is down or unreachable then NoRouteToHostException (subclass of SocketException) is thrown. If connection is remotely refused then ConnectException (subclass of SocketException) is thrown. If SecurityException is caught then SocketException is thrown. Must be synchronized outside.

Throws:
java.lang.NullPointerException
java.io.IOException
Since:
2.2

getInetAddress

public final java.net.InetAddress getInetAddress()
NOTE: Result != null unless connection is closed.

Since:
2.0

send

public void send(byte[] msgBytes)
          throws java.lang.NullPointerException,
                 java.io.IOException
NOTE: msgBytes must be != null. If msgBytes array is too large then it is truncated. msgBytes array is not changed anyway. Data is flushed. Must be synchronized outside.

Throws:
java.lang.NullPointerException
java.io.IOException

receive

public byte[] receive(boolean wait)
               throws java.io.IOException
NOTE: If !wait and no message received yet then result == null. InterruptedIOException and EOFException may be thrown (only if wait is true). Connection remains valid even if IOException is thrown. Must be synchronized outside.

Throws:
java.io.IOException

close

public void close()
NOTE: Must be synchronized outside.


encode

public static final byte[] encode(DNSMsgHeader header,
                                  DNSRecord[] records)
                           throws java.lang.NullPointerException
NOTE: header must be != null, records must be != null and records[index] != null for any index. records length may be not adequate to header. Names compression is performed (relatively to the name of the first record). records array is not changed anyway. Result != null.

Throws:
java.lang.NullPointerException
Since:
3.0

decode

public static final DNSRecord[] decode(byte[] msgBytes)
                                throws java.lang.NullPointerException
NOTE: msgBytes must be != null. If DNS message header is bad then result == null. Else result != null and result[index] != null for any index (the result length may be less than that declared in the decoded header). msgBytes array is not changed anyway.

Throws:
java.lang.NullPointerException
Since:
3.0