001/**
002 *
003 * Copyright 2013-2019 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.javax;
018
019import java.net.InetAddress;
020import java.util.ArrayList;
021import java.util.Hashtable;
022import java.util.List;
023import java.util.logging.Level;
024
025import javax.naming.NameNotFoundException;
026import javax.naming.NamingEnumeration;
027import javax.naming.NamingException;
028import javax.naming.directory.Attribute;
029import javax.naming.directory.Attributes;
030import javax.naming.directory.DirContext;
031import javax.naming.directory.InitialDirContext;
032
033import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
034import org.jivesoftware.smack.initializer.SmackInitializer;
035import org.jivesoftware.smack.util.DNSUtil;
036import org.jivesoftware.smack.util.dns.DNSResolver;
037import org.jivesoftware.smack.util.dns.HostAddress;
038import org.jivesoftware.smack.util.dns.SRVRecord;
039
040import org.minidns.dnsname.DnsName;
041
042/**
043 * A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.
044 * Note that using JavaxResovler requires applications using newer Java versions (at least 11) to declare a dependency on the "sun.jdk" module.
045 *
046 * @author Florian Schmaus
047 *
048 */
049public class JavaxResolver extends DNSResolver implements SmackInitializer {
050
051    private static JavaxResolver instance;
052    private static DirContext dirContext;
053
054    static {
055        try {
056            Hashtable<String, String> env = new Hashtable<>();
057            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
058            dirContext = new InitialDirContext(env);
059        } catch (NamingException e) {
060            LOGGER.log(Level.SEVERE, "Could not construct InitialDirContext", e);
061        }
062
063        // Try to set this DNS resolver as primary one
064        setup();
065    }
066
067    public static synchronized DNSResolver getInstance() {
068        if (instance == null && isSupported()) {
069            instance = new JavaxResolver();
070        }
071        return instance;
072    }
073
074    public static boolean isSupported() {
075        return dirContext != null;
076    }
077
078    public static void setup() {
079        DNSUtil.setDNSResolver(getInstance());
080    }
081
082    public JavaxResolver() {
083         super(false);
084    }
085
086    @Override
087    protected List<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
088        List<SRVRecord> res = null;
089
090        Attribute srvAttribute;
091        try {
092            Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
093            srvAttribute = dnsLookup.get("SRV");
094            if (srvAttribute == null)
095               return null;
096        } catch (NameNotFoundException e) {
097            LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
098            return null;
099        } catch (NamingException e) {
100            LOGGER.log(Level.WARNING, "Exception while resolving DNS SRV RR for " + name, e);
101            return null;
102        }
103
104        try {
105            @SuppressWarnings("unchecked")
106            NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
107            res = new ArrayList<>();
108            while (srvRecords.hasMore()) {
109                String srvRecordString = srvRecords.next();
110                String[] srvRecordEntries = srvRecordString.split(" ");
111                int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
112                int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
113                int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
114                String srvTarget = srvRecordEntries[srvRecordEntries.length - 1];
115                // Strip trailing '.' from srvTarget.
116                // Later MiniDNS version may do the right thing when DnsName.from() is called with a DNS name string
117                // having a trailing dot, so this can possibly be removed in future Smack versions.
118                if (srvTarget.length() > 0 && srvTarget.charAt(srvTarget.length() - 1) == '.') {
119                    srvTarget = srvTarget.substring(0, srvTarget.length() - 1);
120                }
121                DnsName host = DnsName.from(srvTarget);
122
123                List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
124                if (shouldContinue(name, host, hostAddresses)) {
125                    continue;
126                }
127
128                SRVRecord srvRecord = new SRVRecord(host, port, priority, weight, hostAddresses);
129                res.add(srvRecord);
130            }
131        }
132        catch (NamingException e) {
133            LOGGER.log(Level.SEVERE, "Exception while resolving DNS SRV RR for" + name, e);
134        }
135
136        return res;
137    }
138
139    @Override
140    public List<Exception> initialize() {
141        setup();
142        return null;
143    }
144
145}