001/**
002 *
003 * Copyright 2013-2018 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 *
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 (Exception e) {
059            // Ignore.
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<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
087        List<SRVRecord> res = null;
088
089        Attribute srvAttribute;
090        try {
091            Attributes dnsLookup = dirContext.getAttributes(name.ace, new String[] { "SRV" });
092            srvAttribute = dnsLookup.get("SRV");
093            if (srvAttribute == null)
094               return null;
095        } catch (NameNotFoundException e) {
096            LOGGER.log(Level.FINEST, "No DNS SRV RR found for " + name, e);
097            return null;
098        } catch (NamingException e) {
099            LOGGER.log(Level.WARNING, "Exception while resolving DNS SRV RR for " + name, e);
100            return null;
101        }
102
103        try {
104            @SuppressWarnings("unchecked")
105            NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll();
106            res = new ArrayList<>();
107            while (srvRecords.hasMore()) {
108                String srvRecordString = srvRecords.next();
109                String[] srvRecordEntries = srvRecordString.split(" ");
110                int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]);
111                int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]);
112                int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]);
113                String srvTarget = srvRecordEntries[srvRecordEntries.length - 1];
114                // Strip trailing '.' from srvTarget.
115                // Later MiniDNS version may do the right thing when DnsName.from() is called with a DNS name string
116                // having a trailing dot, so this can possibly be removed in future Smack versions.
117                if (srvTarget.length() > 0 && srvTarget.charAt(srvTarget.length() - 1) == '.') {
118                    srvTarget = srvTarget.substring(0, srvTarget.length() - 1);
119                }
120                DnsName host = DnsName.from(srvTarget);
121
122                List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
123                if (shouldContinue(name, host, hostAddresses)) {
124                    continue;
125                }
126
127                SRVRecord srvRecord = new SRVRecord(host, port, priority, weight, hostAddresses);
128                res.add(srvRecord);
129            }
130        }
131        catch (NamingException e) {
132            LOGGER.log(Level.SEVERE, "Exception while resolving DNS SRV RR for" + name, e);
133        }
134
135        return res;
136    }
137
138    @Override
139    public List<Exception> initialize() {
140        setup();
141        return null;
142    }
143
144}