lwres.h 12.8 KB
Newer Older
1
/*
2
 * Copyright (C) 2000  Internet Software Consortium.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#ifndef LWRES_LWRES_H
#define LWRES_LWRES_H 1

21
#include <stdio.h>
22
23
#include <stddef.h>

Michael Graff's avatar
Michael Graff committed
24
#include <lwres/context.h>
25
26
#include <lwres/int.h>
#include <lwres/lang.h>
27
#include <lwres/list.h>
Michael Graff's avatar
Michael Graff committed
28
#include <lwres/lwpacket.h>
29
#include <lwres/result.h>
Michael Graff's avatar
Michael Graff committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

/*
 * Design notes:
 *
 * Each opcode has two structures and three functions which operate on each
 * structure.  For example, using the "no operation/ping" opcode as an
 * example:
 *
 *	lwres_nooprequest_t:
 *
 *		lwres_nooprequest_render() takes a lwres_nooprequest_t and
 *		and renders it into wire format, storing the allocated
 *		buffer information in a passed-in buffer.  When this buffer
 *		is no longer needed, it must be freed by
 *		lwres_context_freemem().  All other memory used by the
 *		caller must be freed manually, including the
 *		lwres_nooprequest_t passed in.
 *
 *		lwres_nooprequest_parse() takes a wire format message and
 *		breaks it out into a lwres_nooprequest_t.  The structure
 *		must be freed via lwres_nooprequest_free() when it is no longer
 *		needed.
 *
 *		lwres_nooprequest_free() releases into the lwres_context_t
 *		any space allocated during parsing.
 *
 *	lwres_noopresponse_t:
 *
 *		The functions used are similar to the three used for
 *		requests, just with different names.
 *
 * Typically, the client will use request_render, response_parse, and
 * response_free, while the daemon will use request_parse, response_render,
 * and request_free.
 *
 * The basic flow of a typical client is:
 *
 *	fill in a request_t, and call the render function.
 *
 *	Transmit the buffer returned to the daemon.
 *
 *	Wait for a response.
 *
 *	When a response is received, parse it into a response_t.
 *
 *	free the request buffer using lwres_context_freemem().
 *
 *	free the response structure and its associated buffer using
 *	response_free().
 */

81
#define LWRES_UDP_PORT		921	/* XXXMLG */
Michael Graff's avatar
Michael Graff committed
82
83
#define LWRES_RECVLENGTH	4096	/* XXXMLG */
#define LWRES_ADDR_MAXLEN	16	/* changing this breaks ABI */
84

85
86
87
88
89
90
91
/*
 * XXXMLG
 */
#ifndef INADDR_LOOPBACK
#define INADDR_LOOPBACK 0x7f000001UL
#endif

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Flags.
 *
 * TRUST* define a two-bit value:
 *
 *	TRUSTDEFAULT:  Let the server decide what to do.
 *
 *	TRUSTNOTREQUIRED:  DNSSEC (or NIS equavalent) is not required.
 *
 *	TRUSTREQUIRED:  DNSSEC (if present) must validate, and the
 *		daemon the client is talking to must be DNSSEC aware.
 *
 *	TRUSTRESERVED:  No not used, reserved for future.
 *
 * XXXMLG -- currently not implemented!
 *
 */
#define LWRES_FLAG_TRUSTDEFAULT		0x00000000U
James Brister's avatar
James Brister committed
110
#define LWRES_FLAG_TRUSTNOTREQUIRED	0x00000001U
111
112
113
114
#define LWRES_FLAG_TRUSTREQUIRED	0x00000010U
#define LWRES_FLAG_TRUSTRESERVED	0x00000011U
#define LWRES_FLAG_TRUSTMASK		0x00000011U  /* mask for the above */

Michael Graff's avatar
Michael Graff committed
115
/*
Michael Graff's avatar
Michael Graff committed
116
 * no-op
Michael Graff's avatar
Michael Graff committed
117
 */
118
#define LWRES_OPCODE_NOOP		0x00000000U
Michael Graff's avatar
Michael Graff committed
119

120
121
typedef struct {
	/* public */
122
123
	lwres_uint16_t			datalength;
	unsigned char		       *data;
Michael Graff's avatar
Michael Graff committed
124
125
126
127
} lwres_nooprequest_t;

typedef struct {
	/* public */
128
129
	lwres_uint16_t			datalength;
	unsigned char		       *data;
Michael Graff's avatar
Michael Graff committed
130
} lwres_noopresponse_t;
131

132
/*
Michael Graff's avatar
Michael Graff committed
133
 * get addresses by name
134
 */
Michael Graff's avatar
Michael Graff committed
135
#define LWRES_OPCODE_GETADDRSBYNAME	0x00010001U
136

137
138
139
140
141
142
typedef struct lwres_addr lwres_addr_t;
typedef LWRES_LIST(lwres_addr_t) lwres_addrlist_t;

struct lwres_addr {
	lwres_uint32_t			family;
	lwres_uint16_t			length;
143
	unsigned char			address[LWRES_ADDR_MAXLEN];
144
145
	LWRES_LINK(lwres_addr_t)	link;
};
146
147
148

typedef struct {
	/* public */
149
	lwres_uint32_t			flags;
150
151
152
	lwres_uint32_t			addrtypes;
	lwres_uint16_t			namelen;
	char			       *name;
Michael Graff's avatar
Michael Graff committed
153
154
155
156
} lwres_gabnrequest_t;

typedef struct {
	/* public */
157
	lwres_uint32_t			flags;
158
159
160
161
162
163
164
	lwres_uint16_t			naliases;
	lwres_uint16_t			naddrs;
	char			       *realname;
	char			      **aliases;
	lwres_uint16_t			realnamelen;
	lwres_uint16_t		       *aliaslen;
	lwres_addrlist_t		addrs;
165
	/* if base != NULL, it will be freed when this structure is freed. */
166
167
	void			       *base;
	size_t				baselen;
Michael Graff's avatar
Michael Graff committed
168
} lwres_gabnresponse_t;
169

Michael Graff's avatar
Michael Graff committed
170
/*
Michael Graff's avatar
Michael Graff committed
171
 * get name by address
Michael Graff's avatar
Michael Graff committed
172
 */
173
174
175
#define LWRES_OPCODE_GETNAMEBYADDR	0x00010002U
typedef struct {
	/* public */
176
	lwres_uint32_t			flags;
177
	lwres_addr_t			addr;
Michael Graff's avatar
Michael Graff committed
178
179
180
181
} lwres_gnbarequest_t;

typedef struct {
	/* public */
182
	lwres_uint32_t			flags;
183
184
185
186
187
	lwres_uint16_t			naliases;
	char			       *realname;
	char			      **aliases;
	lwres_uint16_t			realnamelen;
	lwres_uint16_t		       *aliaslen;
188
	/* if base != NULL, it will be freed when this structure is freed. */
189
190
	void			       *base;
	size_t				baselen;
Michael Graff's avatar
Michael Graff committed
191
} lwres_gnbaresponse_t;
192

193
/*
Michael Graff's avatar
Michael Graff committed
194
 * resolv.conf data
195
 */
196

197
198
199
200
201
#define LWRES_CONFMAXNAMESERVERS 3	/* max 3 "nameserver" entries */
#define LWRES_CONFMAXSEARCH 6		/* max 6 domains in "search" entry */
#define LWRES_CONFMAXLINELEN 256	/* max size of a line */
#define LWRES_CONFMAXSORTLIST 10
typedef struct {
202
	lwres_context_t *lwctx;
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	lwres_addr_t    nameservers[LWRES_CONFMAXNAMESERVERS];
	lwres_uint8_t	nsnext;		/* index for next free slot */

	char	       *domainname;

	char 	       *search[LWRES_CONFMAXSEARCH];
	lwres_uint8_t	searchnxt;	/* index for next free slot */

	struct {
		lwres_addr_t addr;
		/* mask has a non-zero 'family' and 'length' if set */
		lwres_addr_t mask;	
	} sortlist[LWRES_CONFMAXSORTLIST];
	lwres_uint8_t	sortlistnxt;

David Lawrence's avatar
David Lawrence committed
218
219
220
	lwres_uint8_t	resdebug;      /* non-zero if 'options debug' set */
	lwres_uint8_t	ndots;	       /* set to n in 'options ndots:n' */
	lwres_uint8_t	no_tld_query;  /* non-zero if 'options no_tld_query' */
221
222
} lwres_conf_t;
	
223
224
225
#define LWRES_ADDRTYPE_V4		0x00000001U	/* ipv4 */
#define LWRES_ADDRTYPE_V6		0x00000002U	/* ipv6 */

226
227
#define LWRES_MAX_ALIASES		16		/* max # of aliases */
#define LWRES_MAX_ADDRS			64		/* max # of addrs */
228

229
LWRES_LANG_BEGINDECLS
230

231
232
233
234
235
/*
 * This is in host byte order.
 */
extern lwres_uint16_t lwres_udp_port;

236
lwres_result_t
237
lwres_gabnrequest_render(lwres_context_t *ctx, lwres_gabnrequest_t *req,
Michael Graff's avatar
Michael Graff committed
238
			 lwres_lwpacket_t *pkt, lwres_buffer_t *b);
Michael Graff's avatar
Michael Graff committed
239

240
lwres_result_t
241
lwres_gabnresponse_render(lwres_context_t *ctx, lwres_gabnresponse_t *req,
Michael Graff's avatar
Michael Graff committed
242
			  lwres_lwpacket_t *pkt, lwres_buffer_t *b);
Michael Graff's avatar
Michael Graff committed
243

244
lwres_result_t
245
246
lwres_gabnrequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
			lwres_lwpacket_t *pkt, lwres_gabnrequest_t **structp);
247

248
lwres_result_t
249
250
251
lwres_gabnresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
			 lwres_lwpacket_t *pkt,
			 lwres_gabnresponse_t **structp);
Michael Graff's avatar
Michael Graff committed
252
253
254

void
lwres_gabnrequest_free(lwres_context_t *ctx, lwres_gabnrequest_t **structp);
255
/*
Michael Graff's avatar
Michael Graff committed
256
 * Frees any dynamically allocated memory for this structure.
257
258
259
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
260
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
261
 *
Michael Graff's avatar
Michael Graff committed
262
 *	structp != NULL && *structp != NULL.
263
 *
Michael Graff's avatar
Michael Graff committed
264
 * Ensures:
265
 *
Michael Graff's avatar
Michael Graff committed
266
 *	*structp == NULL.
267
 *
Michael Graff's avatar
Michael Graff committed
268
269
 *	All memory allocated by this structure will be returned to the
 *	system via the context's free function.
270
271
272
 */

void
Michael Graff's avatar
Michael Graff committed
273
lwres_gabnresponse_free(lwres_context_t *ctx, lwres_gabnresponse_t **structp);
274
275
276
277
278
/*
 * Frees any dynamically allocated memory for this structure.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
279
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
280
 *
281
282
283
284
285
286
287
 *	structp != NULL && *structp != NULL.
 *
 * Ensures:
 *
 *	*structp == NULL.
 *
 *	All memory allocated by this structure will be returned to the
288
 *	system via the context's free function.
289
290
 */

Michael Graff's avatar
Michael Graff committed
291

292
lwres_result_t
Michael Graff's avatar
Michael Graff committed
293
294
lwres_gnbarequest_render(lwres_context_t *ctx, lwres_gnbarequest_t *req,
			 lwres_lwpacket_t *pkt, lwres_buffer_t *b);
Michael Graff's avatar
Michael Graff committed
295

296
lwres_result_t
Michael Graff's avatar
Michael Graff committed
297
298
lwres_gnbaresponse_render(lwres_context_t *ctx, lwres_gnbaresponse_t *req,
			  lwres_lwpacket_t *pkt, lwres_buffer_t *b);
Michael Graff's avatar
Michael Graff committed
299

300
lwres_result_t
301
302
lwres_gnbarequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
			lwres_lwpacket_t *pkt, lwres_gnbarequest_t **structp);
Michael Graff's avatar
Michael Graff committed
303

304
lwres_result_t
305
306
307
lwres_gnbaresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
			 lwres_lwpacket_t *pkt,
			 lwres_gnbaresponse_t **structp);
Michael Graff's avatar
Michael Graff committed
308
309
310

void
lwres_gnbarequest_free(lwres_context_t *ctx, lwres_gnbarequest_t **structp);
311
/*
Michael Graff's avatar
Michael Graff committed
312
 * Frees any dynamically allocated memory for this structure.
313
314
315
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
316
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
317
 *
Michael Graff's avatar
Michael Graff committed
318
 *	structp != NULL && *structp != NULL.
319
 *
Michael Graff's avatar
Michael Graff committed
320
 * Ensures:
321
 *
Michael Graff's avatar
Michael Graff committed
322
 *	*structp == NULL.
323
 *
Michael Graff's avatar
Michael Graff committed
324
325
 *	All memory allocated by this structure will be returned to the
 *	system via the context's free function.
326
327
328
 */

void
Michael Graff's avatar
Michael Graff committed
329
lwres_gnbaresponse_free(lwres_context_t *ctx, lwres_gnbaresponse_t **structp);
330
331
332
333
334
/*
 * Frees any dynamically allocated memory for this structure.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
335
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
336
 *
337
338
339
340
341
342
343
 *	structp != NULL && *structp != NULL.
 *
 * Ensures:
 *
 *	*structp == NULL.
 *
 *	All memory allocated by this structure will be returned to the
Michael Graff's avatar
Michael Graff committed
344
 *	system via the context's free function.
345
346
 */

347
lwres_result_t
Michael Graff's avatar
Michael Graff committed
348
lwres_nooprequest_render(lwres_context_t *ctx, lwres_nooprequest_t *req,
349
			 lwres_lwpacket_t *pkt, lwres_buffer_t *b);
350
/*
Michael Graff's avatar
Michael Graff committed
351
 * Allocate space and render into wire format a noop request packet.
352
353
354
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
355
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
356
 *
Michael Graff's avatar
Michael Graff committed
357
358
359
 *	b != NULL, and points to a lwres_buffer_t.  The contents of the
 *	buffer structure will be initialized to contain the wire-format
 *	noop request packet.
360
 *
361
362
363
 *	Caller needs to fill in parts of "pkt" before calling:
 *		serial, maxrecv, result.
 *
364
365
366
367
 * Returns:
 *
 *	Returns 0 on success, non-zero on failure.
 *
Michael Graff's avatar
Michael Graff committed
368
369
 *	On successful return, *b will contain data about the wire-format
 *	packet.  It can be transmitted in any way, including lwres_sendblock().
370
371
 */

372
lwres_result_t
Michael Graff's avatar
Michael Graff committed
373
lwres_noopresponse_render(lwres_context_t *ctx, lwres_noopresponse_t *req,
374
			  lwres_lwpacket_t *pkt, lwres_buffer_t *b);
Michael Graff's avatar
Michael Graff committed
375

376
lwres_result_t
Michael Graff's avatar
Michael Graff committed
377
lwres_nooprequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
378
379
380
381
382
383
384
385
			lwres_lwpacket_t *pkt, lwres_nooprequest_t **structp);
/*
 * Parse a noop request.  Note that to get here, the lwpacket must have
 * already been parsed and removed by the caller, otherwise it would be
 * pretty hard for it to know this is the right function to call.
 *
 * The function verifies bits of the header, but does not modify it.
 */
Michael Graff's avatar
Michael Graff committed
386

387
lwres_result_t
388
389
390
lwres_noopresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
			 lwres_lwpacket_t *pkt,
			 lwres_noopresponse_t **structp);
Michael Graff's avatar
Michael Graff committed
391
392
393
394

void
lwres_nooprequest_free(lwres_context_t *ctx, lwres_nooprequest_t **structp);

395
void
Michael Graff's avatar
Michael Graff committed
396
397
lwres_noopresponse_free(lwres_context_t *ctx, lwres_noopresponse_t **structp);

398
399
400
401
402
/*
 * Frees any dynamically allocated memory for this structure.
 *
 * Requires:
 *
Michael Graff's avatar
Michael Graff committed
403
 *	ctx != NULL, and be a context returned via lwres_contextcreate().
404
405
406
407
408
409
410
411
412
413
 *
 *	structp != NULL && *structp != NULL.
 *
 * Ensures:
 *
 *	*structp == NULL.
 *
 *	All memory allocated by this structure will be returned to the
 *	system via the context's free function.
 */
414

Michael Graff's avatar
Michael Graff committed
415
lwres_result_t
416
lwres_conf_parse(lwres_context_t *ctx, const char *filename);
417
418
419
420
421
422
423
424
/*
 * parses a resolv.conf-format file and puts the results into *confdata;
 *
 * Requires:
 *	confdata != NULL
 *	filename != NULL && strlen(filename) > 0
 *
 * Returns:
Michael Graff's avatar
Michael Graff committed
425
426
 *	LWRES_R_SUCCESS on a succesfull parse.
 *	Anything else on error.
427
428
 */

429
void
430
lwres_conf_free(lwres_context_t *ctx);
431
432
433
434
435
436
437
438
/*
 * Returns the data in confdata to the system.
 *
 * Requires:
 *	confdata != NULL
 *	that confdata had been previously passed to lwres_conf_parse()
 */

Michael Graff's avatar
Michael Graff committed
439
lwres_result_t
440
lwres_conf_print(lwres_context_t *ctx, FILE *fp);
441
442
443
444
/*
 * Prints a resolv.conf-format of confdata output to fp.
 */

445
void
446
lwres_conf_init(lwres_context_t *ctx);
447
448
449
450
451
/*
 * sets all internal fields to a default state. Used to initialize a new
 * lwres_conf_t structure (not reset a used on).
 */

452
void
453
lwres_conf_clear(lwres_context_t *ctx);
454
455
/*
 * frees all internally allocated memory in confdata. Uses the memory 
456
 * routines supplied by ctx.
457
458
 */

459
460
461
462
463
464
465
466
lwres_conf_t *
lwres_conf_get(lwres_context_t *ctx);
/*
 * returns a pointer to the current config structure.
 * Be extremely cautions in modifying the contents of this structure; it
 * needs an API to return the various bits of data, walk lists, etc.
 */

Michael Graff's avatar
Michael Graff committed
467
468
469
470
/*
 * Helper functions
 */

471
lwres_result_t
472
lwres_string_parse(lwres_buffer_t *b, char **c, lwres_uint16_t *len);
Michael Graff's avatar
Michael Graff committed
473

474
lwres_result_t
Michael Graff's avatar
Michael Graff committed
475
476
lwres_addr_parse(lwres_buffer_t *b, lwres_addr_t *addr);

477
lwres_result_t
478
lwres_getaddrsbyname(lwres_context_t *ctx, const char *name,
479
		     lwres_uint32_t addrtypes, lwres_gabnresponse_t **structp);
480

481
lwres_result_t
482
483
lwres_getnamebyaddr(lwres_context_t *ctx, lwres_uint32_t addrtype,
		    lwres_uint16_t addrlen, const unsigned char *addr,
484
485
		    lwres_gnbaresponse_t **structp);

486
LWRES_LANG_ENDDECLS
487
488

#endif /* LWRES_LWRES_H */