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 protected List<InetAddress> lookupHostAddress0(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) { 063 // Default implementation of a DNS name lookup for A/AAAA records. It is assumed that this method does never 064 // support DNSSEC. Subclasses are free to override this method. 065 if (dnssecMode != DnssecMode.disabled) { 066 throw new UnsupportedOperationException("This resolver does not support DNSSEC"); 067 } 068 069 InetAddress[] inetAddressArray; 070 try { 071 inetAddressArray = InetAddress.getAllByName(name); 072 } catch (UnknownHostException e) { 073 failedAddresses.add(new HostAddress(name, e)); 074 return null; 075 } 076 077 return Arrays.asList(inetAddressArray); 078 } 079 080 private final void checkIfDnssecRequestedAndSupported(DnssecMode dnssecMode) { 081 if (dnssecMode != DnssecMode.disabled && !supportsDnssec) { 082 throw new UnsupportedOperationException("This resolver does not support DNSSEC"); 083 } 084 } 085}