From: Miek Gieben Date: Fri, 25 Feb 2005 08:16:05 +0000 (+0000) Subject: function added to create RR from a string. String should be a wellformed X-Git-Tag: release-0.50~368 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=47ef493448fe2e41a3be188b9f3a1e77dce899e5;p=thirdparty%2Fldns.git function added to create RR from a string. String should be a wellformed RR: www.miek.nl 3600 IN MX 10 elektron.atoom.net newline, parentheses are not handled - need a normalize RR function for that --- diff --git a/ldns/rr.h b/ldns/rr.h index c892d90b..44e89a71 100644 --- a/ldns/rr.h +++ b/ldns/rr.h @@ -270,6 +270,6 @@ int ldns_rr_compare(const ldns_rr *rr1, const ldns_rr *rr2); void ldns_rr_list_sort(ldns_rr_list *); ldns_rr *ldns_rr_clone(ldns_rr *rr); - +ldns_rr *ldns_rr_new_frm_str(const char *); #endif /* _LDNS_RR_H */ diff --git a/rr.c b/rr.c index 8ab67ece..85b35482 100644 --- a/rr.c +++ b/rr.c @@ -58,6 +58,86 @@ ldns_rr_free(ldns_rr *rr) FREE(rr); } +/** + * \brief create a rr from a string + * string should be a fully filled in rr, like + * ownername TTL CLASS TYPE RDATA + * \param[in] str the string to convert + * \return the new rr + */ +/* we expect 3 spaces, everything there after is rdata + * So the RR should look like. e.g. + * miek.nl. 3600 IN MX 10 elektron.atoom.net + * Everything should be on 1 line, parentheses are not + * handled. We may need a normalize function. + */ +ldns_rr * +ldns_rr_new_frm_str(const char *str) +{ + ldns_rr *new; + const ldns_rr_descriptor *desc; + ldns_rr_type rr_type; + char *owner; + char *ttl; + char *clas; + char *type; + char *rdata; + char *rd; + + ldns_rdf *r; + uint16_t r_cnt; + uint16_t r_min; + uint16_t r_max; + + new = ldns_rr_new(); + + owner = XMALLOC(char, 256); + ttl = XMALLOC(char, 20); + clas = XMALLOC(char, 8); + type = XMALLOC(char, 10); + rdata = XMALLOC(char, MAX_PACKETLEN); + + /* numbers are bogus */ + sscanf(str, "%256s%20s%8s%10s%65535c", owner, ttl, clas, type, rdata); + +#if 0 + printf("owner [%s]\n", owner); + printf("ttl [%s]\n", ttl); + printf("clas [%s]\n", clas); + printf("type [%s]\n", type); + printf("rdata [%s]\n", rdata); +#endif + + ldns_rr_set_owner(new, ldns_dname_new_frm_str(owner)); + ldns_rr_set_ttl(new, (uint32_t) atoi(ttl)); + ldns_rr_set_class(new, ldns_get_class_by_name(clas)); + + rr_type = ldns_rr_get_type_by_name(type); + desc = ldns_rr_descript((uint16_t)rr_type); + ldns_rr_set_type(new, rr_type); + + /* only the rdata remains */ + r_max = ldns_rr_descriptor_maximum(desc); + r_min = ldns_rr_descriptor_minimum(desc); + + /* rdata (rdf's) */ + for(rd = strtok(rdata, "\t "), r_cnt =0; rd; rd = strtok(NULL, "\t "), r_cnt++) { + r = ldns_rdf_new_frm_str(rd, + ldns_rr_descriptor_field_type(desc, r_cnt)); + if (!r) { + printf("rdf conversion mismatch\n"); + return NULL; + } + ldns_rr_push_rdf(new, r); + if (r_cnt > r_max) { + printf("rdf data overflow"); + return NULL; + } + } + return new; +} + + /** * \brief set the owner in the rr structure * \param[in] *rr rr to operate on diff --git a/run-test9.c b/run-test9.c index 35f70963..cab0cbe9 100644 --- a/run-test9.c +++ b/run-test9.c @@ -19,6 +19,7 @@ main(int argc, char **argv) ldns_rdf *nameserver; ldns_pkt *pkt; ldns_rr_list *bla; + ldns_rr *RR; const char *nameserver_address = "127.0.0.1"; if (argc >= 2) { @@ -30,6 +31,13 @@ main(int argc, char **argv) if (!res) return -1; + RR = ldns_rr_new_frm_str("miek.nl. 3600 IN MX 10 elektron.atoom.net."); + ldns_rr_print(stdout, RR); + printf("\n"); + RR = ldns_rr_new_frm_str("miek.nl. 3600 IN MX 10 elektron.atoom.net."); + ldns_rr_print(stdout, RR); + printf("\n"); + nameserver = ldns_rdf_new_frm_str(nameserver_address, LDNS_RDF_TYPE_A); if (ldns_resolver_push_nameserver(res, nameserver) != LDNS_STATUS_OK) { printf("error push nameserver\n");