mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-13 19:19:38 +02:00
SMACK-225 Fixed DNS SRV handling, as per RFC 2782. Added support for multiple DNS SRV resolvers namely javax and org.xbill.dns (aka dnsjava).
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13561 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
21be8c55ee
commit
2eb13f48d2
12 changed files with 756 additions and 210 deletions
|
@ -19,18 +19,20 @@
|
|||
|
||||
package org.jivesoftware.smack.util;
|
||||
|
||||
import java.util.Hashtable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
|
||||
import org.jivesoftware.smack.util.dns.DNSResolver;
|
||||
import org.jivesoftware.smack.util.dns.HostAddress;
|
||||
import org.jivesoftware.smack.util.dns.SRVRecord;
|
||||
|
||||
/**
|
||||
* Utilty class to perform DNS lookups for XMPP services.
|
||||
* Utility class to perform DNS lookups for XMPP services.
|
||||
*
|
||||
* @author Matt Tucker
|
||||
*/
|
||||
|
@ -40,23 +42,30 @@ public class DNSUtil {
|
|||
* Create a cache to hold the 100 most recently accessed DNS lookups for a period of
|
||||
* 10 minutes.
|
||||
*/
|
||||
private static Map<String, HostAddress> cache = new Cache<String, HostAddress>(100, 1000*60*10);
|
||||
private static Map<String, List<HostAddress>> cache = new Cache<String, List<HostAddress>>(100, 1000*60*10);
|
||||
|
||||
private static DirContext context;
|
||||
private static DNSResolver dnsResolver = null;
|
||||
|
||||
static {
|
||||
try {
|
||||
Hashtable<String, String> env = new Hashtable<String, String>();
|
||||
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
|
||||
context = new InitialDirContext(env);
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Ignore.
|
||||
}
|
||||
/**
|
||||
* Set the DNS resolver that should be used to perform DNS lookups.
|
||||
*
|
||||
* @param resolver
|
||||
*/
|
||||
public static void setDNSResolver(DNSResolver resolver) {
|
||||
dnsResolver = resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host name and port that the specified XMPP server can be
|
||||
* Returns the current DNS resolved used to perform DNS lookups.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static DNSResolver getDNSResolver() {
|
||||
return dnsResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of HostAddresses under which the specified XMPP server can be
|
||||
* reached at for client-to-server communication. A DNS lookup for a SRV
|
||||
* record in the form "_xmpp-client._tcp.example.com" is attempted, according
|
||||
* to section 14.4 of RFC 3920. If that lookup fails, a lookup in the older form
|
||||
|
@ -67,81 +76,17 @@ public class DNSUtil {
|
|||
* of 5222.<p>
|
||||
*
|
||||
* As an example, a lookup for "example.com" may return "im.example.com:5269".
|
||||
*
|
||||
* Note on SRV record selection.
|
||||
* We now check priority and weight, but we still don't do this correctly.
|
||||
* The missing behavior is this: if we fail to reach a host based on its SRV
|
||||
* record then we need to select another host from the other SRV records.
|
||||
* In Smack 3.1.1 we're not going to be able to do the major system redesign to
|
||||
* correct this.
|
||||
*
|
||||
* @param domain the domain.
|
||||
* @return a HostAddress, which encompasses the hostname and port that the XMPP
|
||||
* server can be reached at for the specified domain.
|
||||
* @return List of HostAddress, which encompasses the hostname and port that the
|
||||
* XMPP server can be reached at for the specified domain.
|
||||
*/
|
||||
public static HostAddress resolveXMPPDomain(String domain) {
|
||||
if (context == null) {
|
||||
return new HostAddress(domain, 5222);
|
||||
}
|
||||
String key = "c" + domain;
|
||||
// Return item from cache if it exists.
|
||||
if (cache.containsKey(key)) {
|
||||
HostAddress address = (HostAddress)cache.get(key);
|
||||
if (address != null) {
|
||||
return address;
|
||||
}
|
||||
}
|
||||
String bestHost = domain;
|
||||
int bestPort = 5222;
|
||||
int bestPriority = 0;
|
||||
int bestWeight = 0;
|
||||
try {
|
||||
Attributes dnsLookup = context.getAttributes("_xmpp-client._tcp." + domain, new String[]{"SRV"});
|
||||
Attribute srvAttribute = dnsLookup.get("SRV");
|
||||
NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
|
||||
while(srvRecords.hasMore()) {
|
||||
String srvRecord = srvRecords.next();
|
||||
String [] srvRecordEntries = srvRecord.split(" ");
|
||||
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
|
||||
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
|
||||
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
|
||||
String host = srvRecordEntries[srvRecordEntries.length-1];
|
||||
|
||||
// Randomize the weight.
|
||||
weight *= Math.random() * weight;
|
||||
|
||||
if ((bestPriority == 0) || (priority < bestPriority)) {
|
||||
// Choose a server with the lowest priority.
|
||||
bestPriority = priority;
|
||||
bestWeight = weight;
|
||||
bestHost = host;
|
||||
bestPort = port;
|
||||
} else if (priority == bestPriority) {
|
||||
// When we have like priorities then randomly choose a server based on its weight
|
||||
// The weights were randomized above.
|
||||
if (weight > bestWeight) {
|
||||
bestWeight = weight;
|
||||
bestHost = host;
|
||||
bestPort = port;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Ignore.
|
||||
}
|
||||
// Host entries in DNS should end with a ".".
|
||||
if (bestHost.endsWith(".")) {
|
||||
bestHost = bestHost.substring(0, bestHost.length()-1);
|
||||
}
|
||||
HostAddress address = new HostAddress(bestHost, bestPort);
|
||||
// Add item to cache.
|
||||
cache.put(key, address);
|
||||
return address;
|
||||
public static List<HostAddress> resolveXMPPDomain(String domain) {
|
||||
return resolveDomain(domain, 'c');
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the host name and port that the specified XMPP server can be
|
||||
* Returns a list of HostAddresses under which the specified XMPP server can be
|
||||
* reached at for server-to-server communication. A DNS lookup for a SRV
|
||||
* record in the form "_xmpp-server._tcp.example.com" is attempted, according
|
||||
* to section 14.4 of RFC 3920. If that lookup fails, a lookup in the older form
|
||||
|
@ -154,104 +99,131 @@ public class DNSUtil {
|
|||
* As an example, a lookup for "example.com" may return "im.example.com:5269".
|
||||
*
|
||||
* @param domain the domain.
|
||||
* @return a HostAddress, which encompasses the hostname and port that the XMPP
|
||||
* server can be reached at for the specified domain.
|
||||
* @return List of HostAddress, which encompasses the hostname and port that the
|
||||
* XMPP server can be reached at for the specified domain.
|
||||
*/
|
||||
public static HostAddress resolveXMPPServerDomain(String domain) {
|
||||
if (context == null) {
|
||||
return new HostAddress(domain, 5269);
|
||||
}
|
||||
String key = "s" + domain;
|
||||
public static List<HostAddress> resolveXMPPServerDomain(String domain) {
|
||||
return resolveDomain(domain, 's');
|
||||
}
|
||||
|
||||
private static List<HostAddress> resolveDomain(String domain, char keyPrefix) {
|
||||
// Prefix the key with 's' to distinguish him from the client domain lookups
|
||||
String key = keyPrefix + domain;
|
||||
// Return item from cache if it exists.
|
||||
if (cache.containsKey(key)) {
|
||||
HostAddress address = (HostAddress)cache.get(key);
|
||||
if (address != null) {
|
||||
return address;
|
||||
List<HostAddress> addresses = cache.get(key);
|
||||
if (addresses != null) {
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
String host = domain;
|
||||
int port = 5269;
|
||||
try {
|
||||
Attributes dnsLookup =
|
||||
context.getAttributes("_xmpp-server._tcp." + domain, new String[]{"SRV"});
|
||||
String srvRecord = (String)dnsLookup.get("SRV").get();
|
||||
String [] srvRecordEntries = srvRecord.split(" ");
|
||||
port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
|
||||
host = srvRecordEntries[srvRecordEntries.length-1];
|
||||
|
||||
if (dnsResolver == null)
|
||||
throw new IllegalStateException("No DNS resolver active.");
|
||||
|
||||
List<HostAddress> addresses = new ArrayList<HostAddress>();
|
||||
|
||||
// Step one: Do SRV lookups
|
||||
String srvDomain;
|
||||
if (keyPrefix == 's') {
|
||||
srvDomain = "_xmpp-server._tcp." + domain;
|
||||
} else if (keyPrefix == 'c') {
|
||||
srvDomain = "_xmpp-client._tcp." + domain;
|
||||
} else {
|
||||
srvDomain = domain;
|
||||
}
|
||||
catch (Exception e) {
|
||||
// Attempt lookup with older "jabber" name.
|
||||
try {
|
||||
Attributes dnsLookup =
|
||||
context.getAttributes("_jabber._tcp." + domain, new String[]{"SRV"});
|
||||
String srvRecord = (String)dnsLookup.get("SRV").get();
|
||||
String [] srvRecordEntries = srvRecord.split(" ");
|
||||
port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length-2]);
|
||||
host = srvRecordEntries[srvRecordEntries.length-1];
|
||||
}
|
||||
catch (Exception e2) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
// Host entries in DNS should end with a ".".
|
||||
if (host.endsWith(".")) {
|
||||
host = host.substring(0, host.length()-1);
|
||||
}
|
||||
HostAddress address = new HostAddress(host, port);
|
||||
List<SRVRecord> srvRecords = dnsResolver.lookupSRVRecords(srvDomain);
|
||||
List<HostAddress> sortedRecords = sortSRVRecords(srvRecords);
|
||||
if (sortedRecords != null)
|
||||
addresses.addAll(sortedRecords);
|
||||
|
||||
// Step two: Add the hostname to the end of the list
|
||||
addresses.add(new HostAddress(domain));
|
||||
|
||||
// Add item to cache.
|
||||
cache.put(key, address);
|
||||
return address;
|
||||
cache.put(key, addresses);
|
||||
|
||||
return addresses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encapsulates a hostname and port.
|
||||
* Sort a given list of SRVRecords as described in RFC 2782
|
||||
* Note that we follow the RFC with one exception. In a group of the same priority, only the first entry
|
||||
* is calculated by random. The others are ore simply ordered by their priority.
|
||||
*
|
||||
* @param records
|
||||
* @return
|
||||
*/
|
||||
public static class HostAddress {
|
||||
protected static List<HostAddress> sortSRVRecords(List<SRVRecord> records) {
|
||||
// RFC 2782, Usage rules: "If there is precisely one SRV RR, and its Target is "."
|
||||
// (the root domain), abort."
|
||||
if (records.size() == 1 && records.get(0).getFQDN().equals("."))
|
||||
return null;
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
// sorting the records improves the performance of the bisection later
|
||||
Collections.sort(records);
|
||||
|
||||
private HostAddress(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hostname.
|
||||
*
|
||||
* @return the hostname.
|
||||
*/
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port.
|
||||
*
|
||||
* @return the port.
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return host + ":" + port;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
// create the priority buckets
|
||||
SortedMap<Integer, List<SRVRecord>> buckets = new TreeMap<Integer, List<SRVRecord>>();
|
||||
for (SRVRecord r : records) {
|
||||
Integer priority = r.getPriority();
|
||||
List<SRVRecord> bucket = buckets.get(priority);
|
||||
// create the list of SRVRecords if it doesn't exist
|
||||
if (bucket == null) {
|
||||
bucket = new LinkedList<SRVRecord>();
|
||||
buckets.put(priority, bucket);
|
||||
}
|
||||
if (!(o instanceof HostAddress)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final HostAddress address = (HostAddress) o;
|
||||
|
||||
if (!host.equals(address.host)) {
|
||||
return false;
|
||||
}
|
||||
return port == address.port;
|
||||
bucket.add(r);
|
||||
}
|
||||
|
||||
List<HostAddress> res = new ArrayList<HostAddress>(records.size());
|
||||
|
||||
for (Integer priority : buckets.keySet()) {
|
||||
List<SRVRecord> bucket = buckets.get(priority);
|
||||
int bucketSize;
|
||||
while ((bucketSize = bucket.size()) > 0) {
|
||||
int[] totals = new int[bucket.size()];
|
||||
int running_total = 0;
|
||||
int count = 0;
|
||||
int zeroWeight = 1;
|
||||
|
||||
for (SRVRecord r : bucket) {
|
||||
if (r.getWeight() > 0)
|
||||
zeroWeight = 0;
|
||||
}
|
||||
|
||||
for (SRVRecord r : bucket) {
|
||||
running_total += (r.getWeight() + zeroWeight);
|
||||
totals[count] = running_total;
|
||||
count++;
|
||||
}
|
||||
int selectedPos;
|
||||
if (running_total == 0) {
|
||||
// If running total is 0, then all weights in this priority
|
||||
// group are 0. So we simply select one of the weights randomly
|
||||
// as the other 'normal' algorithm is unable to handle this case
|
||||
selectedPos = (int) (Math.random() * bucketSize);
|
||||
} else {
|
||||
double rnd = Math.random() * running_total;
|
||||
selectedPos = bisect(totals, rnd);
|
||||
}
|
||||
// add the SRVRecord that was randomly chosen on it's weight
|
||||
// to the start of the result list
|
||||
SRVRecord chosenSRVRecord = bucket.remove(selectedPos);
|
||||
res.add(chosenSRVRecord);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// TODO this is not yet really bisection just a stupid linear search
|
||||
private static int bisect(int[] array, double value) {
|
||||
int pos = 0;
|
||||
for (int element : array) {
|
||||
if (value < element)
|
||||
break;
|
||||
pos++;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
}
|
72
source/org/jivesoftware/smack/util/dns/DNSJavaResolver.java
Normal file
72
source/org/jivesoftware/smack/util/dns/DNSJavaResolver.java
Normal file
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* Copyright 2013 Florian Schmaus
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util.dns;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.xbill.DNS.Lookup;
|
||||
import org.xbill.DNS.Record;
|
||||
import org.xbill.DNS.Type;
|
||||
|
||||
public class DNSJavaResolver extends DNSResolver {
|
||||
|
||||
private static DNSJavaResolver instance;
|
||||
|
||||
private DNSJavaResolver() {
|
||||
|
||||
}
|
||||
|
||||
public static DNSResolver getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new DNSJavaResolver();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SRVRecord> lookupSRVRecords(String name) {
|
||||
List<SRVRecord> res = new ArrayList<SRVRecord>();
|
||||
|
||||
try {
|
||||
Lookup lookup = new Lookup(name, Type.SRV);
|
||||
Record recs[] = lookup.run();
|
||||
if (recs == null)
|
||||
return res;
|
||||
|
||||
for (Record record : recs) {
|
||||
org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
|
||||
if (srvRecord != null && srvRecord.getTarget() != null) {
|
||||
String host = srvRecord.getTarget().toString();
|
||||
int port = srvRecord.getPort();
|
||||
int priority = srvRecord.getPriority();
|
||||
int weight = srvRecord.getWeight();
|
||||
|
||||
SRVRecord r;
|
||||
try {
|
||||
r = new SRVRecord(host, port, priority, weight);
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
res.add(r);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
24
source/org/jivesoftware/smack/util/dns/DNSResolver.java
Normal file
24
source/org/jivesoftware/smack/util/dns/DNSResolver.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
/**
|
||||
* Copyright 2013 Florian Schmaus
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util.dns;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public abstract class DNSResolver {
|
||||
|
||||
public abstract List<SRVRecord> lookupSRVRecords(String name);
|
||||
|
||||
}
|
93
source/org/jivesoftware/smack/util/dns/HostAddress.java
Normal file
93
source/org/jivesoftware/smack/util/dns/HostAddress.java
Normal file
|
@ -0,0 +1,93 @@
|
|||
/**
|
||||
* Copyright 2013 Florian Schmaus
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util.dns;
|
||||
|
||||
public class HostAddress {
|
||||
private String fqdn;
|
||||
private int port;
|
||||
private Exception exception;
|
||||
|
||||
/**
|
||||
* Creates a new HostAddress with the given FQDN. The port will be set to the default XMPP client port: 5222
|
||||
*
|
||||
* @param fqdn
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
public HostAddress(String fqdn) throws IllegalArgumentException {
|
||||
if (fqdn == null)
|
||||
throw new IllegalArgumentException("FQDN is null");
|
||||
if (fqdn.charAt(fqdn.length() - 1) == '.') {
|
||||
this.fqdn = fqdn.substring(0, fqdn.length() - 1);
|
||||
}
|
||||
else {
|
||||
this.fqdn = fqdn;
|
||||
}
|
||||
// Set port to the default port for XMPP client communication
|
||||
this.port = 5222;
|
||||
}
|
||||
|
||||
public HostAddress(String fqdn, int port) throws IllegalArgumentException {
|
||||
this(fqdn);
|
||||
if (port < 0 || port > 65535)
|
||||
throw new IllegalArgumentException(
|
||||
"DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Port was: " + port);
|
||||
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getFQDN() {
|
||||
return fqdn;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setException(Exception e) {
|
||||
this.exception = e;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return fqdn + ":" + port;
|
||||
}
|
||||
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof HostAddress)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final HostAddress address = (HostAddress) o;
|
||||
|
||||
if (!fqdn.equals(address.fqdn)) {
|
||||
return false;
|
||||
}
|
||||
return port == address.port;
|
||||
}
|
||||
|
||||
public String getErrorMessage() {
|
||||
String error;
|
||||
if (exception == null) {
|
||||
error = "No error logged";
|
||||
}
|
||||
else {
|
||||
error = exception.getMessage();
|
||||
}
|
||||
return toString() + " Exception: " + error;
|
||||
}
|
||||
}
|
99
source/org/jivesoftware/smack/util/dns/JavaxResolver.java
Normal file
99
source/org/jivesoftware/smack/util/dns/JavaxResolver.java
Normal file
|
@ -0,0 +1,99 @@
|
|||
/**
|
||||
* Copyright 2013 Florian Schmaus
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util.dns;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.naming.NamingEnumeration;
|
||||
import javax.naming.directory.Attribute;
|
||||
import javax.naming.directory.Attributes;
|
||||
import javax.naming.directory.DirContext;
|
||||
import javax.naming.directory.InitialDirContext;
|
||||
|
||||
import org.jivesoftware.smack.util.DNSUtil;
|
||||
|
||||
/**
|
||||
* A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namepsace.
|
||||
*
|
||||
* @author Florian Schmaus
|
||||
*
|
||||
*/
|
||||
public class JavaxResolver extends DNSResolver {
|
||||
|
||||
private static JavaxResolver instance;
|
||||
private static DirContext dirContext;
|
||||
|
||||
static {
|
||||
try {
|
||||
Hashtable<String, String> env = new Hashtable<String, String>();
|
||||
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
|
||||
dirContext = new InitialDirContext(env);
|
||||
} catch (Exception e) {
|
||||
// Ignore.
|
||||
}
|
||||
|
||||
// Try to set this DNS resolver as primary one
|
||||
DNSUtil.setDNSResolver(maybeGetInstance());
|
||||
}
|
||||
|
||||
private JavaxResolver() {
|
||||
|
||||
}
|
||||
|
||||
public static DNSResolver maybeGetInstance() {
|
||||
if (instance == null && isSupported()) {
|
||||
instance = new JavaxResolver();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static boolean isSupported() {
|
||||
return dirContext != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SRVRecord> lookupSRVRecords(String name) {
|
||||
List<SRVRecord> res = new ArrayList<SRVRecord>();
|
||||
|
||||
try {
|
||||
Attributes dnsLookup = dirContext.getAttributes(name, new String[]{"SRV"});
|
||||
Attribute srvAttribute = dnsLookup.get("SRV");
|
||||
@SuppressWarnings("unchecked")
|
||||
NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
|
||||
while (srvRecords.hasMore()) {
|
||||
String srvRecordString = srvRecords.next();
|
||||
String[] srvRecordEntries = srvRecordString.split(" ");
|
||||
int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
|
||||
int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
|
||||
int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
|
||||
String host = srvRecordEntries[srvRecordEntries.length - 1];
|
||||
|
||||
SRVRecord srvRecord;
|
||||
try {
|
||||
srvRecord = new SRVRecord(host, port, priority, weight);
|
||||
} catch (Exception e) {
|
||||
continue;
|
||||
}
|
||||
res.add(srvRecord);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
77
source/org/jivesoftware/smack/util/dns/SRVRecord.java
Normal file
77
source/org/jivesoftware/smack/util/dns/SRVRecord.java
Normal file
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* Copyright 2013 Florian Schmaus
|
||||
*
|
||||
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smack.util.dns;
|
||||
|
||||
/**
|
||||
* @see <a href="http://tools.ietf.org/html/rfc2782>RFC 2782: A DNS RR for specifying the location of services (DNS
|
||||
* SRV)<a>
|
||||
* @author Florian Schmaus
|
||||
*
|
||||
*/
|
||||
public class SRVRecord extends HostAddress implements Comparable<SRVRecord> {
|
||||
|
||||
private int weight;
|
||||
private int priority;
|
||||
|
||||
/**
|
||||
* Create a new SRVRecord
|
||||
*
|
||||
* @param fqdn
|
||||
* @param port
|
||||
* @param priority
|
||||
* @param weight
|
||||
* @throws IllegalArgumentException
|
||||
*/
|
||||
public SRVRecord(String fqdn, int port, int priority, int weight) throws IllegalArgumentException {
|
||||
super(fqdn, port);
|
||||
if (weight < 0 || weight > 65535)
|
||||
throw new IllegalArgumentException(
|
||||
"DNS SRV records weight must be a 16-bit unsiged integer (i.e. between 0-65535. Weight was: "
|
||||
+ weight);
|
||||
|
||||
if (priority < 0 || priority > 65535)
|
||||
throw new IllegalArgumentException(
|
||||
"DNS SRV records priority must be a 16-bit unsiged integer (i.e. between 0-65535. Priority was: "
|
||||
+ priority);
|
||||
|
||||
this.priority = priority;
|
||||
this.weight = weight;
|
||||
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public int compareTo(SRVRecord other) {
|
||||
// According to RFC2782,
|
||||
// "[a] client MUST attempt to contact the target host with the lowest-numbered priority it can reach".
|
||||
// This means that a SRV record with a higher priority is 'less' then one with a lower.
|
||||
int res = other.priority - this.priority;
|
||||
if (res == 0) {
|
||||
res = this.weight - other.weight;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return super.toString() + " prio:" + priority + ":w:" + weight;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue