001: package CustomDNS;
002:
003: import java.lang.Exception;
004: import java.io.IOException;
005: import java.net.InetAddress;
006: import net.espeak.infra.cci.exception.*;
007: import net.espeak.jesi.*;
008:
009: import ZoneAuthorityIntf;
010: import CustomDNS.VirtualDNS.VirtualDNS;
011:
012: import org.xbill.DNS.*;
013:
014: // XXX - The magic number 53 is the default DNS port. This should be a
015: // constant somewhere.
016:
017: /*************************************************************************
018: * A DNS server which looks up names using e-speak.
019: *************************************************************************
020: */
021:
022: public class ESpeakDNS extends VirtualDNS {
023:
024: // Instance variables.
025: ZoneAuthorityIntf authority;
026:
027: /*********************************************************************
028: * Create a new DNS server.
029: *********************************************************************
030: */
031:
032: public ESpeakDNS(String zonefile, short port,
033: ZoneAuthorityIntf authority) throws IOException {
034: super (zonefile, port);
035: this .authority = authority;
036: }
037:
038: /*********************************************************************
039: * Create a new DNS server.
040: *********************************************************************
041: */
042:
043: public ESpeakDNS(String zonefile, ZoneAuthorityIntf authority)
044: throws IOException {
045: this (zonefile, (short) 53, authority);
046: }
047:
048: /*********************************************************************
049: * Respond to a DNS query using data from the e-speak core.
050: *********************************************************************
051: * @see CustomDNS.VirtualDNS.VirtualDNS#findDynamicData
052: */
053:
054: protected SetResponse findDynamicData(Name zoneName,
055: Name queryName, short queryType) {
056: try {
057: // Get our hostname and remove the trailing period.
058: String namestr = queryName.toString();
059: namestr = namestr.substring(0, namestr.length() - 1);
060: namestr = namestr.toLowerCase();
061: String address = authority.getAddressForHost(namestr);
062:
063: // Create our response.
064: if (address != null) {
065: InetAddress inetaddr = InetAddress.getByName(address);
066: Record r = new ARecord(queryName, DClass.IN, 60,
067: inetaddr);
068: RRset rrset = new RRset();
069: rrset.addRR(r);
070: SetResponse response = new SetResponse(
071: SetResponse.SUCCESSFUL);
072: response.addRRset(rrset);
073: return response;
074: } else {
075: return new SetResponse(SetResponse.NXDOMAIN);
076: }
077: } catch (Exception e) {
078: // XXX - Do something more intelligent here.
079: System.err
080: .println("Dynamic lookup failed: " + e.toString());
081: return null;
082: }
083: }
084:
085: /*********************************************************************
086: * Create a new DNS server and start it running.
087: *********************************************************************
088: * @param args Our command-line arguments. Pass an espeak connection
089: * properties file and customdns.prop.
090: * @see CustomDNS.Configuration
091: */
092:
093: public static void main(String[] args) {
094: try {
095: // Parse our command line arguments, and get the configuration
096: // information we'll be needing.
097: String appname = "CustomDNS.ESpeakDNS";
098: Configuration config = Configuration.parseArguments(
099: appname, args);
100: String zone = config.getProperty("customdns.dns.zone");
101: String zonefile = config
102: .getProperty("customdns.dns.zonefile");
103: String connfile = config.getConnectionFile();
104: String rawport = config.getProperty("customdns.dns.port",
105: "53");
106: short port = Short.parseShort(rawport);
107:
108: // Connect to e-speak, and find our zone authority.
109: ESConnection core = new ESConnection(connfile);
110: String intfName = "ZoneAuthorityIntf";
111: ESServiceFinder finder = new ESServiceFinder(core, intfName);
112: ESQuery query = new ESQuery("Name == '" + zone + "'");
113: ZoneAuthorityIntf auth = (ZoneAuthorityIntf) finder
114: .find(query);
115:
116: // Create a new DNS server.
117: new ESpeakDNS(zonefile, port, auth);
118: System.err.println("CustomDNS.ESpeakDNS: Running.");
119: } catch (Exception e) {
120: System.err.println("CustomDNS.ESpeakDNS: " + e.toString());
121: System.exit(1);
122: }
123: }
124: }
|