From: Jelte Jansen Date: Mon, 31 Jan 2005 09:47:57 +0000 (+0000) Subject: added 'raw' 2wire functions, see for an example run-test2.c X-Git-Tag: release-0.50~507 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fd60103e3b484923dc0aa49d611bc860250271cc;p=thirdparty%2Fldns.git added 'raw' 2wire functions, see for an example run-test2.c --- diff --git a/host2wire.c b/host2wire.c index f737b093..615a6026 100644 --- a/host2wire.c +++ b/host2wire.c @@ -165,4 +165,72 @@ ldns_pkt2buffer_wire(ldns_buffer *buffer, ldns_pkt *packet) return LDNS_STATUS_OK; } +/** + * Allocates an array of uint8_t, and puts the wireformat of the + * given rdf in that array. The result_size value contains the + * length of the array, if it succeeds, and 0 otherwise (in which case + * the function also returns NULL) + */ +uint8_t * +ldns_rdf2wire(ldns_rdf *rdf, size_t *result_size) +{ + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + uint8_t *result = NULL; + *result_size = 0; + if (ldns_rdf2buffer_wire(buffer, rdf) == LDNS_STATUS_OK) { + *result_size = ldns_buffer_position(buffer); + result = (uint8_t *) ldns_buffer_export(buffer); + } else { + /* TODO: what about the error? */ + } + ldns_buffer_free(buffer); + return result; +} + +/** + * Allocates an array of uint8_t, and puts the wireformat of the + * given rr in that array. The result_size value contains the + * length of the array, if it succeeds, and 0 otherwise (in which case + * the function also returns NULL) + * + * If the section argument is LDNS_SECTION_QUESTION, data like ttl and rdata + * are not put into the result + */ +uint8_t * +ldns_rr2wire(ldns_rr *rr, int section, size_t *result_size) +{ + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + uint8_t *result = NULL; + *result_size = 0; + if (ldns_rr2buffer_wire(buffer, rr, section) == LDNS_STATUS_OK) { + *result_size = ldns_buffer_position(buffer); + result = (uint8_t *) ldns_buffer_export(buffer); + } else { + /* TODO: what about the error? */ + } + ldns_buffer_free(buffer); + return result; +} + +/** + * Allocates an array of uint8_t, and puts the wireformat of the + * given packet in that array. The result_size value contains the + * length of the array, if it succeeds, and 0 otherwise (in which case + * the function also returns NULL) + */ +uint8_t * +ldns_pkt2wire(ldns_pkt *packet, size_t *result_size) +{ + ldns_buffer *buffer = ldns_buffer_new(MAX_PACKET_SIZE); + uint8_t *result = NULL; + *result_size = 0; + if (ldns_pkt2buffer_wire(buffer, packet) == LDNS_STATUS_OK) { + *result_size = ldns_buffer_position(buffer); + result = (uint8_t *) ldns_buffer_export(buffer); + } else { + /* TODO: what about the error? */ + } + ldns_buffer_free(buffer); + return result; +} diff --git a/ldns/host2wire.h b/ldns/host2wire.h index 3cd0d5e4..d17a882c 100644 --- a/ldns/host2wire.h +++ b/ldns/host2wire.h @@ -16,4 +16,8 @@ ldns_status ldns_rdf2buffer_wire(ldns_buffer *buffer, ldns_rdf *rdf); ldns_status ldns_rr2buffer_wire(ldns_buffer *buffer, ldns_rr *rr, int section); ldns_status ldns_pkt2buffer_wire(ldns_buffer *buffer, ldns_pkt *pkt); +uint8_t *ldns_rdf2wire(ldns_rdf *rdf, size_t *result_size); +uint8_t *ldns_rr2wire(ldns_rr *rr, int section, size_t *result_size); +uint8_t *ldns_pkt2wire(ldns_pkt *pkt, size_t *result_size); + #endif diff --git a/run-test2.c b/run-test2.c index 94653e0a..adb1260d 100644 --- a/run-test2.c +++ b/run-test2.c @@ -183,7 +183,6 @@ main(int argc, char **argv) { const char *file; ldns_pkt *pkt; - ldns_buffer *buffer; uint8_t *target_buf; size_t len; uint16_t i; @@ -203,11 +202,8 @@ main(int argc, char **argv) } printf("And back to wire:\n"); - buffer = ldns_buffer_new(65535); - ldns_pkt2buffer_wire(buffer, pkt); - - len = ldns_buffer_position(buffer); - target_buf = (uint8_t *) ldns_buffer_export(buffer); + /*buffer = ldns_buffer_new(65535);*/ + target_buf = ldns_pkt2wire(pkt, &len); printf("Buffer length: %u\n", len);