org.xbill.DNS.Lookup Java Examples
The following examples show how to use
org.xbill.DNS.Lookup.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XBillDnsSrvResolver.java From dns-java with Apache License 2.0 | 7 votes |
@Override public List<LookupResult> resolve(final String fqdn) { Lookup lookup = lookupFactory.forName(fqdn); Record[] queryResult = lookup.run(); switch (lookup.getResult()) { case Lookup.SUCCESSFUL: return toLookupResults(queryResult); case Lookup.HOST_NOT_FOUND: // fallthrough case Lookup.TYPE_NOT_FOUND: LOG.warn("No results returned for query '{}'; result from XBill: {} - {}", fqdn, lookup.getResult(), lookup.getErrorString()); return ImmutableList.of(); default: throw new DnsException( String.format("Lookup of '%s' failed with code: %d - %s ", fqdn, lookup.getResult(), lookup.getErrorString())); } }
Example #2
Source File: GoogleResolverCheck.java From entrada with GNU General Public License v3.0 | 7 votes |
@Override protected List<String> fetch() { try { Resolver resolver = new SimpleResolver(); // dns resolvers may take a long time to return a response. resolver.setTimeout(timeout); Lookup l = new Lookup(StringUtils.endsWith(hostname, ".") ? hostname : hostname + ".", Type.TXT); // always make sure the cache is empty l.setCache(new Cache()); l.setResolver(resolver); Record[] records = l.run(); if (records != null && records.length > 0) { return parse(records[0]); } } catch (Exception e) { log.error("Problem while adding Google resolvers, continue without", e); } log.error("No Google resolver addresses found"); return Collections.emptyList(); }
Example #3
Source File: CustomDns.java From KinoCast with MIT License | 6 votes |
private void init() { if (mInitialized) return; else mInitialized = true; try { // configure the resolvers, starting with the default ones (based on the current network connection) Resolver defaultResolver = Lookup.getDefaultResolver(); // use Google's public DNS services Resolver googleFirstResolver = new SimpleResolver("8.8.8.8"); Resolver googleSecondResolver = new SimpleResolver("8.8.4.4"); // also try using Amazon Resolver amazonResolver = new SimpleResolver("205.251.198.30"); Lookup.setDefaultResolver(new ExtendedResolver(new Resolver[]{ googleFirstResolver, googleSecondResolver, amazonResolver, defaultResolver })); } catch (UnknownHostException e) { Log.w(TAG, "Couldn't initialize custom resolvers"); } }
Example #4
Source File: ForwardLookupHelper.java From yeti with MIT License | 6 votes |
public static List<ForwardLookupResult> getARecord(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.A).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 = ((ARecord) record).getAddress().getHostAddress(); foundSubDomain.setIpAddress(ipAddress); foundSubDomain.setLookupType("A"); entries.add(foundSubDomain); } } } } return entries; }
Example #5
Source File: ForwardLookupHelper.java From yeti with MIT License | 6 votes |
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; }
Example #6
Source File: DNS.java From yeti with MIT License | 6 votes |
public static List<ARecordResult> getARecord(String hostName) throws TextParseException { List<ARecordResult> entries = null; Record[] recs = new Lookup(hostName, Type.A).run(); if (recs != null) { if (recs.length > 0) { entries = new ArrayList<>(); for (Record record : recs) { ARecordResult foundSubDomain = new ARecordResult(NetworkTools.getDomainFromHost(hostName)); foundSubDomain.setHostName(hostName); String ipAddress = ((ARecord) record).getAddress().getHostAddress(); foundSubDomain.setIpAddress(ipAddress); foundSubDomain.setLookupType("A"); entries.add(foundSubDomain); } } } return entries; }
Example #7
Source File: DNSJavaServiceTest.java From james-project with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { dnsServer = new TestableDNSServer(); dnsServer.configure(FileConfigurationProvider.getConfig(new ByteArrayInputStream(DNS_SERVER_CONFIG))); dnsServer.init(); defaultCache = Lookup.getDefaultCache(DClass.IN); defaultResolver = Lookup.getDefaultResolver(); defaultSearchPaths = Lookup.getDefaultSearchPath(); Lookup.setDefaultCache(null, DClass.IN); Lookup.setDefaultResolver(null); Lookup.setDefaultSearchPath(new Name[]{}); dnsServer.setResolver(null); mockedCache = mock(Cache.class); }
Example #8
Source File: NetworkTools.java From yeti with MIT License | 6 votes |
public static String getDomainFromHost(String host) { String tmpHost = host; if (host.isEmpty()) { return ""; } while (true) { try { if (!tmpHost.startsWith("www.")) { Record[] recs = new Lookup(tmpHost, Type.SOA).run(); if (recs != null) { return tmpHost; } } if (tmpHost.contains(".")) { tmpHost = tmpHost.split("\\.", 2)[1]; } else { break; } } catch (TextParseException ex) { Logger.getLogger("networkTools.getDomainFromHost").log(Level.SEVERE, null, ex); break; } } return ""; }
Example #9
Source File: DataAPI.java From yeti with MIT License | 6 votes |
public List<ARecordResult> getARecord(String hostName) throws TextParseException { List<ARecordResult> entries = null; Record[] recs = new Lookup(hostName, Type.A).run(); if (recs != null) { if (recs.length > 0) { entries = new ArrayList<>(); for (Record record : recs) { ARecordResult foundSubDomain = new ARecordResult(NetworkTools.getDomainFromHost(hostName)); foundSubDomain.setHostName(hostName); String ipAddress = ((ARecord) record).getAddress().getHostAddress(); foundSubDomain.setIpAddress(ipAddress); foundSubDomain.setLookupType("A"); entries.add(foundSubDomain); } } } return entries; }
Example #10
Source File: Helperfunctions.java From open-rmbt with Apache License 2.0 | 6 votes |
public static String reverseDNSLookup(final InetAddress adr) { try { final Name name = ReverseMap.fromAddress(adr); final Lookup lookup = new Lookup(name, Type.PTR); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) if (record instanceof PTRRecord) { final PTRRecord ptr = (PTRRecord) record; return ptr.getTarget().toString(); } } catch (final Exception e) { } return null; }
Example #11
Source File: lookup.java From dnsjava with BSD 2-Clause "Simplified" License | 6 votes |
public static void main(String[] args) throws Exception { int type = Type.A; int start = 0; if (args.length > 2 && args[0].equals("-t")) { type = Type.value(args[1]); if (type < 0) { throw new IllegalArgumentException("invalid type"); } start = 2; } for (int i = start; i < args.length; i++) { Lookup l = new Lookup(args[i], type); l.run(); printAnswer(args[i], l); } }
Example #12
Source File: MxLookupTest.java From mireka with Apache License 2.0 | 6 votes |
@Test() public void testNoMxRecords() throws MxLookupException { new Expectations() { { lookup.run(); result = null; lookup.getResult(); result = Lookup.TYPE_NOT_FOUND; } }; Name[] targets = mxLookup.queryMxTargets(EXAMPLE_COM_DOMAIN); assertArrayEquals(new Name[] { EXAMPLE_COM_NAME }, targets); }
Example #13
Source File: lookup.java From dnsjava with BSD 2-Clause "Simplified" License | 6 votes |
public static void printAnswer(String name, Lookup lookup) { System.out.print(name + ":"); int result = lookup.getResult(); if (result != Lookup.SUCCESSFUL) { System.out.print(" " + lookup.getErrorString()); } System.out.println(); Name[] aliases = lookup.getAliases(); if (aliases.length > 0) { System.out.print("# aliases: "); for (int i = 0; i < aliases.length; i++) { System.out.print(aliases[i]); if (i < aliases.length - 1) { System.out.print(" "); } } System.out.println(); } if (lookup.getResult() == Lookup.SUCCESSFUL) { Record[] answers = lookup.getAnswers(); for (Record answer : answers) { System.out.println(answer); } } }
Example #14
Source File: AddressLookupTest.java From mireka with Apache License 2.0 | 6 votes |
@Test public void testTransientDnsFailure() { new Expectations() { { lookup.run(); result = null; lookup.getResult(); result = Lookup.TRY_AGAIN; } }; SendException e; try { addressLookup.queryAddresses(HOST1_EXAMPLE_COM_NAME); fail("An exception must have been thrown."); return; } catch (SendException e1) { e = e1; } assertTrue(e.errorStatus().shouldRetry()); }
Example #15
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #16
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #17
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #18
Source File: TldController.java From yeti with MIT License | 5 votes |
public void expandDomain(String domainToCheck) throws TextParseException { String domainName = null; // Check for domain name alias - CNAME Record[] recs = new Lookup(domainToCheck, Type.CNAME).run(); if (recs != null && recs.length != 0) { domainName = ((CNAMERecord) recs[0]).getName().canonicalize().toString(true); Log.debug("Found: " + domainName + "CNAME rec: " + domainName); } // Now get the SOA record that would signify a domain exists recs = new Lookup(domainToCheck, Type.SOA).run(); for (int idx = 0; idx < retryCount; idx++) { if (recs != null) { if (domainName == null) { domainName = ((SOARecord) recs[0]).getName().canonicalize().toString(true); Log.debug("Found: " + domainName + " SOA rec: " + domainName); } DomainResult newDomain = new DomainResult(domainName); newDomain.setNameServer(((SOARecord) recs[0]).getHost().toString(true)); newDomain.setAdminName(((SOARecord) recs[0]).getAdmin().toString(true)); String name = domainToCheck.split("\\.", 2)[0]; String tld = domainToCheck.split("\\.", 2)[1]; newDomain.setRegistrant(NetworkTools.getHostNameWhoisResult(name, tld, true)); Map<String, String> attrs = new HashMap<>(); attrs.put(DataStore.DNS_RECORD, DataStore.SOA); newDomain.setAttributes(attrs); addResult(newDomain); break; } } }
Example #19
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #20
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #21
Source File: DNSServiceAddressResolver.java From cloudbreak with Apache License 2.0 | 5 votes |
private String dnsSrvLookup(String query) throws ServiceAddressResolvingException { String result; try { Record[] records = new Lookup(query, Type.SRV).run(); if (records != null && records.length > 0) { SRVRecord srv = (SRVRecord) records[0]; result = srv.getTarget().toString().replaceFirst("\\.$", "") + ':' + srv.getPort(); } else { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved"); } } catch (TextParseException e) { throw new ServiceAddressResolvingException("The Service " + query + " cannot be resolved", e); } return result; }
Example #22
Source File: MxLookupTest.java From mireka with Apache License 2.0 | 5 votes |
@Test(expected = SendException.class) public void testHostNotFound() throws MxLookupException { new Expectations() { { lookup.run(); result = null; lookup.getResult(); result = Lookup.HOST_NOT_FOUND; } }; mxLookup.queryMxTargets(EXAMPLE_COM_DOMAIN); }
Example #23
Source File: DNS.java From yeti with MIT License | 5 votes |
public static SOARecordResult getSOARecord(String domainName) { SOARecordResult newDomain = null; try { Record[] recs = new Lookup(domainName, Type.SOA).run(); if (recs != null) { domainName = ((SOARecord) recs[0]).getName().canonicalize().toString(true); newDomain = new SOARecordResult(domainName); newDomain.setNameServer(((SOARecord) recs[0]).getHost().toString(true)); newDomain.setAdminName(((SOARecord) recs[0]).getAdmin().toString(true)); } } catch (TextParseException tpe) { Logger.getLogger(DNS.class.getName()).log(Level.SEVERE, null, tpe); } return newDomain; }
Example #24
Source File: NetworkTools.java From yeti with MIT License | 5 votes |
public static List<String> getIpFromHost(String hostname) throws TextParseException { List<String> result = new ArrayList<>(); Record[] recs = new Lookup(hostname, Type.A).run(); if (recs != null) { if (recs.length > 0) { for (Record rec : recs) { String ipAddress = ((ARecord) rec).getAddress().toString(); result.add(ipAddress.replace("/", "")); } } } return result; }
Example #25
Source File: Helperfunctions.java From open-rmbt with Apache License 2.0 | 5 votes |
public static Long getASN(final InetAddress adr) { try { final Name postfix; if (adr instanceof Inet6Address) postfix = Name.fromConstantString("origin6.asn.cymru.com"); else postfix = Name.fromConstantString("origin.asn.cymru.com"); final Name name = getReverseIPName(adr, postfix); System.out.println("lookup: " + name); final Lookup lookup = new Lookup(name, Type.TXT); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) if (record instanceof TXTRecord) { final TXTRecord txt = (TXTRecord) record; @SuppressWarnings("unchecked") final List<String> strings = txt.getStrings(); if (strings != null && !strings.isEmpty()) { final String result = strings.get(0); final String[] parts = result.split(" ?\\| ?"); if (parts != null && parts.length >= 1) return new Long(parts[0]); } } } catch (final Exception e) { } return null; }
Example #26
Source File: Helperfunctions.java From open-rmbt with Apache License 2.0 | 5 votes |
public static String getASName(final long asn) { try { final Name postfix = Name.fromConstantString("asn.cymru.com."); final Name name = new Name(String.format("AS%d", asn), postfix); System.out.println("lookup: " + name); final Lookup lookup = new Lookup(name, Type.TXT); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) if (record instanceof TXTRecord) { final TXTRecord txt = (TXTRecord) record; @SuppressWarnings("unchecked") final List<String> strings = txt.getStrings(); if (strings != null && !strings.isEmpty()) { System.out.println(strings); final String result = strings.get(0); final String[] parts = result.split(" ?\\| ?"); if (parts != null && parts.length >= 1) return parts[4]; } } } catch (final Exception e) { } return null; }
Example #27
Source File: Helperfunctions.java From open-rmbt with Apache License 2.0 | 5 votes |
public static String getAScountry(final long asn) { try { final Name postfix = Name.fromConstantString("asn.cymru.com."); final Name name = new Name(String.format("AS%d", asn), postfix); System.out.println("lookup: " + name); final Lookup lookup = new Lookup(name, Type.TXT); lookup.setResolver(new SimpleResolver()); lookup.setCache(null); final Record[] records = lookup.run(); if (lookup.getResult() == Lookup.SUCCESSFUL) for (final Record record : records) if (record instanceof TXTRecord) { final TXTRecord txt = (TXTRecord) record; @SuppressWarnings("unchecked") final List<String> strings = txt.getStrings(); if (strings != null && !strings.isEmpty()) { final String result = strings.get(0); final String[] parts = result.split(" ?\\| ?"); if (parts != null && parts.length >= 1) return parts[1]; } } } catch (final Exception e) { } return null; }
Example #28
Source File: DNSUtils.java From buddycloud-android with Apache License 2.0 | 5 votes |
/** * Adapted from https://code.google.com/p/asmack/source/browse/src/custom/org/jivesoftware/smack/util/DNSUtil.java * * @param domain * @return * @throws TextParseException */ @SuppressWarnings("unchecked") private static String resolveAPITXT(String domain) throws TextParseException { Lookup lookup = new Lookup(TXT_PREFIX + domain, Type.TXT); Record recs[] = lookup.run(); if (recs == null) { throw new RuntimeException("Could not lookup domain."); } Map<String, String> stringMap = null; for (Record rec : recs) { String rData = rec.rdataToString().replaceAll("\"", ""); List<String> rDataTokens = Arrays.asList(rData.split("\\s+")); TXTRecord record = new TXTRecord(rec.getName(), rec.getDClass(), rec.getTTL(), rDataTokens); List<String> strings = record.getStrings(); if (strings != null && strings.size() > 0) { stringMap = parseStrings(strings); break; } } if (stringMap == null) { throw new RuntimeException("Domain has no TXT records for buddycloud."); } String host = stringMap.get("host"); String protocol = stringMap.get("protocol"); String path = stringMap.get("path"); String port = stringMap.get("port"); path = path == null || path.equals("/") ? "" : path; port = port == null ? "" : port; return protocol + "://" + host + ":" + port + path; }
Example #29
Source File: IpAddressUtil.java From megatron-java with Apache License 2.0 | 5 votes |
private static String reverseDnsLookupUsingDnsJavaExtendedResolver(long ipAddress) throws UnknownHostException { byte[] address = convertLongAddressToBuf(ipAddress); Name name = ReverseMap.fromAddress(InetAddress.getByAddress(address)); Record[] records = new Lookup(name, Type.PTR).run(); if (records == null) { throw new UnknownHostException(); } String result = ((PTRRecord)records[0]).getTarget().toString(); // remove trailing "." result = result.endsWith(".") ? result.substring(0, result.length() - 1) : result; return result; }
Example #30
Source File: SimpleLookupFactory.java From dns-java with Apache License 2.0 | 5 votes |
@Override public Lookup forName(String fqdn) { try { final Lookup lookup = new Lookup(fqdn, Type.SRV, DClass.IN); if (resolver != null) { lookup.setResolver(resolver); } return lookup; } catch (TextParseException e) { throw new DnsException("unable to create lookup for name: " + fqdn, e); } }