LIBDNS_SOURCES = rdata.c util.c rr.c packet.c wire2host.c \
host2str.c buffer.c str2host.c resolver.c \
- net.c host2wire.c
+ net.c host2wire.c dname.c
LIBDNS_HEADERS = ldns/error.h \
ldns/packet.h \
ldns/prototype.h \
ldns/buffer.h \
ldns/resolver.h \
ldns/net.h \
+ ldns/dname.h \
util.h
LIBDNS_OBJECTS = $(LIBDNS_SOURCES:.c=.o)
--- /dev/null
+/*
+ * dname.c
+ *
+ * dname specific rdata implementations
+ * A dname is a rdf structure with type LDNS_RDF_TYPE_DNAME
+ *
+ * a Net::DNS like library for C
+ *
+ * (c) NLnet Labs, 2004
+ *
+ * See the file LICENSE for the license
+ */
+
+#include <config.h>
+
+#include <ldns/rdata.h>
+#include <ldns/error.h>
+#include <ldns/str2host.h>
+#include <ldns/dns.h>
+
+#include "util.h"
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+
+/**
+ * concatenate two dnames together
+ * \param[in] rd1 the leftside
+ * \param[in] rd2 the rightside
+ * \return a new rdf with leftside/rightside
+ */
+ldns_rdf *
+ldns_dname_concat(ldns_rdf *rd1, ldns_rdf *rd2)
+{
+ ldns_rdf *new;
+ uint16_t new_size;
+ uint8_t *buf;
+
+ if (ldns_rdf_get_type(rd1) != LDNS_RDF_TYPE_DNAME ||
+ ldns_rdf_get_type(rd2) != LDNS_RDF_TYPE_DNAME)
+ {
+ return NULL;
+ }
+
+ /* we overwrite the nullbyte of rd1 */
+ new_size = ldns_rdf_size(rd1) + ldns_rdf_size(rd2) - 1;
+ buf = XMALLOC(uint8_t*, new_size);
+ if (!buf) {
+ return NULL;
+ }
+
+ /* put the two dname's after each other */
+ memcpy(buf, ldns_rdf_data(rd1), ldns_rdf_size(rd1) - 1);
+ memcpy(buf + ldns_rdf_size(rd1),
+ ldns_rdf_data(rd2), ldns_rdf_size(rd2));
+
+ new = ldns_rdf_new_frm_data(new_size, LDNS_RDF_TYPE_DNAME,
+ buf);
+
+ FREE(buf);
+ return new;
+}
+
+/**
+ * count the number of labels inside a LDNS_RDF_DNAME type
+ * rdf
+ * \param[in] *r the rdf
+ * \return the number of labels
+ */
+uint8_t
+ldns_rdf_dname_label_count(ldns_rdf *r)
+{
+ uint8_t src_pos;
+ uint8_t len;
+ uint8_t i;
+ size_t r_size;
+
+ i = 0; src_pos = 0;
+ r_size = ldns_rdf_size(r);
+
+ if (ldns_rdf_get_type(r) != LDNS_RDF_TYPE_DNAME) {
+ return 0;
+ } else {
+ len = ldns_rdf_data(r)[src_pos]; /* start of the label */
+
+ /* single root label */
+ if (1 == r_size) {
+ return 0;
+ } else {
+ while ((len > 0) && src_pos < r_size) {
+ src_pos++;
+ src_pos += len;
+ len = ldns_rdf_data(r)[src_pos];
+ i++;
+ }
+ }
+ return i;
+ }
+}
+
+/**
+ * Create a new dname rdf from a string
+ * \param[in] str string to use
+ * \param[in] t type to use
+ * \return ldns_rdf*
+ */
+ldns_rdf *
+ldns_dname_new_frm_str(const char *str)
+{
+ return
+ ldns_rdf_new_frm_str(str, LDNS_RDF_TYPE_DNAME);
+}
#define LDNS_IP4ADDRLEN (32/8)
#define LDNS_IP6ADDRLEN (128/8)
#define LDNS_PORT 53
+#define LDNS_ROOT_LABEL '\0'
#endif /* _DNS_H_ */
ldns_rdf *ldns_rdf_new_frm_str(const char *, ldns_rdf_type);
struct sockaddr_storage * ldns_rdf2native_sockaddr_storage(ldns_rdf *);
ldns_rdf *ldns_rdf_clone(ldns_rdf *);
-uint8_t ldns_rdf_dname_label_count(ldns_rdf *);
-bool ldns_rdf_dname_absolute(ldns_rdf *);
#endif /* !_LDNS_RDATA_H */
ldns_rdf_data(r)));
}
-/**
- * count the number of labels inside a LDNS_RDF_DNAME type
- * rdf
- * \param[in] *r the rdf
- * \return the number of labels
- */
-uint8_t
-ldns_rdf_dname_label_count(ldns_rdf *r)
-{
- uint8_t src_pos;
- uint8_t len;
- uint8_t i;
- size_t r_size;
-
- i = 0; src_pos = 0;
- r_size = ldns_rdf_size(r);
-
- if (ldns_rdf_get_type(r) != LDNS_RDF_TYPE_DNAME) {
- return 0;
- } else {
- len = ldns_rdf_data(r)[src_pos]; /* start of the label */
-
- /* single root label */
- if (1 == r_size) {
- return 0;
- } else {
- while ((len > 0) && src_pos < r_size) {
- src_pos++;
- src_pos += len;
- len = ldns_rdf_data(r)[src_pos];
- i++;
- }
- }
- return i;
- }
-}
-
/**
* free a rdf structure _and_ free the
* data. rdf should be created with _new_frm_data
#include <ldns/str2host.h>
#include <ldns/host2str.h>
#include <ldns/buffer.h>
+#include <ldns/dname.h>
#include "util.h"
ldns_buffer *buf;
ldns_rdf *rdata;
ldns_rdf *cnt_test;
+ ldns_rdf *cat_test1;
+ ldns_rdf *cat_test2;
+ ldns_rdf *concat;
buf = ldns_buffer_new(10); /* alloc away! */
if (!buf) {
fprintf(stderr, "%s\n", buffer2str(buf));
/* test the label counter */
- cnt_test = ldns_rdf_new_frm_str("miek.nl.", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str("miek.nl.");
printf("Labels miek.nl. %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str("miek.nl", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str("miek.nl");
printf("Labels miek.nl %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str("miek", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str("miek");
printf("Labels miek %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str(".", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str(".");
printf("Labels . %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str(".www.miek.nl.", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str(".www.miek.nl.");
printf("Labels .www.miek.nl. %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str("www.miek.nl.", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str("www.miek.nl.");
printf("Labels www.miek.nl. %d\n", ldns_rdf_dname_label_count(cnt_test));
- cnt_test = ldns_rdf_new_frm_str("nl", LDNS_RDF_TYPE_DNAME);
+ cnt_test = ldns_dname_new_frm_str("nl");
printf("Labels nl %d\n", ldns_rdf_dname_label_count(cnt_test));
+
+
+ /* concat tests */
+ cat_test1 = ldns_dname_new_frm_str("www");
+ cat_test2 = ldns_dname_new_frm_str("miek.nl.");
}
* \todo make this more efficient...
* we do 3 memcpy's in total...
* label_chars2 is used for debugging. TODO: remove
- * \todo DOES IT WORK...
*/
ldns_status
ldns_str2rdf_dname(ldns_rdf **d, const uint8_t* str)
return LDNS_STATUS_DOMAINNAME_OVERFLOW;
}
buf_str[len] = (uint8_t)'.';
- buf_str[len + 1] = (uint8_t)'\0';
+ buf_str[len + 1] = (uint8_t) LDNS_ROOT_LABEL;
len += 1;
} else {
- buf_str[len] = (uint8_t)'\0';
+ buf_str[len] = (uint8_t) LDNS_ROOT_LABEL;
}
/* extend with 1 - the first char will be the
/* DEBUG memcpy(q, &label_chars2, 1); */
memcpy(q + 1, p, label_chars);
q += (label_chars + 1);
- *q = (uint8_t)'\0'; /* end the string */
+ *q = (uint8_t)LDNS_ROOT_LABEL; /* end the string */
/* s - buf_str works because no magic is done in the above for-loop */
/* *d = ldns_rdf_new_frm_data((s - buf_str + 2) , LDNS_RDF_TYPE_DNAME , buf); */