-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathNSAPRecord.java
More file actions
111 lines (99 loc) · 2.66 KB
/
Copy pathNSAPRecord.java
File metadata and controls
111 lines (99 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.xbill.DNS.utils.base16;
/**
* NSAP Address Record.
*
* @author Brian Wellington
* @see <a href="https://datatracker.ietf.org/doc/html/rfc1706">RFC 1706: DNS NSAP Resource
* Records</a>
*/
public class NSAPRecord extends Record {
private byte[] address;
NSAPRecord() {}
private static byte[] checkAndConvertAddress(String address) {
if (!address.substring(0, 2).equalsIgnoreCase("0x")) {
return null;
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
boolean partial = false;
int current = 0;
for (int i = 2; i < address.length(); i++) {
char c = address.charAt(i);
if (c == '.') {
continue;
}
int value = Character.digit(c, 16);
if (value == -1) {
return null;
}
if (partial) {
current += value;
bytes.write(current);
partial = false;
} else {
current = value << 4;
partial = true;
}
}
if (partial) {
return null;
}
return bytes.toByteArray();
}
/**
* Creates an NSAP Record from the given data
*
* @param address The NSAP address.
* @throws IllegalArgumentException The address is not a valid NSAP address.
*/
public NSAPRecord(Name name, int dclass, long ttl, String address) {
super(name, Type.NSAP, dclass, ttl);
this.address = checkAndConvertAddress(address);
if (this.address == null) {
throw new IllegalArgumentException("invalid NSAP address " + address);
}
}
@Override
protected void rrFromWire(DNSInput in) {
address = in.readByteArray();
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
String addr = st.getString();
this.address = checkAndConvertAddress(addr);
if (this.address == null) {
throw st.exception("invalid NSAP address " + addr);
}
}
/**
* Returns the NSAP address as a string, escaped for RR textual representation.
*
* <p>Obsolete, use {@link NSAPRecord#getAddressAsByteArray} instead.
*
* @deprecated
*/
@Deprecated
public String getAddress() {
return byteArrayToString(address, false);
}
/**
* Returns the NSAP address.
*
* @since 3.6.5
*/
public byte[] getAddressAsByteArray() {
return address;
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
out.writeByteArray(address);
}
@Override
protected String rrToString() {
return "0x" + base16.toString(address);
}
}