Search in sources :

Example 1 with Cache

use of org.xbill.DNS.Cache in project nhin-d by DirectProject.

the class RawRecursiveDNSCertQuery method main.

public static void main(String[] args) {
    if (args.length == 0) {
        printUsage();
        System.exit(-1);
    }
    String emailAddress = "";
    String[] servers = null;
    // Check parameters
    for (int i = 0; i < args.length; i++) {
        String arg = args[i];
        // Options
        if (!arg.startsWith("-")) {
            System.err.println("Error: Unexpected argument [" + arg + "]\n");
            printUsage();
            System.exit(-1);
        } else if (arg.equalsIgnoreCase("-add")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing email address");
                System.exit(-1);
            }
            emailAddress = args[++i];
        } else if (arg.equals("-server")) {
            if (i == args.length - 1 || args[i + 1].startsWith("-")) {
                System.err.println("Error: Missing DNS server list");
                System.exit(-1);
            }
            servers = args[++i].split(",");
        } else if (arg.equals("-help")) {
            printUsage();
            System.exit(-1);
        } else {
            System.err.println("Error: Unknown argument " + arg + "\n");
            printUsage();
            System.exit(-1);
        }
    }
    if (emailAddress == null || emailAddress.isEmpty()) {
        System.err.println("You must provide an email address.");
        printUsage();
    }
    setServers(servers);
    int attemptCount = 0;
    while (true) {
        try {
            final String lookupName = emailAddress.replace('@', '.');
            // run this forever
            final Lookup lu = new Lookup(new Name(lookupName), Type.CERT);
            lu.setResolver(createExResolver(dnsServers.toArray(new String[dnsServers.size()]), 0, 6));
            lu.setSearchPath((String[]) null);
            // clear the cache
            lu.setCache(new Cache(DClass.IN));
            long startTime = System.currentTimeMillis();
            final Record[] retRecords = lu.run();
            long endTime = System.currentTimeMillis();
            if (retRecords == null || retRecords.length == 0) {
                System.out.println("----- Found no certificates -------");
                System.out.println("\tDNS search took " + (endTime - startTime) + "ms\r\n");
                System.out.println("Failed after " + attemptCount + " successful resolution attempts.");
                System.exit(0);
            } else {
                System.out.println("Found " + retRecords.length + " certificates");
            }
            System.out.println("\tDNS search took " + (endTime - startTime) + "ms\r\n");
            Thread.sleep(1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ++attemptCount;
    }
}
Also used : Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) UnknownHostException(java.net.UnknownHostException) Name(org.xbill.DNS.Name) Cache(org.xbill.DNS.Cache)

Example 2 with Cache

use of org.xbill.DNS.Cache in project nhin-d by DirectProject.

the class DNSConnectionTest method performLookup.

private static void performLookup() throws Exception {
    // turn on debug settings for the DNS client
    Options.set("verbose", "true");
    Cache ch = Lookup.getDefaultCache(DClass.IN);
    ch.clearCache();
    if (servers == null || servers.length == 0)
        servers = ResolverConfig.getCurrentConfig().servers();
    System.out.println("\r\nConfigure DNS resolvers:");
    for (String server : servers) {
        System.out.println("\t" + server);
    }
    System.out.println("\r\nLookup up record " + lookupRec);
    Lookup lu = new Lookup(new Name(lookupRec), recType);
    ExtendedResolver resolver = new ExtendedResolver(servers);
    resolver.setTCP(useTCP);
    lu.setResolver(resolver);
    Record[] retRecords = lu.run();
    if (retRecords != null && retRecords.length > 0)
        System.out.println(retRecords.length + " records found.");
    else
        System.out.println("No records found.");
}
Also used : ExtendedResolver(org.xbill.DNS.ExtendedResolver) Lookup(org.xbill.DNS.Lookup) Record(org.xbill.DNS.Record) Cache(org.xbill.DNS.Cache) Name(org.xbill.DNS.Name)

Example 3 with Cache

use of org.xbill.DNS.Cache in project opennms by OpenNMS.

the class DNSServer method parseConfiguration.

protected void parseConfiguration(final String conffile) throws ConfigurationException, IOException, ZoneTransferException, UnknownHostException {
    final FileInputStream fs;
    final InputStreamReader isr;
    final BufferedReader br;
    try {
        fs = new FileInputStream(conffile);
        isr = new InputStreamReader(fs);
        br = new BufferedReader(isr);
    } catch (final Exception e) {
        LOG.error("Cannot open {}", conffile, e);
        throw new ConfigurationException("unable to read from " + conffile, e);
    }
    try {
        String line = null;
        while ((line = br.readLine()) != null) {
            final StringTokenizer st = new StringTokenizer(line);
            if (!st.hasMoreTokens()) {
                continue;
            }
            final String keyword = st.nextToken();
            if (!st.hasMoreTokens()) {
                LOG.warn("unable to parse line: {}", line);
                continue;
            }
            if (keyword.charAt(0) == '#') {
                continue;
            }
            if (keyword.equals("primary")) {
                addPrimaryZone(st.nextToken(), st.nextToken());
            } else if (keyword.equals("secondary")) {
                addSecondaryZone(st.nextToken(), st.nextToken());
            } else if (keyword.equals("cache")) {
                final Cache cache = new Cache(st.nextToken());
                m_caches.put(Integer.valueOf(DClass.IN), cache);
            } else if (keyword.equals("key")) {
                final String s1 = st.nextToken();
                final String s2 = st.nextToken();
                if (st.hasMoreTokens()) {
                    addTSIG(s1, s2, st.nextToken());
                } else {
                    addTSIG("hmac-md5", s1, s2);
                }
            } else if (keyword.equals("port")) {
                m_ports.add(Integer.valueOf(st.nextToken()));
            } else if (keyword.equals("address")) {
                final String addr = st.nextToken();
                m_addresses.add(Address.getByAddress(addr));
            } else {
                LOG.warn("unknown keyword: {}", keyword);
            }
        }
    } finally {
        fs.close();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) ZoneTransferException(org.xbill.DNS.ZoneTransferException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) NameTooLongException(org.xbill.DNS.NameTooLongException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Cache(org.xbill.DNS.Cache)

Example 4 with Cache

use of org.xbill.DNS.Cache in project opennms by OpenNMS.

the class DNSServer method getCache.

public Cache getCache(final int dclass) {
    Cache c = m_caches.get(dclass);
    if (c == null) {
        c = new Cache(dclass);
        m_caches.put(Integer.valueOf(dclass), c);
    }
    return c;
}
Also used : Cache(org.xbill.DNS.Cache)

Example 5 with Cache

use of org.xbill.DNS.Cache in project opennms by OpenNMS.

the class DNSServer method findExactMatch.

public RRset findExactMatch(final Name name, final int type, final int dclass, final boolean glue) {
    final Zone zone = findBestZone(name);
    if (zone != null) {
        return zone.findExactMatch(name, type);
    } else {
        final RRset[] rrsets;
        final Cache cache = getCache(dclass);
        if (glue) {
            rrsets = cache.findAnyRecords(name, type);
        } else {
            rrsets = cache.findRecords(name, type);
        }
        if (rrsets == null) {
            return null;
        } else {
            return rrsets[0];
        /* not quite right */
        }
    }
}
Also used : Zone(org.xbill.DNS.Zone) RRset(org.xbill.DNS.RRset) Cache(org.xbill.DNS.Cache)

Aggregations

Cache (org.xbill.DNS.Cache)5 UnknownHostException (java.net.UnknownHostException)2 Lookup (org.xbill.DNS.Lookup)2 Name (org.xbill.DNS.Name)2 Record (org.xbill.DNS.Record)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 InterruptedIOException (java.io.InterruptedIOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 StringTokenizer (java.util.StringTokenizer)1 ExtendedResolver (org.xbill.DNS.ExtendedResolver)1 NameTooLongException (org.xbill.DNS.NameTooLongException)1 RRset (org.xbill.DNS.RRset)1 Zone (org.xbill.DNS.Zone)1 ZoneTransferException (org.xbill.DNS.ZoneTransferException)1