lwres.html revision 4abdfc917e6635a7c81d1f931a0c79227e72d025
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein - Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews - Copyright (C) 2000, 2001 Internet Software Consortium.
ddccd5811feff696ba460dabfb666ce61040f545Andreas Gustafsson - Permission to use, copy, modify, and distribute this software for any
ddccd5811feff696ba460dabfb666ce61040f545Andreas Gustafsson - purpose with or without fee is hereby granted, provided that the above
ddccd5811feff696ba460dabfb666ce61040f545Andreas Gustafsson - copyright notice and this permission notice appear in all copies.
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein - AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
8a66318e41ed14c5a88130e8c362610e8faa2121Mark Andrews - PERFORMANCE OF THIS SOFTWARE.
4abdfc917e6635a7c81d1f931a0c79227e72d025Mark Andrews<!-- $Id: lwres.html,v 1.22 2007/01/26 23:29:04 marka Exp $ -->
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
e21a2904f02a03fa06b6db04d348f65fe9c67b2bMark Andrews<meta name="generator" content="DocBook XSL Stylesheets V1.71.1">
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
4abdfc917e6635a7c81d1f931a0c79227e72d025Mark Andrews<a name="id2476275"></a><div class="titlepage"></div>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein<p>lwres — introduction to the lightweight resolver library</p>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein<div class="funcsynopsis"><pre class="funcsynopsisinfo">#include <lwres/lwres.h></pre></div>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein The BIND 9 lightweight resolver library is a simple, name service
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein independent stub resolver library. It provides hostname-to-address
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein and address-to-hostname lookup services to applications by
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein transmitting lookup requests to a resolver daemon
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span><strong class="command">lwresd</strong></span>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein running on the local host. The resover daemon performs the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lookup using the DNS or possibly other name service protocols,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein and returns the results to the application through the library.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein The library and resolver daemon communicate using a simple
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein UDP-based protocol.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein The lwresd library implements multiple name service APIs.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein The standard
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">gethostbyname_r()</code>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">gethostbyaddr_r()</code>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">getipnodebyname()</code>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein functions are all supported. To allow the lwres library to coexist
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein with system libraries that define functions of the same name,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein the library defines these functions with names prefixed by
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein To define the standard names, applications must include the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="filename"><lwres/netdb.h></code>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein which contains macro definitions mapping the standard function names
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein prefixed ones. Operating system vendors who integrate the lwres
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein library into their base distributions should rename the functions
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein in the library proper so that the renaming macros are not needed.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein The library also provides a native API consisting of the functions
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">lwres_getaddrsbyname()</code>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">lwres_getnamebyaddr()</code>.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein These may be called by applications that require more detailed
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein control over the lookup process than the standard functions
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein In addition to these name service independent address lookup
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein functions, the library implements a new, experimental API
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein for looking up arbitrary DNS resource records, using the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">lwres_getaddrsbyname()</code>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein Finally, there is a low-level API for converting lookup
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein requests and responses to and from raw lwres protocol packets.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein This API can be used by clients requiring nonblocking operation,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein and is also used when implementing the server side of the lwres
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein protocol, for example in the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span><strong class="command">lwresd</strong></span>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein resolver daemon. The use of this low-level API in clients
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein and servers is outlined in the following sections.
4abdfc917e6635a7c81d1f931a0c79227e72d025Mark Andrews<a name="id2543421"></a><h2>CLIENT-SIDE LOW-LEVEL API CALL FLOW</h2>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein When a client program wishes to make an lwres request using the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein native low-level API, it typically performs the following
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein sequence of actions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (1) Allocate or use an existing <span class="type">lwres_packet_t</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (2) Set <em class="structfield"><code>pkt.recvlength</code></em> to the maximum length
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein we will accept.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein This is done so the receiver of our packets knows how large our receive
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein buffer is. The "default" is a constant in
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="filename">lwres.h</code>: <code class="constant">LWRES_RECVLENGTH = 4096</code>.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (3) Set <em class="structfield"><code>pkt.serial</code></em>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein to a unique serial number. This value is echoed
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein back to the application by the remote server.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (4) Set <em class="structfield"><code>pkt.pktflags</code></em>. Usually this is set to
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (5) Set <em class="structfield"><code>pkt.result</code></em> to 0.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (6) Call <code class="function">lwres_*request_render()</code>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein or marshall in the data using the primitives
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein such as <code class="function">lwres_packet_render()</code>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein and storing the packet data.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (7) Transmit the resulting buffer.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (8) Call <code class="function">lwres_*response_parse()</code>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein to parse any packets received.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (9) Verify that the opcode and serial match a request, and process the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein packet specific information contained in the body.
4abdfc917e6635a7c81d1f931a0c79227e72d025Mark Andrews<a name="id2543570"></a><h2>SERVER-SIDE LOW-LEVEL API CALL FLOW</h2>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein When implementing the server side of the lightweight resolver
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein protocol using the lwres library, a sequence of actions like the
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein following is typically involved in processing each request packet.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein Note that the same <span class="type">lwres_packet_t</span> is used
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein in both the <code class="function">_parse()</code> and <code class="function">_render()</code> calls,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein with only a few modifications made
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein to the packet header's contents between uses. This method is
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein as it keeps the serial, opcode, and other fields correct.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (1) When a packet is received, call <code class="function">lwres_*request_parse()</code> to
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein unmarshall it. This returns a <span class="type">lwres_packet_t</span> (also called <code class="varname">pkt</code>, below)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein as well as a data specific type, such as <span class="type">lwres_gabnrequest_t</span>.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (2) Process the request in the data specific type.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (3) Set the <em class="structfield"><code>pkt.result</code></em>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <em class="structfield"><code>pkt.recvlength</code></em> as above. All other fields
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein be left untouched since they were filled in by the <code class="function">*_parse()</code> call
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein above. If using <code class="function">lwres_*response_render()</code>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <em class="structfield"><code>pkt.pktflags</code></em> will be set up
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein properly. Otherwise, the <code class="constant">LWRES_LWPACKETFLAG_RESPONSE</code> bit should be
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (4) Call the data specific rendering function, such as
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <code class="function">lwres_gabnresponse_render()</code>.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein (5) Send the resulting packet to the client.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein<p><span class="citerefentry"><span class="refentrytitle">lwres_gethostent</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_getipnode</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_getnameinfo</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_noop</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_gabn</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_gnba</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_context</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwres_config</span>(3)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">resolver</span>(5)</span>,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein <span class="citerefentry"><span class="refentrytitle">lwresd</span>(8)</span>.