001: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
002:
003: package org.xbill.DNS;
004:
005: import java.io.*;
006: import java.net.*;
007:
008: /**
009: * A6 Record - maps a domain name to an IPv6 address (experimental)
010: *
011: * @author Brian Wellington
012: */
013:
014: public class A6Record extends Record {
015:
016: private int prefixBits;
017: private InetAddress suffix;
018: private Name prefix;
019:
020: A6Record() {
021: }
022:
023: Record getObject() {
024: return new A6Record();
025: }
026:
027: /**
028: * Creates an A6 Record from the given data
029: * @param prefixBits The number of bits in the address prefix
030: * @param suffix The address suffix
031: * @param prefix The name of the prefix
032: */
033: public A6Record(Name name, int dclass, long ttl, int prefixBits,
034: InetAddress suffix, Name prefix) {
035: super (name, Type.A6, dclass, ttl);
036: this .prefixBits = checkU8("prefixBits", prefixBits);
037: if (suffix != null && Address.familyOf(suffix) != Address.IPv6)
038: throw new IllegalArgumentException("invalid IPv6 address");
039: this .suffix = suffix;
040: if (prefix != null)
041: this .prefix = checkName("prefix", prefix);
042: }
043:
044: void rrFromWire(DNSInput in) throws IOException {
045: prefixBits = in.readU8();
046: int suffixbits = 128 - prefixBits;
047: int suffixbytes = (suffixbits + 7) / 8;
048: if (prefixBits < 128) {
049: byte[] bytes = new byte[16];
050: in.readByteArray(bytes, 16 - suffixbytes, suffixbytes);
051: suffix = InetAddress.getByAddress(bytes);
052: }
053: if (prefixBits > 0)
054: prefix = new Name(in);
055: }
056:
057: void rdataFromString(Tokenizer st, Name origin) throws IOException {
058: prefixBits = st.getUInt8();
059: if (prefixBits > 128) {
060: throw st.exception("prefix bits must be [0..128]");
061: } else if (prefixBits < 128) {
062: String s = st.getString();
063: try {
064: suffix = Address.getByAddress(s, Address.IPv6);
065: } catch (UnknownHostException e) {
066: throw st.exception("invalid IPv6 address: " + s);
067: }
068: }
069: if (prefixBits > 0)
070: prefix = st.getName(origin);
071: }
072:
073: /** Converts rdata to a String */
074: String rrToString() {
075: StringBuffer sb = new StringBuffer();
076: sb.append(prefixBits);
077: if (suffix != null) {
078: sb.append(" ");
079: sb.append(suffix.getHostAddress());
080: }
081: if (prefix != null) {
082: sb.append(" ");
083: sb.append(prefix);
084: }
085: return sb.toString();
086: }
087:
088: /** Returns the number of bits in the prefix */
089: public int getPrefixBits() {
090: return prefixBits;
091: }
092:
093: /** Returns the address suffix */
094: public InetAddress getSuffix() {
095: return suffix;
096: }
097:
098: /** Returns the address prefix */
099: public Name getPrefix() {
100: return prefix;
101: }
102:
103: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
104: out.writeU8(prefixBits);
105: if (suffix != null) {
106: int suffixbits = 128 - prefixBits;
107: int suffixbytes = (suffixbits + 7) / 8;
108: byte[] data = suffix.getAddress();
109: out.writeByteArray(data, 16 - suffixbytes, suffixbytes);
110: }
111: if (prefix != null)
112: prefix.toWire(out, null, canonical);
113: }
114:
115: }
|