Most users interact with DNSDB either through the web interface or via the sample command line interface (CLI) tools Farsight provides).
However, sometimes you may need to make complex or conditional queries that don't fit well with either of those options. In that case, DNSDB API access customers can call the DNSDB API directly from their own application.
The DNSDB API documentation describes how to make "bulk, automated DNSDB queries via the HTTP API" but it doesn't provide any actual code samples.
Rather than trying to build a complex application for this post, let's just build a basic example that uses libcurl to make calls to the DNSDB API.
libcurl is the API version of the curl command line web client we all know and love.
Some alternatives to libcurl are available here should you want to check them out.
Please note that libcurl is under active development. The version I installed and used for the following example was:
$ curl-config --version libcurl 7.50.3 [released on Sept 14, 2016]
For details about libcurl's release history and changes, go here and here. I strongly encourage you to use the most recent version of libcurl, and be sure that your installation of openssl is fully up-to-date, too.
3. Sample simple skeleton C code to make DNSDB queries
For this example, we just want to build a small C program that will take a list of fully qualified domain names from
stdin and pump them through DNSDB, returning the results to
stdout. We could easily do this with the existing CLI clients available for use with DNSDB, but we wanted a simple example that nicely illustrates the basics of making DNSDB API calls with libcurl.
The full range of API command options described at in the documentation can be easily added to this example, if desired.
Note: Speaking of extending this example, this skeleton code is just that, an example, and does not purport to be "production-grade" (e.g., it embeds the user's API key, it assumes the input is trusted and doesn't explicitly sanitize it, it does not do extensive error checking, it only takes the first dozen results for each domain, etc.).
Anyone using this example as the basis for actual production code should enhance/rewrite the sample as may be appropriate for their environment and their unique requirements.
Normally we'd use a makefile to build the code, but it in this case it's such a small and simple sample we can build via:
$ gcc -Wall -O3 -o libcurl-sample libcurl-sample.c -I/usr/local/include -L/usr/local/lib/ -lcurl
Next, assume that we have a small test file that perhaps looks something like:
$ cat test-domains.txt www.google.com www.apple.com www.microsoft.com www.amazon.com [etc]
We can run the test by saying:
$ ./libcurl-sample < test-domains.txt > output.txt
This is a very simple single-threaded and untuned example. Nonetheless, how does it perform?
If we take 1,000 domains from https://raw.githubusercontent.com/opendns/public-domain-lists/master/opendns-top-domains.txt and time how long it takes to run those domains through DNSDB with this simple application, we see:
$ time ./sample < top-1000.txt > /dev/null real 1m17.653s (e.g., 77.653 seconds) user 0m22.417s sys 0m0.828s
That implies 1,000/77.653=12.87 queries/second even for just this very simple code. How long would it take to run a million queries with the simple code? Hypothetically, by extrapolation, it should take roughly 1,000 times the time it took to run 1,000 queries: 1,000*77.653/(60*60)=21.57 hours
Obviously, parallelization (e.g., using ten concurrent threads, or simply running multiple parallel jobs), could reduce that time by an order of magnitude. [Farsight generally asks that users limit their queries to no more than ten concurrent threads unless special arrangements have been made in advance.]
You've now learned how to write C code with libcurl that will let you run basic DNSDB queries using your DNSDB API credentials.
If you aren't currently a Farsight DNSDB API customer, and want to lean more, please go here for information on how to purchase DNSDB API service.
Joe St Sauver, Ph.D. is a Scientist with Farsight Security, Inc.
← Blog Home