@Override public void clearDNSCache() { cache.clearCache(); }
public static String shortHostname(final String host) { final Name root = Name.fromConstantString("."); final Name hostname; try { hostname = Name.fromString(host, root); } catch (TextParseException e) { throw new IllegalArgumentException("Invalid hostname '" + host + "'"); } final ResolverConfig currentConfig = ResolverConfig.getCurrentConfig(); if (currentConfig != null) { final Name[] searchPath = currentConfig.searchPath(); if (searchPath != null) { for (final Name domain : searchPath) { if (hostname.subdomain(domain)) { return hostname.relativize(domain).toString(); } } } } return hostname.toString(); }
@Override public void setNegativeDNSCacheTimeout(int timeout, TimeUnit timeUnit) { cache.setMaxNCache((int) TimeUnit.SECONDS.convert(timeout, timeUnit)); }
@Override protected List<SRVRecord> lookupSRVRecords0(DnsName name, List<HostAddress> failedAddresses, DnssecMode dnssecMode) { List<SRVRecord> res = new ArrayList<>(); Lookup lookup; try { lookup = new Lookup(name.ace, Type.SRV); } catch (TextParseException e) { throw new IllegalStateException(e); } Record[] recs = lookup.run(); if (recs == null) return res; for (Record record : recs) { org.xbill.DNS.SRVRecord srvRecord = (org.xbill.DNS.SRVRecord) record; if (srvRecord != null && srvRecord.getTarget() != null) { DnsName host = DnsName.from(srvRecord.getTarget().toString()); int port = srvRecord.getPort(); int priority = srvRecord.getPriority(); int weight = srvRecord.getWeight(); List<InetAddress> hostAddresses = lookupHostAddress0(host, failedAddresses, dnssecMode); if (shouldContinue(name, host, hostAddresses)) { continue; } SRVRecord r = new SRVRecord(host, port, priority, weight, hostAddresses); res.add(r); } } return res; }
Lookup lookup; try { lookup = new Lookup(host, type, DClass.IN); } catch (TextParseException e) { return Collections.emptyList(); lookup.setCache(cache); records = lookup.run(); retryCount++; } while (lookup.getResult() == Lookup.TRY_AGAIN && retryCount < DNS_NETWORK_FAILURE_RETRY_COUNT); InetAddress resolvedAddress = InetAddress.getByAddress(host, ipv4Record.getAddress().getAddress()); InetAddress resolvedAddress = InetAddress.getByAddress(host, ipv6Record.getAddress().getAddress());
/** * Finds PTR records (reverse dns lookups) for the ip address. * * @param ip ip address to lookup. * @return The host name found for the ip address. * @throws UnknownHostException occurs when id is not available in DNS */ public String getHostByAddr(byte[] ip) throws UnknownHostException { try { String addr = DnsUtils.numericToTextFormat(ip); Record[] records = new Lookup(addr, Type.PTR).run(); if (records == null) { throw new UnknownHostException(addr); } PTRRecord ptr = (PTRRecord) records[0]; return ptr.getTarget().toString(); } catch (TextParseException e) { throw new UnknownHostException(e.getMessage()); } } }
protected void storeDNSRecord(final CrawlURI curi, final String dnsName, final CrawlHost targetHost, final Record[] rrecordSet) { // Get TTL and IP info from the first A record (there may be // multiple, e.g. www.washington.edu) then update the CrawlServer ARecord arecord = getFirstARecord(rrecordSet); if (arecord == null) { throw new NullPointerException("Got null arecord for " + dnsName); } targetHost.setIP(arecord.getAddress(), arecord.getTTL()); try { recordDNS(curi, rrecordSet); curi.setFetchStatus(S_DNS_SUCCESS); curi.setDNSServerIPLabel(ResolverConfig.getCurrentConfig().server()); } catch (IOException e) { logger.log(Level.SEVERE, "Failed store of DNS Record for " + curi.toString(), e); setUnresolvable(curi, targetHost); } }
private List<ScoredHost> scoreMatches(final List<String> results) { final ImmutableList.Builder<ScoredHost> scored = ImmutableList.builder(); for (final String name : results) { int score = Integer.MAX_VALUE; for (int i = 0; i < searchPath.length; i++) { if (name.endsWith(searchPath[i].toString())) { if (i < score) { score = i; } } } scored.add(new ScoredHost(name, score)); } return scored.build(); } }
protected ARecord getFirstARecord(Record[] rrecordSet) { ARecord arecord = null; if (rrecordSet == null || rrecordSet.length == 0) { if (logger.isLoggable(Level.FINEST)) { logger.finest("rrecordSet is null or zero length: " + rrecordSet); } return arecord; } for (int i = 0; i < rrecordSet.length; i++) { if (rrecordSet[i].getType() != Type.A) { if (logger.isLoggable(Level.FINEST)) { logger.finest("Record " + Integer.toString(i) + " is not A type but " + rrecordSet[i].getType()); } continue; } arecord = (ARecord) rrecordSet[i]; break; } return arecord; } }
static HostResolver create(HeliosClient client) throws InterruptedException, ExecutionException { final ResolverConfig currentConfig = ResolverConfig.getCurrentConfig(); final Name[] path; if (currentConfig != null) { final Name[] possiblePath = currentConfig.searchPath(); if (possiblePath != null) { path = possiblePath; } else { path = EMPTY_PATH; } } else { path = EMPTY_PATH; } return new HostResolver(Sets.newHashSet(client.listHosts().get()), path); }
Lookup.getDefaultCache(DClass.IN).clearCache();
@After public void after() { Lookup.setDefaultCache(new Cache(), DClass.IN); }
public GoogleResolver() { String dns = findProperServer(); resolver = createResolver(dns); resolver.setTimeout(RESOLVE_TIMEOUT_S); }
@Override public SetResponse lookupRecords(Name name, int type, int minCred) { final SetResponse setResponse = super.lookupRecords(name, type, minCred); if (count++ == 0) { assertThat(setResponse.isUnknown(), is(true)); } else { assertThat(setResponse.isUnknown(), is(false)); } return setResponse; //To change body of overridden methods use File | Settings | File Templates. }
protected Cache createCache() { return new Cache(); }
public void start() { // cache AlertThreadGroup for later ToePool launch AlertThreadGroup atg = AlertThreadGroup.current(); if(atg!=null) { alertThreadGroup = atg; } if(isRunning) { return; } sExit = CrawlStatus.FINISHED_ABNORMAL; // force creation of DNS Cache now -- avoids CacheCleaner in toe-threads group // also cap size at 1 (we never wanta cached value; 0 is non-operative) Lookup.getDefaultCache(DClass.IN).setMaxEntries(1); reserveMemory = new LinkedList<byte[]>(); for(int i = 0; i < RESERVE_BLOCKS; i++) { reserveMemory.add(new byte[RESERVE_BLOCK_SIZE]); } isRunning = true; }
if (address == null) { address = Address.getByName(host);
@Override public void setPositiveDNSCacheTimeout(int timeout, TimeUnit timeUnit) { cache.setMaxCache((int) TimeUnit.SECONDS.convert(timeout, timeUnit)); }
protected byte [] getDNSRecord(final long fetchStart, final Record[] rrecordSet) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Start the record with a 14-digit date per RFC 2540 byte[] fetchDate = ArchiveUtils.get14DigitDate(fetchStart).getBytes(); baos.write(fetchDate); // Don't forget the newline baos.write("\n".getBytes()); if (rrecordSet != null) { for (int i = 0; i < rrecordSet.length; i++) { byte[] record = rrecordSet[i].toString().getBytes(); baos.write(record); // Add the newline between records back in baos.write("\n".getBytes()); } } return baos.toByteArray(); }