001/**
002 *
003 * Copyright 2013-2017 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;
018
019import java.net.InetAddress;
020import java.net.UnknownHostException;
021import java.util.Arrays;
022import java.util.List;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025
026import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode;
027
028/**
029 * Implementations of this interface define a class that is capable of resolving DNS addresses.
030 *
031 */
032public abstract class DNSResolver {
033
034    protected static final Logger LOGGER = Logger.getLogger(DNSResolver.class.getName());
035
036    private final boolean supportsDnssec;
037
038    protected DNSResolver(boolean supportsDnssec) {
039        this.supportsDnssec = supportsDnssec;
040    }
041
042    /**
043     * Gets a list of service records for the specified service.
044     * @param name The symbolic name of the service.
045     * @param failedAddresses list of failed addresses.
046     * @param dnssecMode security mode.
047     * @return The list of SRV records mapped to the service name.
048     */
049    public final List<SRVRecord> lookupSRVRecords(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
050        checkIfDnssecRequestedAndSupported(dnssecMode);
051        return lookupSRVRecords0(name, failedAddresses, dnssecMode);
052    }
053
054    protected abstract List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode);
055
056    public final HostAddress lookupHostAddress(String name, int port, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
057        checkIfDnssecRequestedAndSupported(dnssecMode);
058        List<InetAddress> inetAddresses = lookupHostAddress0(name, failedAddresses, dnssecMode);
059        if (inetAddresses == null || inetAddresses.isEmpty()) {
060            return null;
061        }
062        return new HostAddress(name, port, inetAddresses);
063    }
064
065    /**
066     * Lookup the IP addresses of a given host name. Returns <code>null</code> if there was an error, in which the error
067     * reason will be added in form of a <code>HostAddress</code> to <code>failedAddresses</code>. Returns a empty list
068     * in case the DNS name exists but has no associated A or AAAA resource records. Otherwise, if the resolution was
069     * successful <em>and</em> there is at least one A or AAAA resource record, then a non-empty list will be returned.
070     * <p>
071     * Concrete DNS resolver implementations are free to overwrite this, but have to stick to the interface contract.
072     * </p>
073     *
074     * @param name the DNS name to lookup
075     * @param failedAddresses a list with the failed addresses
076     * @param dnssecMode the selected DNSSEC mode
077     * @return A list, either empty or non-empty, or <code>null</code>
078     */
079    protected List<InetAddress> lookupHostAddress0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) {
080        // Default implementation of a DNS name lookup for A/AAAA records. It is assumed that this method does never
081        // support DNSSEC. Subclasses are free to override this method.
082        if (dnssecMode != DnssecMode.disabled) {
083            throw new UnsupportedOperationException("This resolver does not support DNSSEC");
084        }
085
086        InetAddress[] inetAddressArray;
087        try {
088            inetAddressArray = InetAddress.getAllByName(name);
089        } catch (UnknownHostException e) {
090            failedAddresses.add(new HostAddress(name, e));
091            return null;
092        }
093
094        return Arrays.asList(inetAddressArray);
095    }
096
097    protected final boolean shouldContinue(CharSequence name, CharSequence hostname, List<InetAddress> hostAddresses) {
098        if (hostAddresses == null) {
099            return true;
100        }
101
102        // If hostAddresses is not null but empty, then the DNS resolution was successful but the domain did not
103        // have any A or AAAA resource records.
104        if (hostAddresses.isEmpty()) {
105            LOGGER.log(Level.INFO, "The DNS name " + name + ", points to a hostname (" + hostname
106                            + ") which has neither A or AAAA resource records. This is an indication of a broken DNS setup.");
107            return true;
108        }
109
110        return false;
111    }
112
113    private final void checkIfDnssecRequestedAndSupported(DnssecMode dnssecMode) {
114        if (dnssecMode != DnssecMode.disabled && !supportsDnssec) {
115            throw new UnsupportedOperationException("This resolver does not support DNSSEC");
116        }
117    }
118}