1 /*	$NetBSD: lwresconf_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: lwresconf_test.c,v 1.13 2007/06/19 23:46:59 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <stdlib.h>
25 
26 #include <isc/mem.h>
27 #include <isc/util.h>
28 
29 #include <lwres/lwres.h>
30 
31 #define USE_ISC_MEM
32 
33 static inline void
CHECK(int val,const char * msg)34 CHECK(int val, const char *msg) {
35 	if (val != 0) {
36 		fprintf(stderr, "%s returned %d\n", msg, val);
37 		exit(1);
38 	}
39 }
40 
41 #ifdef USE_ISC_MEM
42 /*
43  * Wrappers around our memory management stuff, for the lwres functions.
44  */
45 static void *
mem_alloc(void * arg,size_t size)46 mem_alloc(void *arg, size_t size) {
47 	return (isc_mem_get(arg, size));
48 }
49 
50 static void
mem_free(void * arg,void * mem,size_t size)51 mem_free(void *arg, void *mem, size_t size) {
52 	isc_mem_put(arg, mem, size);
53 }
54 #endif
55 
56 int
main(int argc,char * argv[])57 main(int argc, char *argv[]) {
58 	lwres_context_t *ctx;
59 	const char *file = "/etc/resolv.conf";
60 	int ret;
61 #ifdef USE_ISC_MEM
62 	isc_mem_t *mem;
63 	isc_result_t result;
64 #endif
65 
66 	if (argc > 1) {
67 		file = argv[1];
68 	}
69 
70 #ifdef USE_ISC_MEM
71 	mem = NULL;
72 	result = isc_mem_create(0, 0, &mem);
73 	INSIST(result == ISC_R_SUCCESS);
74 #endif
75 
76 	ctx = NULL;
77 #ifdef USE_ISC_MEM
78 	ret = lwres_context_create(&ctx, mem, mem_alloc, mem_free, 0);
79 #else
80 	ret = lwres_context_create(&ctx, NULL, NULL, NULL, 0);
81 #endif
82 	CHECK(ret, "lwres_context_create");
83 
84 	lwres_conf_init(ctx);
85 	if (lwres_conf_parse(ctx, file) == 0) {
86 		lwres_conf_print(ctx, stderr);
87 	} else {
88 		perror("lwres_conf_parse");
89 	}
90 
91 	lwres_conf_clear(ctx);
92 	lwres_context_destroy(&ctx);
93 
94 #ifdef USE_ISC_MEM
95 	isc_mem_stats(mem, stdout);
96 	isc_mem_destroy(&mem);
97 #endif
98 
99 	return (0);
100 }
101