Search in sources :

Example 6 with SimpleResolver

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

the class DnsMonitorIT method testDnsJavaQuadARecord.

@Test
public void testDnsJavaQuadARecord() throws IOException {
    assumeTrue(!Boolean.getBoolean("skipIpv6Tests"));
    final Lookup l = new Lookup("ipv6.example.com", Type.AAAA);
    // make sure we use a temporary cache so don't get results from a previously cached query
    // from another test
    l.setCache(null);
    final SimpleResolver resolver = new SimpleResolver("::1");
    resolver.setPort(9153);
    l.setResolver(resolver);
    l.run();
    System.out.println("result: " + l.getResult());
    if (l.getResult() == Lookup.SUCCESSFUL) {
        System.out.println(l.getAnswers()[0].rdataToString());
    }
    assertTrue(l.getResult() == Lookup.SUCCESSFUL);
}
Also used : Lookup(org.xbill.DNS.Lookup) SimpleResolver(org.xbill.DNS.SimpleResolver) Test(org.junit.Test)

Example 7 with SimpleResolver

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

the class DnsMonitorIT method testNoAnswer.

@Test
@JUnitDNSServer(port = 9153, zones = {})
public void testNoAnswer() throws Exception {
    final Lookup l = new Lookup("example.com", Type.AAAA);
    // make sure we use a temporary cache so don't get results from a previously cached query
    // from another test
    l.setCache(null);
    final SimpleResolver resolver = new SimpleResolver("::1");
    resolver.setPort(9153);
    l.setResolver(resolver);
    l.run();
    // and NXRRSET record should be sent meaning that the server has no records for
    // example.com at all.  This results in a null answers.  This is result 4 I think
    System.out.println("result: " + l.getResult());
    final Record[] answers = l.getAnswers();
    assertNull(answers);
}
Also used : Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) SimpleResolver(org.xbill.DNS.SimpleResolver) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) Test(org.junit.Test)

Example 8 with SimpleResolver

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

the class ReverseDnsProvisioningAdapter method afterPropertiesSet.

/**
 * <p>afterPropertiesSet</p>
 *
 * @throws java.lang.Exception if any.
 */
@Override
public void afterPropertiesSet() throws Exception {
    String dnsServer = System.getProperty("importer.adapter.dns.server");
    if (!StringUtils.isBlank(dnsServer)) {
        LOG.info("DNS property found: {}", dnsServer);
        if (dnsServer.contains(":")) {
            final String[] serverAddress = dnsServer.split(":");
            m_resolver = new SimpleResolver(serverAddress[0]);
            m_resolver.setPort(Integer.valueOf(serverAddress[1]));
        } else {
            m_resolver = new SimpleResolver(dnsServer);
        }
        // Doesn't work for some reason, haven't figured out why yet
        String key = System.getProperty("importer.adapter.dns.privatekey");
        if (key != null && key.length() > 0) {
            m_signature = key;
            m_resolver.setTSIGKey(TSIG.fromString(m_signature));
        }
    } else {
        LOG.warn("no DNS server configured, ReverseDnsProvisioningAdapter will not do anything!");
    }
}
Also used : SimpleResolver(org.xbill.DNS.SimpleResolver)

Example 9 with SimpleResolver

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

the class DnsSdDiscoveryAdvertiser method sendToServer.

protected void sendToServer(final IServiceInfo serviceInfo, final boolean mode) {
    Assert.isNotNull(serviceInfo);
    Assert.isLegal(serviceInfo.getServiceID() instanceof DnsSdServiceID);
    final DnsSdServiceID serviceID = (DnsSdServiceID) serviceInfo.getServiceID();
    try {
        // TYPE.SRV
        final Record srvRecord = serviceID.toSRVRecord();
        // TYPE.TXT
        final Record[] txtRecords = serviceID.toTXTRecords(srvRecord);
        final Name name = serviceID.getDnsName();
        final String[] registrationDomains = getRegistrationDomains(serviceID.getServiceTypeID());
        for (int i = 0; i < registrationDomains.length; i++) {
            final Name zone = new Name(registrationDomains[i]);
            // $NON-NLS-1$
            final Name fqdn = new Name(name.toString() + "." + zone.toString());
            final Update update = new Update(zone);
            // TYPE.SRV
            if (mode == ADD) {
                // TODO add absent/present condition checks
                update.replace(srvRecord.withName(fqdn));
            } else {
                update.delete(srvRecord.withName(fqdn));
            }
            // TYPE.TXT
            for (int j = 0; j < txtRecords.length; j++) {
                if (mode == ADD) {
                    update.add(txtRecords[j].withName(fqdn));
                } else {
                    update.delete(txtRecords[j].withName(fqdn));
                }
            }
            // set up a the resolver for the given domain (a scope might use different domains)
            final Collection dnsServers = getUpdateDomain(zone);
            if (dnsServers.size() == 0) {
                throw new DnsSdDiscoveryException(Messages.DnsSdDiscoveryAdvertiser_No_DynDns_Servers_Found);
            }
            for (final Iterator iterator = dnsServers.iterator(); iterator.hasNext(); ) {
                final SRVRecord dnsServer = (SRVRecord) iterator.next();
                // try to send msg and fail gracefully if more dns servers are available
                final Name target = dnsServer.getTarget();
                final Message response;
                final InetAddress byName;
                try {
                    byName = InetAddress.getByName(target.toString());
                    ((SimpleResolver) resolver).setAddress(byName);
                    ((SimpleResolver) resolver).setPort(dnsServer.getPort());
                    response = resolver.send(update);
                } catch (UnknownHostException uhe) {
                    if (iterator.hasNext()) {
                        continue;
                    } else {
                        throw new DnsSdDiscoveryException(uhe);
                    }
                } catch (EOFException eof) {
                    if (iterator.hasNext()) {
                        continue;
                    } else {
                        throw new DnsSdDiscoveryException(eof);
                    }
                }
                // catch some errors and fall back to the next dnsServer
                if (response.getRcode() != Rcode.NOERROR) {
                    if (iterator.hasNext()) {
                        continue;
                    } else {
                        throw DnsSdDiscoveryException.getException(response.getRcode());
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new DnsSdDiscoveryException(e);
    }
}
Also used : Message(org.xbill.DNS.Message) UnknownHostException(java.net.UnknownHostException) Update(org.xbill.DNS.Update) SimpleResolver(org.xbill.DNS.SimpleResolver) IDCreateException(org.eclipse.ecf.core.identity.IDCreateException) TextParseException(org.xbill.DNS.TextParseException) EOFException(java.io.EOFException) UnknownHostException(java.net.UnknownHostException) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) Name(org.xbill.DNS.Name) Iterator(java.util.Iterator) EOFException(java.io.EOFException) Collection(java.util.Collection) Record(org.xbill.DNS.Record) SOARecord(org.xbill.DNS.SOARecord) NSRecord(org.xbill.DNS.NSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SRVRecord(org.xbill.DNS.SRVRecord) InetAddress(java.net.InetAddress)

Example 10 with SimpleResolver

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

the class DnsSdDiscoveryLocator method connect.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ecf.core.IContainer#connect(org.eclipse.ecf.core.identity.ID,
	 * org.eclipse.ecf.core.security.IConnectContext)
	 */
public void connect(ID aTargetID, IConnectContext connectContext) throws ContainerConnectException {
    // connect can only be called once
    if (targetID != null || getConfig() == null) {
        throw new ContainerConnectException(Messages.DnsSdDiscoveryLocator_Container_Already_Connected);
    }
    // fall back to the search path as last resort
    if (aTargetID == null || !(aTargetID instanceof DnsSdServiceTypeID)) {
        ResolverConfig config = new ResolverConfig();
        Name[] searchPaths = config.searchPath();
        if (searchPaths != null && searchPaths.length > 0) {
            targetID = new DnsSdServiceTypeID();
            targetID.setSearchPath(searchPaths);
        } else {
            throw new ContainerConnectException(Messages.DnsSdDiscoveryLocator_No_Target_ID);
        }
    } else {
        final Namespace ns = getConnectNamespace();
        try {
            targetID = (DnsSdServiceTypeID) ns.createInstance(new Object[] { aTargetID });
        } catch (IDCreateException e) {
            throw new ContainerConnectException(e);
        }
    }
    // instantiate a default resolver
    if (resolver == null) {
        try {
            resolver = new SimpleResolver();
        } catch (UnknownHostException e) {
            throw new ContainerConnectException(e);
        }
    }
    // read browsing domains for the given targetID/searchpath and merge with existing
    targetID.addSearchPath(getBrowsingDomains(targetID));
    // done setting up this provider, send event
    fireContainerEvent(new ContainerConnectingEvent(this.getID(), targetID, connectContext));
    fireContainerEvent(new ContainerConnectedEvent(this.getID(), targetID));
}
Also used : ResolverConfig(org.xbill.DNS.ResolverConfig) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) UnknownHostException(java.net.UnknownHostException) IDCreateException(org.eclipse.ecf.core.identity.IDCreateException) ContainerConnectedEvent(org.eclipse.ecf.core.events.ContainerConnectedEvent) ContainerConnectingEvent(org.eclipse.ecf.core.events.ContainerConnectingEvent) SimpleResolver(org.xbill.DNS.SimpleResolver) Namespace(org.eclipse.ecf.core.identity.Namespace) Name(org.xbill.DNS.Name)

Aggregations

SimpleResolver (org.xbill.DNS.SimpleResolver)19 Record (org.xbill.DNS.Record)11 Name (org.xbill.DNS.Name)9 Message (org.xbill.DNS.Message)7 UnknownHostException (java.net.UnknownHostException)6 Lookup (org.xbill.DNS.Lookup)6 Test (org.junit.Test)4 SRVRecord (org.xbill.DNS.SRVRecord)4 Update (org.xbill.DNS.Update)4 ContainerConnectException (org.eclipse.ecf.core.ContainerConnectException)3 IDCreateException (org.eclipse.ecf.core.identity.IDCreateException)3 Resolver (org.xbill.DNS.Resolver)3 TSIG (org.xbill.DNS.TSIG)3 TXTRecord (org.xbill.DNS.TXTRecord)3 InetAddress (java.net.InetAddress)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Endpoint (org.apache.camel.Endpoint)2 ContainerConnectedEvent (org.eclipse.ecf.core.events.ContainerConnectedEvent)2 ContainerConnectingEvent (org.eclipse.ecf.core.events.ContainerConnectingEvent)2