aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/ReverseMap.java
blob: f4293decd754bf3fcd76293fa57dfa1d24a4046b (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
// Copyright (c) 2003-2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

import java.net.*;

/**
 * A set functions designed to deal with DNS names used in reverse mappings.
 * For the IPv4 address a.b.c.d, the reverse map name is d.c.b.a.in-addr.arpa.
 * For an IPv6 address, the reverse map name is ...ip6.arpa.
 *
 * @author Brian Wellington
 */

public final class ReverseMap {

private static Name inaddr4 = Name.fromConstantString("in-addr.arpa.");
private static Name inaddr6 = Name.fromConstantString("ip6.arpa.");

/* Otherwise the class could be instantiated */
private
ReverseMap() {}

/**
 * Creates a reverse map name corresponding to an address contained in
 * an array of 4 bytes (for an IPv4 address) or 16 bytes (for an IPv6 address).
 * @param addr The address from which to build a name.
 * @return The name corresponding to the address in the reverse map.
 */
public static Name
fromAddress(byte [] addr) {
	if (addr.length != 4 && addr.length != 16)
		throw new IllegalArgumentException("array must contain " +
						   "4 or 16 elements");

	StringBuffer sb = new StringBuffer();
	if (addr.length == 4) {
		for (int i = addr.length - 1; i >= 0; i--) {
			sb.append(addr[i] & 0xFF);
			if (i > 0)
				sb.append(".");
		}
	} else {
		int [] nibbles = new int[2];
		for (int i = addr.length - 1; i >= 0; i--) {
			nibbles[0] = (addr[i] & 0xFF) >> 4;
			nibbles[1] = (addr[i] & 0xFF) & 0xF;
			for (int j = nibbles.length - 1; j >= 0; j--) {
				sb.append(Integer.toHexString(nibbles[j]));
				if (i > 0 || j > 0)
					sb.append(".");
			}
		}
	}

	try {
		if (addr.length == 4)
			return Name.fromString(sb.toString(), inaddr4);
		else
			return Name.fromString(sb.toString(), inaddr6);
	}
	catch (TextParseException e) {
		throw new IllegalStateException("name cannot be invalid");
	}
}

/**
 * Creates a reverse map name corresponding to an address contained in
 * an array of 4 integers between 0 and 255 (for an IPv4 address) or 16
 * integers between 0 and 255 (for an IPv6 address).
 * @param addr The address from which to build a name.
 * @return The name corresponding to the address in the reverse map.
 */
public static Name
fromAddress(int [] addr) {
	byte [] bytes = new byte[addr.length];
	for (int i = 0; i < addr.length; i++) {
		if (addr[i] < 0 || addr[i] > 0xFF)
			throw new IllegalArgumentException("array must " +
							   "contain values " +
							   "between 0 and 255");
		bytes[i] = (byte) addr[i];
	}
	return fromAddress(bytes);
}

/**
 * Creates a reverse map name corresponding to an address contained in
 * an InetAddress.
 * @param addr The address from which to build a name.
 * @return The name corresponding to the address in the reverse map.
 */
public static Name
fromAddress(InetAddress addr) {
	return fromAddress(addr.getAddress());
}

/**
 * Creates a reverse map name corresponding to an address contained in
 * a String.
 * @param addr The address from which to build a name.
 * @return The name corresponding to the address in the reverse map.
 * @throws UnknownHostException The string does not contain a valid address.
 */
public static Name
fromAddress(String addr, int family) throws UnknownHostException {
	byte [] array = Address.toByteArray(addr, family);
	if (array == null)
		throw new UnknownHostException("Invalid IP address");
	return fromAddress(array);
}

/**
 * Creates a reverse map name corresponding to an address contained in
 * a String.
 * @param addr The address from which to build a name.
 * @return The name corresponding to the address in the reverse map.
 * @throws UnknownHostException The string does not contain a valid address.
 */
public static Name
fromAddress(String addr) throws UnknownHostException {
	byte [] array = Address.toByteArray(addr, Address.IPv4);
	if (array == null)
		array = Address.toByteArray(addr, Address.IPv6);
	if (array == null)
		throw new UnknownHostException("Invalid IP address");
	return fromAddress(array);
}

}