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();
}
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);
}
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");
}
}
}
}
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;
}
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;
}
Aggregations