001/*
002 * Copyright 2015-2018 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    {
046        int priority = dis.readUnsignedShort();
047        DnsName name = DnsName.parse(dis, data);
048        return new MX(priority, name);
049    }
050
051    public MX(int priority, String name) {
052        this(priority, DnsName.from(name));
053    }
054
055    public MX(int priority, DnsName name) {
056        this.priority = priority;
057        this.target = name;
058        this.name = target;
059    }
060
061    @Override
062    public void serialize(DataOutputStream dos) throws IOException {
063        dos.writeShort(priority);
064        target.writeToStream(dos);
065    }
066
067    @Override
068    public String toString() {
069        return priority + " " + target + '.';
070    }
071
072    @Override
073    public TYPE getType() {
074        return TYPE.MX;
075    }
076
077}