aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/APLRecord.java
blob: 5940da238bc244f05af48dc80ff6365773c3a138 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) 2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

import java.io.*;
import java.net.*;
import java.util.*;
import org.xbill.DNS.utils.*;

/**
 * APL - Address Prefix List.  See RFC 3123.
 *
 * @author Brian Wellington
 */

/*
 * Note: this currently uses the same constants as the Address class;
 * this could change if more constants are defined for APL records.
 */

public class APLRecord extends Record {

public static class Element {
	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);
	}

	public String
	toString() {
		StringBuffer sb = new StringBuffer();
		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();
	}

	public boolean
	equals(Object arg) {
		if (arg == null || !(arg instanceof Element))
			return false;
		Element elt = (Element) arg;
		return (family == elt.family &&
			negative == elt.negative &&
			prefixLength == elt.prefixLength &&
			address.equals(elt.address));
	}

	public int
	hashCode() {
		return address.hashCode() + prefixLength + (negative ? 1 : 0);
	}
}

private static final long serialVersionUID = -1348173791712935864L;

private List elements;

APLRecord() {} 

Record
getObject() {
	return new APLRecord();
}

private static boolean
validatePrefixLength(int family, int prefixLength) {
	if (prefixLength < 0 || prefixLength >= 256)
		return false;
	if ((family == Address.IPv4 && prefixLength > 32) ||
	    (family == Address.IPv6 && prefixLength > 128))
		return false;
	return true;
}

/**
 * Creates an APL Record from the given data.
 * @param elements The list of APL elements.
 */
public
APLRecord(Name name, int dclass, long ttl, List elements) {
	super(name, Type.APL, dclass, ttl);
	this.elements = new ArrayList(elements.size());
	for (Iterator it = elements.iterator(); it.hasNext(); ) {
		Object o = it.next();
		if (!(o instanceof Element)) {
			throw new IllegalArgumentException("illegal element");
		}
		Element element = (Element) o;
		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;
}

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);

	}
}

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 = 0;
		int prefix = 0;

		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();
}

String
rrToString() {
	StringBuffer sb = new StringBuffer();
	for (Iterator it = elements.iterator(); it.hasNext(); ) {
		Element element = (Element) it.next();
		sb.append(element);
		if (it.hasNext())
			sb.append(" ");
	}
	return sb.toString();
}

/** Returns the list of APL elements. */
public List
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;
}

void
rrToWire(DNSOutput out, Compression c, boolean canonical) {
	for (Iterator it = elements.iterator(); it.hasNext(); ) {
		Element element = (Element) it.next();
		int length = 0;
		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);
	}
}

}