~ubuntu-branches/ubuntu/wily/dnsjava/wily-proposed

« back to all changes in this revision

Viewing changes to org/xbill/DNS/ARecord.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2009-07-21 15:17:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090721151703-6v0107p1s3h7gv1c
Tags: upstream-2.0.6
ImportĀ upstreamĀ versionĀ 2.0.6

Show diffs side-by-side

added added

removed removed

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