From: Miek Gieben Date: Mon, 21 Nov 2005 10:38:27 +0000 (+0000) Subject: completed ldns-rrsig and made a few changes to the library X-Git-Tag: release-1.1.0~612 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d683fd564695bfa06b801bc04782a9bea2090ee;p=thirdparty%2Fldns.git completed ldns-rrsig and made a few changes to the library --- diff --git a/examples/Makefile.in b/examples/Makefile.in index 769fb7c8..dc85f631 100644 --- a/examples/Makefile.in +++ b/examples/Makefile.in @@ -21,15 +21,16 @@ COMPILE = $(CC) $(CPPFLAGS) $(CFLAGS) LINK = $(CC) $(CFLAGS) $(LDFLAGS) $(LIBS) HEADER = config.h -PROGRAMS = ldns-read-zone \ - ldns-mx \ - ldns-chaos \ - ldns-update \ - ldns-keygen \ - ldns-key2ds \ - ldns-signzone \ - ldns-version \ - ldns-rrsig +SOURCES = ldns-read-zone.c \ + ldns-mx.c \ + ldns-chaos.c \ + ldns-update.c \ + ldns-keygen.c \ + ldns-key2ds.c \ + ldns-signzone.c \ + ldns-version.c \ + ldns-rrsig.c +PROGRAMS=$(SOURCES:.c=) .PHONY: all clean realclean @@ -63,8 +64,8 @@ ldns-rrsig: ldns-rrsig.o $(LINK) -o $@ $+ ## implicit rule -%.o: - $(COMPILE) -c $(srcdir)/$*.c +%.o: $(srcdir)/%.c + $(COMPILE) -c $(srcdir)/$< clean: rm -f *.o diff --git a/examples/ldns-rrsig.1 b/examples/ldns-rrsig.1 index 9cd76126..3aee863e 100644 --- a/examples/ldns-rrsig.1 +++ b/examples/ldns-rrsig.1 @@ -4,18 +4,19 @@ ldns-rrsig \- print out the inception and expiration dates in human readable form .SH SYNOPSIS .B ldns-rrsig -.IR [ --t +.IR domain +[ +.IR type ] -.IR DOMAIN - .SH DESCRIPTION \fBldns-rrsig\fR is used to print the expiration and inception date of -a RRSIG - -.SH OPTIONS -\fB-t\fR use the RRSIG which covers this type instead of SOA. +a RRSIG. The first argument is a domain name. \fBldns-rrsig\fR will +query the authoritative servers for that domain to get a list of RRSIGs. +It will then print out the inception and experiration dates for the RRSIG +covering the SOA record. +.PP +If the second argument \fBtype\fR is given the RRSIG covering that type will be shown. .SH AUTHOR Written by the ldns team as an example for ldns usage. diff --git a/examples/ldns-rrsig.c b/examples/ldns-rrsig.c index c99ec87a..b054d188 100644 --- a/examples/ldns-rrsig.c +++ b/examples/ldns-rrsig.c @@ -11,10 +11,10 @@ int usage(FILE *fp, char *prog) { - fprintf(fp, "%s domain\n", prog); + fprintf(fp, "%s domain [type]\n", prog); fprintf(fp, " print out the inception and expiration dates\n"); fprintf(fp, " in a more human readable form\n"); - fprintf(fp, " -t \tquery for RRSIG()\n"); + fprintf(fp, " \tquery for RRSIG(), defaults to SOA\n"); return 0; } @@ -31,6 +31,7 @@ main(int argc, char *argv[]) ldns_rr_list *ns_ip; uint8_t i, j; ldns_rr_type t; + char * type_name; time_t incep, expir; char incep_buf[26]; char expir_buf[26]; @@ -41,11 +42,10 @@ main(int argc, char *argv[]) domain = NULL; res = NULL; localres = NULL; - t = LDNS_RR_TYPE_SOA; /* can be overruled on the cmd line, -t switch */ /* option parsing */ - if (argc != 2) { + if (argc < 2) { usage(stdout, argv[0]); exit(EXIT_FAILURE); } else { @@ -57,6 +57,20 @@ main(int argc, char *argv[]) } } + if (argc == 3) { + /* optional type arg */ + type_name = strdup(argv[2]); + t = ldns_rdf2rr_type( + ldns_rdf_new_frm_str(LDNS_RDF_TYPE_TYPE, type_name)); + if (t == 0) { + fprintf(stderr, " *** %s is not a valid RR type\n", type_name); + exit(EXIT_FAILURE); + } + } else { + t = LDNS_RR_TYPE_SOA; + type_name = "SOA"; + } + /* create a new resolver from /etc/resolv.conf */ localres = ldns_resolver_new_frm_file(NULL); @@ -124,24 +138,20 @@ main(int argc, char *argv[]) } else { rrsig_type = ldns_rr_list_new(); - /* okay, this needs to be doctered out, the rdf type - * is LDNS_RDF_TYPE_TYPE, but I need to compare it - * with LDNS_RR_TYPEs. How to convert, do we want to - * convert? XXX */ - /* for(i = 0; i < ldns_rr_list_rr_count(rrsig); i++) { - if (ldns_rr_get_type( + if (ldns_rdf2rr_type( ldns_rr_rrsig_typecovered( ldns_rr_list_rr(rrsig, i))) == t) { ldns_rr_list_push_rr(rrsig_type, ldns_rr_list_rr(rrsig, i)); } } - */ - /* FOR NOW TAKE ONLY THE FIRST ONE */ - ldns_rr_list_push_rr(rrsig_type, - ldns_rr_list_rr(rrsig, 0)); - + if (ldns_rr_list_rr_count(rrsig_type) == 0) { + fprintf(stderr, " *** No RRSIG(%S) type found\n", + type_name); + exit(EXIT_FAILURE); + } + for(i = 0; i < ldns_rr_list_rr_count(rrsig_type); i++) { incep = ldns_rdf2native_time_t( ldns_rr_rrsig_inception( @@ -157,11 +167,8 @@ main(int argc, char *argv[]) incep_buf[24] = '\0'; expir_buf[24] = '\0'; - /* assume SOA XXX*/ - fprintf(stdout, "%s RRSIG(SOA): %s - %s\n", - argv[1], incep_buf, expir_buf); - - + fprintf(stdout, "%s RRSIG(%s): %s - %s\n", + argv[1], type_name, incep_buf, expir_buf); } } diff --git a/ldns/rdata.h b/ldns/rdata.h index ec89c272..42fd1fb4 100644 --- a/ldns/rdata.h +++ b/ldns/rdata.h @@ -286,28 +286,28 @@ ldns_rdf *ldns_rdf_address_reverse(ldns_rdf *rd); * \param[in] rd the ldns_rdf to operate on * \return uint8_t the value extracted */ -uint8_t ldns_rdf2native_int8(ldns_rdf *rd); +uint8_t ldns_rdf2native_int8(const ldns_rdf *rd); /** * returns the native uint16_t representation from the rdf. * \param[in] rd the ldns_rdf to operate on * \return uint16_t the value extracted */ -uint16_t ldns_rdf2native_int16(ldns_rdf *rd); +uint16_t ldns_rdf2native_int16(const ldns_rdf *rd); /** * returns the native uint32_t representation from the rdf. * \param[in] rd the ldns_rdf to operate on * \return uint32_t the value extracted */ -uint32_t ldns_rdf2native_int32(ldns_rdf *rd); +uint32_t ldns_rdf2native_int32(const ldns_rdf *rd); /** * returns the native time_t representation from the rdf. * \param[in] rd the ldns_rdf to operate on * \return time_t the value extracted (32 bits currently) */ -time_t ldns_rdf2native_time_t(ldns_rdf *rd); +time_t ldns_rdf2native_time_t(const ldns_rdf *rd); /** * converts a ttl value (like 5d2h) to a long. @@ -325,7 +325,7 @@ uint32_t ldns_str2period(const char *nptr, const char **endptr); * \return struct sockaddr* the address in the format so other * functions can use it (sendto) */ -struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(ldns_rdf *rd, uint16_t port, size_t *size); +struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size); /** * returns an rdf with the sockaddr info. works for ip4 and ip6 diff --git a/ldns/rr.h b/ldns/rr.h index 885ea602..bd291471 100644 --- a/ldns/rr.h +++ b/ldns/rr.h @@ -674,6 +674,16 @@ ldns_rdf_type ldns_rr_descriptor_field_type(const ldns_rr_descriptor *descriptor */ ldns_rr_list *ldns_rr_list_subtype_by_rdf(ldns_rr_list *l, ldns_rdf *r, size_t pos); +/** + * convert an rdf of type LDNS_RDF_TYPE_TYPE to an actual + * LDNS_RR_TYPE. This is usefull in the case when inspecting + * the rrtype covered field of an RRSIG. + * \param[in] rd the rdf to look at + * \return a ldns_rr_type with equivalent LDNS_RR_TYPE + * + */ +ldns_rr_type ldns_rdf2rr_type(const ldns_rdf *rd); + /* added while doing lua */ bool ldns_rr_list_insert_rr(ldns_rr_list *rr_list, ldns_rr *r, size_t count); diff --git a/rdata.c b/rdata.c index 0647c57a..052ab694 100644 --- a/rdata.c +++ b/rdata.c @@ -71,51 +71,49 @@ ldns_rdf_set_data(ldns_rdf *rd, void *d) /* for types that allow it, return * the native/host order type */ uint8_t -ldns_rdf2native_int8(ldns_rdf *rd) +ldns_rdf2native_int8(const ldns_rdf *rd) { uint8_t data; - - switch(ldns_rdf_get_type(rd)) { - case LDNS_RDF_TYPE_CLASS: - case LDNS_RDF_TYPE_ALG: - case LDNS_RDF_TYPE_INT8: - memcpy(&data, ldns_rdf_data(rd), sizeof(data)); - return data; - default: - return 0; + + /* only allow 8 bit rdfs */ + if (ldns_rdf_size(rd) != LDNS_RDF_SIZE_BYTE) { + return 0; } + + memcpy(&data, ldns_rdf_data(rd), sizeof(data)); + return data; } uint16_t -ldns_rdf2native_int16(ldns_rdf *rd) +ldns_rdf2native_int16(const ldns_rdf *rd) { uint16_t data; - - switch(ldns_rdf_get_type(rd)) { - case LDNS_RDF_TYPE_INT16: - memcpy(&data, ldns_rdf_data(rd), sizeof(data)); - return ntohs(data); - default: - return 0; + + /* only allow 16 bit rdfs */ + if (ldns_rdf_size(rd) != LDNS_RDF_SIZE_WORD) { + return 0; } + + memcpy(&data, ldns_rdf_data(rd), sizeof(data)); + return ntohs(data); } uint32_t -ldns_rdf2native_int32(ldns_rdf *rd) +ldns_rdf2native_int32(const ldns_rdf *rd) { uint32_t data; - - switch(ldns_rdf_get_type(rd)) { - case LDNS_RDF_TYPE_INT32: - memcpy(&data, ldns_rdf_data(rd), sizeof(data)); - return ntohl(data); - default: - return 0; + + /* only allow 32 bit rdfs */ + if (ldns_rdf_size(rd) != LDNS_RDF_SIZE_DOUBLEWORD) { + return 0; } + + memcpy(&data, ldns_rdf_data(rd), sizeof(data)); + return ntohl(data); } time_t -ldns_rdf2native_time_t(ldns_rdf *rd) +ldns_rdf2native_time_t(const ldns_rdf *rd) { uint32_t data; @@ -129,7 +127,7 @@ ldns_rdf2native_time_t(ldns_rdf *rd) } struct sockaddr_storage * -ldns_rdf2native_sockaddr_storage(ldns_rdf *rd, uint16_t port, size_t *size) +ldns_rdf2native_sockaddr_storage(const ldns_rdf *rd, uint16_t port, size_t *size) { struct sockaddr_storage *data; struct sockaddr_in *data_in; @@ -203,7 +201,7 @@ ldns_sockaddr_storage2rdf(struct sockaddr_storage *sock, uint16_t *port) ldns_rdf * ldns_native2rdf_int8(ldns_rdf_type type, uint8_t value) { - return ldns_rdf_new_frm_data(type, 1, &value); + return ldns_rdf_new_frm_data(type, LDNS_RDF_SIZE_BYTE, &value); } ldns_rdf * @@ -211,7 +209,7 @@ ldns_native2rdf_int16(ldns_rdf_type type, uint16_t value) { uint16_t *rdf_data = LDNS_XMALLOC(uint16_t, 1); ldns_write_uint16(rdf_data, value); - return ldns_rdf_new(type, 2, rdf_data); + return ldns_rdf_new(type, LDNS_RDF_SIZE_WORD, rdf_data); } ldns_rdf * @@ -219,7 +217,7 @@ ldns_native2rdf_int32(ldns_rdf_type type, uint32_t value) { uint32_t *rdf_data = LDNS_XMALLOC(uint32_t, 1); ldns_write_uint32(rdf_data, value); - return ldns_rdf_new(type, 4, rdf_data); + return ldns_rdf_new(type, LDNS_RDF_SIZE_DOUBLEWORD, rdf_data); } ldns_rdf * diff --git a/rr.c b/rr.c index 9867d428..c3e55c55 100644 --- a/rr.c +++ b/rr.c @@ -374,8 +374,6 @@ ldns_rr_new_frm_str(const char *str, uint16_t default_ttl, ldns_rdf *origin) } } } - - LDNS_FREE(rd); ldns_buffer_free(rd_buf); ldns_buffer_free(rr_buf); @@ -1809,3 +1807,20 @@ ldns_get_rr_class_by_name(const char *name) return 0; } + +ldns_rr_type +ldns_rdf2rr_type(const ldns_rdf *rd) +{ + ldns_rr_type r; + + if (!rd) { + return 0; + } + + if (ldns_rdf_get_type(rd) != LDNS_RDF_TYPE_TYPE) { + return 0; + } + + r = (ldns_rr_type) ldns_rdf2native_int16(rd); + return r; +}