2013-03-18 09:53:11 +01:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
2019-03-02 14:47:28 +01:00
|
|
|
* Copyright 2013-2019 Florian Schmaus
|
2013-03-18 09:53:11 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-03-18 09:53:11 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-05-15 15:04:46 +02:00
|
|
|
package org.jivesoftware.smack.util.dns.javax;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2016-10-31 10:45:38 +01:00
|
|
|
import java.net.InetAddress;
|
2013-03-18 09:53:11 +01:00
|
|
|
import java.util.ArrayList;
|
2019-03-02 14:42:19 +01:00
|
|
|
import java.util.Hashtable;
|
2013-03-18 09:53:11 +01:00
|
|
|
import java.util.List;
|
2017-01-24 20:14:01 +01:00
|
|
|
import java.util.logging.Level;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2017-01-24 20:14:01 +01:00
|
|
|
import javax.naming.NameNotFoundException;
|
2013-03-18 09:53:11 +01:00
|
|
|
import javax.naming.NamingEnumeration;
|
2014-03-18 17:29:38 +01:00
|
|
|
import javax.naming.NamingException;
|
2013-03-18 09:53:11 +01:00
|
|
|
import javax.naming.directory.Attribute;
|
|
|
|
import javax.naming.directory.Attributes;
|
|
|
|
import javax.naming.directory.DirContext;
|
|
|
|
import javax.naming.directory.InitialDirContext;
|
|
|
|
|
2016-10-31 10:45:38 +01:00
|
|
|
import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
|
2014-12-18 12:57:21 +01:00
|
|
|
import org.jivesoftware.smack.initializer.SmackInitializer;
|
2013-03-18 09:53:11 +01:00
|
|
|
import org.jivesoftware.smack.util.DNSUtil;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.util.dns.DNSResolver;
|
2016-10-31 10:45:38 +01:00
|
|
|
import org.jivesoftware.smack.util.dns.HostAddress;
|
2014-05-15 15:04:46 +02:00
|
|
|
import org.jivesoftware.smack.util.dns.SRVRecord;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2018-05-05 10:24:45 +02:00
|
|
|
import org.minidns.dnsname.DnsName;
|
2018-04-24 21:41:31 +02:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
/**
|
2013-04-01 15:40:02 +02:00
|
|
|
* A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.
|
2019-03-02 14:47:28 +01:00
|
|
|
* Note that using JavaxResovler requires applications using newer Java versions (at least 11) to declare a dependency on the "sun.jdk" module.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2013-03-18 09:53:11 +01:00
|
|
|
* @author Florian Schmaus
|
|
|
|
*
|
|
|
|
*/
|
2016-10-31 10:45:38 +01:00
|
|
|
public class JavaxResolver extends DNSResolver implements SmackInitializer {
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
private static JavaxResolver instance;
|
|
|
|
private static DirContext dirContext;
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
static {
|
|
|
|
try {
|
2019-03-02 14:42:19 +01:00
|
|
|
Hashtable<String, String> env = new Hashtable<>();
|
|
|
|
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
|
|
|
|
dirContext = new InitialDirContext(env);
|
2019-02-16 10:03:32 +01:00
|
|
|
} catch (NamingException e) {
|
2019-02-16 10:03:05 +01:00
|
|
|
LOGGER.log(Level.SEVERE, "Could not construct InitialDirContext", e);
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to set this DNS resolver as primary one
|
2014-06-17 18:01:36 +02:00
|
|
|
setup();
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|
2014-06-17 18:01:36 +02:00
|
|
|
|
|
|
|
public static synchronized DNSResolver getInstance() {
|
2013-03-18 09:53:11 +01:00
|
|
|
if (instance == null && isSupported()) {
|
|
|
|
instance = new JavaxResolver();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
2014-06-17 18:01:36 +02:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
public static boolean isSupported() {
|
|
|
|
return dirContext != null;
|
|
|
|
}
|
|
|
|
|
2014-06-17 18:01:36 +02:00
|
|
|
public static void setup() {
|
|
|
|
DNSUtil.setDNSResolver(getInstance());
|
|
|
|
}
|
|
|
|
|
2016-10-31 10:45:38 +01:00
|
|
|
public JavaxResolver() {
|
|
|
|
super(false);
|
|
|
|
}
|
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
@Override
|
2018-05-05 10:24:45 +02:00
|
|
|
protected List<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
|
2017-01-24 20:14:01 +01:00
|
|
|
List<SRVRecord> res = null;
|
2013-03-18 09:53:11 +01:00
|
|
|
|
2017-01-24 20:14:01 +01:00
|
|
|
Attribute srvAttribute;
|
2016-10-31 10:45:38 +01:00
|
|
|
try {
|
2018-04-24 21:41:31 +02:00
|
|
|
Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
|
2017-01-24 20:14:01 +01:00
|
|
|
srvAttribute = dnsLookup.get("SRV");
|
2016-10-31 10:45:38 +01:00
|
|
|
if (srvAttribute == null)
|
2017-01-24 20:14:01 +01:00
|
|
|
return null;
|
|
|
|
} catch (NameNotFoundException e) {
|
|
|
|
LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
|
|
|
|
return null;
|
|
|
|
} catch (NamingException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Exception while resolving DNS SRV RR for " + name, e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2016-10-31 10:45:38 +01:00
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
|
2017-01-24 20:14:01 +01:00
|
|
|
res = new ArrayList<>();
|
2016-10-31 10:45:38 +01:00
|
|
|
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]);
|
2018-04-24 21:41:31 +02:00
|
|
|
String srvTarget = srvRecordEntries[srvRecordEntries.length - 1];
|
|
|
|
// Strip trailing '.' from srvTarget.
|
2018-05-05 10:24:45 +02:00
|
|
|
// Later MiniDNS version may do the right thing when DnsName.from() is called with a DNS name string
|
2018-04-24 21:41:31 +02:00
|
|
|
// having a trailing dot, so this can possibly be removed in future Smack versions.
|
|
|
|
if (srvTarget.length() > 0 && srvTarget.charAt(srvTarget.length() - 1) == '.') {
|
|
|
|
srvTarget = srvTarget.substring(0, srvTarget.length() - 1);
|
|
|
|
}
|
2018-05-05 10:24:45 +02:00
|
|
|
DnsName host = DnsName.from(srvTarget);
|
2016-10-31 10:45:38 +01:00
|
|
|
|
|
|
|
List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
|
2017-12-01 09:28:21 +01:00
|
|
|
if (shouldContinue(name, host, hostAddresses)) {
|
2016-10-31 10:45:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SRVRecord srvRecord = new SRVRecord(host, port, priority, weight, hostAddresses);
|
|
|
|
res.add(srvRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (NamingException e) {
|
2017-01-24 20:14:01 +01:00
|
|
|
LOGGER.log(Level.SEVERE, "Exception while resolving DNS SRV RR for" + name, e);
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|
2016-10-31 10:45:38 +01:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
return res;
|
|
|
|
}
|
2014-06-17 18:01:36 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Exception> initialize() {
|
|
|
|
setup();
|
2014-08-11 19:21:43 +02:00
|
|
|
return null;
|
2014-06-17 18:01:36 +02:00
|
|
|
}
|
2014-08-11 19:21:43 +02:00
|
|
|
|
2013-03-18 09:53:11 +01:00
|
|
|
}
|