xref: /netbsd-src/external/mpl/bind/dist/lib/dns/callbacks.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /*	$NetBSD: callbacks.c,v 1.3 2020/05/24 19:46:22 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*! \file */
15 
16 #include <isc/print.h>
17 #include <isc/util.h>
18 
19 #include <dns/callbacks.h>
20 #include <dns/log.h>
21 
22 static void
23 stdio_error_warn_callback(dns_rdatacallbacks_t *, const char *, ...)
24 	ISC_FORMAT_PRINTF(2, 3);
25 
26 static void
27 isclog_error_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...)
28 	ISC_FORMAT_PRINTF(2, 3);
29 
30 static void
31 isclog_warn_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...)
32 	ISC_FORMAT_PRINTF(2, 3);
33 
34 /*
35  * Private
36  */
37 
38 static void
39 stdio_error_warn_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
40 			  ...) {
41 	va_list ap;
42 
43 	UNUSED(callbacks);
44 
45 	va_start(ap, fmt);
46 	vfprintf(stderr, fmt, ap);
47 	va_end(ap);
48 	fprintf(stderr, "\n");
49 }
50 
51 static void
52 isclog_error_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...) {
53 	va_list ap;
54 
55 	UNUSED(callbacks);
56 
57 	va_start(ap, fmt);
58 	isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
59 		       DNS_LOGMODULE_MASTER, /* XXX */
60 		       ISC_LOG_ERROR, fmt, ap);
61 	va_end(ap);
62 }
63 
64 static void
65 isclog_warn_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...) {
66 	va_list ap;
67 
68 	UNUSED(callbacks);
69 
70 	va_start(ap, fmt);
71 
72 	isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
73 		       DNS_LOGMODULE_MASTER, /* XXX */
74 		       ISC_LOG_WARNING, fmt, ap);
75 	va_end(ap);
76 }
77 
78 static void
79 dns_rdatacallbacks_initcommon(dns_rdatacallbacks_t *callbacks) {
80 	REQUIRE(callbacks != NULL);
81 
82 	callbacks->magic = DNS_CALLBACK_MAGIC;
83 	callbacks->add = NULL;
84 	callbacks->rawdata = NULL;
85 	callbacks->zone = NULL;
86 	callbacks->add_private = NULL;
87 	callbacks->error_private = NULL;
88 	callbacks->warn_private = NULL;
89 }
90 
91 /*
92  * Public.
93  */
94 
95 void
96 dns_rdatacallbacks_init(dns_rdatacallbacks_t *callbacks) {
97 	dns_rdatacallbacks_initcommon(callbacks);
98 	callbacks->error = isclog_error_callback;
99 	callbacks->warn = isclog_warn_callback;
100 }
101 
102 void
103 dns_rdatacallbacks_init_stdio(dns_rdatacallbacks_t *callbacks) {
104 	dns_rdatacallbacks_initcommon(callbacks);
105 	callbacks->error = stdio_error_warn_callback;
106 	callbacks->warn = stdio_error_warn_callback;
107 }
108