1 /* $NetBSD: lwres.h,v 1.1.1.1 2009/03/22 15:02:40 christos Exp $ */
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: lwres.h,v 1.57 2007/06/19 23:47:23 tbox Exp */
23 #define LWRES_LWRES_H 1
27 #include <lwres/context.h>
28 #include <lwres/lang.h>
29 #include <lwres/list.h>
30 #include <lwres/lwpacket.h>
31 #include <lwres/platform.h>
33 /*! \file lwres/lwres.h */
38 * Each opcode has two structures and three functions which operate on each
39 * structure. For example, using the "no operation/ping" opcode as an
42 * <ul><li>lwres_nooprequest_t:
44 * lwres_nooprequest_render() takes a lwres_nooprequest_t and
45 * and renders it into wire format, storing the allocated
46 * buffer information in a passed-in buffer. When this buffer
47 * is no longer needed, it must be freed by
48 * lwres_context_freemem(). All other memory used by the
49 * caller must be freed manually, including the
50 * lwres_nooprequest_t passed in.<br /><br />
52 * lwres_nooprequest_parse() takes a wire format message and
53 * breaks it out into a lwres_nooprequest_t. The structure
54 * must be freed via lwres_nooprequest_free() when it is no longer
57 * lwres_nooprequest_free() releases into the lwres_context_t
58 * any space allocated during parsing.</li>
60 * <li>lwres_noopresponse_t:
62 * The functions used are similar to the three used for
63 * requests, just with different names.</li></ul>
65 * Typically, the client will use request_render, response_parse, and
66 * response_free, while the daemon will use request_parse, response_render,
69 * The basic flow of a typical client is:
71 * \li fill in a request_t, and call the render function.
73 * \li Transmit the buffer returned to the daemon.
75 * \li Wait for a response.
77 * \li When a response is received, parse it into a response_t.
79 * \li free the request buffer using lwres_context_freemem().
81 * \li free the response structure and its associated buffer using
85 #define LWRES_UDP_PORT 921 /*%< UDP Port Number */
86 #define LWRES_RECVLENGTH 16384 /*%< Maximum Packet Length */
87 #define LWRES_ADDR_MAXLEN 16 /*%< changing this breaks ABI */
88 #define LWRES_RESOLV_CONF "/etc/resolv.conf" /*%< Location of resolv.conf */
90 /*% DNSSEC is not required (input). Only relevant to rrset queries. */
91 #define LWRES_FLAG_TRUSTNOTREQUIRED 0x00000001U
92 /*% The data was crypto-verified with DNSSEC (output). */
93 #define LWRES_FLAG_SECUREDATA 0x00000002U
96 #define LWRES_OPCODE_NOOP 0x00000000U
98 /*% lwres_nooprequest_t */
101 lwres_uint16_t datalength
;
103 } lwres_nooprequest_t
;
105 /*% lwres_noopresponse_t */
108 lwres_uint16_t datalength
;
110 } lwres_noopresponse_t
;
112 /*% get addresses by name */
113 #define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U
116 typedef struct lwres_addr lwres_addr_t
;
119 typedef LWRES_LIST(lwres_addr_t
) lwres_addrlist_t
;
123 lwres_uint32_t family
;
124 lwres_uint16_t length
;
125 unsigned char address
[LWRES_ADDR_MAXLEN
];
126 LWRES_LINK(lwres_addr_t
) link
;
129 /*% lwres_gabnrequest_t */
132 lwres_uint32_t flags
;
133 lwres_uint32_t addrtypes
;
134 lwres_uint16_t namelen
;
136 } lwres_gabnrequest_t
;
138 /*% lwres_gabnresponse_t */
141 lwres_uint32_t flags
;
142 lwres_uint16_t naliases
;
143 lwres_uint16_t naddrs
;
146 lwres_uint16_t realnamelen
;
147 lwres_uint16_t
*aliaslen
;
148 lwres_addrlist_t addrs
;
149 /*! if base != NULL, it will be freed when this structure is freed. */
152 } lwres_gabnresponse_t
;
154 /*% get name by address */
155 #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
157 /*% lwres_gnbarequest_t */
160 lwres_uint32_t flags
;
162 } lwres_gnbarequest_t
;
164 /*% lwres_gnbaresponse_t */
167 lwres_uint32_t flags
;
168 lwres_uint16_t naliases
;
171 lwres_uint16_t realnamelen
;
172 lwres_uint16_t
*aliaslen
;
173 /*! if base != NULL, it will be freed when this structure is freed. */
176 } lwres_gnbaresponse_t
;
178 /*% get rdata by name */
179 #define LWRES_OPCODE_GETRDATABYNAME 0x00010003U
181 /*% lwres_grbnrequest_t */
184 lwres_uint32_t flags
;
185 lwres_uint16_t rdclass
;
186 lwres_uint16_t rdtype
;
187 lwres_uint16_t namelen
;
189 } lwres_grbnrequest_t
;
191 /*% lwres_grbnresponse_t */
194 lwres_uint32_t flags
;
195 lwres_uint16_t rdclass
;
196 lwres_uint16_t rdtype
;
198 lwres_uint16_t nrdatas
;
199 lwres_uint16_t nsigs
;
201 lwres_uint16_t realnamelen
;
202 unsigned char **rdatas
;
203 lwres_uint16_t
*rdatalen
;
204 unsigned char **sigs
;
205 lwres_uint16_t
*siglen
;
206 /*% if base != NULL, it will be freed when this structure is freed. */
209 } lwres_grbnresponse_t
;
211 /*% Used by lwres_getrrsetbyname() */
212 #define LWRDATA_VALIDATED 0x00000001
218 #define LWRES_CONFMAXNAMESERVERS 3 /*%< max 3 nameserver entries */
219 #define LWRES_CONFMAXLWSERVERS 1 /*%< max 1 lwserver entry */
220 #define LWRES_CONFMAXSEARCH 8 /*%< max 8 domains in search entry */
221 #define LWRES_CONFMAXLINELEN 256 /*%< max size of a line */
222 #define LWRES_CONFMAXSORTLIST 10 /*%< max 10 */
226 lwres_context_t
*lwctx
;
227 lwres_addr_t nameservers
[LWRES_CONFMAXNAMESERVERS
];
228 lwres_uint8_t nsnext
; /*%< index for next free slot */
230 lwres_addr_t lwservers
[LWRES_CONFMAXLWSERVERS
];
231 lwres_uint8_t lwnext
; /*%< index for next free slot */
235 char *search
[LWRES_CONFMAXSEARCH
];
236 lwres_uint8_t searchnxt
; /*%< index for next free slot */
240 /*% mask has a non-zero 'family' and 'length' if set */
242 } sortlist
[LWRES_CONFMAXSORTLIST
];
243 lwres_uint8_t sortlistnxt
;
245 lwres_uint8_t resdebug
; /*%< non-zero if 'options debug' set */
246 lwres_uint8_t ndots
; /*%< set to n in 'options ndots:n' */
247 lwres_uint8_t no_tld_query
; /*%< non-zero if 'options no_tld_query' */
250 #define LWRES_ADDRTYPE_V4 0x00000001U /*%< ipv4 */
251 #define LWRES_ADDRTYPE_V6 0x00000002U /*%< ipv6 */
253 #define LWRES_MAX_ALIASES 16 /*%< max # of aliases */
254 #define LWRES_MAX_ADDRS 64 /*%< max # of addrs */
256 LWRES_LANG_BEGINDECLS
258 /*% This is in host byte order. */
259 LIBLWRES_EXTERNAL_DATA
extern lwres_uint16_t lwres_udp_port
;
261 LIBLWRES_EXTERNAL_DATA
extern const char *lwres_resolv_conf
;
264 lwres_gabnrequest_render(lwres_context_t
*ctx
, lwres_gabnrequest_t
*req
,
265 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
268 lwres_gabnresponse_render(lwres_context_t
*ctx
, lwres_gabnresponse_t
*req
,
269 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
272 lwres_gabnrequest_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
273 lwres_lwpacket_t
*pkt
, lwres_gabnrequest_t
**structp
);
276 lwres_gabnresponse_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
277 lwres_lwpacket_t
*pkt
,
278 lwres_gabnresponse_t
**structp
);
281 lwres_gabnrequest_free(lwres_context_t
*ctx
, lwres_gabnrequest_t
**structp
);
283 * Frees any dynamically allocated memory for this structure.
287 * ctx != NULL, and be a context returned via lwres_context_create().
289 * structp != NULL && *structp != NULL.
295 * All memory allocated by this structure will be returned to the
296 * system via the context's free function.
300 lwres_gabnresponse_free(lwres_context_t
*ctx
, lwres_gabnresponse_t
**structp
);
302 * Frees any dynamically allocated memory for this structure.
306 * ctx != NULL, and be a context returned via lwres_context_create().
308 * structp != NULL && *structp != NULL.
314 * All memory allocated by this structure will be returned to the
315 * system via the context's free function.
320 lwres_gnbarequest_render(lwres_context_t
*ctx
, lwres_gnbarequest_t
*req
,
321 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
324 lwres_gnbaresponse_render(lwres_context_t
*ctx
, lwres_gnbaresponse_t
*req
,
325 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
328 lwres_gnbarequest_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
329 lwres_lwpacket_t
*pkt
, lwres_gnbarequest_t
**structp
);
332 lwres_gnbaresponse_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
333 lwres_lwpacket_t
*pkt
,
334 lwres_gnbaresponse_t
**structp
);
337 lwres_gnbarequest_free(lwres_context_t
*ctx
, lwres_gnbarequest_t
**structp
);
339 * Frees any dynamically allocated memory for this structure.
343 * ctx != NULL, and be a context returned via lwres_context_create().
345 * structp != NULL && *structp != NULL.
351 * All memory allocated by this structure will be returned to the
352 * system via the context's free function.
356 lwres_gnbaresponse_free(lwres_context_t
*ctx
, lwres_gnbaresponse_t
**structp
);
358 * Frees any dynamically allocated memory for this structure.
362 * ctx != NULL, and be a context returned via lwres_context_create().
364 * structp != NULL && *structp != NULL.
370 * All memory allocated by this structure will be returned to the
371 * system via the context's free function.
375 lwres_grbnrequest_render(lwres_context_t
*ctx
, lwres_grbnrequest_t
*req
,
376 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
379 lwres_grbnresponse_render(lwres_context_t
*ctx
, lwres_grbnresponse_t
*req
,
380 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
383 lwres_grbnrequest_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
384 lwres_lwpacket_t
*pkt
, lwres_grbnrequest_t
**structp
);
387 lwres_grbnresponse_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
388 lwres_lwpacket_t
*pkt
,
389 lwres_grbnresponse_t
**structp
);
392 lwres_grbnrequest_free(lwres_context_t
*ctx
, lwres_grbnrequest_t
**structp
);
394 * Frees any dynamically allocated memory for this structure.
398 * ctx != NULL, and be a context returned via lwres_context_create().
400 * structp != NULL && *structp != NULL.
406 * All memory allocated by this structure will be returned to the
407 * system via the context's free function.
411 lwres_grbnresponse_free(lwres_context_t
*ctx
, lwres_grbnresponse_t
**structp
);
413 * Frees any dynamically allocated memory for this structure.
417 * ctx != NULL, and be a context returned via lwres_context_create().
419 * structp != NULL && *structp != NULL.
425 * All memory allocated by this structure will be returned to the
426 * system via the context's free function.
430 lwres_nooprequest_render(lwres_context_t
*ctx
, lwres_nooprequest_t
*req
,
431 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
433 * Allocate space and render into wire format a noop request packet.
437 * ctx != NULL, and be a context returned via lwres_context_create().
439 * b != NULL, and points to a lwres_buffer_t. The contents of the
440 * buffer structure will be initialized to contain the wire-format
441 * noop request packet.
443 * Caller needs to fill in parts of "pkt" before calling:
444 * serial, maxrecv, result.
448 * Returns 0 on success, non-zero on failure.
450 * On successful return, *b will contain data about the wire-format
451 * packet. It can be transmitted in any way, including lwres_sendblock().
455 lwres_noopresponse_render(lwres_context_t
*ctx
, lwres_noopresponse_t
*req
,
456 lwres_lwpacket_t
*pkt
, lwres_buffer_t
*b
);
459 lwres_nooprequest_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
460 lwres_lwpacket_t
*pkt
, lwres_nooprequest_t
**structp
);
462 * Parse a noop request. Note that to get here, the lwpacket must have
463 * already been parsed and removed by the caller, otherwise it would be
464 * pretty hard for it to know this is the right function to call.
466 * The function verifies bits of the header, but does not modify it.
470 lwres_noopresponse_parse(lwres_context_t
*ctx
, lwres_buffer_t
*b
,
471 lwres_lwpacket_t
*pkt
,
472 lwres_noopresponse_t
**structp
);
475 lwres_nooprequest_free(lwres_context_t
*ctx
, lwres_nooprequest_t
**structp
);
478 lwres_noopresponse_free(lwres_context_t
*ctx
, lwres_noopresponse_t
**structp
);
481 * Frees any dynamically allocated memory for this structure.
485 * ctx != NULL, and be a context returned via lwres_context_create().
487 * structp != NULL && *structp != NULL.
493 * All memory allocated by this structure will be returned to the
494 * system via the context's free function.
498 lwres_conf_parse(lwres_context_t
*ctx
, const char *filename
);
500 * parses a resolv.conf-format file and stores the results in the structure
501 * pointed to by *ctx.
505 * filename != NULL && strlen(filename) > 0
508 * LWRES_R_SUCCESS on a successful parse.
509 * Anything else on error, although the structure may be partially filled
514 lwres_conf_print(lwres_context_t
*ctx
, FILE *fp
);
516 * Prints a resolv.conf-format of confdata output to fp.
523 lwres_conf_init(lwres_context_t
*ctx
);
525 * sets all internal fields to a default state. Used to initialize a new
526 * lwres_conf_t structure (not reset a used on).
533 lwres_conf_clear(lwres_context_t
*ctx
);
535 * frees all internally allocated memory in confdata. Uses the memory
536 * routines supplied by ctx.
543 lwres_conf_get(lwres_context_t
*ctx
);
545 * Be extremely cautions in modifying the contents of this structure; it
546 * needs an API to return the various bits of data, walk lists, etc.
557 lwres_data_parse(lwres_buffer_t
*b
, unsigned char **p
, lwres_uint16_t
*len
);
560 lwres_string_parse(lwres_buffer_t
*b
, char **c
, lwres_uint16_t
*len
);
563 lwres_addr_parse(lwres_buffer_t
*b
, lwres_addr_t
*addr
);
566 lwres_getaddrsbyname(lwres_context_t
*ctx
, const char *name
,
567 lwres_uint32_t addrtypes
, lwres_gabnresponse_t
**structp
);
570 lwres_getnamebyaddr(lwres_context_t
*ctx
, lwres_uint32_t addrtype
,
571 lwres_uint16_t addrlen
, const unsigned char *addr
,
572 lwres_gnbaresponse_t
**structp
);
575 lwres_getrdatabyname(lwres_context_t
*ctx
, const char *name
,
576 lwres_uint16_t rdclass
, lwres_uint16_t rdtype
,
577 lwres_uint32_t flags
, lwres_grbnresponse_t
**structp
);
581 #endif /* LWRES_LWRES_H */