Home | History | Annotate | Download | only in DNS
      1 // Copyright (c) 1999-2004 Brian Wellington (bwelling (at) xbill.org)
      2 
      3 package org.xbill.DNS;
      4 
      5 import java.io.*;
      6 import java.net.*;
      7 
      8 /**
      9  * A6 Record - maps a domain name to an IPv6 address (experimental)
     10  *
     11  * @author Brian Wellington
     12  */
     13 
     14 public class A6Record extends Record {
     15 
     16 private static final long serialVersionUID = -8815026887337346789L;
     17 
     18 private int prefixBits;
     19 private InetAddress suffix;
     20 private Name prefix;
     21 
     22 A6Record() {}
     23 
     24 Record
     25 getObject() {
     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  */
     35 public
     36 A6Record(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 
     48 void
     49 rrFromWire(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 
     62 void
     63 rdataFromString(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 */
     81 String
     82 rrToString() {
     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 */
     97 public int
     98 getPrefixBits() {
     99 	return prefixBits;
    100 }
    101 
    102 /** Returns the address suffix */
    103 public InetAddress
    104 getSuffix() {
    105 	return suffix;
    106 }
    107 
    108 /** Returns the address prefix */
    109 public Name
    110 getPrefix() {
    111 	return prefix;
    112 }
    113 
    114 void
    115 rrToWire(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