Search in sources :

Example 76 with Name

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

the class JUnitDNSServerExecutionListener method beforeTestMethod.

/**
 * {@inheritDoc}
 */
@Override
public void beforeTestMethod(final TestContext testContext) throws Exception {
    super.beforeTestMethod(testContext);
    final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext);
    if (config == null) {
        return;
    }
    LOG.info("initializing DNS on port {}", config.port());
    m_server = new DNSServer();
    m_server.addPort(config.port());
    for (final DNSZone dnsZone : config.zones()) {
        String name = dnsZone.name();
        if (!name.endsWith(".")) {
            name = name + ".";
        }
        final Name zoneName = Name.fromString(name, Name.root);
        LOG.debug("zoneName = {}", zoneName);
        final Zone zone = new Zone(zoneName, new Record[] { new SOARecord(zoneName, DClass.IN, DEFAULT_TTL, zoneName, Name.fromString("admin." + name), 1, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL, DEFAULT_TTL), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver1.opendns.com.")), new NSRecord(zoneName, DClass.IN, DEFAULT_TTL, Name.fromString("resolver2.opendns.com.")), new ARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v4address())), new AAAARecord(zoneName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(dnsZone.v6address())) });
        LOG.debug("zone = {}", zone);
        Matcher m;
        for (final DNSEntry entry : dnsZone.entries()) {
            LOG.debug("adding entry: {}", entry);
            String hostname = entry.hostname();
            final Name recordName = Name.fromString(hostname, zoneName);
            LOG.debug("name = {}", recordName);
            switch(entry.type()) {
                case "A":
                    zone.addRecord(new ARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "AAAA":
                    zone.addRecord(new AAAARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, InetAddressUtils.addr(entry.data())));
                    break;
                case "CNAME":
                    zone.addRecord(new CNAMERecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "NS":
                    zone.addRecord(new NSRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "MX":
                    m = s_mxPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new MXRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Name.fromString(m.group(2))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "PTR":
                    zone.addRecord(new PTRRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(entry.data())));
                    break;
                case "SOA":
                    m = s_soaPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SOARecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Name.fromString(m.group(1)), Name.fromString(m.group(2)), Long.valueOf(m.group(3)), Long.valueOf(m.group(4)), Long.valueOf(m.group(5)), Long.valueOf(m.group(6)), Long.valueOf(m.group(7))));
                    } else {
                        LOG.error("Entry data '{}' does not match SOA pattern", entry.data());
                    }
                    break;
                case "SRV":
                    m = s_srvPattern.matcher(entry.data());
                    if (m.matches()) {
                        zone.addRecord(new SRVRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, Integer.valueOf(m.group(1)), Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)), Name.fromString(m.group(4))));
                    } else {
                        LOG.error("Entry data '{}' does not match MX pattern", entry.data());
                    }
                    break;
                case "TXT":
                    zone.addRecord(new TXTRecord(recordName, DClass.value(entry.dclass()), DEFAULT_TTL, entry.data()));
                    break;
                default:
                    LOG.error("DNS entry type {} not supported.", entry.type());
                    break;
            }
        }
        m_server.addZone(zone);
    }
    LOG.debug("starting DNS server");
    m_server.start();
}
Also used : JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) AAAARecord(org.xbill.DNS.AAAARecord) Matcher(java.util.regex.Matcher) Zone(org.xbill.DNS.Zone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) DNSZone(org.opennms.core.test.dns.annotations.DNSZone) Name(org.xbill.DNS.Name) CNAMERecord(org.xbill.DNS.CNAMERecord) PTRRecord(org.xbill.DNS.PTRRecord) DNSEntry(org.opennms.core.test.dns.annotations.DNSEntry) SOARecord(org.xbill.DNS.SOARecord) ARecord(org.xbill.DNS.ARecord) AAAARecord(org.xbill.DNS.AAAARecord) TXTRecord(org.xbill.DNS.TXTRecord) MXRecord(org.xbill.DNS.MXRecord) JUnitDNSServer(org.opennms.core.test.dns.annotations.JUnitDNSServer) NSRecord(org.xbill.DNS.NSRecord) SOARecord(org.xbill.DNS.SOARecord) SRVRecord(org.xbill.DNS.SRVRecord)

Example 77 with Name

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

the class DnsSdAdvertiserServiceTest method createSRVRecord.

private void createSRVRecord() throws TextParseException, IOException, UnknownHostException {
    // create a service manually
    final Name zone = Name.fromString(DnsSdTestHelper.REG_DOMAIN + ".");
    final Name type = Name.fromString("_" + DnsSdTestHelper.REG_SCHEME + "._" + DnsSdTestHelper.PROTO, zone);
    final String s = serviceInfo.getPriority() + " " + serviceInfo.getWeight() + " " + serviceInfo.getLocation().getPort() + " " + serviceInfo.getLocation().getHost() + ".";
    final Record record = Record.fromString(type, Type.SRV, DClass.IN, DnsSdTestHelper.TTL, s, zone);
    final Update 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 : 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) SimpleResolver(org.xbill.DNS.SimpleResolver) Name(org.xbill.DNS.Name)

Example 78 with Name

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

the class DnsSdAdvertiserServiceTest method testUnregisterServiceWithProperties.

/**
 * Test that all service properties are removed along with the service
 */
public void testUnregisterServiceWithProperties() throws ContainerConnectException, IOException, ZoneTransferException {
    createSRVRecord();
    createTXTRecords();
    // unregister via ECF discovery
    discoveryAdvertiser.unregisterService(serviceInfo);
    // check SRV record is gone
    final ZoneTransferIn xfr = ZoneTransferIn.newAXFR(new Name(DnsSdTestHelper.REG_DOMAIN), DnsSdTestHelper.DNS_SERVER, null);
    final List records = xfr.run();
    for (final Iterator itr = records.iterator(); itr.hasNext(); ) {
        final Record record = (Record) itr.next();
        if (record instanceof SRVRecord) {
            if (comparator.compare(serviceInfo, record) >= 0) {
                fail("Service not removed/unregisterd");
            }
        } else if (record instanceof TXTRecord) {
            if (comparator.compare(serviceInfo, record) >= 0) {
                fail("TXT record not removed/unregisterd");
            }
        }
    }
}
Also used : TXTRecord(org.xbill.DNS.TXTRecord) 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) SRVRecord(org.xbill.DNS.SRVRecord) Name(org.xbill.DNS.Name)

Example 79 with Name

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

the class DnsSdDiscoveryAdvertiser method getAuthoritativeNameServer.

protected Collection getAuthoritativeNameServer(final Name zone) throws TextParseException {
    // $NON-NLS-1$ //$NON-NLS-2$
    Trace.trace(Activator.PLUGIN_ID, DnsSdDebugOptions.METHODS_TRACING, this.getClass(), "getAuthoritativeNameServer(Name zone)", "Trying to find authoritative name server");
    final Set result = new HashSet();
    final Name name = new Name(_DNS_UPDATE + zone);
    // query for NS records
    Lookup query = new Lookup(zone, Type.NS);
    query.setResolver(resolver);
    Record[] queryResult = query.run();
    // TODO file bug upstream that queryResult may never be null
    int length = queryResult == null ? 0 : queryResult.length;
    for (int j = 0; j < length; j++) {
        final Record record = queryResult[j];
        if (record instanceof NSRecord) {
            final NSRecord nsRecord = (NSRecord) record;
            final Name target = nsRecord.getTarget();
            result.add(new SRVRecord(name, DClass.IN, nsRecord.getTTL(), 0, 0, SimpleResolver.DEFAULT_PORT, target));
        }
    }
    // query for primary ns in SOA record (may overwrite/be equal to one of the ns records)
    query = new Lookup(zone, Type.SOA);
    query.setResolver(resolver);
    queryResult = query.run();
    // TODO file bug upstream that queryResult may never be null
    length = queryResult == null ? 0 : queryResult.length;
    for (int j = 0; j < length; j++) {
        final Record record = queryResult[j];
        if (record instanceof SOARecord) {
            final SOARecord soaRecord = (SOARecord) record;
            result.add(new SRVRecord(name, DClass.IN, soaRecord.getTTL(), 0, 0, SimpleResolver.DEFAULT_PORT, soaRecord.getHost()));
        }
    }
    return result;
}
Also used : SortedSet(java.util.SortedSet) HashSet(java.util.HashSet) Set(java.util.Set) Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) SOARecord(org.xbill.DNS.SOARecord) NSRecord(org.xbill.DNS.NSRecord) SRVRecord(org.xbill.DNS.SRVRecord) NSRecord(org.xbill.DNS.NSRecord) SRVRecord(org.xbill.DNS.SRVRecord) SOARecord(org.xbill.DNS.SOARecord) HashSet(java.util.HashSet) Name(org.xbill.DNS.Name)

Example 80 with Name

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

the class DnsSdDiscoveryContainerAdapter method getSRVRecord.

protected SortedSet getSRVRecord(Lookup query, Comparator aComparator) {
    final SortedSet srvRecords = new TreeSet(aComparator);
    query.setResolver(resolver);
    final Record[] queryResult = query.run();
    // TODO file bug upstream that queryResult may never be null
    final int length = queryResult == null ? 0 : queryResult.length;
    for (int j = 0; j < length; j++) {
        Record[] srvQueryResult = null;
        final Record record = queryResult[j];
        if (record instanceof PTRRecord) {
            final PTRRecord ptrRecord = (PTRRecord) record;
            final Name target = ptrRecord.getTarget();
            final Lookup srvQuery = new Lookup(target, Type.SRV);
            srvQuery.setResolver(resolver);
            srvQueryResult = srvQuery.run();
        } else if (record instanceof SRVRecord) {
            srvQueryResult = new SRVRecord[] { (SRVRecord) record };
        } else {
            // avoid NPE
            srvQueryResult = new SRVRecord[0];
        }
        srvRecords.addAll(Arrays.asList(srvQueryResult));
    }
    return srvRecords;
}
Also used : PTRRecord(org.xbill.DNS.PTRRecord) TreeSet(java.util.TreeSet) PTRRecord(org.xbill.DNS.PTRRecord) Record(org.xbill.DNS.Record) SRVRecord(org.xbill.DNS.SRVRecord) Lookup(org.xbill.DNS.Lookup) SRVRecord(org.xbill.DNS.SRVRecord) SortedSet(java.util.SortedSet) Name(org.xbill.DNS.Name)

Aggregations

Name (org.xbill.DNS.Name)101 Record (org.xbill.DNS.Record)36 Message (org.xbill.DNS.Message)18 SRVRecord (org.xbill.DNS.SRVRecord)18 IOException (java.io.IOException)11 UnknownHostException (java.net.UnknownHostException)11 ArrayList (java.util.ArrayList)11 Lookup (org.xbill.DNS.Lookup)10 ARecord (org.xbill.DNS.ARecord)9 ExtendedResolver (org.xbill.DNS.ExtendedResolver)9 SimpleResolver (org.xbill.DNS.SimpleResolver)9 RRset (org.xbill.DNS.RRset)8 NSRecord (org.xbill.DNS.NSRecord)7 TXTRecord (org.xbill.DNS.TXTRecord)7 HashSet (java.util.HashSet)6 Iterator (java.util.Iterator)6 CNAMERecord (org.xbill.DNS.CNAMERecord)6 TextParseException (org.xbill.DNS.TextParseException)6 Update (org.xbill.DNS.Update)6 ZoneTransferIn (org.xbill.DNS.ZoneTransferIn)6