001/** 002 * 003 * Copyright 2013-2018 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smack.util.dns; 018 019import java.net.InetAddress; 020import java.util.List; 021 022import org.jivesoftware.smack.util.StringUtils; 023 024import org.minidns.dnsname.DnsName; 025 026/** 027 * A DNS SRV RR. 028 * 029 * @see <a href="http://tools.ietf.org/html/rfc2782">RFC 2782: A DNS RR for specifying the location of services (DNS 030 * SRV)</a> 031 * @author Florian Schmaus 032 * 033 */ 034public class SRVRecord extends HostAddress implements Comparable<SRVRecord> { 035 036 private int weight; 037 private int priority; 038 039 /** 040 * SRV Record constructor. 041 * 042 * @param fqdn Fully qualified domain name 043 * @param port The connection port 044 * @param priority Priority of the target host 045 * @param weight Relative weight for records with same priority 046 * @param inetAddresses list of addresses. 047 * @throws IllegalArgumentException fqdn is null or any other field is not in valid range (0-65535). 048 */ 049 public SRVRecord(DnsName fqdn, int port, int priority, int weight, List<InetAddress> inetAddresses) { 050 super(fqdn, port, inetAddresses); 051 StringUtils.requireNotNullOrEmpty(fqdn, "The FQDN must not be null"); 052 if (weight < 0 || weight > 65535) 053 throw new IllegalArgumentException( 054 "DNS SRV records weight must be a 16-bit unsigned integer (i.e. between 0-65535. Weight was: " 055 + weight); 056 057 if (priority < 0 || priority > 65535) 058 throw new IllegalArgumentException( 059 "DNS SRV records priority must be a 16-bit unsigned integer (i.e. between 0-65535. Priority was: " 060 + priority); 061 062 this.priority = priority; 063 this.weight = weight; 064 065 } 066 067 public int getPriority() { 068 return priority; 069 } 070 071 public int getWeight() { 072 return weight; 073 } 074 075 @Override 076 public int compareTo(SRVRecord other) { 077 // According to RFC2782, 078 // "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach". 079 // This means that a SRV record with a higher priority is 'less' then one with a lower. 080 int res = other.priority - this.priority; 081 if (res == 0) { 082 res = this.weight - other.weight; 083 } 084 return res; 085 } 086 087 @Override 088 public String toString() { 089 return super.toString() + " prio:" + priority + ":w:" + weight; 090 } 091}