// Copyright (c) 1999 Brian Wellington (bwelling@xbill.org)
// Portions Copyright (c) 1999 Network Associates, Inc.

package org.xbill.DNS;

import java.net.*;
import java.io.*;
import java.util.*;
import org.xbill.DNS.utils.*;

/**
 * Address Record - maps a domain name to an IPv6 address
 *
 * @author Brian Wellington
 */

public class A6Record extends Record {

private short prefixBits;
private Inet6Address suffix;
private Name prefix;

private
A6Record() {}

/**
 * Creates an A6 Record from the given data
 * @param prefixBits The number of bits in the address prefix
 * @param suffix The address suffix
 * @param prefix The name of the prefix
 */
public
A6Record(Name _name, short _dclass, int _ttl, int _prefixBits,
	 Inet6Address _suffix, Name _prefix)
throws IOException
{
	super(_name, Type.A6, _dclass, _ttl);
	prefixBits = (short) _prefixBits;
	suffix = _suffix;
	prefix = _prefix;
}

A6Record(Name _name, short _dclass, int _ttl, int length,
	 DataByteInputStream in, Compression c) throws IOException
{
	super(_name, Type.A6, _dclass, _ttl);

	if (in == null)
		return;

	prefixBits = in.readByte();
	int suffixbits = 128 - prefixBits;
	int suffixbytes = (suffixbits + 7) / 8;
	byte [] data = new byte[suffixbytes];
	in.read(data);
	suffix = new Inet6Address(128 - prefixBits, data);
	prefix = new Name(in, c);
}

A6Record(Name _name, short _dclass, int _ttl, MyStringTokenizer st, Name origin)
throws IOException
{
	super(_name, Type.A6, _dclass, _ttl);
	prefixBits = Short.parseShort(st.nextToken());
	suffix = new Inet6Address(st.nextToken());
	prefix = new Name(st.nextToken(), origin);
}

/** Converts to a String */
public String
toString() {
	StringBuffer sb = toStringNoData();
	if (prefix != null) {
		sb.append(prefixBits);
		sb.append(" ");
		sb.append(suffix);
		sb.append(" ");
		sb.append(prefix);
	}
	return sb.toString();
}

/** Returns the number of bits in the prefix */
public short
getPrefixBits() {
	return prefixBits;
}

/** Returns the address suffix */
public Inet6Address
getSuffix() {
	return suffix;
}

/** Returns the address prefix */
public Name
getPrefix() {
	return prefix;
}

void
rrToWire(DataByteOutputStream out, Compression c) throws IOException {
	if (prefix == null)
		return;
	out.write(prefixBits);
	int suffixbits = 128 - prefixBits;
	int suffixbytes = (suffixbits + 7) / 8;
	byte [] data = suffix.toBytes();
	out.write(data, 16 - suffixbytes, suffixbytes);
	prefix.toWire(out, null);
}

}

@@ -1295,16 +1295,38 @@
  data);%0A
+%09if (prefixBits %3E 0)%0A%09
 %09prefix 
@@ -1344,16 +1344,16 @@
 in, c);%0A
-
 %7D%0A%0AA6Rec
@@ -1580,16 +1580,38 @@
 ken());%0A
+%09if (prefixBits %3E 0)%0A%09
 %09prefix 
@@ -1646,16 +1646,16 @@
 rigin);%0A
-
 %7D%0A%0A/** C
@@ -1742,27 +1742,27 @@
 ata();%0A%09if (
-pre
+suf
 fix != null)
@@ -1828,16 +1828,41 @@
 uffix);%0A
+%09%09if (prefix != null) %7B%0A%09
 %09%09sb.app
@@ -1863,32 +1863,33 @@
 sb.append(%22 %22);%0A
+%09
 %09%09sb.append(pref
@@ -1893,16 +1893,20 @@
 refix);%0A
+%09%09%7D%0A
 %09%7D%0A%09retu
@@ -2271,27 +2271,27 @@
 tion %7B%0A%09if (
-pre
+suf
 fix == null)
@@ -2485,16 +2485,38 @@
 bytes);%0A
+%09if (prefix != null)%0A%09
 %09prefix.

handle 0 length prefix