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.dnsjava;
018
019import java.net.InetAddress;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
024import org.jivesoftware.smack.initializer.SmackInitializer;
025import org.jivesoftware.smack.util.DNSUtil;
026import org.jivesoftware.smack.util.dns.DNSResolver;
027import org.jivesoftware.smack.util.dns.HostAddress;
028import org.jivesoftware.smack.util.dns.SRVRecord;
029
030import org.minidns.dnsname.DnsName;
031import org.xbill.DNS.Lookup;
032import org.xbill.DNS.Record;
033import org.xbill.DNS.TextParseException;
034import org.xbill.DNS.Type;
035
036/**
037 * This implementation uses the <a href="http://www.dnsjava.org/">dnsjava</a> implementation for resolving DNS addresses.
038 *
039 */
040public class DNSJavaResolver extends DNSResolver implements SmackInitializer {
041
042    private static final DNSJavaResolver instance = new DNSJavaResolver();
043
044    public static DNSResolver getInstance() {
045        return instance;
046    }
047
048    public DNSJavaResolver() {
049        super(false);
050    }
051
052    @Override
053    protected List<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
054        List<SRVRecord> res = new ArrayList<>();
055
056        Lookup lookup;
057        try {
058            lookup = new Lookup(name.ace, Type.SRV);
059        }
060        catch (TextParseException e) {
061            throw new IllegalStateException(e);
062        }
063
064        Record[] recs = lookup.run();
065        if (recs == null)
066            return res;
067
068        for (Record record : recs) {
069            org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record;
070            if (srvRecord != null && srvRecord.getTarget() != null) {
071                DnsName host = DnsName.from(srvRecord.getTarget().toString());
072                int port = srvRecord.getPort();
073                int priority = srvRecord.getPriority();
074                int weight = srvRecord.getWeight();
075
076                List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
077                if (shouldContinue(name, host, hostAddresses)) {
078                    continue;
079                }
080
081                SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses);
082                res.add(r);
083            }
084        }
085
086        return res;
087    }
088
089    public static void setup() {
090        DNSUtil.setDNSResolver(getInstance());
091    }
092
093    @Override
094    public List<Exception> initialize() {
095        setup();
096        return null;
097    }
098
099}