how to get DNS server in c-ares
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want to know which DNS servers a c-ares channel is using, the answer is not buried inside a resolver query callback. c-ares exposes dedicated APIs for reading the configured server list after the channel is initialized. The main choice is whether you use the newer string-based API or the older linked-list style functions.
Initialize the Channel First
The server list belongs to an ares_channel, so you need to initialize c-ares and create a channel before you ask for it.
After ares_init, c-ares has loaded its resolver configuration from the operating system or from options you provided explicitly.
Preferred Modern Option: ares_get_servers_csv
In newer c-ares versions, the easiest way to inspect the configured servers is ares_get_servers_csv. It returns the server list as a CSV string that you can print or parse.
This is often the simplest answer if your installed c-ares version supports it. In current c-ares documentation, the older linked-list retrieval functions are marked deprecated in favor of this CSV-based API.
Older API: ares_get_servers
Older code often uses ares_get_servers or ares_get_servers_ports. Those APIs return a linked list of ares_addr_node or ares_addr_port_node structures. That works fine, but it is more verbose:
This older form is still useful when you want structured access to each address rather than a single string.
Why the Channel Matters
c-ares does not expose one global resolver state that every program shares uniformly. Different channels can be initialized with different options, including custom server lists. So the correct question is not only "what DNS servers does the machine use" but also "what DNS servers does this channel use after initialization and overrides."
That distinction matters if you call ares_set_servers, pass options during initialization, or load configuration from a non-default environment.
Common Pitfalls
- Trying to read DNS server information before calling
ares_init. - Assuming the process has one global resolver list when different c-ares channels may be configured differently.
- Using
ares_get_serverswithout freeing the returned data withares_free_data. - Forgetting that the newer CSV API and older linked-list APIs may differ by c-ares version.
- Confusing the configured server list with the result of a specific DNS query.
Summary
- c-ares exposes APIs to read the configured DNS server list from an initialized channel.
- '
ares_get_servers_csvis the simplest modern option when available.' - '
ares_get_serversand related older APIs return structured linked-list data.' - Always free returned memory with the matching c-ares cleanup function.
- Think in terms of channel configuration, not just machine-wide resolver settings.
Related reading
- How to get Elastic Beanstalk nginx-backed proxy server to auto-redirect from HTTP to HTTPS?
- How to get GET request values in Django?
- how to get hold of the azure kubernetes cluster outbound ip address
- How to get HTTP/2 working in a Kubernetes cluster using ingress-nginx
- How to get the iterator for a successful binary_search?
- How to get the minimum or maximum element in a vector of structures in C, based on some field in the structure
- How to get http headers in flask?
- How to get HTTP response code for a URL in Java?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.