@Override protected List<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) { List<SRVRecord> res = new ArrayList<>(); Lookup lookup; try { lookup = new Lookup(name.ace, Type.SRV); } catch (TextParseException e) { throw new IllegalStateException(e); } 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) { DnsName host = DnsName.from(srvRecord.getTarget().toString()); int port = srvRecord.getPort(); int priority = srvRecord.getPriority(); int weight = srvRecord.getWeight(); List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode); if (shouldContinue(name, host, hostAddresses)) { continue; } SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses); res.add(r); } } return res; }
/** * Creates the LDAP connection URLs from a set of SRV records. * @param retRecords SRV records containing the LDAP connection information. * @return List of URLs delimited by a space. */ protected String createLDAPUrl(Record[] retRecords){ StringBuilder builder = new StringBuilder(); // sort the records based on priority Arrays.sort(retRecords, new SRVRecordComparitor()); for (Record rec : retRecords){ SRVRecord srvRec = (SRVRecord)rec; if (builder.length() > 0) builder.append(" "); String target = srvRec.getTarget().toString(); target = (target.endsWith(".")) ? target.substring(0, target.length() - 1) : target; String url = "ldap://" + target+ ":" + srvRec.getPort(); builder.append(url); } return builder.toString(); }
public List<ResolvedAddress> resolveXmppServer(String domain) { List<ResolvedAddress> addresses = new ArrayList<ResolvedAddress>(); try { Record[] records = new Lookup("_xmpp-server._tcp." + domain, Type.SRV).run(); if(records != null) { for (int i = 0; i < records.length; i++) { SRVRecord srv = (SRVRecord) records[i]; addresses.add(new ResolvedAddress(srv.getTarget(), srv.getPort(), srv.getPriority())); } // sort by priority Collections.sort(addresses, new Comparator<ResolvedAddress>() { public int compare(ResolvedAddress a1, ResolvedAddress a2) { return a1.getPriority() - a2.getPriority(); } }); } else { addresses.add(new ResolvedAddress(domain, SERVER_PORT, 0)); } } catch (TextParseException e) { // ignore } return addresses; }
public int compare(Record o1, Record o2) { SRVRecord o1SRVRecord = (SRVRecord) o1; SRVRecord o2SRVRecord = (SRVRecord) o2; int o1Priority = o1SRVRecord.getPriority(); int o2Priority = o2SRVRecord.getPriority(); // the lower priority is the best if(o1Priority > o2Priority) return 1; if(o1Priority < o2Priority) return -1; // if they are the same sort them through weight int o1Weight = o1SRVRecord.getWeight(); int o2Weight = o2SRVRecord.getWeight(); // the higher weight is the best if(o1Weight < o2Weight) return 1; if(o1Weight > o2Weight) return -1; // RFC 3263 Section 4.4 return o1SRVRecord.getTarget().compareTo(o2SRVRecord.getTarget()); }
Record getObject() { return new SRVRecord(); }
@Nullable protected HostService toHostService(@Nonnull SRVRecord record) { final InetSocketAddress address = toInetSocketAddress(record); return address != null ? new HostService(address, record.getPriority(), record.getWeight(), record.getTTL()) : null; }
@Override public int compare(Record rec1, Record rec2) { if (((SRVRecord)rec1).getPriority() == ((SRVRecord)rec2).getPriority()) { if (((SRVRecord)rec1).getWeight() < ((SRVRecord)rec2).getWeight()) return 1; else return (((SRVRecord)rec1).getWeight() > ((SRVRecord)rec2).getWeight()) ? -1 : 0; } return (((SRVRecord)rec1).getPriority() < ((SRVRecord)rec2).getPriority()) ? -1 : 1; }
@Nullable protected InetSocketAddress toInetSocketAddress(@Nonnull SRVRecord record) { final InetAddress address = toInetAddress(record); return address != null ? new InetSocketAddress(address, record.getPort()) : null; }
SRVRecord rec = new SRVRecord(Name.fromString(name), DClass.IN, ttl, priority, weight, port, Name.fromString(target)); return DNSRecord.fromWire(rec.toWireCanonical());
@Nullable protected InetAddress toInetAddress(@Nonnull SRVRecord record) { final String name = record.getTarget().toString(); InetAddress address; try { address = getByAddress(name); } catch (final UnknownHostException ignored) { final Record [] records = createLookupFor(name, Type.A).run(); if (records != null && records.length > 0) { address = toInetAddress(name, (ARecord) records[0]); } else { address = null; } } return address; }
/** * Creates an SRV Record from the given data * @param priority The priority of this SRV. Records with lower priority * are preferred. * @param weight The weight, used to select between records at the same * priority. * @param port The TCP/UDP port that the service uses * @param target The host running the service */ public SRVRecord(Name name, int dclass, long ttl, int priority, int weight, int port, Name target) { super(name, Type.SRV, dclass, ttl); this.priority = checkU16("priority", priority); this.weight = checkU16("weight", weight); this.port = checkU16("port", port); this.target = checkName("target", target); }
SRVRecord srvRecord = new SRVRecord(service.getName(), DClass.IN, 3600, 0, 0, service.getPort(), service.getHost()); if (!srvRecord.equals(records[r])) records.add(new SRVRecord(shortSRVName, DClass.IN + CACHE_FLUSH, DEFAULT_SRV_TTL, 0, 0, service.getPort(), service.getHost())); records.add(new TXTRecord(shortSRVName, DClass.IN + CACHE_FLUSH, DEFAULT_TXT_TTL, Arrays.asList(service.getText()))); additionalRecords.add(new NSECRecord(shortSRVName, DClass.IN + CACHE_FLUSH, DEFAULT_RR_WITHOUT_HOST_TTL, shortSRVName, new int[] {Type.TXT, Type.SRV}));
public static Map<String, String> foundCachedEntry(String host, String transport, Record[] records) { Map<String, String> entry = cachedLookup.get(host+transport); if(entry == null) { return null; } String entryResolvedName = entry.get("hostName"); String hostAddress = entry.get("hostAddress"); String hostPort = entry.get("hostPort"); for (Record record : records) { if(record instanceof SRVRecord) { SRVRecord srvRecord = (SRVRecord) record; String resolvedName = srvRecord.getTarget().toString(); String resolvedHostAddress; try { resolvedHostAddress = InetAddress.getByName(resolvedName).getHostAddress(); int recordPort = srvRecord.getPort(); if(entryResolvedName.equalsIgnoreCase(resolvedName) && hostAddress.equalsIgnoreCase(resolvedHostAddress) && hostPort.equalsIgnoreCase("" + recordPort)) { return entry; } } catch (UnknownHostException e) { logger.warn("Couldn't resolve address " + resolvedName); } } } return null; } }
Record getObject() { return new SRVRecord(); }
SRVRecord rec = new SRVRecord(Name.fromString(name), DClass.IN, ttl, priority, weight, port, Name.fromString(target)); return fromWire(rec.toWireCanonical());
case Type.SRV: SRVRecord srv = (SRVRecord) record; additionalNames.add(srv.getTarget()); break; default:
/** * Creates an SRV Record from the given data * @param priority The priority of this SRV. Records with lower priority * are preferred. * @param weight The weight, used to select between records at the same * priority. * @param port The TCP/UDP port that the service uses * @param target The host running the service */ public SRVRecord(Name name, int dclass, long ttl, int priority, int weight, int port, Name target) { super(name, Type.SRV, dclass, ttl); this.priority = checkU16("priority", priority); this.weight = checkU16("weight", weight); this.port = checkU16("port", port); this.target = checkName("target", target); }
public ServiceInstance(final SRVRecord srv) throws TextParseException { this(new ServiceName(srv.getName()), srv.getPriority(), srv.getWeight(), srv.getPort(), srv.getTarget(), null, (Map) null); }