001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.record;
012
013import java.io.DataInputStream;
014import java.io.IOException;
015import java.net.Inet4Address;
016
017import org.minidns.record.Record.TYPE;
018import org.minidns.util.InetAddressUtil;
019
020/**
021 * A record payload (ip pointer).
022 */
023public class A extends InternetAddressRR<Inet4Address> {
024
025    @Override
026    public TYPE getType() {
027        return TYPE.A;
028    }
029
030    public A(Inet4Address inet4Address) {
031        super(inet4Address);
032        assert ip.length == 4;
033    }
034
035    public A(int q1, int q2, int q3, int q4) {
036        super(new byte[] { (byte) q1, (byte) q2, (byte) q3, (byte) q4 });
037        if (q1 < 0 || q1 > 255 || q2 < 0 || q2 > 255 || q3 < 0 || q3 > 255 || q4 < 0 || q4 > 255) {
038            throw new IllegalArgumentException();
039        }
040    }
041
042    public A(byte[] ip) {
043        super(ip);
044        if (ip.length != 4) {
045            throw new IllegalArgumentException("IPv4 address in A record is always 4 byte");
046        }
047    }
048
049    public A(CharSequence ipv4CharSequence) {
050        this(InetAddressUtil.ipv4From(ipv4CharSequence));
051    }
052
053    public static A parse(DataInputStream dis)
054            throws IOException {
055        byte[] ip = new byte[4];
056        dis.readFully(ip);
057        return new A(ip);
058    }
059
060    @Override
061    public String toString() {
062        return Integer.toString(ip[0] & 0xff) + "." +
063               Integer.toString(ip[1] & 0xff) + "." +
064               Integer.toString(ip[2] & 0xff) + "." +
065               Integer.toString(ip[3] & 0xff);
066    }
067
068}