Search in sources :

Example 11 with Lookup

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()]);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Lookup(org.xbill.DNS.Lookup) TextParseException(org.xbill.DNS.TextParseException)

Example 12 with Lookup

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;
}
Also used : SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) TextParseException(org.xbill.DNS.TextParseException)

Example 13 with Lookup

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;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) IOException(java.io.IOException) Socket(java.net.Socket) DatagramSocket(java.net.DatagramSocket) TextParseException(org.xbill.DNS.TextParseException)

Example 14 with Lookup

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;
}
Also used : ArrayList(java.util.ArrayList) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) SRVRecord(org.xbill.DNS.SRVRecord) TextParseException(org.xbill.DNS.TextParseException)

Example 15 with Lookup

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;
}
Also used : SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

Lookup (org.xbill.DNS.Lookup)33 Record (org.xbill.DNS.Record)26 TextParseException (org.xbill.DNS.TextParseException)12 ArrayList (java.util.ArrayList)10 SRVRecord (org.xbill.DNS.SRVRecord)10 Name (org.xbill.DNS.Name)8 SimpleResolver (org.xbill.DNS.SimpleResolver)6 UnknownHostException (java.net.UnknownHostException)5 List (java.util.List)5 Test (org.junit.Test)5 ARecord (org.xbill.DNS.ARecord)5 InetAddress (java.net.InetAddress)4 ExtendedResolver (org.xbill.DNS.ExtendedResolver)4 SortedSet (java.util.SortedSet)3 PTRRecord (org.xbill.DNS.PTRRecord)3 HashSet (java.util.HashSet)2 Cache (org.xbill.DNS.Cache)2 NSRecord (org.xbill.DNS.NSRecord)2 NonNull (androidx.annotation.NonNull)1 Stream (com.annimon.stream.Stream)1