001/**
002 *
003 * Copyright 2013-2020 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.util.ArrayList;
020import java.util.Hashtable;
021import java.util.List;
022import java.util.logging.Level;
023
024import javax.naming.NameNotFoundException;
025import javax.naming.NamingEnumeration;
026import javax.naming.NamingException;
027import javax.naming.directory.Attribute;
028import javax.naming.directory.Attributes;
029import javax.naming.directory.DirContext;
030import javax.naming.directory.InitialDirContext;
031
032import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
033import org.jivesoftware.smack.initializer.SmackInitializer;
034import org.jivesoftware.smack.util.DNSUtil;
035import org.jivesoftware.smack.util.dns.DNSResolver;
036import org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure;
037
038import org.minidns.dnsname.DnsName;
039import org.minidns.record.SRV;
040
041/**
042 * A DNS resolver (mostly for SRV records), which makes use of the API provided in the javax.* namespace.
043 * Note that using JavaxResovler requires applications using newer Java versions (at least 11) to declare a dependency on the "sun.jdk" module.
044 *
045 * @author Florian Schmaus
046 *
047 */
048public class JavaxResolver extends DNSResolver implements SmackInitializer {
049
050    private static JavaxResolver instance;
051    private static DirContext dirContext;
052
053    static {
054        try {
055            Hashtable<String, String> env = new Hashtable<>();
056            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
057            dirContext = new InitialDirContext(env);
058        } catch (NamingException e) {
059            LOGGER.log(Level.SEVERE, "Could not construct InitialDirContext", e);
060        }
061
062        // Try to set this DNS resolver as primary one
063        setup();
064    }
065
066    public static synchronized DNSResolver getInstance() {
067        if (instance == null && isSupported()) {
068            instance = new JavaxResolver();
069        }
070        return instance;
071    }
072
073    public static boolean isSupported() {
074        return dirContext != null;
075    }
076
077    public static void setup() {
078        DNSUtil.setDNSResolver(getInstance());
079    }
080
081    public JavaxResolver() {
082         super(false);
083    }
084
085    @Override
086    protected List<SRV> lookupSrvRecords0(DnsName name, List<RemoteConnectionEndpointLookupFailure> lookupFailures,
087                    DnssecMode dnssecMode) {
088        Attribute srvAttribute;
089        try {
090            Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
091            srvAttribute = dnsLookup.get("SRV");
092            if (srvAttribute == null)
093               return null;
094        } catch (NameNotFoundException e) {
095            LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
096            return null;
097        } catch (NamingException e) {
098            RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(
099                            name, e);
100            lookupFailures.add(failure);
101            return null;
102        }
103
104        List<SRV> res = new ArrayList<>();
105        try {
106            @SuppressWarnings("unchecked")
107            NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
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
122                SRV srvRecord = new SRV(priority, weight, port, srvTarget);
123                res.add(srvRecord);
124            }
125        }
126        catch (NamingException e) {
127            RemoteConnectionEndpointLookupFailure failure = new RemoteConnectionEndpointLookupFailure.DnsLookupFailure(
128                            name, e);
129            lookupFailures.add(failure);
130        }
131
132        return res;
133    }
134
135    @Override
136    public List<Exception> initialize() {
137        setup();
138        return null;
139    }
140
141}