From 3e885fdaff115df3269a2770f0039ae50ec2c814 Mon Sep 17 00:00:00 2001
From: Jan Kadlec <jan@hp4-jankadlec.(none)>
Date: Sun, 14 Nov 2010 13:35:47 +0100
Subject: [PATCH] Few comments added. Strlcpy from FreeBSD.

---
 src/dnslib/descriptor.c | 61 +++++++++++++++++++++++++++++++++++++++--
 src/dnslib/descriptor.h | 48 ++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 3 deletions(-)

diff --git a/src/dnslib/descriptor.c b/src/dnslib/descriptor.c
index a6f319353..d9f5752b1 100644
--- a/src/dnslib/descriptor.c
+++ b/src/dnslib/descriptor.c
@@ -10,6 +10,9 @@
 #include <string.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <assert.h>
+#include <string.h>
+#include <sys/types.h>
 
 #include "descriptor.h"
 
@@ -19,6 +22,15 @@
 
 const uint16_t DNSLIB_PSEUDO_TYPE_DLV = 32769;
 
+/* Taken from RFC 1035, section 3.2.4.  */
+static lookup_table_type dns_rrclasses[] = {
+	{ DNSLIB_CLASS_IN, "IN" },	/* the Internet */
+	{ DNSLIB_CLASS_CS, "CS" },	/* the CSNET class (Obsolete) */
+	{ DNSLIB_CLASS_CH, "CH" },	/* the CHAOS class */
+	{ DNSLIB_CLASS_HS, "HS" },	/* Hesiod */
+	{ 0, NULL }
+};
+
 static dnslib_rrtype_descriptor_t dnslib_rrtype_descriptors[101] = { //XXX magic constant
 	/* 0 */
 	{ 0, NULL, 1, { DNSLIB_RDATA_WF_BINARY } },
@@ -243,6 +255,50 @@ static dnslib_rrtype_descriptor_t dnslib_rrtype_descriptors[101] = { //XXX magic
 	  { DNSLIB_RDATA_WF_SHORT, DNSLIB_RDATA_WF_BYTE, DNSLIB_RDATA_WF_BYTE, DNSLIB_RDATA_WF_BINARY } },*/
 };
 
+lookup_table_type *lookup_by_name(lookup_table_type *table, const char *name)
+{
+    while (table->name != NULL) {
+        if (strcasecmp(name, table->name) == 0)
+            return table;
+    		table++;
+	  }
+	  return NULL;
+}
+
+lookup_table_type *lookup_by_id(lookup_table_type *table, int id)
+{
+    while (table->name != NULL) {
+        if (table->id == id)
+            return table;
+        table++;
+    }
+    return NULL;
+}
+
+size_t strlcpy(char *dst, const char *src, size_t siz)
+{
+    char *d = dst;
+    const char *s = src;
+    size_t n = siz;
+
+    /* Copy as many bytes as will fit */
+    if (n != 0 && --n != 0) {
+        do {
+              if ((*d++ = *s++) == 0)
+                  break;
+        } while (--n != 0);
+    }
+
+    /* Not enough room in dst, add NUL and traverse rest of src */
+    if (n == 0) {
+        if (siz != 0)
+            *d = '\0';                /* NUL-terminate dst */
+        while (*s++)
+             ;
+    }
+    return(s - src - 1);        /* count does not include NUL */
+}
+
 dnslib_rrtype_descriptor_t *dnslib_rrtype_descriptor_by_type(uint16_t type)
 {
 	if (type < DNSLIB_RRTYPE_DESCRIPTORS_LENGTH)
@@ -322,13 +378,13 @@ uint16_t rrtype_from_string(const char *name)
         return (uint16_t) rrtype;
 }
 
-/*const char *rrclass_to_string(uint16_t rrclass)
+const char *rrclass_to_string(uint16_t rrclass)
 {
 	static char buf[20];
 	lookup_table_type *entry = lookup_by_id(dns_rrclasses, rrclass);
 	if (entry) {
 		assert(strlen(entry->name) < sizeof(buf));
-		strlcpy(buf, entry->name, sizeof(buf));
+    strlcpy(buf, entry->name, sizeof(buf)); 
 	} else {
 		snprintf(buf, sizeof(buf), "CLASS%d", (int) rrclass);
 	}
@@ -365,5 +421,4 @@ uint16_t rrclass_from_string(const char *name)
 	return (uint16_t) rrclass;
 }
 
-*/
 /* end of file descriptor.c */
diff --git a/src/dnslib/descriptor.h b/src/dnslib/descriptor.h
index 41bbc7571..5521c416c 100644
--- a/src/dnslib/descriptor.h
+++ b/src/dnslib/descriptor.h
@@ -18,6 +18,30 @@
 #define MAXRDATALEN 64
 /* 64 is in NSD. Seems a little too much, but I'd say it's not a real issue. */
 
+/* A general purpose lookup table */
+typedef struct lookup_table lookup_table_type;
+struct lookup_table {
+    int id;
+    const char *name;
+};
+
+enum dnslib_rr_class
+{
+    DNSLIB_CLASS_IN,
+    DNSLIB_CLASS_CS, 
+    DNSLIB_CLASS_CH,
+    DNSLIB_CLASS_HS,
+    DNSLIB_CLASS_NONE = 254,
+    DNSLIB_CLASS_ANY = 255
+};
+
+/*!
+ * \brief Enum containing RR type codes.
+ *
+ * \todo Not all indices can be used for indexing.
+ */
+typedef enum dnslib_rr_class dnslib_rr_class_t;
+
 enum dnslib_rr_type 
 {
     DNSLIB_RRTYPE_UNKNOWN, /* 0 - an unknown type */
@@ -92,6 +116,11 @@ enum dnslib_rr_type
     DNSLIB_RRTYPE_DLV = 32769/* RFC 4431 */
 };
 
+/*!
+ * \brief Enum containing RR type codes.
+ *
+ * \todo Not all indices can be used for indexing.
+ */
 typedef enum dnslib_rr_type dnslib_rr_type_t;
 
 enum dnslib_rdata_wireformat
@@ -111,6 +140,9 @@ enum dnslib_rdata_wireformat
 	  DNSLIB_RDATA_WF_IPSECGATEWAY	       /* IPSECKEY gateway ip4, ip6 or dname. */
 };
 
+/*!
+ * \brief Enum containing wireformat codes. Taken from NSD's "dns.h"
+ */
 typedef enum dnslib_rdatawireformat dnslib_rdata_wireformat_t;
 
 struct dnslib_rrtype_descriptor
@@ -121,8 +153,24 @@ struct dnslib_rrtype_descriptor
   	uint8_t     wireformat[MAXRDATALEN]; /* rdata_wireformat_type */
 };
 
+/*!
+ * \brief Structure holding RR descriptor
+ */
 typedef struct dnslib_rrtype_descriptor dnslib_rrtype_descriptor_t;
 
+/*!
+ * \brief Strlcpy - safe string copy function, based on FreeBDS implementation
+ *  
+ * http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/string/
+ *
+ * \param dst Destination string.
+ * \param src Source string.
+ * \param siz How many characters to copy - 1.
+ *
+ * \return strlen(src), if retval >= siz, truncation occurred.
+ */
+size_t strlcpy( char *dst, const char *src, size_t siz );
+
 #endif
 
 /* end of file descriptor.h */
-- 
GitLab