// Copyright (c) 2003-2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

import java.util.*;
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 the IPv6 address, the reverse map name is ...
 *
 * @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 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) {
	if (addr.length != 4 && addr.length != 16)
		throw new IllegalArgumentException("array must contain " +
						   "4 or 16 elements");
	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");
	}

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

	try {
		return Name.fromString(sb.toString(), inaddr4);
	}
	catch (TextParseException e) {
		throw new IllegalStateException("name cannot be invalid");
	}
}

/**
 * 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) {
	byte [] bytes = addr.getAddress();
	int [] array = new int[bytes.length];
	for (int i = 0; i < bytes.length; i++) {
		array[i] = bytes[i] & 0xFF;
	}
	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 {
	int [] array = Address.toArray(addr);
	if (array == null)
		throw new UnknownHostException("Invalid IP address");
	return fromAddress(array);
}

}

@@ -286,27 +286,26 @@
 rpa.%0A * For 
-the
+an
  IPv6 addres
@@ -334,16 +334,25 @@
 e is ...
+ip6.arpa.
 %0A *%0A * @

Update a comment.