public static List<ForwardLookupResult> attemptZoneTransfer(String domain, List<ForwardLookupResult> nameServers) throws TextParseException { List<ForwardLookupResult> result = new ArrayList<>(); ZoneTransferIn xfr; Iterator i = nameServers.iterator(); for (ForwardLookupResult nameServer : nameServers) { try { xfr = ZoneTransferIn.newAXFR(new Name(domain), nameServer.getIpAddress(), null); List records = xfr.run(); for (Iterator it = records.iterator(); it.hasNext();) { Record r = (Record) it.next(); if (r.getType() == 1) { ForwardLookupResult rec = new ForwardLookupResult(domain); String hostName = ((ARecord) r).getName().toString().toLowerCase(); if (hostName.endsWith(".")) { hostName = hostName.substring(0, hostName.length() - 1); } rec.setHostName(hostName); rec.setIpAddress(((ARecord) r).getAddress().getHostAddress()); rec.setLookupType("A"); result.add(rec); } } } catch (IOException ioex) { Logger.getLogger("ForwardLookupHelper.attemptZoneTransfer").log(Level.WARNING, null, ioex); } catch (ZoneTransferException zex) { Log.debug("ForwardLookupHelper.attemptZoneTransfer: Failed zonetransfer"); } } return result; }
/** * Builds a Requisition based on the A records returned in a zone transfer from the * specified zone. * * @return an instance of the JaxB annotated Requisition class than can be marshaled * into the XML and streamed to the Provisioner * * @throws IOException * @throws ZoneTransferException */ private Requisition buildRequisitionFromZoneTransfer() throws IOException, ZoneTransferException { ZoneTransferIn xfer = null; List<Record> records = null; LogUtils.debugf(this, "connecting to host %s:%d", m_url.getHost(), m_port); try { xfer = ZoneTransferIn.newIXFR(new Name(m_zone), m_serial.longValue(), m_fallback.booleanValue(), m_url.getHost(), m_port, m_key); records = getRecords(xfer); } catch (ZoneTransferException e) // Fallbacking to AXFR { String message = "IXFR not supported trying AXFR: "+e; log().warn(message, e); xfer = ZoneTransferIn.newAXFR(new Name(m_zone), m_url.getHost(), m_key); records = getRecords(xfer); } Requisition r = null; if (records.size() > 0) { //for now, set the foreign source to the specified dns zone r = new Requisition(getForeignSource()); for (Record rec : records) { if (matchingRecord(rec)) { r.insertNode(createRequisitionNode(rec)); } } } return r; }
@SuppressWarnings("unchecked") private List<Record> getRecords(ZoneTransferIn xfer) throws IOException, ZoneTransferException { return (List<Record>) xfer.run(); }