Description

These routines are used for making, sending, and interpreting query and reply
messages with Internet domain name servers.

State information is kept in statp and is used to control the
behavior of these functions. Set statp to all zeros prior to making
the first call to any of these functions.

The res_ndestroy() function should be called to free memory allocated by res_ninit()
after the last use of statp.

The functions res_init(), res_query(), res_search(), res_mkquery(), res_send(), and herror() are deprecated. They
are supplied for backwards compatability. They use global configuration and state information that
is kept in the structure _res rather than state information referenced through
statp.

Most of the values in statp and _res are initialized to reasonable
defaults on the first call to res_ninit() or res_init() and can be
ignored. Options stored in statp->options or _res.options are defined in <resolv.h>. They
are stored as a simple bit mask containing the bitwise OR of
the options enabled.

RES_INIT

True if the initial name server address and default domain name are initialized, that is, res_init() or res_ninit() has been called.

RES_DEBUG

Print debugging messages.

RES_AAONLY

Accept authoritative answers only. With this option, res_send() will continue until it finds an authoritative answer or finds an error. Currently this option is not implemented.

RES_USEVC

Use TCP connections for queries instead of UDP datagrams.

RES_STAYOPEN

Use with RES_USEVC to keep the TCP connection open between queries. This is a useful option for programs that regularly do many queries. The normal mode used should be UDP.

RES_IGNTC

Ignore truncation errors; that is, do not retry with TCP.

RES_RECURSE

Set the recursion-desired bit in queries. This is the default. res_send() and res_nsend() do not do iterative queries and expect the name server to handle recursion.

RES_DEFNAMES

If set, res_search() and res_nsearch() append the default domain name to single-component names, that is, names that do not contain a dot. This option is enabled by default.

RES_DNSRCH

If this option is set, res_search() and res_nsearch() search for host names in the current domain and in parent domains. See hostname(1). This option is used by the standard host lookup routine gethostbyname(3NSL). This option is enabled by default.

RES_NOALIASES

This option turns off the user level aliasing feature controlled by the HOSTALIASES environment variable. Network daemons should set this option.

RES_BLAST

If the RES_BLAST option is defined, resolver() queries will be sent to all servers. If the RES_BLAST option is not defined, but RES_ROTATE is , the list of nameservers are rotated according to a round-robin scheme. RES_BLAST overrides RES_ROTATE.

RES_ROTATE

This option causes res_nsend() and res_send() to rotate the list of nameservers in statp->nsaddr_list or _res.nsaddr_list.

RES_KEEPTSIG

This option causes res_nsendsigned() to leave the message unchanged after TSIG verification. Otherwise the TSIG record would be removed and the header would be updated.

res_ninit(), res_init()

The res_ninit() and res_init() routines read the configuration file, if any is
present, to get the default domain name, search list and the Internet
address of the local name server(s). See resolv.conf(4). If no server is
configured, res_init() or res_ninit() will try to obtain name resolution services from
the host on which it is running. The current domain name is
defined by domainname(1M), or by the hostname if it is not specified in
the configuration file. Use the environment variable LOCALDOMAIN to override the domain name.
This environment variable may contain several blank-separated tokens if you wish to
override the search list on a per-process basis. This is similar to
the search command in the configuration file. You can set the RES_OPTIONS
environment variable to override certain internal resolver options. You can otherwise set them
by changing fields in the statp /_res structure. Alternatively, they are inherited
from the configuration file's options command. See resolv.conf(4) for information regarding the
syntax of the RES_OPTIONS environment variable. Initialization normally occurs on the first
call to one of the other resolver routines.

res_nquery(), res_query()

The res_nquery() and res_query() functions provide interfaces to the server query mechanism.
They construct a query, send it to the local server, await a
response, and make preliminary checks on the reply. The query requests information
of the specified type and class for the specified fully-qualified domain name
dname. The reply message is left in the answer buffer with length
anslen supplied by the caller. res_nquery() and res_query() return the length of
the answer, or -1 upon error.

The res_nquery() and res_query() routines return a length that may be bigger
than anslen. In that case, retry the query with a larger buf.
The answer to the second query may be larger still], so it is
recommended that you supply a buf larger than the answer returned by
the previous query. answer must be large enough to receive a maximum
UDP response from the server or parts of the answer will be silently
discarded. The default maximum UDP response size is 512 bytes.

res_nsearch(), res_search()

The res_nsearch() and res_search() routines make a query and await a response,
just like like res_nquery() and res_query(). In addition, they implement the default
and search rules controlled by the RES_DEFNAMES and RES_DNSRCH options. They return
the length of the first successful reply which is stored in answer.
On error, they reurn –1.

The res_nsearch() and res_search() routines return a length that may be bigger
than anslen. In that case, retry the query with a larger buf.
The answer to the second query may be larger still], so it is
recommended that you supply a buf larger than the answer returned by
the previous query. answer must be large enough to receive a maximum
UDP response from the server or parts of the answer will be silently
discarded. The default maximum UDP response size is 512 bytes.

res_nquerydomain()

The res_nquerydomain() function calls res_query() on the concatenation of name and domain,
removing a trailing dot from name if domain is NULL.

res_nmkquery(), res_mkquery()

These routines are used by res_nquery() and res_query(). The res_nmkquery() and res_mkquery()
functions construct a standard query message and place it in buf. The
routine returns the size of the query, or -1 if the query
is larger than buflen. The query type op is usually QUERY, but
can be any of the query types defined in <arpa/nameser.h>. The domain
name for the query is given by dname. newrr is currently unused but
is intended for making update messages.

res_nsend(), res_send(), res_nsendsigned()

The res_nsend(), res_send(), and res_nsendsigned() routines send a pre-formatted query that returns
an answer. The routine calls res_ninit() or res_init(). If RES_INIT is not
set, the routine sends the query to the local name server and
handles timeouts and retries. Additionally, the res_nsendsigned() uses TSIG signatures to add
authentication to the query and verify the response. In this case, only
one name server will be contacted. The routines return the length of the
reply message, or -1 if there are errors.

The res_nsend() and res_send() routines return a length that may be bigger
than anslen. In that case, retry the query with a larger buf.
The answer to the second query may be larger still], so it is
recommended that you supply a buf larger than the answer returned by
the previous query. answer must be large enough to receive a maximum
UDP response from the server or parts of the answer will be silently
discarded. The default maximum UDP response size is 512 bytes.

fp_resstat()

The function fp_resstat() prints out the active flag bits in statp->options preceded
by the text “;; res options:” on file.

res_hostalias()

The function res_hostalias() looks up name in the file referred to by
the HOSTALIASES environment variable and returns the fully qualified host name. If
name is not found or an error occurs, NULL is returned. res_hostalias()
stores the result in buf.

res_nclose()

The res_nclose() function closes any open files referenced through statp.

res_ndestroy()

The res_ndestroy() function calls res_nclose(), then frees any memory allocated by res_ninit()
referenced through statp.

dn_comp()

The dn_comp() function compresses the domain name exp_dn and stores it in
comp_dn. The dn_comp() function returns the size of the compressed name, or
-1 if there were errors. length is the size of the array
pointed to by comp_dn.

The dnptrs parameter is a pointer to the head of the list
of pointers to previously compressed names in the current message. The first
pointer must point to the beginning of the message. The list ends
with NULL. The limit to the array is specified by lastdnptr.

A side effect of calling dn_comp() is to update the list of
pointers for labels inserted into the message by dn_comp() as the name
is compressed. If dnptrs is NULL, names are not compressed. If lastdnptr
is NULL, dn_comp() does not update the list of labels.

dn_expand()

The dn_expand() function expands the compressed domain name comp_dn to a full
domain name. The compressed name is contained in a query or reply
message. msg is a pointer to the beginning of that message. The
uncompressed name is placed in the buffer indicated by exp_dn, which is of
size length. The dn_expand() function returns the size of the compressed name,
or -1 if there was an error.

hstrerror(), herror()

The variables statp->res_h_errno and _res.res_h_errno and external variable h_errno are set whenever
an error occurs during a resolver operation. The following definitions are given
in <netdb.h>:

Notes

When the caller supplies a work buffer, for example the answer buffer
argument to res_nsend() or res_send(), the buffer should be aligned on an
eight byte boundary. Otherwise, an error such as a SIGBUS may result.