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