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