1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
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 https://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 #include <stdlib.h>
15 #include <string.h>
16 
17 #include <isc/buffer.h>
18 #include <isc/mem.h>
19 #include <isc/print.h>
20 #include <isc/util.h>
21 
22 #include <dns/callbacks.h>
23 #include <dns/master.h>
24 #include <dns/name.h>
25 #include <dns/rdataset.h>
26 #include <dns/result.h>
27 
28 isc_mem_t *mctx;
29 
30 static isc_result_t
print_dataset(void * arg,const dns_name_t * owner,dns_rdataset_t * dataset)31 print_dataset(void *arg, const dns_name_t *owner, dns_rdataset_t *dataset) {
32 	char buf[64 * 1024];
33 	isc_buffer_t target;
34 	isc_result_t result;
35 
36 	UNUSED(arg);
37 
38 	isc_buffer_init(&target, buf, 64 * 1024);
39 	result = dns_rdataset_totext(dataset, owner, false, false, &target);
40 	if (result == ISC_R_SUCCESS) {
41 		fprintf(stdout, "%.*s\n", (int)target.used,
42 			(char *)target.base);
43 	} else {
44 		fprintf(stdout, "dns_rdataset_totext: %s\n",
45 			dns_result_totext(result));
46 	}
47 
48 	return (ISC_R_SUCCESS);
49 }
50 
51 int
main(int argc,char * argv[])52 main(int argc, char *argv[]) {
53 	isc_result_t result;
54 	dns_name_t origin;
55 	isc_buffer_t source;
56 	isc_buffer_t target;
57 	unsigned char name_buf[255];
58 	dns_rdatacallbacks_t callbacks;
59 
60 	UNUSED(argc);
61 
62 	isc_mem_create(&mctx);
63 
64 	if (argv[1]) {
65 		isc_buffer_init(&source, argv[1], strlen(argv[1]));
66 		isc_buffer_add(&source, strlen(argv[1]));
67 		isc_buffer_setactive(&source, strlen(argv[1]));
68 		isc_buffer_init(&target, name_buf, 255);
69 		dns_name_init(&origin, NULL);
70 		result = dns_name_fromtext(&origin, &source, dns_rootname, 0,
71 					   &target);
72 		if (result != ISC_R_SUCCESS) {
73 			fprintf(stdout, "dns_name_fromtext: %s\n",
74 				dns_result_totext(result));
75 			exit(1);
76 		}
77 
78 		dns_rdatacallbacks_init_stdio(&callbacks);
79 		callbacks.add = print_dataset;
80 
81 		result = dns_master_loadfile(
82 			argv[1], &origin, &origin, dns_rdataclass_in, 0, 0,
83 			&callbacks, NULL, NULL, mctx, dns_masterformat_text, 0);
84 		fprintf(stdout, "dns_master_loadfile: %s\n",
85 			dns_result_totext(result));
86 	}
87 	return (0);
88 }
89