Search in sources :

Example 21 with Lookup

use of org.xbill.DNS.Lookup in project camel by apache.

the class DnsLookupProducer method process.

@Override
public void process(Exchange exchange) throws Exception {
    String dnsName = exchange.getIn().getHeader(DnsConstants.DNS_NAME, String.class);
    ObjectHelper.notEmpty(dnsName, "Header " + DnsConstants.DNS_NAME);
    Object type = exchange.getIn().getHeader(DnsConstants.DNS_TYPE);
    Integer dnsType = null;
    if (type != null) {
        dnsType = Type.value(String.valueOf(type));
    }
    Object dclass = exchange.getIn().getHeader(DnsConstants.DNS_CLASS);
    Integer dnsClass = null;
    if (dclass != null) {
        dnsClass = DClass.value(String.valueOf(dclass));
    }
    Lookup lookup = (dnsClass == null) ? (dnsType == null ? new Lookup(dnsName) : new Lookup(dnsName, dnsType)) : new Lookup(dnsName, dnsType, dnsClass);
    lookup.run();
    if (lookup.getAnswers() != null) {
        exchange.getIn().setBody(lookup.getAnswers());
    } else {
        throw new CamelException(lookup.getErrorString());
    }
}
Also used : CamelException(org.apache.camel.CamelException) Lookup(org.xbill.DNS.Lookup)

Example 22 with Lookup

use of org.xbill.DNS.Lookup in project opennms by OpenNMS.

the class DnsUtils method resolveHostname.

/**
 * This function is used inside XSLT documents, do a string search before refactoring.
 */
public static InetAddress resolveHostname(final String hostname, final boolean preferInet6Address, final boolean throwException) throws UnknownHostException {
    InetAddress retval = null;
    // return valid A and AAAA records for "localhost".
    if ("localhost".equals(hostname)) {
        return preferInet6Address ? InetAddress.getByName("::1") : InetAddress.getByName("127.0.0.1");
    }
    try {
        // 2011-05-22 - Matt is seeing some platform-specific inconsistencies when using
        // InetAddress.getAllByName(). It seems to miss some addresses occasionally on Mac.
        // We need to use dnsjava here instead since it should be 100% reliable.
        // 
        // InetAddress[] addresses = InetAddress.getAllByName(hostname);
        // 
        List<InetAddress> v4Addresses = new ArrayList<>();
        try {
            Record[] aRecs = new Lookup(hostname, Type.A).run();
            if (aRecs != null) {
                for (Record aRec : aRecs) {
                    if (aRec instanceof ARecord) {
                        InetAddress addr = ((ARecord) aRec).getAddress();
                        if (addr instanceof Inet4Address) {
                            v4Addresses.add(addr);
                        } else {
                            // Should never happen
                            throw new UnknownHostException("Non-IPv4 address found via A record DNS lookup of host: " + hostname + ": " + addr.toString());
                        }
                    }
                }
            } else {
            // throw new UnknownHostException("No IPv4 addresses found via A record DNS lookup of host: " + hostname);
            }
        } catch (final TextParseException e) {
            final UnknownHostException ex = new UnknownHostException("Could not perform A record lookup for host: " + hostname);
            ex.initCause(e);
            throw ex;
        }
        final List<InetAddress> v6Addresses = new ArrayList<>();
        try {
            final Record[] quadARecs = new Lookup(hostname, Type.AAAA).run();
            if (quadARecs != null) {
                for (final Record quadARec : quadARecs) {
                    final InetAddress addr = ((AAAARecord) quadARec).getAddress();
                    if (addr instanceof Inet6Address) {
                        v6Addresses.add(addr);
                    } else {
                        // Should never happen
                        throw new UnknownHostException("Non-IPv6 address found via AAAA record DNS lookup of host: " + hostname + ": " + addr.toString());
                    }
                }
            } else {
            // throw new UnknownHostException("No IPv6 addresses found via AAAA record DNS lookup of host: " + hostname);
            }
        } catch (final TextParseException e) {
            final UnknownHostException ex = new UnknownHostException("Could not perform AAAA record lookup for host: " + hostname);
            ex.initCause(e);
            throw ex;
        }
        final List<InetAddress> addresses = new ArrayList<>();
        if (preferInet6Address) {
            addresses.addAll(v6Addresses);
            addresses.addAll(v4Addresses);
        } else {
            addresses.addAll(v4Addresses);
            addresses.addAll(v6Addresses);
        }
        for (final InetAddress address : addresses) {
            retval = address;
            if (!preferInet6Address && retval instanceof Inet4Address)
                break;
            if (preferInet6Address && retval instanceof Inet6Address)
                break;
        }
        if (preferInet6Address && !(retval instanceof Inet6Address)) {
            throw new UnknownHostException("No IPv6 address could be found for the hostname: " + hostname);
        }
    } catch (final UnknownHostException e) {
        if (throwException) {
            throw e;
        } else {
            // e.printStackTrace();
            return null;
        }
    }
    return retval;
}
Also used : ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Inet4Address(java.net.Inet4Address) AAAARecord(org.xbill.DNS.AAAARecord) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) Record(org.xbill.DNS.Record) Lookup(org.xbill.DNS.Lookup) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Example 23 with Lookup

use of org.xbill.DNS.Lookup in project fabric8 by fabric8io.

the class KubernetesHelper method lookupServiceInDns.

/**
 * Looks up the service in DNS.
 * If this is a headless service, this call returns the endpoint IPs from DNS.
 * If this is a non-headless service, this call returns the service IP only.
 * <p/>
 * See https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/services.md#headless-services
 */
public static Set<String> lookupServiceInDns(String serviceName) throws IllegalArgumentException, UnknownHostException {
    try {
        Lookup l = new Lookup(serviceName);
        Record[] records = l.run();
        if (l.getResult() == Lookup.SUCCESSFUL) {
            Set<String> endpointAddresses = new HashSet<>(records.length);
            for (int i = 0; i < records.length; i++) {
                ARecord aRecord = (ARecord) records[i];
                endpointAddresses.add(aRecord.getAddress().getHostAddress());
            }
            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_SET;
}
Also used : ARecord(org.xbill.DNS.ARecord) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) HashSet(java.util.HashSet) TextParseException(org.xbill.DNS.TextParseException)

Example 24 with Lookup

use of org.xbill.DNS.Lookup in project ecf by eclipse.

the class BnRDnsSdServiceTypeID method getInternalQueries.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.provider.dnssd.DnsSdServiceTypeID#getInternalQueries()
	 */
Lookup[] getInternalQueries() {
    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);
        }
        Lookup query;
        try {
            query = new // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            Lookup(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            services[0] + "._udp" + "." + scope + ".", Type.PTR);
        } catch (TextParseException e) {
            continue;
        }
        result.add(query);
    }
    return (Lookup[]) result.toArray(new Lookup[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Lookup(org.xbill.DNS.Lookup) TextParseException(org.xbill.DNS.TextParseException)

Example 25 with Lookup

use of org.xbill.DNS.Lookup in project ecf by eclipse.

the class DnsSdDiscoveryAdvertiser method getUpdateDomain.

protected Collection getUpdateDomain(final Name zone) throws TextParseException {
    // $NON-NLS-1$ //$NON-NLS-2$
    Trace.trace(Activator.PLUGIN_ID, DnsSdDebugOptions.METHODS_TRACING, this.getClass(), "getUpdateDomain(Name zone)", "Getting update domain");
    // query for special "_dns-update" SRV records which mark the server to use for dyndns
    final Lookup query = new Lookup(_DNS_UPDATE + zone, Type.SRV);
    // use the SRV record with the lowest priority/weight first
    final SortedSet srvRecords = getSRVRecord(query, new SRVRecordComparator());
    // if no dedicated "_dns-update" server is configured, fall back to regular authoritative server
    if (srvRecords.size() == 0) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Trace.trace(Activator.PLUGIN_ID, DnsSdDebugOptions.METHODS_TRACING, this.getClass(), "getUpdateDomain(Name zone)", "Found no _dns-update SRV records in zone");
        return getAuthoritativeNameServer(zone);
    }
    return srvRecords;
}
Also used : Lookup(org.xbill.DNS.Lookup) SortedSet(java.util.SortedSet)

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