Home | History | Annotate | Download | only in DNS
      1 // Copyright (c) 2004 Brian Wellington (bwelling (at) xbill.org)
      2 
      3 package org.xbill.DNS;
      4 
      5 import java.io.*;
      6 import org.xbill.DNS.utils.*;
      7 
      8 /**
      9  * NSAP Address Record.
     10  *
     11  * @author Brian Wellington
     12  */
     13 
     14 public class NSAPRecord extends Record {
     15 
     16 private static final long serialVersionUID = -1037209403185658593L;
     17 
     18 private byte [] address;
     19 
     20 NSAPRecord() {}
     21 
     22 Record
     23 getObject() {
     24 	return new NSAPRecord();
     25 }
     26 
     27 private static final byte []
     28 checkAndConvertAddress(String address) {
     29 	if (!address.substring(0, 2).equalsIgnoreCase("0x")) {
     30 		return null;
     31 	}
     32 	ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     33 	boolean partial = false;
     34 	int current = 0;
     35 	for (int i = 2; i < address.length(); i++) {
     36 		char c = address.charAt(i);
     37 		if (c == '.') {
     38 			continue;
     39 		}
     40 		int value = Character.digit(c, 16);
     41 		if (value == -1) {
     42 			return null;
     43 		}
     44 		if (partial) {
     45 			current += value;
     46 			bytes.write(current);
     47 			partial = false;
     48 		} else {
     49 			current = value << 4;
     50 			partial = true;
     51 		}
     52 
     53 	}
     54 	if (partial) {
     55 		return null;
     56 	}
     57 	return bytes.toByteArray();
     58 }
     59 
     60 /**
     61  * Creates an NSAP Record from the given data
     62  * @param address The NSAP address.
     63  * @throws IllegalArgumentException The address is not a valid NSAP address.
     64  */
     65 public
     66 NSAPRecord(Name name, int dclass, long ttl, String address) {
     67 	super(name, Type.NSAP, dclass, ttl);
     68 	this.address = checkAndConvertAddress(address);
     69 	if (this.address == null) {
     70 		throw new IllegalArgumentException("invalid NSAP address " +
     71 						   address);
     72 	}
     73 }
     74 
     75 void
     76 rrFromWire(DNSInput in) throws IOException {
     77 	address = in.readByteArray();
     78 }
     79 
     80 void
     81 rdataFromString(Tokenizer st, Name origin) throws IOException {
     82 	String addr = st.getString();
     83 	this.address = checkAndConvertAddress(addr);
     84 	if (this.address == null)
     85 		throw st.exception("invalid NSAP address " + addr);
     86 }
     87 
     88 /**
     89  * Returns the NSAP address.
     90  */
     91 public String
     92 getAddress() {
     93 	return byteArrayToString(address, false);
     94 }
     95 
     96 void
     97 rrToWire(DNSOutput out, Compression c, boolean canonical) {
     98 	out.writeByteArray(address);
     99 }
    100 
    101 String
    102 rrToString() {
    103 	return "0x" + base16.toString(address);
    104 }
    105 
    106 }
    107