@Override public List<SRVRecord> lookupSRVRecords(String name) throws TextParseException { List<SRVRecord> res = new ArrayList<SRVRecord>(); Lookup lookup = new Lookup(name, Type.SRV); Record[] recs = lookup.run(); if (recs == null) return res; for (Record record : recs) { org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record; if (srvRecord != null && srvRecord.getTarget() != null) { String host = srvRecord.getTarget().toString(); int port = srvRecord.getPort(); int priority = srvRecord.getPriority(); int weight = srvRecord.getWeight(); SRVRecord r = new SRVRecord(host, port, priority, weight); res.add(r); } } return res; }
@Override public List<SRVRecord> lookupSRVRecords(String name) throws NamingException { List<SRVRecord> res = new ArrayList<SRVRecord>(); Attributes dnsLookup = dirContext.getAttributes(name, new String[] { "SRV" }); Attribute srvAttribute = dnsLookup.get("SRV"); if (srvAttribute == null) return res; @SuppressWarnings("unchecked") NamingEnumeration<String> srvRecords = (NamingEnumeration<String>) srvAttribute.getAll(); while (srvRecords.hasMore()) { String srvRecordString = srvRecords.next(); String[] srvRecordEntries = srvRecordString.split(" "); int priority = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 4]); int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]); int weight = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 3]); String host = srvRecordEntries[srvRecordEntries.length - 1]; SRVRecord srvRecord = new SRVRecord(host, port, priority, weight); res.add(srvRecord); } return res; }
private static List<SRVRecord> createSRVRecords() { List<SRVRecord> records = new ArrayList<SRVRecord>(); // We create one record with priority 0 that should also be tried first // Then 4 records with priority 5 and different weights (50, 20, 20, 10) // Then 2 records with priority 10 and weight 0 which should be treaded equal // These records are added in a 'random' way to the list try { records.add(new SRVRecord("5.20.one.foo.bar", 42, 5, 20)); // Priority 5, Weight 20 records.add(new SRVRecord("10.0.one.foo.bar", 42, 10, 0)); // Priority 10, Weight 0 records.add(new SRVRecord("5.10.foo.bar", 42, 5, 10)); // Priority 5, Weight 10 records.add(new SRVRecord("10.0.two.foo.bar", 42, 10, 0)); // Priority 10, Weight 0 records.add(new SRVRecord("5.20.two.foo.bar", 42, 5, 20)); // Priority 5, Weight 20 records.add(new SRVRecord("0.20.foo.bar", 42, 0, 20)); // Priority 0, Weight 20 records.add(new SRVRecord("5.50.foo.bar", 42, 5, 50)); // Priority 5, Weight 50 } catch (IllegalArgumentException e) { // Ignore } assertTrue(records.size() > 0); return records; }
@Override public List<SRVRecord> lookupSRVRecords(String name) { List<SRVRecord> res = new LinkedList<SRVRecord>(); DNSMessage message = client.query(name, TYPE.SRV, CLASS.IN); if (message == null) { return res; } for (Record record : message.getAnswers()) { SRV srv = (SRV) record.getPayload(); res.add(new SRVRecord(srv.getName(), srv.getPort(), srv.getPriority(), srv.getWeight())); } return res; }
/** * * @param domain the domain. * @param domainType the XMPP domain type, server or client. * @param failedAddresses on optional list that will be populated with host addresses that failed to resolve. * @return a list of resolver host addresses for this domain. */ private static List<HostAddress> resolveDomain(String domain, DomainType domainType, List<HostAddress> failedAddresses) { List<HostAddress> addresses = new ArrayList<HostAddress>(); // Step one: Do SRV lookups String srvDomain; switch (domainType) { case Server: srvDomain = "_xmpp-server._tcp." + domain; break; case Client: srvDomain = "_xmpp-client._tcp." + domain; break; default: throw new AssertionError(); } try { List<SRVRecord> srvRecords = dnsResolver.lookupSRVRecords(srvDomain); if (LOGGER.isLoggable(Level.FINE)) { String logMessage = "Resolved SRV RR for " + srvDomain + ":"; for (SRVRecord r : srvRecords) logMessage += " " + r; LOGGER.fine(logMessage); } List<HostAddress> sortedRecords = sortSRVRecords(srvRecords); addresses.addAll(sortedRecords); } catch (Exception e) { LOGGER.log(Level.WARNING, "Exception while resovling SRV records for " + domain + ". Consider adding '_xmpp-(server|client)._tcp' DNS SRV Records", e); if (failedAddresses != null) { HostAddress failedHostAddress = new HostAddress(srvDomain); failedHostAddress.setException(e); failedAddresses.add(failedHostAddress); } } // Step two: Add the hostname to the end of the list addresses.add(new HostAddress(domain)); return addresses; }