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