001: // Copyright (c) 2005 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS.spi;
004:
005: import java.net.*;
006: import java.util.*;
007: import org.xbill.DNS.*;
008: import sun.net.spi.nameservice.*;
009:
010: /**
011: * This class implements a Name Service Provider, which Java can use
012: * (starting with version 1.4), to perform DNS resolutions instead of using
013: * the standard calls.
014: * <p>
015: * This Name Service Provider uses dnsjava.
016: * <p>
017: * To use this provider, you must set the following system propery:
018: * <b>sun.net.spi.nameservice.provider.1=dns,dnsjava</b>
019: *
020: * @author Brian Wellington
021: * @author Paul Cowan (pwc21@yahoo.com)
022: */
023:
024: public class DNSJavaNameService implements NameService {
025:
026: private static final String nsProperty = "sun.net.spi.nameservice.nameservers";
027: private static final String domainProperty = "sun.net.spi.nameservice.domain";
028: private static final String v6Property = "java.net.preferIPv6Addresses";
029:
030: private boolean preferV6 = false;
031:
032: /**
033: * Creates a DNSJavaNameService instance.
034: * <p>
035: * Uses the
036: * <b>sun.net.spi.nameservice.nameservers</b>,
037: * <b>sun.net.spi.nameservice.domain</b>, and
038: * <b>java.net.preferIPv6Addresses</b> properties for configuration.
039: */
040: protected DNSJavaNameService() {
041: String nameServers = System.getProperty(nsProperty);
042: String domain = System.getProperty(domainProperty);
043: String v6 = System.getProperty(v6Property);
044:
045: if (nameServers != null) {
046: StringTokenizer st = new StringTokenizer(nameServers, ",");
047: String[] servers = new String[st.countTokens()];
048: int n = 0;
049: while (st.hasMoreTokens())
050: servers[n++] = st.nextToken();
051: try {
052: Resolver res = new ExtendedResolver(servers);
053: Lookup.setDefaultResolver(res);
054: } catch (UnknownHostException e) {
055: System.err.println("DNSJavaNameService: invalid "
056: + nsProperty);
057: }
058: }
059:
060: if (domain != null) {
061: try {
062: Lookup.setDefaultSearchPath(new String[] { domain });
063: } catch (TextParseException e) {
064: System.err.println("DNSJavaNameService: invalid "
065: + domainProperty);
066: }
067: }
068:
069: if (v6 != null && v6.equalsIgnoreCase("true"))
070: preferV6 = true;
071: }
072:
073: /**
074: * Performs a forward DNS lookup for the host name.
075: * @param host The host name to resolve.
076: * @return All the ip addresses found for the host name.
077: */
078: public InetAddress[] lookupAllHostAddr(String host)
079: throws UnknownHostException {
080: Name name = null;
081:
082: try {
083: name = new Name(host);
084: } catch (TextParseException e) {
085: throw new UnknownHostException(host);
086: }
087:
088: Record[] records = null;
089: if (preferV6)
090: records = new Lookup(name, Type.AAAA).run();
091: if (records == null)
092: records = new Lookup(name, Type.A).run();
093: if (records == null && !preferV6)
094: records = new Lookup(name, Type.AAAA).run();
095: if (records == null)
096: throw new UnknownHostException(host);
097:
098: InetAddress[] array = new InetAddress[records.length];
099: for (int i = 0; i < records.length; i++) {
100: Record record = records[i];
101: if (records[i] instanceof ARecord) {
102: ARecord a = (ARecord) records[i];
103: array[i] = a.getAddress();
104: } else {
105: AAAARecord aaaa = (AAAARecord) records[i];
106: array[i] = aaaa.getAddress();
107: }
108: }
109: return array;
110: }
111:
112: /**
113: * Performs a reverse DNS lookup.
114: * @param addr The ip address to lookup.
115: * @return The host name found for the ip address.
116: */
117: public String getHostByAddr(byte[] addr)
118: throws UnknownHostException {
119: Name name = ReverseMap.fromAddress(InetAddress
120: .getByAddress(addr));
121: Record[] records = new Lookup(name, Type.PTR).run();
122: if (records == null)
123: throw new UnknownHostException();
124: return ((PTRRecord) records[0]).getTarget().toString();
125: }
126:
127: }
|