c-ares: the C library the internet is built on
By Ifeanyi Obi, Network Protocols Instructor
Before a program can connect to example.com, it has to turn that name into an IP address. The classic C function for this, getaddrinfo, blocks: the calling thread waits until the answer comes back. For a command-line tool fetching one page that is fine. For a server handling thousands of connections, or a tool downloading from dozens of hosts at once, a thread stuck waiting on DNS is a thread doing nothing.
c-ares exists to solve exactly that. It describes itself as a modern DNS stub resolver library written in C, and its job is to send DNS queries and hand you the answers asynchronously, so the rest of your program keeps running while it waits.
Where it came from
c-ares grew out of an earlier library called ares, written by Greg Hudson at MIT. In the early 2000s Daniel Stenberg, the author of curl, needed asynchronous name resolution for curl, forked ares and started c-ares. It has been released under the permissive MIT license ever since, which is a big reason it ended up in so many other projects.
Who uses it
- curl can be built with c-ares as its resolver, so transfers to many hosts don't wait on each other's DNS lookups.
- Node.js uses c-ares for its dns module functions such as dns.resolve, which query DNS servers directly. (dns.lookup, by contrast, goes through the operating system's getaddrinfo on a thread pool.)
- gRPC can use c-ares as its DNS resolver for service discovery.
- Plenty of other servers, proxies and language runtimes embed it because it is small, portable and has no heavy dependencies.
That is why we call it a library the internet is built on: most developers never call it directly, but a large share of the network software they use every day depends on it.
What using it looks like
Recent versions can run their own event thread, which makes a small example much shorter: you start queries, each one calls you back when its answer arrives, and you wait until the queue is empty. This sketch follows the API in c-ares 1.34's ares.h. It needs a c-ares build with threading support, which is the default on Linux and macOS.
#include <ares.h>
#include <arpa/inet.h>
#include <stdio.h>
static void on_result(void *arg, int status, int timeouts, struct ares_addrinfo *res) {
const char *name = arg;
(void)timeouts;
if (status != ARES_SUCCESS) {
printf("%s: %s\n", name, ares_strerror(status));
return;
}
for (struct ares_addrinfo_node *n = res->nodes; n; n = n->ai_next) {
char ip[INET6_ADDRSTRLEN];
const void *addr = n->ai_family == AF_INET
? (const void *)&((struct sockaddr_in *)n->ai_addr)->sin_addr
: (const void *)&((struct sockaddr_in6 *)n->ai_addr)->sin6_addr;
inet_ntop(n->ai_family, addr, ip, sizeof ip);
printf("%s -> %s\n", name, ip);
}
ares_freeaddrinfo(res);
}
int main(void) {
ares_library_init(ARES_LIB_INIT_ALL);
struct ares_options opts = {.evsys = ARES_EVSYS_DEFAULT};
ares_channel_t *channel;
if (ares_init_options(&channel, &opts, ARES_OPT_EVENT_THREAD) != ARES_SUCCESS)
return 1;
struct ares_addrinfo_hints hints = {.ai_family = AF_UNSPEC};
const char *names[] = {"example.com", "curl.se", "nodejs.org"};
for (int i = 0; i < 3; i++) // all three lookups run at the same time
ares_getaddrinfo(channel, names[i], NULL, &hints, on_result, (void *)names[i]);
ares_queue_wait_empty(channel, -1); // wait until every callback has run
ares_destroy(channel);
ares_library_cleanup();
return 0;
}Build it with cc lookup.c -lcares. The answers can print in any order, because each lookup finishes whenever its DNS server replies. That is the whole point.
Why it's worth studying
c-ares is a good codebase to read once you are comfortable with C and sockets. It is real DNS: message encoding and parsing, retries and timeouts, server rotation, reading resolv.conf, and running on Linux, macOS, Windows and more from one source tree. Students in our Network Protocol Developer course start with the ping program from an earlier post and work up to exactly this kind of library.