1// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS;
4
5import java.io.*;
6import java.net.*;
7
8/**
9 * A6 Record - maps a domain name to an IPv6 address (experimental)
10 *
11 * @author Brian Wellington
12 */
13
14public class A6Record extends Record {
15
16private static final long serialVersionUID = -8815026887337346789L;
17
18private int prefixBits;
19private InetAddress suffix;
20private Name prefix;
21
22A6Record() {}
23
24Record
25getObject() {
26	return new A6Record();
27}
28
29/**
30 * Creates an A6 Record from the given data
31 * @param prefixBits The number of bits in the address prefix
32 * @param suffix The address suffix
33 * @param prefix The name of the prefix
34 */
35public
36A6Record(Name name, int dclass, long ttl, int prefixBits,
37	 InetAddress suffix, Name prefix)
38{
39	super(name, Type.A6, dclass, ttl);
40	this.prefixBits = checkU8("prefixBits", prefixBits);
41	if (suffix != null && Address.familyOf(suffix) != Address.IPv6)
42		throw new IllegalArgumentException("invalid IPv6 address");
43	this.suffix = suffix;
44	if (prefix != null)
45		this.prefix = checkName("prefix", prefix);
46}
47
48void
49rrFromWire(DNSInput in) throws IOException {
50	prefixBits = in.readU8();
51	int suffixbits = 128 - prefixBits;
52	int suffixbytes = (suffixbits + 7) / 8;
53	if (prefixBits < 128) {
54		byte [] bytes = new byte[16];
55		in.readByteArray(bytes, 16 - suffixbytes, suffixbytes);
56		suffix = InetAddress.getByAddress(bytes);
57	}
58	if (prefixBits > 0)
59		prefix = new Name(in);
60}
61
62void
63rdataFromString(Tokenizer st, Name origin) throws IOException {
64	prefixBits = st.getUInt8();
65	if (prefixBits > 128) {
66		throw st.exception("prefix bits must be [0..128]");
67	} else if (prefixBits < 128) {
68		String s = st.getString();
69		try {
70			suffix = Address.getByAddress(s, Address.IPv6);
71		}
72		catch (UnknownHostException e) {
73			throw st.exception("invalid IPv6 address: " + s);
74		}
75	}
76	if (prefixBits > 0)
77		prefix = st.getName(origin);
78}
79
80/** Converts rdata to a String */
81String
82rrToString() {
83	StringBuffer sb = new StringBuffer();
84	sb.append(prefixBits);
85	if (suffix != null) {
86		sb.append(" ");
87		sb.append(suffix.getHostAddress());
88	}
89	if (prefix != null) {
90		sb.append(" ");
91		sb.append(prefix);
92	}
93	return sb.toString();
94}
95
96/** Returns the number of bits in the prefix */
97public int
98getPrefixBits() {
99	return prefixBits;
100}
101
102/** Returns the address suffix */
103public InetAddress
104getSuffix() {
105	return suffix;
106}
107
108/** Returns the address prefix */
109public Name
110getPrefix() {
111	return prefix;
112}
113
114void
115rrToWire(DNSOutput out, Compression c, boolean canonical) {
116	out.writeU8(prefixBits);
117	if (suffix != null) {
118		int suffixbits = 128 - prefixBits;
119		int suffixbytes = (suffixbits + 7) / 8;
120		byte [] data = suffix.getAddress();
121		out.writeByteArray(data, 16 - suffixbytes, suffixbytes);
122	}
123	if (prefix != null)
124		prefix.toWire(out, null, canonical);
125}
126
127}
128