1/*
2 * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: lwresconf_test.c,v 1.13 2007/06/19 23:46:59 tbox Exp $ */
19
20#include <config.h>
21
22#include <stdlib.h>
23
24#include <isc/mem.h>
25#include <isc/util.h>
26
27#include <lwres/lwres.h>
28
29#define USE_ISC_MEM
30
31static inline void
32CHECK(int val, const char *msg) {
33	if (val != 0) {
34		fprintf(stderr, "%s returned %d\n", msg, val);
35		exit(1);
36	}
37}
38
39#ifdef USE_ISC_MEM
40/*
41 * Wrappers around our memory management stuff, for the lwres functions.
42 */
43static void *
44mem_alloc(void *arg, size_t size) {
45	return (isc_mem_get(arg, size));
46}
47
48static void
49mem_free(void *arg, void *mem, size_t size) {
50	isc_mem_put(arg, mem, size);
51}
52#endif
53
54int
55main(int argc, char *argv[]) {
56	lwres_context_t *ctx;
57	const char *file = "/etc/resolv.conf";
58	int ret;
59#ifdef USE_ISC_MEM
60	isc_mem_t *mem;
61	isc_result_t result;
62#endif
63
64	if (argc > 1) {
65		file = argv[1];
66	}
67
68#ifdef USE_ISC_MEM
69	mem = NULL;
70	result = isc_mem_create(0, 0, &mem);
71	INSIST(result == ISC_R_SUCCESS);
72#endif
73
74	ctx = NULL;
75#ifdef USE_ISC_MEM
76	ret = lwres_context_create(&ctx, mem, mem_alloc, mem_free, 0);
77#else
78	ret = lwres_context_create(&ctx, NULL, NULL, NULL, 0);
79#endif
80	CHECK(ret, "lwres_context_create");
81
82	lwres_conf_init(ctx);
83	if (lwres_conf_parse(ctx, file) == 0) {
84		lwres_conf_print(ctx, stderr);
85	} else {
86		perror("lwres_conf_parse");
87	}
88
89	lwres_conf_clear(ctx);
90	lwres_context_destroy(&ctx);
91
92#ifdef USE_ISC_MEM
93	isc_mem_stats(mem, stdout);
94	isc_mem_destroy(&mem);
95#endif
96
97	return (0);
98}
99