001/** 002 * 003 * Copyright 2013-2016 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; 022import java.util.logging.Level; 023 024import org.jivesoftware.smack.ConnectionConfiguration.DnssecMode; 025import org.jivesoftware.smack.initializer.SmackInitializer; 026import org.jivesoftware.smack.util.DNSUtil; 027import org.jivesoftware.smack.util.dns.DNSResolver; 028import org.jivesoftware.smack.util.dns.HostAddress; 029import org.jivesoftware.smack.util.dns.SRVRecord; 030 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 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(String name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) { 054 List<SRVRecord> res = new ArrayList<SRVRecord>(); 055 056 Lookup lookup; 057 try { 058 lookup = new Lookup(name, 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 String host = 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 (hostAddresses == null || hostAddresses.isEmpty()) { 078 // If hostAddresses is not null but empty, then the DNS resolution was successful but the domain did not 079 // have any A or AAAA resource records. 080 if (hostAddresses.isEmpty()) { 081 LOGGER.log(Level.INFO, "The DNS name " + name + ", points to a hostname (" + host 082 + ") which has neither A or AAAA resource records. This is an indication of a broken DNS setup."); 083 } 084 continue; 085 } 086 087 SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses); 088 res.add(r); 089 } 090 } 091 092 return res; 093 } 094 095 public static void setup() { 096 DNSUtil.setDNSResolver(getInstance()); 097 } 098 099 @Override 100 public List<Exception> initialize() { 101 setup(); 102 return null; 103 } 104 105}