]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
simple resolv.conf parser made.
authorMiek Gieben <miekg@NLnetLabs.nl>
Wed, 2 Mar 2005 10:59:50 +0000 (10:59 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Wed, 2 Mar 2005 10:59:50 +0000 (10:59 +0000)
It cannot handle searchlist, as searchlist is the only usefull option
that allows multiple arguments... :(
It does handle nameserver, see run-test14.c

Makefile.in
ldns/resolver.h
resolver.c
rr.c
run-test14.c [new file with mode: 0644]
util.c
util.h

index bad052e89cd19dd56879146cafdad87fa80996ee..ac14d10a2cd197d385464716e21d0b17472c9366 100644 (file)
@@ -45,7 +45,7 @@ LIBDNS_OBJECTS        =       $(LIBDNS_SOURCES:.c=.o)
 ALL_SOURCES    =       run-test0.c run-test1.c run-test2.c run-test3.c \
                        run-test4.c run-test5.c run-test6.c run-test7.c \
                        run-test8.c run-test9.c run-test10.c run-test11.c \
-                       run-test12.c run-test13.c \
+                       run-test12.c run-test13.c run-test14.c \
                        $(LIBDNS_SOURCES)
 
 TESTS          =       run-test0               \
@@ -60,7 +60,8 @@ TESTS         =       run-test0               \
                        run-test9               \
                        run-test10              \
                        run-test11              \
-                       run-test13
+                       run-test13              \
+                       run-test14              
 
 COMPILE                = $(CC) $(CPPFLAGS) $(CFLAGS)
 COMP_LIB       = $(LIBTOOL) $(CC) $(CPPFLAGS) $(CFLAGS)
@@ -115,6 +116,8 @@ run-test12: run-test12.o $(LIBDNS_OBJECTS) $(LIBOBJS)
                $(LINK) -lpcap ${LIBS} -o $@ $+
 run-test13:    run-test13.o $(LIBDNS_OBJECTS) $(LIBOBJS)
                $(LINK) ${LIBS} -o $@ $+
+run-test14:    run-test14.o $(LIBDNS_OBJECTS) $(LIBOBJS)
+               $(LINK) ${LIBS} -o $@ $+
 
 run-test-trace:        run-test-trace.o $(LIBDNS_OBJECTS) $(LIBOBJS)
                $(LINK) ${LIBS} -o $@ $+
index 0382869469688dd13544f3c479d76fbee1df2e6b..0022f92b083acf992191418cda53f9646d301212 100644 (file)
@@ -24,6 +24,9 @@
 /** \brief where to find the resolv.conf file */
 #define RESOLV_CONF    "/etc/resolv.conf"
 #define MAXLINE_LEN    256
+#define RESOLV_KEYWORD         0
+#define RESOLV_DEFDOMAIN       1
+#define RESOLV_NAMESERVER      2
 
 /**
  * \brief Structure of a dns resolver
index 9e9d1abea91285561d9f08d0f8281c4715bac770..d5dcdac13948515c8621e5ebe2117055875d15d8 100644 (file)
@@ -22,6 +22,8 @@
 #include <ldns/dns.h>
 #include <ldns/dname.h>
 
+#include <strings.h>
+
 #include "util.h"
 
 /* Access function for reading 
@@ -293,16 +295,24 @@ ldns_resolver_new_frm_file(const char *filename)
 {
        ldns_resolver *r;
        FILE *fp;
-       const char *keyword[3];
-       char *line;
-       size_t len;
-       
+       const char *keyword[2];
+       char *word;
+       uint8_t expect;
+       uint8_t i;
+       ldns_rdf *tmp;
+
+       /* do this better 
+        * expect = 
+        * 0: keyword
+        * 1: default domain dname
+        * 2: NS aaaa or a record
+        */
 
-       keyword[0] = "nameserver";
-       keyword[1] = "domain";
-       keyword[2] = "searchlist";
-       line = XMALLOC(char, MAXLINE_LEN);
-       len = MAXLINE_LEN;
+       /* recognized keywords */
+       keyword[0] = "domain";
+       keyword[1] = "nameserver";
+       word = XMALLOC(char, MAXLINE_LEN);
+       expect = 0;
 
        r = ldns_resolver_new();
        if (!r) {
@@ -319,12 +329,57 @@ ldns_resolver_new_frm_file(const char *filename)
        }
        /* the file is opened. it's line based - this will be a bit messy
         */
-#if 0  
-       while (getline(&line, &len, fp) != -1) {
+
+       while (readword(word, fp, MAXLINE_LEN) != -1) {
                /* do something */
-               printf("line %s\n", line);
+               switch(expect) {
+                       case RESOLV_KEYWORD:
+                               /* keyword */
+                               for(i = 0; i < 2; i++) {
+                                       if (strcasecmp(keyword[i], word) == 0) {
+                                               /* chosen the keyword and
+                                                * expect values carefully
+                                                */
+                                               expect = i + 1;
+                                               break;
+                                       }
+                               }
+                               /* no keyword recognized */
+                               if (expect == 0) {
+                                       printf("[%s] unreg keyword\n", word);
+                               }
+                               break;
+                       case RESOLV_DEFDOMAIN:
+                               /* default domain dname */
+                               tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, word);
+                               if (!tmp) {
+                                       expect = RESOLV_KEYWORD;
+                                       break;
+                               }
+                               ldns_resolver_set_domain(r, tmp);
+                               expect = RESOLV_KEYWORD;
+                               break;
+                       case RESOLV_NAMESERVER:
+                               /* NS aaaa or a record */
+                               tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA, word);
+                               if (!tmp) {
+                                       /* try ip4 */
+                                       tmp = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A, word);
+                               }
+                               if (!tmp) {
+                                       expect = RESOLV_KEYWORD;
+                                       break;
+                               }
+                               (void)ldns_resolver_push_nameserver(r, tmp);
+                               expect = RESOLV_KEYWORD;
+                               break;
+                       default:
+                               /* huh?! */
+                               printf("BIG FAT WARNING should never reach this\n");
+                               expect = RESOLV_KEYWORD;
+                               break;
+               }
        }
-#endif
 
        fclose(fp);
        return r;
diff --git a/rr.c b/rr.c
index 602a68484b71835ba201c121fa184d2789aceedd..b8d31199033126f9113c5b7c042080ee545f3ea4 100644 (file)
--- a/rr.c
+++ b/rr.c
@@ -172,8 +172,10 @@ ldns_rr_new_frm_str(const char *str)
                        ldns_rr_descriptor_field_type(desc, r_cnt),
                        rd);
 
+#ifdef DEBUG
                printf("rd str [%s] %d\n", rd, r_cnt);
                printf("type %d\n",ldns_rr_descriptor_field_type(desc, r_cnt));
+#endif 
 
                if (!r) {
                        printf("rdf conversion mismatch\n");
diff --git a/run-test14.c b/run-test14.c
new file mode 100644 (file)
index 0000000..edd24dc
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * An example ldns program
+ *
+ * Setup a resolver
+ * Query a nameserver
+ * Print the result
+ */
+
+#include <config.h>
+#include <ldns/resolver.h>
+#include <ldns/dname.h>        
+
+void
+print_usage(char *file)
+{
+       printf("Usage: %s <type> <name>\n", file);
+       exit(0);
+}
+
+int
+main(int argc, char **argv)
+{       
+        ldns_resolver *res;
+        ldns_rdf *qname;
+       ldns_rdf *defdomain;
+        ldns_pkt *pkt;
+        char *name = NULL;
+        char *type = NULL;
+        
+        if (argc < 3) {
+               print_usage(argv[0]);
+       } else {
+               type = argv[1];
+               name = argv[2];
+       }
+                
+        /* init */
+        res = ldns_resolver_new_frm_file(NULL); 
+        if (!res) {
+               printf("resolver creation failed\n");
+                return -1;
+       }
+
+       /* UDP query */
+       ldns_resolver_set_usevc(res, false);
+        qname = ldns_dname_new_frm_str(name);
+       if (!qname) {
+               printf("error making qname\n");
+               return -1;
+       }
+
+        pkt = ldns_resolver_query(res, qname, ldns_get_rr_type_by_name(type), 0, LDNS_RD);
+       if (!pkt)  {
+               printf("error pkt sending\n");
+       } else {
+               ldns_pkt_print(stdout, pkt);
+       }
+        
+        return 0;
+}
diff --git a/util.c b/util.c
index 45504f7516c969f654c179c960d13343c0e16c01..a290357757d415b926fd70a1bbfdf317b7e20893 100644 (file)
--- a/util.c
+++ b/util.c
@@ -126,3 +126,29 @@ hexdigit_to_int(char ch)
                abort();
        }
 }
+
+/**
+ * read a word from a stream. Return the number
+ * of character read or -1 on failure or EOF
+ */
+int
+readword(char *line, FILE *from, size_t lim)
+{
+       int c;
+       char *l;
+       int i;
+
+       l = line; i = 0;
+       while ((c = getc(from)) != EOF) {
+               if (c != '\n' && c != ' ' && c != '\t') {
+                       *l++ = c; lim--; i++;
+               } else {
+                       *l = '\0'; 
+                       return i;
+               }
+               if ((size_t)i > lim)  {
+                       return -1;
+               }
+       }
+       return -1;
+}
diff --git a/util.h b/util.h
index 84875ec895eb325a7be53f26ee721d3fd174708e..cd6fa9df9e8a93548bfcecf25623dbdb400b2527 100644 (file)
--- a/util.h
+++ b/util.h
@@ -144,5 +144,9 @@ long power(long a, long b);
  */
 int hexdigit_to_int(char ch);
 
+/**
+ * return a word from a stream
+ */
+int readword(char *, FILE *, size_t);
 
 #endif /* !_UTIL_H */