use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DnsSdDiscoveryAdvertiser method getAuthoritativeNameServer.
protected Collection getAuthoritativeNameServer(final Name zone) throws TextParseException {
// $NON-NLS-1$ //$NON-NLS-2$
Trace.trace(Activator.PLUGIN_ID, DnsSdDebugOptions.METHODS_TRACING, this.getClass(), "getAuthoritativeNameServer(Name zone)", "Trying to find authoritative name server");
final Set result = new HashSet();
final Name name = new Name(_DNS_UPDATE + zone);
// query for NS records
Lookup query = new Lookup(zone, Type.NS);
query.setResolver(resolver);
Record[] queryResult = query.run();
// TODO file bug upstream that queryResult may never be null
int length = queryResult == null ? 0 : queryResult.length;
for (int j = 0; j < length; j++) {
final Record record = queryResult[j];
if (record instanceof NSRecord) {
final NSRecord nsRecord = (NSRecord) record;
final Name target = nsRecord.getTarget();
result.add(new SRVRecord(name, DClass.IN, nsRecord.getTTL(), 0, 0, SimpleResolver.DEFAULT_PORT, target));
}
}
// query for primary ns in SOA record (may overwrite/be equal to one of the ns records)
query = new Lookup(zone, Type.SOA);
query.setResolver(resolver);
queryResult = query.run();
// TODO file bug upstream that queryResult may never be null
length = queryResult == null ? 0 : queryResult.length;
for (int j = 0; j < length; j++) {
final Record record = queryResult[j];
if (record instanceof SOARecord) {
final SOARecord soaRecord = (SOARecord) record;
result.add(new SRVRecord(name, DClass.IN, soaRecord.getTTL(), 0, 0, SimpleResolver.DEFAULT_PORT, soaRecord.getHost()));
}
}
return result;
}
use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DnsSdDiscoveryContainerAdapter method getSRVRecord.
protected SortedSet getSRVRecord(Lookup query, Comparator aComparator) {
final SortedSet srvRecords = new TreeSet(aComparator);
query.setResolver(resolver);
final Record[] queryResult = query.run();
// TODO file bug upstream that queryResult may never be null
final int length = queryResult == null ? 0 : queryResult.length;
for (int j = 0; j < length; j++) {
Record[] srvQueryResult = null;
final Record record = queryResult[j];
if (record instanceof PTRRecord) {
final PTRRecord ptrRecord = (PTRRecord) record;
final Name target = ptrRecord.getTarget();
final Lookup srvQuery = new Lookup(target, Type.SRV);
srvQuery.setResolver(resolver);
srvQueryResult = srvQuery.run();
} else if (record instanceof SRVRecord) {
srvQueryResult = new SRVRecord[] { (SRVRecord) record };
} else {
// avoid NPE
srvQueryResult = new SRVRecord[0];
}
srvRecords.addAll(Arrays.asList(srvQueryResult));
}
return srvRecords;
}
use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DnsSdDiscoveryContainerAdapter method getRecords.
protected Record[] getRecords(final DnsSdServiceTypeID serviceTypeId) {
final List result = new ArrayList();
final Lookup[] queries = serviceTypeId.getInternalQueries();
for (int i = 0; i < queries.length; i++) {
final Lookup query = queries[i];
query.setResolver(resolver);
final Record[] queryResult = query.run();
if (queryResult != null) {
result.addAll(Arrays.asList(queryResult));
}
}
return (Record[]) result.toArray(new Record[result.size()]);
}
use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DnsSdDiscoveryLocator method getServiceInfos.
private List getServiceInfos(Collection srvQueryResult) {
List infos = new ArrayList();
for (Iterator iterator = srvQueryResult.iterator(); iterator.hasNext(); ) {
SRVRecord srvRecord = (SRVRecord) iterator.next();
long ttl = srvRecord.getTTL();
int priority = srvRecord.getPriority();
int weight = srvRecord.getWeight();
int port = srvRecord.getPort();
Name target = srvRecord.getTarget();
String host = target.toString();
host = host.substring(0, host.length() - 1);
IServiceTypeID aServiceTypeID = new DnsSdServiceTypeID(getConnectNamespace(), srvRecord.getName());
// query for txt records (attributes)
Properties props = new Properties();
Lookup txtQuery = new Lookup(srvRecord.getName(), Type.TXT);
txtQuery.setResolver(resolver);
Record[] txtQueryResults = txtQuery.run();
int length = txtQueryResults == null ? 0 : txtQueryResults.length;
for (int l = 0; l < length; l++) {
TXTRecord txtResult = (TXTRecord) txtQueryResults[l];
List strings = txtResult.getStrings();
for (Iterator itr = strings.iterator(); itr.hasNext(); ) {
String str = (String) itr.next();
// $NON-NLS-1$
String[] split = str.split("=");
props.put(split[0], split[1]);
}
}
String path = props.getProperty(DNS_SD_PATH);
String proto = props.getProperty(DNS_SD_PTCL) == null ? aServiceTypeID.getProtocols()[0] : props.getProperty(DNS_SD_PTCL);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
URI uri = URI.create(proto + "://" + host + ":" + port + (path == null ? "" : path));
IServiceInfo info = new ServiceInfo(uri, host, aServiceTypeID, priority, weight, new ServiceProperties(props), ttl);
infos.add(info);
}
return infos;
}
use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DNSJavaResolver method lookupSRVRecords.
public List<SRVRecord> lookupSRVRecords(String name) {
List<SRVRecord> res = new ArrayList<SRVRecord>();
try {
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;
try {
r = new SRVRecord(host, port, priority, weight);
} catch (Exception e) {
continue;
}
res.add(r);
}
}
} catch (Exception e) {
}
return res;
}
Aggregations