01: // Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
02:
03: package org.xbill.DNS;
04:
05: import java.net.*;
06: import java.io.*;
07:
08: /**
09: * Address Record - maps a domain name to an Internet address
10: *
11: * @author Brian Wellington
12: */
13:
14: public class ARecord extends Record {
15:
16: private int addr;
17:
18: ARecord() {
19: }
20:
21: Record getObject() {
22: return new ARecord();
23: }
24:
25: private static final int fromArray(byte[] array) {
26: return (((array[0] & 0xFF) << 24) | ((array[1] & 0xFF) << 16)
27: | ((array[2] & 0xFF) << 8) | (array[3] & 0xFF));
28: }
29:
30: private static final byte[] toArray(int addr) {
31: byte[] bytes = new byte[4];
32: bytes[0] = (byte) ((addr >>> 24) & 0xFF);
33: bytes[1] = (byte) ((addr >>> 16) & 0xFF);
34: bytes[2] = (byte) ((addr >>> 8) & 0xFF);
35: bytes[3] = (byte) (addr & 0xFF);
36: return bytes;
37: }
38:
39: /**
40: * Creates an A Record from the given data
41: * @param address The address that the name refers to
42: */
43: public ARecord(Name name, int dclass, long ttl, InetAddress address) {
44: super (name, Type.A, dclass, ttl);
45: if (Address.familyOf(address) != Address.IPv4)
46: throw new IllegalArgumentException("invalid IPv4 address");
47: addr = fromArray(address.getAddress());
48: }
49:
50: void rrFromWire(DNSInput in) throws IOException {
51: addr = fromArray(in.readByteArray(4));
52: }
53:
54: void rdataFromString(Tokenizer st, Name origin) throws IOException {
55: InetAddress address = st.getAddress(Address.IPv4);
56: addr = fromArray(address.getAddress());
57: }
58:
59: /** Converts rdata to a String */
60: String rrToString() {
61: return (Address.toDottedQuad(toArray(addr)));
62: }
63:
64: /** Returns the Internet address */
65: public InetAddress getAddress() {
66: try {
67: return InetAddress.getByAddress(toArray(addr));
68: } catch (UnknownHostException e) {
69: return null;
70: }
71: }
72:
73: void rrToWire(DNSOutput out, Compression c, boolean canonical) {
74: out.writeU32(((long) addr) & 0xFFFFFFFFL);
75: }
76:
77: }
|