DNS
Class DNS

java.lang.Object
  extended bysimnet.Resolver
      extended byDNS.DNS

public class DNS
extends Resolver

A simple DNS resolver. Assuming that all domain names are lower case (or, treat domain names as case sensitive). No support for retransmissions of requests or wildcards. No support for multiple addresses for one host.


Nested Class Summary
(package private)  class DNS.CacheCleaner
          TimerTask to remove stale items from the cache.
(package private)  class DNS.PendingRequest
          An object for doRemoteQuery to wait for, and to contain the response packet.
(package private)  class DNS.Processor
          Thread that handles the processing of all requests/responses.
(package private)  class DNS.RecursiveRequestProcessor
          A thread that processes a recursive request and returns a response to the sender.
 
Field Summary
(package private)  java.util.Hashtable cache
          Cache of RRGroups returned by queries to remote name servers, indexed by RRGroup name.
protected  int CACHE_TTL
          Global Parameter -- how long RRs may be cached before they are considered invalid.
protected  java.util.Timer cacheCleaner
          Thread to remove expired items from our cache.
protected  boolean client
          Whether or not this resolver is a stub (client) or local (!client) resolver
protected  int nameserver
          If we are a client, then this is our nameserver
protected  java.util.Hashtable pending
          Used to keep track of pending requests.
protected  DNS.Processor processor
          Our primary thread to handle requests/responses.
protected  java.util.Random random
          Used to generate request id's, so they (probably) will be unique.
protected  java.lang.String resolverName
          the name of our resolver (not DNS, but node, mostly used in DNSSEC)
protected  int ROOT
          Global Parameter -- ip address of root node.
(package private)  RRGroup rootARRG
          Address of the ROOT name server.
protected  java.lang.String ROOTNAME
          Global Parameter -- name of root node.
(package private)  RRGroup rootNSRRG
          Information for ROOT name server.
protected  int RR_DURATION
          Global Parameter -- how long RRs may last until they are considered invalid.
protected  DatagramSocket sock
          Used to communicate DNS requests/responses
protected  int TIMEOUT
          Global Parameter -- amount of time that should pass before we consider a request failed.
(package private)  java.util.Hashtable zones
          Holds information about our zone(s) -- for servers only.
 
Fields inherited from class simnet.Resolver
node, sim
 
Constructor Summary
DNS()
           
 
Method Summary
 boolean authoritativeFor(java.lang.String name)
          Check if we are authoritative for a certain name.
protected  void cache(RRGroup rrg)
          Put an RRGroup in the cache.
protected  RRGroup checkCache(java.lang.String name, java.lang.String type)
          Check the cache for an RRGroup that matches this name and type.
 java.lang.String chop(java.lang.String name)
          Utility method to remove the first part of a period-delimited name.
protected  void clientPreProcessRequest(DNSRequest req)
          Method where stub resolver can do any processing that it needs to before sending a request.
protected  void createRootRRGs()
          Fill out our pointers to the ROOT of the DNS
 void customInit(java.lang.String[] config)
          Loads information for this resolver.
 void dds()
          Wrapper method to display the DNS state of a resolver.
 java.util.LinkedList doIterativeQuery(java.lang.String name, java.lang.String type)
          Method that checks our zones database and the cache for information pertaining to to the specified name.
 java.util.LinkedList doRecursiveQuery(java.lang.String name, java.lang.String type)
          Method to recursively search for the answer to a query.
protected  java.util.LinkedList doRemoteQuery(java.lang.String name, java.lang.String type, int server, boolean recurse)
          Query a remote name server, get the results and cache and return them.
 void dump_dns_state()
          Prints the state (zone database, cache and pending requests).
protected  java.util.LinkedList getBestNS(java.lang.String name, boolean includeCache)
          Method to search all name servers (in our zones and cache) to get the one that is 'most' authoratative for the specified name.
 int getHostByName(java.lang.String name)
          Performs a lookup of the specified name.
protected  java.util.LinkedList getNameServers(boolean includeCache)
          Method that finds all Name servers that we know about.
 java.lang.String getNSByName(java.lang.String name)
           
protected  RRGroup getRRGroup(java.lang.String name, java.lang.String type)
          Method to search all of our zones for an RRGroup holding an RR for a certain name/type.
 boolean hasFinalAnswer(java.util.LinkedList rrgs, java.lang.String name, java.lang.String type)
          Check to see if the first item in the linked list is the answer we are looking for -- an NX or A record that has the correct name.
protected  boolean loadRR(java.lang.String zone, java.lang.String[] tokens)
          Loads the tokenized record for the specified zone into the database.
 void nslookup(java.lang.String name)
          Method that can be called from the UI to lookup an ip address.
protected  void preProcessRequest(DNSRequest req, int ip)
          Method where local resolver can do any processing that it needs to on receiving a request.
protected  void preProcessResponse(DNSResponse resp, int ip)
          Method where local resolver can do any processing that it needs to on receiving a response.
protected  boolean verify(DNSResponse resp)
           
protected  boolean verify(RRGroup rrg)
          Determine whether this RRGroup can be used.
 boolean zoneEndsWith(java.lang.String name, java.lang.String zoneName)
           
 
Methods inherited from class simnet.Resolver
finalize, initialize
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

client

protected boolean client
Whether or not this resolver is a stub (client) or local (!client) resolver


nameserver

protected int nameserver
If we are a client, then this is our nameserver


resolverName

protected java.lang.String resolverName
the name of our resolver (not DNS, but node, mostly used in DNSSEC)


random

protected java.util.Random random
Used to generate request id's, so they (probably) will be unique.


sock

protected DatagramSocket sock
Used to communicate DNS requests/responses


pending

protected java.util.Hashtable pending
Used to keep track of pending requests. Indexed by request ID, holds Objects of type DNS.PendingRequest.


processor

protected DNS.Processor processor
Our primary thread to handle requests/responses.


cacheCleaner

protected java.util.Timer cacheCleaner
Thread to remove expired items from our cache. (Only for local resolvers)


zones

java.util.Hashtable zones
Holds information about our zone(s) -- for servers only. Objects of type ZoneData are indexed by zone name. Each ZoneData object holds RRGroups, which in turn hold RRs. (only used by local resolvers)


cache

java.util.Hashtable cache
Cache of RRGroups returned by queries to remote name servers, indexed by RRGroup name. (only used by local resolvers)


rootNSRRG

RRGroup rootNSRRG
Information for ROOT name server. All name servers may use this as a fall back when they cannot produce any information about a request.


rootARRG

RRGroup rootARRG
Address of the ROOT name server. All name servers may use this as a fall back when they cannot produce any information about a request.


CACHE_TTL

protected int CACHE_TTL
Global Parameter -- how long RRs may be cached before they are considered invalid.


RR_DURATION

protected int RR_DURATION
Global Parameter -- how long RRs may last until they are considered invalid.


ROOT

protected int ROOT
Global Parameter -- ip address of root node.


ROOTNAME

protected java.lang.String ROOTNAME
Global Parameter -- name of root node.


TIMEOUT

protected int TIMEOUT
Global Parameter -- amount of time that should pass before we consider a request failed.

Constructor Detail

DNS

public DNS()
Method Detail

customInit

public void customInit(java.lang.String[] config)
                throws java.lang.Exception
Loads information for this resolver. This is where a resolver can do any resolver-specific initialization it requires.

Specified by:
customInit in class Resolver
Parameters:
config - Parsed array that represents resource records and server information.
Throws:
java.lang.Exception - If an entry of config cannot be processed correctly

loadRR

protected boolean loadRR(java.lang.String zone,
                         java.lang.String[] tokens)
                  throws java.lang.Exception
Loads the tokenized record for the specified zone into the database. Returns if the record was valid.

Parameters:
zone - The zone this record is in
tokens - Parsed string that specifies an RR
Returns:
whether or not the tokens could correspond to a valid RR
Throws:
java.lang.Exception - If the RR isn't defined correctly

createRootRRGs

protected void createRootRRGs()
Fill out our pointers to the ROOT of the DNS


doIterativeQuery

public java.util.LinkedList doIterativeQuery(java.lang.String name,
                                             java.lang.String type)
Method that checks our zones database and the cache for information pertaining to to the specified name.

Parameters:
name - The name of the node we are looking up.
Returns:
A LinkedList containing pertinent RRGroups. If we found an RRGroup containing an A or NX RR, it will be the first RRGroup in the list. If we have an NS record, it will be the first in the list and will be followed by an A record indicating the IP address of the NS.

doRecursiveQuery

public java.util.LinkedList doRecursiveQuery(java.lang.String name,
                                             java.lang.String type)
                                      throws DNSException
Method to recursively search for the answer to a query. Unlike doIterativeQuery(), does not return until it has received an A or NX record.

Parameters:
name - The name of the node to query
Returns:
a LinkedList of pertinant records, starting with the most useful (either an A or NX). The second record is the NS that gave the response, the third is the A for that NS, etc..
Throws:
DNSException - if verification of RRGroups fail. This is only if they are expired for regular DNS

doRemoteQuery

protected java.util.LinkedList doRemoteQuery(java.lang.String name,
                                             java.lang.String type,
                                             int server,
                                             boolean recurse)
                                      throws DNSException
Query a remote name server, get the results and cache and return them.

Parameters:
name - The name to lookup
server - The name server to query
recurse - Whether or not this should be recurive. Clients should set this to true, servers should not.
Returns:
The responses from the remote host
Throws:
DNSException - When verification of results fail.

cache

protected void cache(RRGroup rrg)
Put an RRGroup in the cache.

Parameters:
rrg - The RRGroup to cache.

checkCache

protected RRGroup checkCache(java.lang.String name,
                             java.lang.String type)
Check the cache for an RRGroup that matches this name and type.

Parameters:
name - the name the RRGroup should match
type - the type of the RR that this RRGroup should hold
Returns:
the specific RRGroup, null if there in no match

verify

protected boolean verify(RRGroup rrg)
Determine whether this RRGroup can be used. This method should be over-ridden to verify signatures, right now it just checks that no Resource Record has expired (it never should be).

Parameters:
rrg - The RRGroup to verify
Returns:
whether or not this RRGroup should be trusted

verify

protected boolean verify(DNSResponse resp)

getBestNS

protected java.util.LinkedList getBestNS(java.lang.String name,
                                         boolean includeCache)
Method to search all name servers (in our zones and cache) to get the one that is 'most' authoratative for the specified name.

Parameters:
name - the name we are querying
includeCache - whether or not we should search our cache too
Returns:
a 2-RRGroup linked list holding the NS that we feel is most likely responsible for the queried node and a RRGroup containing an A record for the NS.

zoneEndsWith

public boolean zoneEndsWith(java.lang.String name,
                            java.lang.String zoneName)

getNameServers

protected java.util.LinkedList getNameServers(boolean includeCache)
Method that finds all Name servers that we know about. Checks our zones and our cache.

Parameters:
includeCache - whether or not to check the cache
Returns:
a Linked list of alternating seta of RRGroups, even entries (0,2,4...) are RRGroups that contain NS RRs, odd entries contain A RRs describing the NS

getRRGroup

protected RRGroup getRRGroup(java.lang.String name,
                             java.lang.String type)
Method to search all of our zones for an RRGroup holding an RR for a certain name/type.

Parameters:
name - The name we want to search.
Returns:
the RRGroup holding the corresponding A record, null if we can't find one.

authoritativeFor

public boolean authoritativeFor(java.lang.String name)
Check if we are authoritative for a certain name.

Parameters:
name - The name to check
Returns:
whether or not we are authoratative for this name

nslookup

public void nslookup(java.lang.String name)
Method that can be called from the UI to lookup an ip address.

Parameters:
name - The name we are looking up

getNSByName

public java.lang.String getNSByName(java.lang.String name)

getHostByName

public int getHostByName(java.lang.String name)
Performs a lookup of the specified name.

Specified by:
getHostByName in class Resolver
Parameters:
name - The name we are querying
Returns:
This ip address of the name, -1 if we couldn't find an answer.

hasFinalAnswer

public boolean hasFinalAnswer(java.util.LinkedList rrgs,
                              java.lang.String name,
                              java.lang.String type)
Check to see if the first item in the linked list is the answer we are looking for -- an NX or A record that has the correct name.

Parameters:
rrgs - list or RRGroups that we are checking
name - the name we are looking for
Returns:
whether or not this linked list contains an answer that we can use (A or NX)

preProcessResponse

protected void preProcessResponse(DNSResponse resp,
                                  int ip)
Method where local resolver can do any processing that it needs to on receiving a response.

Parameters:
resp - The response we received.
ip - The ip this response will be sent to.

preProcessRequest

protected void preProcessRequest(DNSRequest req,
                                 int ip)
Method where local resolver can do any processing that it needs to on receiving a request.

Parameters:
req - The request we received.

clientPreProcessRequest

protected void clientPreProcessRequest(DNSRequest req)
Method where stub resolver can do any processing that it needs to before sending a request.

Parameters:
req - The request we will send.

chop

public java.lang.String chop(java.lang.String name)
Utility method to remove the first part of a period-delimited name.


dds

public void dds()
Wrapper method to display the DNS state of a resolver. (got sick of typing "dump_dns_state")


dump_dns_state

public void dump_dns_state()
Prints the state (zone database, cache and pending requests).