Search in sources :

Example 1 with Update

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

the class ReverseDnsProvisioningAdapter method doUpdate.

private void doUpdate(AdapterOperation op) {
    LOG.debug("doUpdate: operation: {}", op.getType().name());
    for (ReverseDnsRecord record : m_reverseDnsProvisioningAdapterService.get(op.getNodeId())) {
        LOG.debug("doUpdate: ReverseDnsRecord: hostname: {} zone: {} ip address: {}", record.getIp().getHostAddress(), record.getHostname(), record.getZone());
        try {
            Update update = new Update(Name.fromString(record.getZone()));
            Name ptrRecord = ReverseMap.fromAddress(record.getIp());
            update.replace(ptrRecord, Type.PTR, 3600, record.getHostname());
            m_resolver.send(update);
            m_reverseDnsProvisioningAdapterService.update(op.getNodeId(), record);
        } catch (Exception e) {
            LOG.error("updateNode: Error handling updated event.", e);
            sendAndThrow(op.getNodeId(), e);
        }
    }
}
Also used : Update(org.xbill.DNS.Update) Name(org.xbill.DNS.Name)

Example 2 with Update

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

the class DnsProvisioningAdapter method doUpdate.

private void doUpdate(AdapterOperation op) {
    OnmsNode node = null;
    LOG.debug("doUpdate: operation: {}", op.getType().name());
    try {
        node = m_nodeDao.get(op.getNodeId());
        if (node == null) {
            doDelete(op);
            return;
        }
        DnsRecord record = new DnsRecord(node, m_level);
        LOG.debug("doUpdate: DnsRecord: hostname: {} zone: {} ip address {}", record.getIp().getHostAddress(), record.getHostname(), record.getZone());
        DnsRecord oldRecord = m_nodeDnsRecordMap.get(Integer.valueOf(node.getId()));
        Update update = new Update(Name.fromString(record.getZone()));
        if (oldRecord != null && oldRecord.getHostname() != record.getHostname()) {
            update.delete(Name.fromString(oldRecord.getHostname()), Type.A);
        }
        update.replace(Name.fromString(record.getHostname()), Type.A, 3600, record.getIp().getHostAddress());
        m_resolver.send(update);
        m_nodeDnsRecordMap.put(Integer.valueOf(op.getNodeId()), record);
    } catch (Throwable e) {
        LOG.error("addNode: Error handling node added event.", e);
        sendAndThrow(op.getNodeId(), e);
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Update(org.xbill.DNS.Update)

Example 3 with Update

use of org.xbill.DNS.Update 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 4 with Update

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

the class DnsSdAdvertiserServiceTest method setUp.

/* (non-Javadoc)
	 * @see org.eclipse.ecf.tests.discovery.AbstractDiscoveryTest#setUp()
	 */
protected void setUp() throws Exception {
    super.setUp();
    // purge all SRV records from the test domain
    ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name(DnsSdTestHelper.REG_DOMAIN), DnsSdTestHelper.DNS_SERVER, null);
    List records = xfr.run();
    for (Iterator itr = records.iterator(); itr.hasNext(); ) {
        Record record = (Record) itr.next();
        String name = record.getName().toString();
        if (name.startsWith("_" + DnsSdTestHelper.REG_SCHEME + "._" + DnsSdTestHelper.PROTO)) {
            Update update = new Update(Name.fromString(DnsSdTestHelper.REG_DOMAIN + "."));
            update.delete(record);
            SimpleResolver resolver = new SimpleResolver(DnsSdTestHelper.DNS_SERVER);
            resolver.setTCP(true);
            resolver.setTSIGKey(new TSIG(DnsSdTestHelper.TSIG_KEY_NAME, DnsSdTestHelper.TSIG_KEY));
            Message response = resolver.send(update);
            int rcode = response.getRcode();
            assertTrue("", rcode == 0);
        }
    }
}
Also used : Message(org.xbill.DNS.Message) ZoneTransferIn(org.xbill.DNS.ZoneTransferIn) Iterator(java.util.Iterator) List(java.util.List) SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) TXTRecord(org.xbill.DNS.TXTRecord) TSIG(org.xbill.DNS.TSIG) Update(org.xbill.DNS.Update) SimpleResolver(org.xbill.DNS.SimpleResolver) Name(org.xbill.DNS.Name)

Example 5 with Update

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

the class DnsSdAdvertiserServiceTest method createTXTRecords.

private void createTXTRecords() throws TextParseException, IOException, UnknownHostException {
    final Name zone = Name.fromString(DnsSdTestHelper.REG_DOMAIN + ".");
    final Name name = Name.fromString("_" + DnsSdTestHelper.REG_SCHEME + "._" + DnsSdTestHelper.PROTO, zone);
    Update update = null;
    final IServiceProperties properties = serviceInfo.getServiceProperties();
    final Enumeration enumeration = properties.getPropertyNames();
    while (enumeration.hasMoreElements()) {
        final Object property = enumeration.nextElement();
        final String key = property.toString();
        final String value = (String) properties.getProperty(key).toString();
        final Record record = Record.fromString(name, Type.TXT, DClass.IN, serviceInfo.getTTL(), key + "=" + value, zone);
        update = new Update(zone);
        update.add(record);
    }
    final SimpleResolver resolver = new SimpleResolver(DnsSdTestHelper.DNS_SERVER);
    resolver.setTCP(true);
    resolver.setTSIGKey(new TSIG(DnsSdTestHelper.TSIG_KEY_NAME, DnsSdTestHelper.TSIG_KEY));
    final Message response = resolver.send(update);
    final int rcode = response.getRcode();
    assertTrue("", rcode == 0);
}
Also used : Enumeration(java.util.Enumeration) Message(org.xbill.DNS.Message) SRVRecord(org.xbill.DNS.SRVRecord) Record(org.xbill.DNS.Record) TXTRecord(org.xbill.DNS.TXTRecord) TSIG(org.xbill.DNS.TSIG) Update(org.xbill.DNS.Update) IServiceProperties(org.eclipse.ecf.discovery.IServiceProperties) SimpleResolver(org.xbill.DNS.SimpleResolver) Name(org.xbill.DNS.Name)

Aggregations

Update (org.xbill.DNS.Update)7 Name (org.xbill.DNS.Name)5 Message (org.xbill.DNS.Message)4 Record (org.xbill.DNS.Record)4 SRVRecord (org.xbill.DNS.SRVRecord)4 SimpleResolver (org.xbill.DNS.SimpleResolver)4 TSIG (org.xbill.DNS.TSIG)3 TXTRecord (org.xbill.DNS.TXTRecord)3 Iterator (java.util.Iterator)2 EOFException (java.io.EOFException)1 InetAddress (java.net.InetAddress)1 UnknownHostException (java.net.UnknownHostException)1 Collection (java.util.Collection)1 Enumeration (java.util.Enumeration)1 List (java.util.List)1 ContainerConnectException (org.eclipse.ecf.core.ContainerConnectException)1 IDCreateException (org.eclipse.ecf.core.identity.IDCreateException)1 IServiceProperties (org.eclipse.ecf.discovery.IServiceProperties)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 NSRecord (org.xbill.DNS.NSRecord)1