#include <netinet/in.h> #include <arpa/nameser.h> #include <resolv.h> extern struct __res_state _res; int res_init(void); int res_query(const char *dname, int class, int type,
Link with -lresolv.
The res_init() function reads the configuration files (see resolv.conf(5)) to get the default domain name, search order and name server address(es). If no server is given, the local host is tried. If no domain is given, that associated with the local host is used. It can be overridden with the environment variable LOCALDOMAIN. res_init() is normally executed by the first call to one of the other functions.
The res_query() function queries the name server for the fully qualified domain name name of specified type and class. The reply is left in the buffer answer of length anslen supplied by the caller.
The res_search() function makes a query and waits for the response like res_query(), but in addition implements the default and search rules controlled by RES_DEFNAMES and RES_DNSRCH (see description of _res options below).
The res_querydomain() function makes a query using res_query() on the concatenation of name and domain.
The following functions are lower-level routines used by res_query().
The res_mkquery() function constructs a query message in buf of length buflen for the domain name dname. The query type op is usually QUERY, but can be any of the types defined in <arpa/nameser.h>. newrr is currently unused.
The res_send() function sends a preformatted query given in msg of length msglen and returns the answer in answer which is of length anslen. It will call res_init(), if it has not already been called.
The dn_comp() function compresses the domain name exp_dn and stores it in the buffer comp_dn of length length. The compression uses an array of pointers dnptrs to previously compressed names in the current message. The first pointer points to the beginning of the message and the list ends with NULL. The limit of the array is specified by lastdnptr. If dnptr is NULL, domain names are not compressed. If lastdnptr is NULL, the list of labels is not updated.
The dn_expand() function expands the compressed domain name comp_dn to a full domain name, which is placed in the buffer exp_dn of size length. The compressed name is contained in a query or reply message, and msg points to the beginning of the message.
The resolver routines use global configuration and state information contained in the structure _res, which is defined in <resolv.h>. The only field that is normally manipulated by the user is _res.options. This field can contain the bitwise "OR" of the following options:
This list is not complete. You can find some other flags described in resolv.conf(5).
The res_query(), res_search(), res_querydomain(), res_mkquery() and res_send() functions return the length of the response, or -1 if an error occurs.
The dn_comp() and dn_expand() functions return the length of the compressed name, or -1 if an error occurs.
/etc/resolv.conf resolver configuration file /etc/host.conf resolver configuration file