Search in sources :

Example 16 with TextParseException

use of org.xbill.DNS.TextParseException in project fabric8 by jboss-fuse.

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 17 with TextParseException

use of org.xbill.DNS.TextParseException in project nhin-d by DirectProject.

the class ServiceTest method testSOA.

//  @Test
public void testSOA() {
    DNSEntryForm SoadnsForm = new DNSEntryForm();
    SoadnsForm.setName("savvy");
    SoadnsForm.setTtl(84555L);
    SoadnsForm.setAdmin("ns.savvy.com");
    SoadnsForm.setDomain("ns2.savvy.com");
    SoadnsForm.setSerial(4L);
    SoadnsForm.setRefresh(6L);
    SoadnsForm.setRetry(8L);
    SoadnsForm.setExpire(66L);
    SoadnsForm.setMinimum(22L);
    Collection<DNSRecord> records = new ArrayList<DNSRecord>();
    records.add(DNSRecordUtils.createSOARecord(SoadnsForm.getName(), SoadnsForm.getTtl(), SoadnsForm.getDomain(), SoadnsForm.getAdmin(), (int) SoadnsForm.getSerial(), SoadnsForm.getRefresh(), SoadnsForm.getRetry(), SoadnsForm.getExpire(), SoadnsForm.getMinimum()));
    try {
        configSvc.addDNS(records);
        Collection<DNSRecord> arecords = configSvc.getDNSByType(DNSType.SOA.getValue());
        for (Iterator<DNSRecord> iter = arecords.iterator(); iter.hasNext(); ) {
            DNSRecord arec = iter.next();
            SOARecord newrec = (SOARecord) Record.newRecord(Name.fromString(arec.getName()), arec.getType(), arec.getDclass(), arec.getTtl(), arec.getData());
            System.out.println("A admin: " + newrec.getAdmin());
            System.out.println("A name: " + newrec.getName());
        }
    } catch (ConfigurationServiceException e) {
        e.printStackTrace();
    } catch (TextParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : DNSRecord(org.nhindirect.config.store.DNSRecord) ArrayList(java.util.ArrayList) ConfigurationServiceException(org.nhindirect.config.service.ConfigurationServiceException) DNSEntryForm(org.nhindirect.config.ui.form.DNSEntryForm) SOARecord(org.xbill.DNS.SOARecord) TextParseException(org.xbill.DNS.TextParseException)

Example 18 with TextParseException

use of org.xbill.DNS.TextParseException 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 19 with TextParseException

use of org.xbill.DNS.TextParseException in project xabber-android by redsolution.

the class ExtDNSJavaResolver method lookupSRVRecords0.

@Override
protected List<SRVRecord> lookupSRVRecords0(String name, List<HostAddress> failedAddresses, ConnectionConfiguration.DnssecMode dnssecMode) {
    List<SRVRecord> res = new ArrayList<SRVRecord>();
    org.xbill.DNS.ResolverConfig.refresh();
    ExtLookup lookup;
    String[] servers = getDNSServersListForOreo();
    try {
        lookup = new ExtLookup(name, Type.SRV);
        if (servers != null && servers.length > 0)
            lookup.setResolver(new ExtendedResolver(servers));
        else
            lookup.setResolver(new ExtendedResolver());
    } catch (TextParseException e) {
        throw new IllegalStateException(e);
    } catch (UnknownHostException e) {
        throw new RuntimeException("Failed to initialize resolver");
    }
    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();
            List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode);
            if (hostAddresses == null) {
                continue;
            }
            SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses);
            res.add(r);
        }
    }
    return res;
}
Also used : ExtendedResolver(org.xbill.DNS.ExtendedResolver) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) ExtLookup(org.xbill.DNS.ExtLookup) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) Record(org.xbill.DNS.Record) SRVRecord(org.jivesoftware.smack.util.dns.SRVRecord) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Example 20 with TextParseException

use of org.xbill.DNS.TextParseException in project jmeter by apache.

the class DNSCacheManager method customRequestLookup.

private InetAddress[] customRequestLookup(String host) throws UnknownHostException {
    InetAddress[] addresses = null;
    try {
        Lookup lookup = new Lookup(host, Type.A);
        lookup.setCache(lookupCache);
        if (timeoutMs > 0) {
            resolver.setTimeout(timeoutMs / 1000, timeoutMs % 1000);
        }
        lookup.setResolver(resolver);
        Record[] records = lookup.run();
        if (records == null || records.length == 0) {
            throw new UnknownHostException("Failed to resolve host name: " + host);
        }
        addresses = new InetAddress[records.length];
        for (int i = 0; i < records.length; i++) {
            addresses[i] = ((ARecord) records[i]).getAddress();
        }
    } catch (TextParseException tpe) {
        // NOSONAR Exception handled
        log.debug("Failed to create Lookup object for host:{}, error message:{}", host, tpe.toString());
    }
    return addresses;
}
Also used : UnknownHostException(java.net.UnknownHostException) Lookup(org.xbill.DNS.Lookup) ARecord(org.xbill.DNS.ARecord) Record(org.xbill.DNS.Record) InetAddress(java.net.InetAddress) TextParseException(org.xbill.DNS.TextParseException)

Aggregations

TextParseException (org.xbill.DNS.TextParseException)23 Lookup (org.xbill.DNS.Lookup)14 Record (org.xbill.DNS.Record)12 ArrayList (java.util.ArrayList)11 ARecord (org.xbill.DNS.ARecord)11 SRVRecord (org.xbill.DNS.SRVRecord)9 UnknownHostException (java.net.UnknownHostException)7 Name (org.xbill.DNS.Name)5 InetAddress (java.net.InetAddress)4 IOException (java.io.IOException)3 JSONArray (org.json.JSONArray)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 DNSEntryForm (org.nhindirect.config.ui.form.DNSEntryForm)3 AAAARecord (org.xbill.DNS.AAAARecord)3 MXRecord (org.xbill.DNS.MXRecord)3 NSRecord (org.xbill.DNS.NSRecord)3 SOARecord (org.xbill.DNS.SOARecord)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 X509Certificate (java.security.cert.X509Certificate)2