1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49: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:
Florian Schmaus 2013-03-18 08:53:11 +00:00 committed by flow
parent 21be8c55ee
commit 2eb13f48d2
12 changed files with 756 additions and 210 deletions

View 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;
}
}

View 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);
}

View 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;
}
}

View 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;
}
}

View 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;
}
}