use of org.xbill.DNS.Lookup in project ecf by eclipse.
the class DnsSdServiceTypeID method getInternalQueries.
Lookup[] getInternalQueries() {
String[] protos = protocols;
int type = Type.SRV;
String service = null;
if (services == null || services.length == 0 || (services.length == 1 && services[0].equals(""))) {
// $NON-NLS-1$
// if no service is set, create a non service specific query
// $NON-NLS-1$
service = "_services._dns-sd._";
// and set proto to "udp" irregardless what has been set
// $NON-NLS-1$
protos = new String[] { "udp" };
// and query for PTR records
type = Type.PTR;
} else {
// $NON-NLS-1$
service = "_";
for (int i = 0; i < services.length; i++) {
service += services[i];
// $NON-NLS-1$
service += "._";
}
}
List result = new ArrayList();
for (int i = 0; i < scopes.length; i++) {
String scope = scopes[i];
// remove dangling "."
if (scope.endsWith(".")) {
// $NON-NLS-1$
scope = scope.substring(0, scope.length() - 1);
}
for (int j = 0; j < protos.length; j++) {
Lookup query;
try {
query = new // $NON-NLS-1$ //$NON-NLS-2$
Lookup(// $NON-NLS-1$ //$NON-NLS-2$
service + protos[j] + "." + scope + ".", type);
} catch (TextParseException e) {
continue;
}
result.add(query);
}
}
return (Lookup[]) result.toArray(new Lookup[result.size()]);
}
use of org.xbill.DNS.Lookup in project cloudbreak by hortonworks.
the class DNSServiceAddressResolver method dnsSrvLookup.
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException {
String result;
try {
Record[] records = new Lookup(query, Type.SRV).run();
if (records != null && records.length > 0) {
SRVRecord srv = (SRVRecord) records[0];
result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort();
} else {
throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved");
}
} catch (TextParseException e) {
throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e);
}
return result;
}
use of org.xbill.DNS.Lookup in project PorkBot by DaMatrix.
the class Query method pingServer.
/**
* Try pinging the server
*
* @return <code>true</code> if the server can be reached within 1.5 second
*/
public boolean pingServer() {
// try pinging the given server
String service = "_minecraft._tcp." + address.getHostName();
try {
Record[] records = new Lookup(service, Type.SRV).run();
if (records != null)
for (Record record : records) {
SRVRecord srv = (SRVRecord) record;
String hostname = srv.getTarget().toString();
int port = srv.getPort();
System.out.println(hostname + ":" + port);
address = new InetSocketAddress(hostname, port);
queryAddress = new InetSocketAddress(hostname, port);
}
} catch (TextParseException e1) {
e1.printStackTrace();
}
try {
final Socket socket = new Socket();
socket.connect(address, 1500);
socket.close();
return true;
} catch (IOException e) {
}
return false;
}
use of org.xbill.DNS.Lookup in project fabric8 by fabric8io.
the class KubernetesHelper method lookupServiceEndpointsInDns.
/**
* Looks up the service endpoints in DNS.
* <p/>
* Endpoints are registered as SRV records in DNS so this method returns
* endpoints in the format "host:port". This is a list as SRV records are ordered
* by priority & weight before being returned to the client.
* <p/>
* See https://github.com/GoogleCloudPlatform/kubernetes/blob/master/cluster/addons/dns/README.md
*/
public static List<String> lookupServiceEndpointsInDns(String serviceName) throws IllegalArgumentException, UnknownHostException {
try {
Lookup l = new Lookup(serviceName, Type.SRV);
Record[] records = l.run();
if (l.getResult() == Lookup.SUCCESSFUL) {
SRVRecord[] srvRecords = Arrays.copyOf(records, records.length, SRVRecord[].class);
Arrays.sort(srvRecords, new Comparator<SRVRecord>() {
@Override
public int compare(SRVRecord a, SRVRecord b) {
int ret = Integer.compare(b.getPriority(), a.getPriority());
if (ret == 0) {
ret = Integer.compare(b.getWeight(), a.getWeight());
}
return ret;
}
});
List<String> endpointAddresses = new ArrayList<>(srvRecords.length);
for (SRVRecord srvRecord : srvRecords) {
endpointAddresses.add(srvRecord.getTarget().toString(true).concat(":").concat(String.valueOf(srvRecord.getPort())));
}
return endpointAddresses;
} else {
LOG.warn("Lookup {} result: {}", serviceName, l.getErrorString());
}
} catch (TextParseException e) {
LOG.error("Unparseable service name: {}", serviceName, e);
} catch (ClassCastException e) {
LOG.error("Invalid response from DNS server - should have been A records", e);
}
return Collections.EMPTY_LIST;
}
use of org.xbill.DNS.Lookup in project cloudbreak by hortonworks.
the class DNSServiceAddressResolver method dnsSrvLookup.
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException {
String result;
try {
Record[] records = new Lookup(query, Type.SRV).run();
if (records != null && records.length > 0) {
SRVRecord srv = (SRVRecord) records[0];
result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort();
} else {
throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved");
}
} catch (TextParseException e) {
throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e);
}
return result;
}
Aggregations