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