aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/spi/DNSJavaNameService.java
blob: 14d8adb6a2a3191578fc35ca01b4cfe1a9931de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright (c) 2005 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS.spi;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import org.xbill.DNS.AAAARecord;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.ExtendedResolver;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Name;
import org.xbill.DNS.PTRRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.ReverseMap;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

/**
 * This class implements a Name Service Provider, which Java can use 
 * (starting with version 1.4), to perform DNS resolutions instead of using 
 * the standard calls. 
 * <p>
 * This Name Service Provider uses dnsjava.
 * <p>
 * To use this provider, you must set the following system property:
 * <b>sun.net.spi.nameservice.provider.1=dns,dnsjava</b>
 *
 * @author Brian Wellington
 * @author Paul Cowan (pwc21@yahoo.com)
 */

public class DNSJavaNameService implements InvocationHandler {

private static final String nsProperty = "sun.net.spi.nameservice.nameservers";
private static final String domainProperty = "sun.net.spi.nameservice.domain";
private static final String v6Property = "java.net.preferIPv6Addresses";

private boolean preferV6 = false;

/**
 * Creates a DNSJavaNameService instance.
 * <p>
 * Uses the
 * <b>sun.net.spi.nameservice.nameservers</b>,
 * <b>sun.net.spi.nameservice.domain</b>, and
 * <b>java.net.preferIPv6Addresses</b> properties for configuration.
 */
protected
DNSJavaNameService() {
	String nameServers = System.getProperty(nsProperty);
	String domain = System.getProperty(domainProperty);
	String v6 = System.getProperty(v6Property);

	if (nameServers != null) {
		StringTokenizer st = new StringTokenizer(nameServers, ",");
		String [] servers = new String[st.countTokens()];
		int n = 0;
		while (st.hasMoreTokens())
			servers[n++] = st.nextToken();
		try {
			Resolver res = new ExtendedResolver(servers);
			Lookup.setDefaultResolver(res);
		}
		catch (UnknownHostException e) {
			System.err.println("DNSJavaNameService: invalid " +
					   nsProperty);
		}
	}

	if (domain != null) {
		try {
			Lookup.setDefaultSearchPath(new String[] {domain});
		}
		catch (TextParseException e) {
			System.err.println("DNSJavaNameService: invalid " +
					   domainProperty);
		}
	}

	if (v6 != null && v6.equalsIgnoreCase("true"))
		preferV6 = true;
}


public Object
invoke(Object proxy, Method method, Object[] args) throws Throwable {
	try {
		if (method.getName().equals("getHostByAddr")) {
			return this.getHostByAddr((byte[]) args[0]);
		} else if (method.getName().equals("lookupAllHostAddr")) {
			InetAddress[] addresses;
			addresses = this.lookupAllHostAddr((String) args[0]);
			Class returnType = method.getReturnType();
			if (returnType.equals(InetAddress[].class)) {
				// method for Java >= 1.6
				return addresses;
			} else if (returnType.equals(byte[][].class)) {
				// method for Java <= 1.5
				int naddrs = addresses.length;
				byte [][] byteAddresses = new byte[naddrs][];
				byte [] addr;
				for (int i = 0; i < naddrs; i++) {
					addr = addresses[i].getAddress();
					byteAddresses[i] = addr;
				}
				return byteAddresses;
			}
		}		
	} catch (Throwable e) {
		System.err.println("DNSJavaNameService: Unexpected error.");
		e.printStackTrace();
		throw e;
	}
	throw new IllegalArgumentException(
					"Unknown function name or arguments.");
}

/**
 * Performs a forward DNS lookup for the host name.
 * @param host The host name to resolve.
 * @return All the ip addresses found for the host name.
 */
public InetAddress []
lookupAllHostAddr(String host) throws UnknownHostException {
	Name name = null;

	try {
		name = new Name(host);
	}
	catch (TextParseException e) {
		throw new UnknownHostException(host);
	}

	Record [] records = null;
	if (preferV6)
		records = new Lookup(name, Type.AAAA).run();
	if (records == null)
		records = new Lookup(name, Type.A).run();
	if (records == null && !preferV6)
		records = new Lookup(name, Type.AAAA).run();
	if (records == null)
		throw new UnknownHostException(host);

	InetAddress[] array = new InetAddress[records.length];
	for (int i = 0; i < records.length; i++) {
		Record record = records[i];
		if (records[i] instanceof ARecord) {
			ARecord a = (ARecord) records[i];
			array[i] = a.getAddress();
		} else {
			AAAARecord aaaa = (AAAARecord) records[i];
			array[i] = aaaa.getAddress();
		}
	}
	return array;
}

/**
 * Performs a reverse DNS lookup.
 * @param addr The ip address to lookup.
 * @return The host name found for the ip address.
 */
public String
getHostByAddr(byte [] addr) throws UnknownHostException {
	Name name = ReverseMap.fromAddress(InetAddress.getByAddress(addr));
	Record [] records = new Lookup(name, Type.PTR).run();
	if (records == null)
		throw new UnknownHostException();
	return ((PTRRecord) records[0]).getTarget().toString();
}
}