aboutsummaryrefslogtreecommitdiff
path: root/src/org/xbill/DNS/Address.java
blob: 799185b242a4830210ac7e36a16787bae67fcdf1 (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)

package org.xbill.DNS;

import java.net.*;
import java.net.Inet6Address;

/**
 * Routines dealing with IP addresses.  Includes functions similar to
 * those in the java.net.InetAddress class.
 *
 * @author Brian Wellington
 */

public final class Address {

public static final int IPv4 = 1;
public static final int IPv6 = 2;

private
Address() {}

private static byte []
parseV4(String s) {
	int numDigits;
	int currentOctet;
	byte [] values = new byte[4];
	int currentValue;
	int length = s.length();

	currentOctet = 0;
	currentValue = 0;
	numDigits = 0;
	for (int i = 0; i < length; i++) {
		char c = s.charAt(i);
		if (c >= '0' && c <= '9') {
			/* Can't have more than 3 digits per octet. */
			if (numDigits == 3)
				return null;
			/* Octets shouldn't start with 0, unless they are 0. */
			if (numDigits > 0 && currentValue == 0)
				return null;
			numDigits++;
			currentValue *= 10;
			currentValue += (c - '0');
			/* 255 is the maximum value for an octet. */
			if (currentValue > 255)
				return null;
		} else if (c == '.') {
			/* Can't have more than 3 dots. */
			if (currentOctet == 3)
				return null;
			/* Two consecutive dots are bad. */
			if (numDigits == 0)
				return null;
			values[currentOctet++] = (byte) currentValue;
			currentValue = 0;
			numDigits = 0;
		} else
			return null;
	}
	/* Must have 4 octets. */
	if (currentOctet != 3)
		return null;
	/* The fourth octet can't be empty. */
	if (numDigits == 0)
		return null;
	values[currentOctet] = (byte) currentValue;
	return values;
}

private static byte []
parseV6(String s) {
	int range = -1;
	byte [] data = new byte[16];

	String [] tokens = s.split(":", -1);

	int first = 0;
	int last = tokens.length - 1;

	if (tokens[0].length() == 0) {
		// If the first two tokens are empty, it means the string
		// started with ::, which is fine.  If only the first is
		// empty, the string started with :, which is bad.
		if (last - first > 0 && tokens[1].length() == 0)
			first++;
		else
			return null;
	}

	if (tokens[last].length() == 0) {
		// If the last two tokens are empty, it means the string
		// ended with ::, which is fine.  If only the last is
		// empty, the string ended with :, which is bad.
		if (last - first > 0 && tokens[last - 1].length() == 0)
			last--;
		else
			return null;
	}

	if (last - first + 1 > 8)
		return null;

	int i, j;
	for (i = first, j = 0; i <= last; i++) {
		if (tokens[i].length() == 0) {
			if (range >= 0)
				return null;
			range = j;
			continue;
		}

		if (tokens[i].indexOf('.') >= 0) {
			// An IPv4 address must be the last component
			if (i < last)
				return null;
			// There can't have been more than 6 components.
			if (i > 6)
				return null;
			byte [] v4addr = Address.toByteArray(tokens[i], IPv4);
			if (v4addr == null)
				return null;
			for (int k = 0; k < 4; k++)
				data[j++] = v4addr[k];
			break;
		}

		try {
			for (int k = 0; k < tokens[i].length(); k++) {
				char c = tokens[i].charAt(k);
				if (Character.digit(c, 16) < 0)
					return null;
			}
			int x = Integer.parseInt(tokens[i], 16);
			if (x > 0xFFFF || x < 0)
				return null;
			data[j++] = (byte)(x >>> 8);
			data[j++] = (byte)(x & 0xFF);
		}
		catch (NumberFormatException e) {
			return null;
		}
	}

	if (j < 16 && range < 0)
		return null;

	if (range >= 0) {
		int empty = 16 - j;
		System.arraycopy(data, range, data, range + empty, j - range);
		for (i = range; i < range + empty; i++)
			data[i] = 0;
	}

	return data;
}

/**
 * Convert a string containing an IP address to an array of 4 or 16 integers.
 * @param s The address, in text format.
 * @param family The address family.
 * @return The address
 */
public static int []
toArray(String s, int family) {
	byte [] byteArray = toByteArray(s, family);
	if (byteArray == null)
		return null;
	int [] intArray = new int[byteArray.length];
	for (int i = 0; i < byteArray.length; i++)
		intArray[i] = byteArray[i] & 0xFF;
	return intArray;
}

/**
 * Convert a string containing an IPv4 address to an array of 4 integers.
 * @param s The address, in text format.
 * @return The address
 */
public static int []
toArray(String s) {
	return toArray(s, IPv4);
}

/**
 * Convert a string containing an IP address to an array of 4 or 16 bytes.
 * @param s The address, in text format.
 * @param family The address family.
 * @return The address
 */
public static byte []
toByteArray(String s, int family) {
	if (family == IPv4)
		return parseV4(s);
	else if (family == IPv6)
		return parseV6(s);
	else
		throw new IllegalArgumentException("unknown address family");
}

/**
 * Determines if a string contains a valid IP address.
 * @param s The string
 * @return Whether the string contains a valid IP address
 */
public static boolean
isDottedQuad(String s) {
	byte [] address = Address.toByteArray(s, IPv4);
	return (address != null);
}

/**
 * Converts a byte array containing an IPv4 address into a dotted quad string.
 * @param addr The array
 * @return The string representation
 */
public static String
toDottedQuad(byte [] addr) {
	return ((addr[0] & 0xFF) + "." + (addr[1] & 0xFF) + "." +
		(addr[2] & 0xFF) + "." + (addr[3] & 0xFF));
}

/**
 * Converts an int array containing an IPv4 address into a dotted quad string.
 * @param addr The array
 * @return The string representation
 */
public static String
toDottedQuad(int [] addr) {
	return (addr[0] + "." + addr[1] + "." + addr[2] + "." + addr[3]);
}

private static Record []
lookupHostName(String name) throws UnknownHostException {
	try {
		Record [] records = new Lookup(name).run();
		if (records == null)
			throw new UnknownHostException("unknown host");
		return records;
	}
	catch (TextParseException e) {
		throw new UnknownHostException("invalid name");
	}
}

private static InetAddress
addrFromRecord(String name, Record r) throws UnknownHostException {
	ARecord a = (ARecord) r;
	return InetAddress.getByAddress(name, a.getAddress().getAddress());
}

/**
 * Determines the IP address of a host
 * @param name The hostname to look up
 * @return The first matching IP address
 * @exception UnknownHostException The hostname does not have any addresses
 */
public static InetAddress
getByName(String name) throws UnknownHostException {
	try {
		return getByAddress(name);
	} catch (UnknownHostException e) {
		Record [] records = lookupHostName(name);
		return addrFromRecord(name, records[0]);
	}
}

/**
 * Determines all IP address of a host
 * @param name The hostname to look up
 * @return All matching IP addresses
 * @exception UnknownHostException The hostname does not have any addresses
 */
public static InetAddress []
getAllByName(String name) throws UnknownHostException {
	try {
		InetAddress addr = getByAddress(name);
		return new InetAddress[] {addr};
	} catch (UnknownHostException e) {
		Record [] records = lookupHostName(name);
		InetAddress [] addrs = new InetAddress[records.length];
		for (int i = 0; i < records.length; i++)
			addrs[i] = addrFromRecord(name, records[i]);
		return addrs;
	}
}

/**
 * Converts an address from its string representation to an IP address.
 * The address can be either IPv4 or IPv6.
 * @param addr The address, in string form
 * @return The IP addresses
 * @exception UnknownHostException The address is not a valid IP address.
 */
public static InetAddress
getByAddress(String addr) throws UnknownHostException {
	byte [] bytes;
	bytes = toByteArray(addr, IPv4);
	if (bytes != null)
		return InetAddress.getByAddress(addr, bytes);
	bytes = toByteArray(addr, IPv6);
	if (bytes != null)
		return InetAddress.getByAddress(addr, bytes);
	throw new UnknownHostException("Invalid address: " + addr);
}

/**
 * Converts an address from its string representation to an IP address in
 * a particular family.
 * @param addr The address, in string form
 * @param family The address family, either IPv4 or IPv6.
 * @return The IP addresses
 * @exception UnknownHostException The address is not a valid IP address in
 * the specified address family.
 */
public static InetAddress
getByAddress(String addr, int family) throws UnknownHostException {
	if (family != IPv4 && family != IPv6)
		throw new IllegalArgumentException("unknown address family");
	byte [] bytes;
	bytes = toByteArray(addr, family);
	if (bytes != null)
		return InetAddress.getByAddress(addr, bytes);
	throw new UnknownHostException("Invalid address: " + addr);
}

/**
 * Determines the hostname for an address
 * @param addr The address to look up
 * @return The associated host name
 * @exception UnknownHostException There is no hostname for the address
 */
public static String
getHostName(InetAddress addr) throws UnknownHostException {
	Name name = ReverseMap.fromAddress(addr);
	Record [] records = new Lookup(name, Type.PTR).run();
	if (records == null)
		throw new UnknownHostException("unknown address");
	PTRRecord ptr = (PTRRecord) records[0];
	return ptr.getTarget().toString();
}

/**
 * Returns the family of an InetAddress.
 * @param address The supplied address.
 * @return The family, either IPv4 or IPv6.
 */
public static int
familyOf(InetAddress address) {
	if (address instanceof Inet4Address)
		return IPv4;
	if (address instanceof Inet6Address)
		return IPv6;
	throw new IllegalArgumentException("unknown address family");
}

/**
 * Returns the length of an address in a particular family.
 * @param family The address family, either IPv4 or IPv6.
 * @return The length of addresses in that family.
 */
public static int
addressLength(int family) {
	if (family == IPv4)
		return 4;
	if (family == IPv6)
		return 16;
	throw new IllegalArgumentException("unknown address family");
}

/**
 * Truncates an address to the specified number of bits.  For example,
 * truncating the address 10.1.2.3 to 8 bits would yield 10.0.0.0.
 * @param address The source address
 * @param maskLength The number of bits to truncate the address to.
 */
public static InetAddress
truncate(InetAddress address, int maskLength)
{
	int family = familyOf(address);
	int maxMaskLength = addressLength(family) * 8;
	if (maskLength < 0 || maskLength > maxMaskLength)
		throw new IllegalArgumentException("invalid mask length");
	if (maskLength == maxMaskLength)
		return address;
	byte [] bytes = address.getAddress();
	for (int i = maskLength / 8 + 1; i < bytes.length; i++)
		bytes[i] = 0;
	int maskBits = maskLength % 8;
	int bitmask = 0;
	for (int i = 0; i < maskBits; i++)
		bitmask |= (1 << (7 - i));
	bytes[maskLength / 8] &= bitmask;
	try {
		return InetAddress.getByAddress(bytes);
	} catch (UnknownHostException e) {
		throw new IllegalArgumentException("invalid address");
	}
}

}