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