001: // Copyright (c) 2003-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.net.*;
006:
007: /**
008: * A set functions designed to deal with DNS names used in reverse mappings.
009: * For the IPv4 address a.b.c.d, the reverse map name is d.c.b.a.in-addr.arpa.
010: * For an IPv6 address, the reverse map name is ...ip6.arpa.
011: *
012: * @author Brian Wellington
013: */
014:
015: public final class ReverseMap {
016:
017: private static Name inaddr4 = Name
018: .fromConstantString("in-addr.arpa.");
019: private static Name inaddr6 = Name.fromConstantString("ip6.arpa.");
020:
021: /* Otherwise the class could be instantiated */
022: private ReverseMap() {
023: }
024:
025: /**
026: * Creates a reverse map name corresponding to an address contained in
027: * an array of 4 bytes (for an IPv4 address) or 16 bytes (for an IPv6 address).
028: * @param addr The address from which to build a name.
029: * @return The name corresponding to the address in the reverse map.
030: */
031: public static Name fromAddress(byte[] addr) {
032: if (addr.length != 4 && addr.length != 16)
033: throw new IllegalArgumentException("array must contain "
034: + "4 or 16 elements");
035:
036: StringBuffer sb = new StringBuffer();
037: if (addr.length == 4) {
038: for (int i = addr.length - 1; i >= 0; i--) {
039: sb.append(addr[i] & 0xFF);
040: if (i > 0)
041: sb.append(".");
042: }
043: } else {
044: int[] nibbles = new int[2];
045: for (int i = addr.length - 1; i >= 0; i--) {
046: nibbles[0] = (addr[i] & 0xFF) >> 4;
047: nibbles[1] = (addr[i] & 0xFF) & 0xF;
048: for (int j = nibbles.length - 1; j >= 0; j--) {
049: sb.append(Integer.toHexString(nibbles[j]));
050: if (i > 0 || j > 0)
051: sb.append(".");
052: }
053: }
054: }
055:
056: try {
057: if (addr.length == 4)
058: return Name.fromString(sb.toString(), inaddr4);
059: else
060: return Name.fromString(sb.toString(), inaddr6);
061: } catch (TextParseException e) {
062: throw new IllegalStateException("name cannot be invalid");
063: }
064: }
065:
066: /**
067: * Creates a reverse map name corresponding to an address contained in
068: * an array of 4 integers between 0 and 255 (for an IPv4 address) or 16
069: * integers between 0 and 255 (for an IPv6 address).
070: * @param addr The address from which to build a name.
071: * @return The name corresponding to the address in the reverse map.
072: */
073: public static Name fromAddress(int[] addr) {
074: byte[] bytes = new byte[addr.length];
075: for (int i = 0; i < addr.length; i++) {
076: if (addr[i] < 0 || addr[i] > 0xFF)
077: throw new IllegalArgumentException("array must "
078: + "contain values " + "between 0 and 255");
079: bytes[i] = (byte) addr[i];
080: }
081: return fromAddress(bytes);
082: }
083:
084: /**
085: * Creates a reverse map name corresponding to an address contained in
086: * an InetAddress.
087: * @param addr The address from which to build a name.
088: * @return The name corresponding to the address in the reverse map.
089: */
090: public static Name fromAddress(InetAddress addr) {
091: return fromAddress(addr.getAddress());
092: }
093:
094: /**
095: * Creates a reverse map name corresponding to an address contained in
096: * a String.
097: * @param addr The address from which to build a name.
098: * @return The name corresponding to the address in the reverse map.
099: * @throws UnknownHostException The string does not contain a valid address.
100: */
101: public static Name fromAddress(String addr, int family)
102: throws UnknownHostException {
103: byte[] array = Address.toByteArray(addr, family);
104: if (array == null)
105: throw new UnknownHostException("Invalid IP address");
106: return fromAddress(array);
107: }
108:
109: /**
110: * Creates a reverse map name corresponding to an address contained in
111: * a String.
112: * @param addr The address from which to build a name.
113: * @return The name corresponding to the address in the reverse map.
114: * @throws UnknownHostException The string does not contain a valid address.
115: */
116: public static Name fromAddress(String addr)
117: throws UnknownHostException {
118: return fromAddress(addr, Address.IPv4);
119: }
120:
121: }
|