1 /*
2  * Copyright (C) 2004, 2005, 2007, 2011, 2015  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-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: callbacks.c,v 1.19 2011/12/09 23:47:05 tbox Exp $ */
19 
20 /*! \file */
21 
22 #include <config.h>
23 
24 #include <isc/print.h>
25 #include <isc/util.h>
26 
27 #include <dns/callbacks.h>
28 #include <dns/log.h>
29 
30 static void
31 stdio_error_warn_callback(dns_rdatacallbacks_t *, const char *, ...)
32      ISC_FORMAT_PRINTF(2, 3);
33 
34 static void
35 isclog_error_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...)
36      ISC_FORMAT_PRINTF(2, 3);
37 
38 static void
39 isclog_warn_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...)
40      ISC_FORMAT_PRINTF(2, 3);
41 
42 /*
43  * Private
44  */
45 
46 static void
stdio_error_warn_callback(dns_rdatacallbacks_t * callbacks,const char * fmt,...)47 stdio_error_warn_callback(dns_rdatacallbacks_t *callbacks,
48 			  const char *fmt, ...)
49 {
50 	va_list ap;
51 
52 	UNUSED(callbacks);
53 
54 	va_start(ap, fmt);
55 	vfprintf(stderr, fmt, ap);
56 	va_end(ap);
57 	fprintf(stderr, "\n");
58 }
59 
60 static void
isclog_error_callback(dns_rdatacallbacks_t * callbacks,const char * fmt,...)61 isclog_error_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...) {
62 	va_list ap;
63 
64 	UNUSED(callbacks);
65 
66 	va_start(ap, fmt);
67 	isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
68 		       DNS_LOGMODULE_MASTER, /* XXX */
69 		       ISC_LOG_ERROR, fmt, ap);
70 	va_end(ap);
71 }
72 
73 static void
isclog_warn_callback(dns_rdatacallbacks_t * callbacks,const char * fmt,...)74 isclog_warn_callback(dns_rdatacallbacks_t *callbacks, const char *fmt, ...) {
75 	va_list ap;
76 
77 	UNUSED(callbacks);
78 
79 	va_start(ap, fmt);
80 
81 	isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_GENERAL,
82 		       DNS_LOGMODULE_MASTER, /* XXX */
83 		       ISC_LOG_WARNING, fmt, ap);
84 	va_end(ap);
85 }
86 
87 static void
dns_rdatacallbacks_initcommon(dns_rdatacallbacks_t * callbacks)88 dns_rdatacallbacks_initcommon(dns_rdatacallbacks_t *callbacks) {
89 	REQUIRE(callbacks != NULL);
90 
91 	callbacks->add = NULL;
92 	callbacks->rawdata = NULL;
93 	callbacks->zone = NULL;
94 	callbacks->add_private = NULL;
95 	callbacks->error_private = NULL;
96 	callbacks->warn_private = NULL;
97 }
98 
99 /*
100  * Public.
101  */
102 
103 void
dns_rdatacallbacks_init(dns_rdatacallbacks_t * callbacks)104 dns_rdatacallbacks_init(dns_rdatacallbacks_t *callbacks) {
105 	dns_rdatacallbacks_initcommon(callbacks);
106 	callbacks->error = isclog_error_callback;
107 	callbacks->warn = isclog_warn_callback;
108 }
109 
110 void
dns_rdatacallbacks_init_stdio(dns_rdatacallbacks_t * callbacks)111 dns_rdatacallbacks_init_stdio(dns_rdatacallbacks_t *callbacks) {
112 	dns_rdatacallbacks_initcommon(callbacks);
113 	callbacks->error = stdio_error_warn_callback;
114 	callbacks->warn = stdio_error_warn_callback;
115 }
116 
117