-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathAPLRecord.java
More file actions
266 lines (236 loc) · 7.49 KB
/
Copy pathAPLRecord.java
File metadata and controls
266 lines (236 loc) · 7.49 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)
package org.xbill.DNS;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.xbill.DNS.utils.base16;
/**
* APL - Address Prefix List.
*
* @author Brian Wellington
* @see <a href="https://datatracker.ietf.org/doc/html/rfc3123">RFC 3123: A DNS RR Type for Lists of
* Address Prefixes (APL RR)</a>
*/
public class APLRecord extends Record {
public static class Element implements Serializable {
public final int family;
public final boolean negative;
public final int prefixLength;
public final Object address;
private Element(int family, boolean negative, Object address, int prefixLength) {
this.family = family;
this.negative = negative;
this.address = address;
this.prefixLength = prefixLength;
if (!validatePrefixLength(family, prefixLength)) {
throw new IllegalArgumentException("invalid prefix length");
}
}
/**
* Creates an APL element corresponding to an IPv4 or IPv6 prefix.
*
* @param negative Indicates if this prefix is a negation.
* @param address The IPv4 or IPv6 address.
* @param prefixLength The length of this prefix, in bits.
* @throws IllegalArgumentException The prefix length is invalid.
*/
public Element(boolean negative, InetAddress address, int prefixLength) {
this(Address.familyOf(address), negative, address, prefixLength);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if (negative) {
sb.append("!");
}
sb.append(family);
sb.append(":");
if (family == Address.IPv4 || family == Address.IPv6) {
sb.append(((InetAddress) address).getHostAddress());
} else {
sb.append(base16.toString((byte[]) address));
}
sb.append("/");
sb.append(prefixLength);
return sb.toString();
}
@Override
public boolean equals(Object arg) {
if (!(arg instanceof Element)) {
return false;
}
Element elt = (Element) arg;
return family == elt.family
&& negative == elt.negative
&& prefixLength == elt.prefixLength
&& address.equals(elt.address);
}
@Override
public int hashCode() {
return address.hashCode() + prefixLength + (negative ? 1 : 0);
}
}
private List<Element> elements;
APLRecord() {}
private static boolean validatePrefixLength(int family, int prefixLength) {
if (prefixLength < 0 || prefixLength >= 256) {
return false;
}
return !((family == Address.IPv4 && prefixLength > 32)
|| (family == Address.IPv6 && prefixLength > 128));
}
/**
* Creates an APL Record from the given data.
*
* @param elements The list of APL elements.
*/
public APLRecord(Name name, int dclass, long ttl, List<Element> elements) {
super(name, Type.APL, dclass, ttl);
this.elements = new ArrayList<>(elements.size());
for (Element element : elements) {
if (element.family != Address.IPv4 && element.family != Address.IPv6) {
throw new IllegalArgumentException("unknown family");
}
this.elements.add(element);
}
}
private static byte[] parseAddress(byte[] in, int length) throws WireParseException {
if (in.length > length) {
throw new WireParseException("invalid address length");
}
if (in.length == length) {
return in;
}
byte[] out = new byte[length];
System.arraycopy(in, 0, out, 0, in.length);
return out;
}
@Override
protected void rrFromWire(DNSInput in) throws IOException {
elements = new ArrayList<>(1);
while (in.remaining() != 0) {
int family = in.readU16();
int prefix = in.readU8();
int length = in.readU8();
boolean negative = (length & 0x80) != 0;
length &= ~0x80;
byte[] data = in.readByteArray(length);
Element element;
if (!validatePrefixLength(family, prefix)) {
throw new WireParseException("invalid prefix length");
}
if (family == Address.IPv4 || family == Address.IPv6) {
data = parseAddress(data, Address.addressLength(family));
InetAddress addr = InetAddress.getByAddress(data);
element = new Element(negative, addr, prefix);
} else {
element = new Element(family, negative, data, prefix);
}
elements.add(element);
}
}
@Override
protected void rdataFromString(Tokenizer st, Name origin) throws IOException {
elements = new ArrayList<>(1);
while (true) {
Tokenizer.Token t = st.get();
if (!t.isString()) {
break;
}
boolean negative = false;
int family;
int prefix;
String s = t.value();
int start = 0;
if (s.startsWith("!")) {
negative = true;
start = 1;
}
int colon = s.indexOf(':', start);
if (colon < 0) {
throw st.exception("invalid address prefix element");
}
int slash = s.indexOf('/', colon);
if (slash < 0) {
throw st.exception("invalid address prefix element");
}
String familyString = s.substring(start, colon);
String addressString = s.substring(colon + 1, slash);
String prefixString = s.substring(slash + 1);
try {
family = Integer.parseInt(familyString);
} catch (NumberFormatException e) {
throw st.exception("invalid family");
}
if (family != Address.IPv4 && family != Address.IPv6) {
throw st.exception("unknown family");
}
try {
prefix = Integer.parseInt(prefixString);
} catch (NumberFormatException e) {
throw st.exception("invalid prefix length");
}
if (!validatePrefixLength(family, prefix)) {
throw st.exception("invalid prefix length");
}
byte[] bytes = Address.toByteArray(addressString, family);
if (bytes == null) {
throw st.exception("invalid IP address " + addressString);
}
InetAddress address = InetAddress.getByAddress(bytes);
elements.add(new Element(negative, address, prefix));
}
st.unget();
}
@Override
protected String rrToString() {
StringBuilder sb = new StringBuilder();
for (Iterator<Element> it = elements.iterator(); it.hasNext(); ) {
Element element = it.next();
sb.append(element);
if (it.hasNext()) {
sb.append(" ");
}
}
return sb.toString();
}
/** Returns the list of APL elements. */
public List<Element> getElements() {
return elements;
}
private static int addressLength(byte[] addr) {
for (int i = addr.length - 1; i >= 0; i--) {
if (addr[i] != 0) {
return i + 1;
}
}
return 0;
}
@Override
protected void rrToWire(DNSOutput out, Compression c, boolean canonical) {
for (Element element : elements) {
int length;
byte[] data;
if (element.family == Address.IPv4 || element.family == Address.IPv6) {
InetAddress addr = (InetAddress) element.address;
data = addr.getAddress();
length = addressLength(data);
} else {
data = (byte[]) element.address;
length = data.length;
}
int wlength = length;
if (element.negative) {
wlength |= 0x80;
}
out.writeU16(element.family);
out.writeU8(element.prefixLength);
out.writeU8(wlength);
out.writeByteArray(data, 0, length);
}
}
}