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.DataOutputStream;
015import java.io.IOException;
016
017import org.minidns.dnsname.DnsName;
018import org.minidns.record.Record.TYPE;
019
020/**
021 * MX record payload (mail service pointer).
022 */
023public class MX extends Data {
024
025    /**
026     * The priority of this service. Lower values mean higher priority.
027     */
028    public final int priority;
029
030    /**
031     * The name of the target server.
032     */
033    public final DnsName target;
034
035    /**
036     * The name of the target server.
037     *
038     * @deprecated use {@link #target} instead.
039     */
040    @Deprecated
041    public final DnsName name;
042
043    public static MX parse(DataInputStream dis, byte[] data)
044        throws IOException {
045        int priority = dis.readUnsignedShort();
046        DnsName name = DnsName.parse(dis, data);
047        return new MX(priority, name);
048    }
049
050    public MX(int priority, String name) {
051        this(priority, DnsName.from(name));
052    }
053
054    public MX(int priority, DnsName name) {
055        this.priority = priority;
056        this.target = name;
057        this.name = target;
058    }
059
060    @Override
061    public void serialize(DataOutputStream dos) throws IOException {
062        dos.writeShort(priority);
063        target.writeToStream(dos);
064    }
065
066    @Override
067    public String toString() {
068        return priority + " " + target + '.';
069    }
070
071    @Override
072    public TYPE getType() {
073        return TYPE.MX;
074    }
075
076}