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 * SRV record payload (service pointer). 022 */ 023public class SRV extends Data implements Comparable<SRV> { 024 025 /** 026 * The priority of this service. Lower values mean higher priority. 027 */ 028 public final int priority; 029 030 /** 031 * The weight of this service. Services with the same priority should be 032 * balanced based on weight. 033 */ 034 public final int weight; 035 036 /** 037 * The target port. 038 */ 039 public final int port; 040 041 /** 042 * The target server. 043 */ 044 public final DnsName target; 045 046 /** 047 * The target server. 048 * 049 * @deprecated use {@link #target} instead. 050 */ 051 @Deprecated 052 public final DnsName name; 053 054 public static SRV parse(DataInputStream dis, byte[] data) 055 throws IOException 056 { 057 int priority = dis.readUnsignedShort(); 058 int weight = dis.readUnsignedShort(); 059 int port = dis.readUnsignedShort(); 060 DnsName name = DnsName.parse(dis, data); 061 return new SRV(priority, weight, port, name); 062 } 063 064 public SRV(int priority, int weight, int port, String name) { 065 this(priority, weight, port, DnsName.from(name)); 066 } 067 068 public SRV(int priority, int weight, int port, DnsName name) { 069 this.priority = priority; 070 this.weight = weight; 071 this.port = port; 072 this.target = name; 073 this.name = target; 074 } 075 076 @Override 077 public void serialize(DataOutputStream dos) throws IOException { 078 dos.writeShort(priority); 079 dos.writeShort(weight); 080 dos.writeShort(port); 081 target.writeToStream(dos); 082 } 083 084 @Override 085 public String toString() { 086 return priority + " " + weight + " " + port + " " + target + "."; 087 } 088 089 @Override 090 public TYPE getType() { 091 return TYPE.SRV; 092 } 093 094 @Override 095 public int compareTo(SRV other) { 096 int res = other.priority - this.priority; 097 if (res == 0) { 098 res = this.weight - other.weight; 099 } 100 return res; 101 } 102}