ldns-read-zone.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "config.h"
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010
00011 #include <ldns/ldns.h>
00012
00013 #include <errno.h>
00014
00015 int
00016 main(int argc, char **argv)
00017 {
00018 char *filename;
00019 FILE *fp;
00020 ldns_zone *z;
00021 int line_nr = 0;
00022 int c;
00023 bool canonicalize = false;
00024 bool sort = false;
00025 bool strip = false;
00026 ldns_status s;
00027 size_t i;
00028 ldns_rr_list *stripped_list;
00029 ldns_rr *cur_rr;
00030
00031 while ((c = getopt(argc, argv, "chsvz")) != -1) {
00032 switch(c) {
00033 case 'c':
00034 canonicalize = true;
00035 break;
00036 case 'h':
00037 printf("Usage: %s [-c] [-v] [-z] <zonefile>\n", argv[0]);
00038 printf("\tReads the zonefile and prints it.\n");
00039 printf("\tThe RR count of the zone is printed to stderr.\n");
00040 printf("\t-c canonicalize all rrs in the zone.\n");
00041 printf("\t-h show this text\n");
00042 printf("\t-s strip DNSSEC data from the zone\n");
00043 printf("\t-v shows the version and exits\n");
00044 printf("\t-z sort the zone (implies -c).\n");
00045 printf("\nif no file is given standard input is read\n");
00046 exit(EXIT_SUCCESS);
00047 break;
00048 case 's':
00049 strip = true;
00050 break;
00051 case 'v':
00052 printf("read zone version %s (ldns version %s)\n", LDNS_VERSION, ldns_version());
00053 exit(EXIT_SUCCESS);
00054 break;
00055 case 'z':
00056 canonicalize = true;
00057 sort = true;
00058 break;
00059 }
00060 }
00061
00062 argc -= optind;
00063 argv += optind;
00064
00065 if (argc == 0) {
00066 fp = stdin;
00067 } else {
00068 filename = argv[0];
00069
00070 fp = fopen(filename, "r");
00071 if (!fp) {
00072 fprintf(stderr, "Unable to open %s: %s\n", filename, strerror(errno));
00073 exit(EXIT_FAILURE);
00074 }
00075 }
00076
00077 s = ldns_zone_new_frm_fp_l(&z, fp, NULL, 0, LDNS_RR_CLASS_IN, &line_nr);
00078
00079 if (strip) {
00080 stripped_list = ldns_rr_list_new();
00081 while ((cur_rr = ldns_rr_list_pop_rr(ldns_zone_rrs(z)))) {
00082 if (ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_RRSIG ||
00083 ldns_rr_get_type(cur_rr) == LDNS_RR_TYPE_NSEC
00084 ) {
00085
00086 ldns_rr_free(cur_rr);
00087 } else {
00088 ldns_rr_list_push_rr(stripped_list, cur_rr);
00089 }
00090 }
00091 ldns_rr_list_free(ldns_zone_rrs(z));
00092 ldns_zone_set_rrs(z, stripped_list);
00093 }
00094
00095 if (s == LDNS_STATUS_OK) {
00096 if (canonicalize) {
00097 ldns_rr2canonical(ldns_zone_soa(z));
00098 for (i = 0; i < ldns_rr_list_rr_count(ldns_zone_rrs(z)); i++) {
00099 ldns_rr2canonical(ldns_rr_list_rr(ldns_zone_rrs(z), i));
00100 }
00101 }
00102 if (sort) {
00103 ldns_zone_sort(z);
00104 }
00105
00106 ldns_zone_print(stdout, z);
00107
00108 ldns_zone_deep_free(z);
00109 } else {
00110 fprintf(stderr, "%s at %d\n",
00111 ldns_get_errorstr_by_id(s),
00112 line_nr);
00113 exit(EXIT_FAILURE);
00114 }
00115 fclose(fp);
00116
00117 exit(EXIT_SUCCESS);
00118 }