]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
added ldns-ified nsd buffers for easy buffer manupulation
authorJelte Jansen <jeltejan@NLnetLabs.nl>
Tue, 21 Dec 2004 12:54:00 +0000 (12:54 +0000)
committerJelte Jansen <jeltejan@NLnetLabs.nl>
Tue, 21 Dec 2004 12:54:00 +0000 (12:54 +0000)
made a very first start with 2str functions (:p)

Makefile.in
buffer.c [new file with mode: 0644]
configure.ac
host2str.c [new file with mode: 0644]
ldns/buffer.h [new file with mode: 0644]
ldns/host2str.h [new file with mode: 0644]
ldns/ldns.h
run-test0.c
wire2host.c

index 5126af0b6b35e7ea030d588ad6ffdb9bda8b2261..b1ef97c823e2713b6b707d458a35d760362b757c 100644 (file)
@@ -19,14 +19,17 @@ LINTFLAGS   = +quiet +posixlib -weak -warnposix -unrecog
 #INSTALL = $(srcdir)/install-sh -c
 #INSTALL_PROGRAM = $(INSTALL)
 
-LIBDNS_SOURCES =       rdata.c util.c rr.c packet.c wire2host.c
+LIBDNS_SOURCES =       rdata.c util.c rr.c packet.c wire2host.c \
+                       host2str.c buffer.c
 LIBDNS_HEADERS =       ldns/error.h            \
                        ldns/packet.h           \
                        ldns/prototype.h        \
                        ldns/rdata.h            \
                        ldns/rr.h               \
                        ldns/wire2host.h        \
+                       ldns/host2str.h         \
                        util.h                  \
+                       buffer.h                \
                        buf.h
 LIBDNS_OBJECTS =       $(LIBDNS_SOURCES:.c=.o)
 
diff --git a/buffer.c b/buffer.c
new file mode 100644 (file)
index 0000000..914470f
--- /dev/null
+++ b/buffer.c
@@ -0,0 +1,126 @@
+/*
+ * buffer.c -- generic memory buffer .
+ *
+ * Copyright (c) 2001-2004, NLnet Labs. All rights reserved.
+ *
+ * See LICENSE for the license.
+ *
+ */
+
+#include <config.h>
+
+#include <ldns/buffer.h>
+
+ldns_buffer *
+ldns_buffer_create(size_t capacity)
+{
+       ldns_buffer *buffer
+               = MALLOC(ldns_buffer);
+       if (!buffer)
+               return NULL;
+       
+       buffer->_data = (uint8_t *) XMALLOC(uint8_t, capacity);
+       buffer->_position = 0;
+       buffer->_limit = buffer->_capacity = capacity;
+       buffer->_fixed = 0;
+       ldns_buffer_invariant(buffer);
+       
+       return buffer;
+}
+
+void
+ldns_buffer_create_from(ldns_buffer *buffer, void *data, size_t size)
+{
+       assert(data != NULL);
+
+       buffer->_position = 0;
+       buffer->_limit = buffer->_capacity = size;
+       buffer->_data = (uint8_t *) data;
+       buffer->_fixed = 1;
+       
+       ldns_buffer_invariant(buffer);
+}
+
+void
+ldns_buffer_clear(ldns_buffer *buffer)
+{
+       ldns_buffer_invariant(buffer);
+       
+       buffer->_position = 0;
+       buffer->_limit = buffer->_capacity;
+}
+
+void
+ldns_buffer_flip(ldns_buffer *buffer)
+{
+       ldns_buffer_invariant(buffer);
+       
+       buffer->_limit = buffer->_position;
+       buffer->_position = 0;
+}
+
+void
+ldns_buffer_rewind(ldns_buffer *buffer)
+{
+       ldns_buffer_invariant(buffer);
+       
+       buffer->_position = 0;
+}
+
+void
+ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity)
+{
+       ldns_buffer_invariant(buffer);
+       assert(buffer->_position <= capacity);
+       buffer->_data = (uint8_t *) XREALLOC(buffer->_data, uint8_t, capacity);
+       buffer->_limit = buffer->_capacity = capacity;
+}
+
+void
+ldns_buffer_reserve(ldns_buffer *buffer, size_t amount)
+{
+       ldns_buffer_invariant(buffer);
+       assert(!buffer->_fixed);
+       if (buffer->_capacity < buffer->_position + amount) {
+               size_t new_capacity = buffer->_capacity * 3 / 2;
+               if (new_capacity < buffer->_position + amount) {
+                       new_capacity = buffer->_position + amount;
+               }
+               ldns_buffer_set_capacity(buffer, new_capacity);
+       }
+       buffer->_limit = buffer->_capacity;
+}
+
+int
+ldns_buffer_printf(ldns_buffer *buffer, const char *format, ...)
+{
+       int result;
+       va_list args;
+       va_start(args, format);
+       result = ldns_buffer_vprintf(buffer, format, args);
+       va_end(args);
+       return result;
+}
+
+int
+ldns_buffer_vprintf(ldns_buffer *buffer, const char *format, va_list args)
+{
+       int written;
+       size_t remaining;
+       
+       ldns_buffer_invariant(buffer);
+       assert(buffer->_limit == buffer->_capacity);
+
+       /* TODO this is probably not good  (see recent nsd loggin bug)*/
+       remaining = ldns_buffer_remaining(buffer);
+       written = vsnprintf((char *) ldns_buffer_current(buffer), remaining,
+                           format, args);
+       if (written >= 0 && ((size_t) written >= remaining)) {
+               ldns_buffer_reserve(buffer, (size_t) written + 1);
+               written = vsnprintf((char *) ldns_buffer_current(buffer),
+                                   ldns_buffer_remaining(buffer),
+                                   format, args);
+       }
+       buffer->_position += written;
+       return written;
+}
index e491a42acebae54fad1db1f752a8523cab0bbb46..1687acb34eccf5ddd5c3e77055279debbdefa5db 100644 (file)
@@ -19,6 +19,29 @@ AC_PROG_MAKE_SET
 AC_C_CONST
 AC_C_INLINE
 
+AC_DEFUN([AC_CHECK_FORMAT_ATTRIBUTE],
+[AC_REQUIRE([AC_PROG_CC])
+AC_MSG_CHECKING(whether the C compiler (${CC-cc}) accepts the "format" attribute)
+AC_CACHE_VAL(ac_cv_c_format_attribute,
+[ac_cv_c_format_attribute=no
+AC_TRY_COMPILE(
+[#include <stdio.h>
+void f (char *format, ...) __attribute__ ((format (printf, 1, 2)));
+void (*pf) (char *format, ...) __attribute__ ((format (printf, 1, 2)));
+], [
+   f ("%s", "str");
+],
+[ac_cv_c_format_attribute="yes"],
+[ac_cv_c_format_attribute="no"])
+])
+
+AC_MSG_RESULT($ac_cv_c_format_attribute)
+if test $ac_cv_c_format_attribute = yes; then
+  AC_DEFINE(HAVE_ATTR_FORMAT, 1, [Whether the C compiler accepts the "format" attribute])
+fi
+])dnl
+
+
 # my own checks
 AC_PATH_PROG(doxygen, doxygen, "/usr/bin/doxygen")
 
@@ -86,6 +109,14 @@ AH_BOTTOM([
 #ifdef HAVE_ARPA_INET_H
 #include <arpa/inet.h>
 #endif
+
+#ifdef HAVE_ATTR_FORMAT
+#define ATTR_FORMAT(archetype, string_index, first_to_check) \
+    __attribute__ ((format (archetype, string_index, first_to_check)))
+#else /* !HAVE_ATTR_FORMAT */
+#define ATTR_FORMAT(archetype, string_index, first_to_check) /* empty */
+#endif /* !HAVE_ATTR_FORMAT */
+
 ])
 
 
diff --git a/host2str.c b/host2str.c
new file mode 100644 (file)
index 0000000..6cfa7e2
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * host2str.c
+ *
+ * conversion routines from the host format
+ * to the presentation format (strings)
+ *
+ * a Net::DNS like library for C
+ *
+ * (c) NLnet Labs, 2004
+ *
+ * See the file LICENSE for the license
+ */
+#include <config.h>
+
+#include <limits.h>
+
+#include <ldns/host2str.h>
+
+#include "util.h"
+
+/* TODO: general rdata2str or dname2str, with error
+         checks and return status etc */
+/* this is temp function for debugging wire2rr */
+/* do NOT pass compressed data here :p */
+char *
+ldns_dname2str(ldns_rdf *dname)
+{
+       /* can we do with 1 pos var? or without at all? */
+       uint8_t src_pos = 0;
+       uint8_t dest_pos = 0;
+       uint8_t len;
+       char *dest = XMALLOC(char, MAXDOMAINLEN);
+       char *res;
+       len = dname->_data[src_pos];
+       while (len > 0) {
+               src_pos++;
+               memcpy(&dest[dest_pos], &(dname->_data[src_pos]), len);
+               dest_pos += len;
+               src_pos += len;
+               len = dname->_data[src_pos];
+               dest[dest_pos] = '.';
+               dest_pos++;
+       }
+       dest[dest_pos] = '\0';
+       res = XMALLOC(char, sizeof(dest));
+       memcpy(res, dest, sizeof(dest));
+       
+       return dest;
+}
+
+int
+rdata_text_to_string(ldns_buffer *output, ldns_rdf *rdf)
+{
+       const uint8_t *data = ldns_rdf_data(rdf);
+       uint8_t length = data[0];
+       size_t i;
+
+       ldns_buffer_printf(output, "\"");
+       for (i = 1; i <= length; ++i) {
+               char ch = (char) data[i];
+               if (isprint(ch)) {
+                       if (ch == '"' || ch == '\\') {
+                               ldns_buffer_printf(output, "\\");
+                       }
+                       ldns_buffer_printf(output, "%c", ch);
+               } else {
+                       ldns_buffer_printf(output, "\\%03u",
+                                     (unsigned) ch);
+               }
+       }
+       ldns_buffer_printf(output, "\"");
+       return 1;
+}
+
+/**
+ * Returns string representation of the specified rdf
+ * Data is not static
+ */
+char *
+ldns_rdf2str(ldns_rdf *rdf)
+{
+       char *res = NULL;
+
+       switch(rdf->_type) {
+       case LDNS_RDF_TYPE_NONE:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_DNAME:
+               res = ldns_dname2str(rdf);
+               break;
+       case LDNS_RDF_TYPE_INT8:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "INT8");
+               break;
+       case LDNS_RDF_TYPE_INT16:
+               res = XMALLOC(char, 6);
+               snprintf(res, 6, "INT16");
+               break;
+       case LDNS_RDF_TYPE_INT32:
+               res = XMALLOC(char, 6);
+               snprintf(res, 6, "INT32");
+               break;
+       case LDNS_RDF_TYPE_INT48:
+               res = XMALLOC(char, 6);
+               snprintf(res, 6, "INT48");
+               break;
+       case LDNS_RDF_TYPE_A:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_AAAA:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_STR:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_APL:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_B64:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_HEX:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_NSEC: 
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_TYPE: 
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_CLASS:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_CERT:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_ALG:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_UNKNOWN:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_TIME:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_SERVICE:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       case LDNS_RDF_TYPE_LOC:
+               res = XMALLOC(char, 5);
+               snprintf(res, 5, "NONE");
+               break;
+       }
+       
+       return res;
+}
+
diff --git a/ldns/buffer.h b/ldns/buffer.h
new file mode 100644 (file)
index 0000000..a08ebf6
--- /dev/null
@@ -0,0 +1,385 @@
+/*
+ * buffer.h -- generic memory buffer.
+ *
+ * Copyright (c) 2001-2004, NLnet Labs. All rights reserved.
+ *
+ * See LICENSE for the license.
+ *
+ *
+ * The buffer module implements a generic buffer.  The API is based on
+ * the java.nio.Buffer interface.
+ */
+
+#ifndef _BUFFER_H_
+#define _BUFFER_H_
+
+#include <assert.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include "util.h"
+
+typedef struct buffer ldns_buffer;
+
+struct buffer
+{
+       /*
+        * The current position used for reading/writing.
+        */ 
+       size_t   _position;
+
+       /*
+        * The read/write limit.
+        */
+       size_t   _limit;
+
+       /*
+        * The amount of data the buffer can contain.
+        */
+       size_t   _capacity;
+
+       /*
+        * The data contained in the buffer.
+        */
+       uint8_t *_data;
+
+       /*
+        * If the buffer is fixed it cannot be resized.
+        */
+       unsigned _fixed : 1;
+};
+
+#ifdef NDEBUG
+INLINE void
+ldns_buffer_invariant(ldns_buffer *ATTR_UNUSED(buffer))
+{
+}
+#else
+INLINE void
+ldns_buffer_invariant(ldns_buffer *buffer)
+{
+       assert(buffer != NULL);
+       assert(buffer->_position <= buffer->_limit);
+       assert(buffer->_limit <= buffer->_capacity);
+       assert(buffer->_data != NULL);
+}
+#endif
+
+/*
+ * Create a new buffer with the specified capacity.
+ */
+ldns_buffer *ldns_buffer_create(size_t capacity);
+
+/*
+ * Create a buffer with the specified data.  The data is not copied
+ * and no memory allocations are done.  The buffer is fixed and cannot
+ * be resized using buffer_reserve().
+ */
+void ldns_ldns_buffer_create_from(ldns_buffer *buffer, void *data, size_t size);
+
+/*
+ * Clear the buffer and make it ready for writing.  The buffer's limit
+ * is set to the capacity and the position is set to 0.
+ */
+void ldns_buffer_clear(ldns_buffer *buffer);
+
+/*
+ * Make the buffer ready for reading the data that has been written to
+ * the buffer.  The buffer's limit is set to the current position and
+ * the position is set to 0.
+ */
+void lnds_buffer_flip(ldns_buffer *buffer);
+
+/*
+ * Make the buffer ready for re-reading the data.  The buffer's
+ * position is reset to 0.
+ */
+void ldns_buffer_rewind(ldns_buffer *buffer);
+
+INLINE size_t
+ldns_buffer_position(ldns_buffer *buffer)
+{
+       return buffer->_position;
+}
+
+/*
+ * Set the buffer's position to MARK.  The position must be less than
+ * or equal to the buffer's limit.
+ */
+INLINE void
+ldns_buffer_set_position(ldns_buffer *buffer, size_t mark)
+{
+       assert(mark <= buffer->_limit);
+       buffer->_position = mark;
+}
+
+/*
+ * Change the buffer's position by COUNT bytes.  The position must not
+ * be moved behind the buffer's limit or before the beginning of the
+ * buffer.
+ */
+INLINE void
+ldns_buffer_skip(ldns_buffer *buffer, ssize_t count)
+{
+       assert(buffer->_position + count <= buffer->_limit);
+       buffer->_position += count;
+}
+
+INLINE size_t
+ldns_buffer_limit(ldns_buffer *buffer)
+{
+       return buffer->_limit;
+}
+
+/*
+ * Change the buffer's limit.  If the buffer's position is greater
+ * than the new limit the position is set to the limit.
+ */
+INLINE void
+ldns_buffer_set_limit(ldns_buffer *buffer, size_t limit)
+{
+       assert(limit <= buffer->_capacity);
+       buffer->_limit = limit;
+       if (buffer->_position > buffer->_limit)
+               buffer->_position = buffer->_limit;
+}
+
+
+INLINE size_t
+ldns_buffer_capacity(ldns_buffer *buffer)
+{
+       return buffer->_capacity;
+}
+
+/*
+ * Change the buffer's capacity.  The data is reallocated so any
+ * pointers to the data may become invalid.  The buffer's limit is set
+ * to the buffer's new capacity.
+ */
+void ldns_buffer_set_capacity(ldns_buffer *buffer, size_t capacity);
+
+/*
+ * Ensure BUFFER can contain at least AMOUNT more bytes.  The buffer's
+ * capacity is increased if necessary using buffer_set_capacity().
+ *
+ * The buffer's limit is always set to the (possibly increased)
+ * capacity.
+ */
+void ldns_buffer_reserve(ldns_buffer *buffer, size_t amount);
+
+/*
+ * Return a pointer to the data at the indicated position.
+ */
+INLINE uint8_t *
+ldns_buffer_at(ldns_buffer *buffer, size_t at)
+{
+       assert(at <= buffer->_limit);
+       return buffer->_data + at;
+}
+
+/*
+ * Return a pointer to the beginning of the buffer (the data at
+ * position 0).
+ */
+INLINE uint8_t *
+ldns_buffer_begin(ldns_buffer *buffer)
+{
+       return ldns_buffer_at(buffer, 0);
+}
+
+/*
+ * Return a pointer to the end of the buffer (the data at the buffer's
+ * limit).
+ */
+INLINE uint8_t *
+ldns_buffer_end(ldns_buffer *buffer)
+{
+       return ldns_buffer_at(buffer, buffer->_limit);
+}
+
+/*
+ * Return a pointer to the data at the buffer's current position.
+ */
+INLINE uint8_t *
+ldns_buffer_current(ldns_buffer *buffer)
+{
+       return ldns_buffer_at(buffer, buffer->_position);
+}
+
+/*
+ * The number of bytes remaining between the indicated position and
+ * the limit.
+ */
+INLINE size_t
+ldns_buffer_remaining_at(ldns_buffer *buffer, size_t at)
+{
+       ldns_buffer_invariant(buffer);
+       assert(at <= buffer->_limit);
+       return buffer->_limit - at;
+}
+
+/*
+ * The number of bytes remaining between the buffer's position and
+ * limit.
+ */
+INLINE size_t
+ldns_buffer_remaining(ldns_buffer *buffer)
+{
+       return ldns_buffer_remaining_at(buffer, buffer->_position);
+}
+
+/*
+ * Check if the buffer has at least COUNT more bytes available.
+ * Before reading or writing the caller needs to ensure enough space
+ * is available!
+ */
+INLINE int
+ldns_buffer_available_at(ldns_buffer *buffer, size_t at, size_t count)
+{
+       return count <= ldns_buffer_remaining_at(buffer, at);
+}
+
+INLINE int
+ldns_buffer_available(ldns_buffer *buffer, size_t count)
+{
+       return ldns_buffer_available_at(buffer, buffer->_position, count);
+}
+
+INLINE void
+ldns_buffer_write_at(ldns_buffer *buffer, size_t at, const void *data, size_t count)
+{
+       assert(ldns_buffer_available_at(buffer, at, count));
+       memcpy(buffer->_data + at, data, count);
+}
+
+INLINE void
+ldns_buffer_write(ldns_buffer *buffer, const void *data, size_t count)
+{
+       ldns_buffer_write_at(buffer, buffer->_position, data, count);
+       buffer->_position += count;
+}
+
+INLINE void
+ldns_buffer_write_string_at(ldns_buffer *buffer, size_t at, const char *str)
+{
+       ldns_buffer_write_at(buffer, at, str, strlen(str));
+}
+
+INLINE void
+ldns_buffer_write_string(ldns_buffer *buffer, const char *str)
+{
+       ldns_buffer_write(buffer, str, strlen(str));
+}
+
+INLINE void
+ldns_buffer_write_u8_at(ldns_buffer *buffer, size_t at, uint8_t data)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
+       buffer->_data[at] = data;
+}
+
+INLINE void
+ldns_buffer_write_u8(ldns_buffer *buffer, uint8_t data)
+{
+       ldns_buffer_write_u8_at(buffer, buffer->_position, data);
+       buffer->_position += sizeof(data);
+}
+
+INLINE void
+ldns_buffer_write_u16_at(ldns_buffer *buffer, size_t at, uint16_t data)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
+       write_uint16(buffer->_data + at, data);
+}
+
+INLINE void
+ldns_buffer_write_u16(ldns_buffer *buffer, uint16_t data)
+{
+       ldns_buffer_write_u16_at(buffer, buffer->_position, data);
+       buffer->_position += sizeof(data);
+}
+
+INLINE void
+ldns_buffer_write_u32_at(ldns_buffer *buffer, size_t at, uint32_t data)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(data)));
+       write_uint32(buffer->_data + at, data);
+}
+
+INLINE void
+ldns_buffer_write_u32(ldns_buffer *buffer, uint32_t data)
+{
+       ldns_buffer_write_u32_at(buffer, buffer->_position, data);
+       buffer->_position += sizeof(data);
+}
+
+INLINE void
+ldns_buffer_read_at(ldns_buffer *buffer, size_t at, void *data, size_t count)
+{
+       assert(ldns_buffer_available_at(buffer, at, count));
+       memcpy(data, buffer->_data + at, count);
+}
+
+INLINE void
+ldns_buffer_read(ldns_buffer *buffer, void *data, size_t count)
+{
+       ldns_buffer_read_at(buffer, buffer->_position, data, count);
+       buffer->_position += count;
+}
+
+INLINE uint8_t
+ldns_buffer_read_u8_at(ldns_buffer *buffer, size_t at)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(uint8_t)));
+       return buffer->_data[at];
+}
+
+INLINE uint8_t
+ldns_buffer_read_u8(ldns_buffer *buffer)
+{
+       uint8_t result = ldns_buffer_read_u8_at(buffer, buffer->_position);
+       buffer->_position += sizeof(uint8_t);
+       return result;
+}
+
+INLINE uint16_t
+ldns_buffer_read_u16_at(ldns_buffer *buffer, size_t at)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(uint16_t)));
+       return read_uint16(buffer->_data + at);
+}
+
+INLINE uint16_t
+ldns_buffer_read_u16(ldns_buffer *buffer)
+{
+       uint16_t result = ldns_buffer_read_u16_at(buffer, buffer->_position);
+       buffer->_position += sizeof(uint16_t);
+       return result;
+}
+
+INLINE uint32_t
+ldns_buffer_read_u32_at(ldns_buffer *buffer, size_t at)
+{
+       assert(ldns_buffer_available_at(buffer, at, sizeof(uint32_t)));
+       return read_uint32(buffer->_data + at);
+}
+
+INLINE uint32_t
+ldns_buffer_read_u32(ldns_buffer *buffer)
+{
+       uint32_t result = ldns_buffer_read_u32_at(buffer, buffer->_position);
+       buffer->_position += sizeof(uint32_t);
+       return result;
+}
+
+/*
+ * Print to the buffer, increasing the capacity if required using
+ * buffer_reserve(). The buffer's position is set to the terminating
+ * '\0'. Returns the number of characters written (not including the
+ * terminating '\0').
+ */
+int ldns_buffer_printf(ldns_buffer *buffer, const char *format, ...)
+       ATTR_FORMAT(printf, 2, 3);
+int ldns_buffer_vprintf(ldns_buffer *buffer, const char *format, va_list args);
+
+#endif /* _BUFFER_H_ */
diff --git a/ldns/host2str.h b/ldns/host2str.h
new file mode 100644 (file)
index 0000000..36783e8
--- /dev/null
@@ -0,0 +1,16 @@
+#include <ldns/rdata.h>
+
+#ifndef _LDNS_HOST2STR_H
+#define _LDNS_HOST2STR_H
+
+#include <ldns/common.h>
+#include <ldns/error.h>
+#include <ldns/rr.h>
+#include <ldns/packet.h>
+#include <ldns/buffer.h>
+#include <ctype.h>
+
+char *ldns_rdf2str(ldns_rdf *rdf);
+
+#endif
+
index 78af9bc4442bf1b2e3e0bffb079b7b28daa59599..62079528a20dafab539e4634f4a5bb097c867e3a 100644 (file)
@@ -19,5 +19,6 @@
 #include <ldns/rr.h>
 #include <ldns/packet.h>
 #include <ldns/wire2host.h>
+#include <ldns/host2str.h>
 
 #endif /* _LDNS_H */
index 13ad647a954770ed3201fdc2882978e893939a61..6a85b107097ac60b5ac40ff7045378e2c71488e9 100644 (file)
@@ -40,10 +40,20 @@ main(void)
        ldns_rr *rr;
        ldns_pkt *packet;
        ldns_status status;
+       char *rdfstr;
+       uint8_t *rdf_data;
        
        rr = ldns_rr_new();
 
-       rd_f = ldns_rdf_new(20, LDNS_RDF_TYPE_DNAME, (uint8_t*)"hallo.nl");
+       rdf_data = (uint8_t *) XMALLOC(char, MAXDOMAINLEN);
+       rdf_data[0] = 3;
+       memcpy(rdf_data+1, "www", 3);
+       rdf_data[4] = 4;
+       memcpy(rdf_data+5, "test", 4);
+       rdf_data[9] = 3;
+       memcpy(rdf_data+10, "net", 3);
+       rdf_data[13] = 0;
+       rd_f = ldns_rdf_new(20, LDNS_RDF_TYPE_DNAME, rdf_data);
        xprintf_rdf(rd_f);
        
        ldns_rr_push_rdf(rr, rd_f);
@@ -70,5 +80,10 @@ main(void)
        } else {
                printf("error in wire2packet: %d\n", status);
        }
+
+       printf("host2str:\n");
+       rdfstr = ldns_rdf2str(rr->_rdata_fields[0]);
+       printf("%s\n", rdfstr);
        return 0;
 }
+
index 8b73fafd955cee07c70a43349ba0b66fbe1082a5..6e61f719c293ee0502ab2cd7bb56d3ecc83996d4 100644 (file)
 #define        ARCOUNT(wirebuf)                (read_uint16(wirebuf+ARCOUNT_OFF))
 
 
-/* TODO: general rdata2str or dname2str, with error
-         checks and return status etc */
-/* this is temp function for debugging wire2rr */
-/* do NOT pass compressed data here :p */
-void
-ldns_dname2str(char *dest, ldns_rdf *dname)
-{
-       /* can we do with 1 pos var? or without at all? */
-       uint8_t src_pos = 0;
-       uint8_t dest_pos = 0;
-       uint8_t len;
-       len = dname->_data[src_pos];
-       while (len > 0) {
-               src_pos++;
-               memcpy(&dest[dest_pos], &(dname->_data[src_pos]), len);
-               dest_pos += len;
-               src_pos += len;
-               len = dname->_data[src_pos];
-               dest[dest_pos] = '.';
-               dest_pos++;
-       }
-       dest[dest_pos] = '\0';
-}
-
 /* TODO:
          status_type return and remove printfs
          #defines */