public static String resolve(String host, int addrType) { try { Lookup lookup = new Lookup(host, addrType); SimpleResolver resolver = new SimpleResolver("114.114.114.114"); resolver.setTimeout(5); lookup.setResolver(resolver); Record[] result = lookup.run(); if (result == null) return null; List<Record> records = java.util.Arrays.asList(result); java.util.Collections.shuffle(records); for (Record record : records) { if (addrType == Type.A) { return ((ARecord) record).getAddress().getHostAddress(); } else if (addrType == Type.AAAA) { return ((AAAARecord) record).getAddress().getHostAddress(); } } } catch (Exception ex) { return null; } return null; }
@Test public void testQueryAddressesIpv6() throws SendException { new Expectations() { { lookup.run(); result = new Record[] { new AAAARecord(HOST6_EXAMPLE_COM_NAME, 0, 0, IPV6) }; } }; InetAddress[] addresses = addressLookup.queryAddresses(HOST1_EXAMPLE_COM_NAME); InetAddress[] expected = new InetAddress[] { IPV6 }; assertArrayEquals(expected, addresses); }
private String[] findAddresses(Name target, Record[] records) { ArrayList<String> addresses = new ArrayList<String>(); for (Record record : records) { if (target == null || target.equals(record.getName())) { int recordType = record.getType(); if (Type.A == recordType) addresses.add(((ARecord)record).getAddress().getHostAddress()); else if (Type.AAAA == recordType) addresses.add(((AAAARecord)record).getAddress().getHostAddress()); } } if (addresses.size() == 0) return null; return addresses.toArray(new String[addresses.size()]); }
public static List<ForwardLookupResult> getAAAARecord(String hostName, String domainName) throws TextParseException { List<ForwardLookupResult> entries = null; if (hostName != null && !hostName.isEmpty() && domainName != null && !domainName.isEmpty()) { Record[] recs = new Lookup(hostName, Type.AAAA).run(); if (recs != null) { if (recs.length > 0) { entries = new ArrayList<>(); for (Record record : recs) { ForwardLookupResult foundSubDomain = new ForwardLookupResult(domainName); foundSubDomain.setHostName(hostName); String ipAddress = ((AAAARecord) record).getAddress().getHostAddress(); foundSubDomain.setIpAddress(ipAddress); foundSubDomain.setLookupType("A"); entries.add(foundSubDomain); } } } } return entries; }
private RRset makeV6AddressSet(HostResource host) { RRset addressSet = new RRset(); for (InetAddress address : host.getInetAddresses()) { if (address instanceof Inet6Address) { AAAARecord record = new AAAARecord( toAbsoluteName(host.getFullyQualifiedHostName()), DClass.IN, dnsDefaultATtl.getStandardSeconds(), address); addressSet.addRR(record); } } return addressSet; }
private InetAddress[] convertAddressRecordsToAddresses(Record[] records) { InetAddress[] addresses = new InetAddress[records.length]; for (int i = 0; i < records.length; i++) { Record record = records[i]; if (record instanceof ARecord) { addresses[i] = ((ARecord) record).getAddress(); } else if (record instanceof AAAARecord) { addresses[i] = ((AAAARecord) record).getAddress(); } else { throw new RuntimeException(); } } return addresses; }
@Test() public void testGetSectionByType() throws UnknownHostException { Message m = new Message(); Record r1 = new ARecord(Name.root, DClass.IN, 0, InetAddress.getByAddress(new byte[]{0,0,0,0})); m.addRecord(r1, Section.ANSWER); Record r2 = new AAAARecord(Name.root, DClass.IN, 0, InetAddress.getByAddress(new byte[]{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1})); m.addRecord(r2, Section.ANSWER); SMessage sm = new SMessage(m); SRRset[] result = sm.getSectionRRsets(Section.ANSWER, Type.A); assertEquals(1, result.length); assertEquals(Type.A, result[0].getType()); }
/** * Creates an instance of the JaxB annotated RequisionNode class. * * @param rec * @return a populated RequisitionNode based on defaults and data from the * A record returned from a DNS zone transfer query. */ private RequisitionNode createRequisitionNode(Record rec) { String addr = null; if ("A".equals(Type.string(rec.getType()))) { ARecord arec = (ARecord)rec; addr = StringUtils.stripStart(arec.getAddress().toString(), "/"); } else if ("AAAA".equals(Type.string(rec.getType()))) { AAAARecord aaaarec = (AAAARecord)rec; addr = aaaarec.rdataToString(); } else { throw new IllegalArgumentException("Invalid record type " + Type.string(rec.getType()) + ". A or AAAA expected."); } RequisitionNode n = new RequisitionNode(); String host = rec.getName().toString(); String nodeLabel = StringUtils.stripEnd(StringUtils.stripStart(host, "."), "."); n.setBuilding(getForeignSource()); switch(m_foreignIdHashSource) { case 1: n.setForeignId(computeHashCode(nodeLabel)); log().debug("Generating foreignId from hash of nodelabel " + nodeLabel); break; case 2: n.setForeignId(computeHashCode(addr)); log().debug("Generating foreignId from hash of ipAddress " + addr); break; case 3: n.setForeignId(computeHashCode(nodeLabel+addr)); log().debug("Generating foreignId from hash of nodelabel+ipAddress " + nodeLabel + addr); break; default: n.setForeignId(computeHashCode(nodeLabel)); log().debug("Default case: Generating foreignId from hash of nodelabel " + nodeLabel); break; } n.setNodeLabel(nodeLabel); RequisitionInterface i = new RequisitionInterface(); i.setDescr("DNS-" + Type.string(rec.getType())); i.setIpAddr(addr); i.setSnmpPrimary(PrimaryType.PRIMARY); i.setManaged(Boolean.TRUE); i.setStatus(Integer.valueOf(1)); for (String service : m_services) { service = service.trim(); i.insertMonitoredService(new RequisitionMonitoredService(service)); log().debug("Adding provisioned service " + service); } n.putInterface(i); return n; }
/** {@inheritDoc} */ @Override public void beforeTestMethod(final TestContext testContext) throws Exception { super.beforeTestMethod(testContext); final JUnitDNSServer config = findTestAnnotation(JUnitDNSServer.class, testContext); if (config == null) { return; } LogUtils.infof(this, "initializing DNS on port %d", 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); LogUtils.debugf(this, "zoneName = %s", 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())) }); LogUtils.debugf(this, "zone = %s", zone); for (final DNSEntry entry : dnsZone.entries()) { LogUtils.debugf(this, "adding entry: %s", entry); String hostname = entry.hostname(); final Name recordName = Name.fromString(hostname, zoneName); LogUtils.debugf(this, "name = %s", recordName); if (entry.ipv6()) { zone.addRecord(new AAAARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address()))); } else { zone.addRecord(new ARecord(recordName, DClass.IN, DEFAULT_TTL, InetAddressUtils.addr(entry.address()))); } } m_server.addZone(zone); } LogUtils.debugf(this, "starting DNS server"); m_server.start(); try { Thread.sleep(50); } catch (final InterruptedException e) { LogUtils.debugf(this, e, "interrupted while waiting for server to come up"); Thread.currentThread().interrupt(); } }
public Inet6Address getIPV6ByName( String query ) throws UnknownHostException { try{ Lookup a6_l = new Lookup( query, Type.AAAA ); setCache( null, a6_l ); a6_l.run(); Record[] a6_results = a6_l.getAnswers(); if ( a6_results != null ){ for ( Record r: a6_results ){ AAAARecord a6_record = (AAAARecord)r; return((Inet6Address)a6_record.getAddress()); } } throw( new UnknownHostException( query )); }catch( Exception e ){ throw( new UnknownHostException( query + ": " + Debug.getNestedExceptionMessage( e ))); } }