]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
function added to create RR from a string. String should be a wellformed
authorMiek Gieben <miekg@NLnetLabs.nl>
Fri, 25 Feb 2005 08:16:05 +0000 (08:16 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Fri, 25 Feb 2005 08:16:05 +0000 (08:16 +0000)
RR: www.miek.nl 3600 IN MX 10 elektron.atoom.net
newline, parentheses are not handled - need a normalize RR function for
that

ldns/rr.h
rr.c
run-test9.c

index c892d90b9b1ee513586e7a4e1e51ddc7eb0037c8..44e89a71a797da7fdc64e23bf5800c6021ed0c77 100644 (file)
--- 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 8ab67ecefed7c69f5bc3b2c9cccc9882f5e93802..85b354822ebf09434471b0bf52a698e0accd8885 100644 (file)
--- 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 <space> TTL <space> CLASS <space> TYPE <space> 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
index 35f70963083de22de16fa511abadc23e1666356e..cab0cbe9e0c5f679c628ec6bdfafc300d4120625 100644 (file)
@@ -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");