#include <netinet/in.h>#include <arpa/nameser.h>#include <resolv.h>extern struct state _res;int res_init(void);I int res_query(const char * dname , int class , int type ,
I unsigned char * answer , int anslen );I int res_search(const char * dname , int class , int type ,
I unsigned char * answer , int anslen );I int res_querydomain(const char * name , const char * domain ,
I int class , int type , unsigned char * answer ,I int anslen );I int res_mkquery(int op , const char * dname , int class ,
I int type , char * data , int datalen , struct rrec * newrr ,I char * buf , int buflen );I int res_send(const char * msg , int msglen , char * answer ,
I int anslen );I int dn_comp(unsigned char * exp_dn , unsigned char * comp_dn ,
I int length , unsigned char ** dnptrs , unsigned char * exp_dn ,I unsigned char ** lastdnptr );I int dn_expand(unsigned char * msg , unsigned char * eomorig ,
I unsigned char * comp_dn , unsigned char * exp_dn ,I int length );
Link with -lresolv.
DESCRIPTION
These functions make queries to and interpret the responses from Internet
domain name servers.
The
R 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.
R res_init ()
is normally executed by the first call to one of the
other functions.
The
R 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
R res_search ()
function makes a query and waits for the response
like
R 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
R res_querydomain ()
function makes a query using
R res_query ()
on the concatenation of name and domain.
The following functions are lower-level routines used by
R res_query ().
The
R 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
R res_send ()
function sends a pre-formatted query given in
msg of length msglen and returns the answer in answer
which is of length anslen.
It will call
R res_init (),
if it
has not already been called.
The
R 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:
RES_INIT
True if
R res_init ()
has been called.
RES_DEBUG
Print debugging messages.
RES_AAONLY
Accept authoritative answers only.
R res_send ()
continues until
it fins an authoritative answer or returns an error. [Not currently
implemented].
RES_USEVC
Use TCP connections for queries rather than UDP datagrams.
RES_PRIMARY
Query primary domain name server only.
RES_IGNTC
Ignore truncation errors.
Don't retry with TCP. [Not currently
implemented].
RES_RECURSE
Set the recursion desired bit in queries.
Recursion is carried out
by the domain name server, not by
R res_send ().
[Enabled by default].
RES_DEFNAMES
If set,
R res_search ()
will append the default domain name to
single component names, ie. those that do not contain a dot.
[Enabled by default].
RES_STAYOPEN
Used with RES_USEVC to keep the TCP connection open between queries.
RES_DNSRCH
If set,
R res_search ()
will search for host names in the current
domain and in parent domains.
This option is used by
gethostbyname(3).
[Enabled by default].
RETURN VALUE
The
R res_init ()
function returns 0 on success, or -1 if an error
occurs.
The
R res_query (),R res_search (),R res_querydomain (),R res_mkquery ()
and
R res_send ()
functions return the length
of the response, or -1 if an error occurs.
The
R dn_comp ()
and
R dn_expand ()
functions return the length
of the compressed name, or -1 if an error occurs.