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