From: Miek Gieben Date: Thu, 31 Mar 2005 12:27:30 +0000 (+0000) Subject: re-do the parser. This is the lowlevel framework. X-Git-Tag: release-0.50~182 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d7fed4e84116fe3be5d48e7805c194325aba5ca;p=thirdparty%2Fldns.git re-do the parser. This is the lowlevel framework. Next up: incorperate this in the rr_frm_str function and see what other changes are needed --- diff --git a/Makefile.in b/Makefile.in index b4fa2471..4be727f8 100644 --- a/Makefile.in +++ b/Makefile.in @@ -26,7 +26,7 @@ LINTFLAGS = +quiet -weak -warnposix -unrecog -Din_addr_t=uint32_t -Du_int=unsign LIBDNS_SOURCES = rdata.c util.c rr.c packet.c wire2host.c \ host2str.c buffer.c str2host.c resolver.c \ net.c host2wire.c dname.c dnssec.c keys.c \ - higher.c rr_functions.c + higher.c rr_functions.c parse.c LIBDNS_HEADERS = ldns/error.h \ ldns/packet.h \ ldns/prototype.h \ @@ -43,13 +43,14 @@ LIBDNS_HEADERS = ldns/error.h \ ldns/dnssec.h \ ldns/keys.h \ ldns/higher.h \ + ldns/parse.h \ util.h PROG_SOURCES = mx.c PROG_TARGETS = $(PROG_SOURCES:.c=) LIBDNS_OBJECTS = $(LIBDNS_SOURCES:.c=.o) -TEST_SOURCES = run-test7.c run-test11.c run-test18.c +TEST_SOURCES = run-test7.c run-test11.c run-test18.c run-test19.c #ALL_SOURCES = $(TEST_SOURCES) $(LIBDNS_SOURCES) $(PROG_SOURCES) ALL_SOURCES = $(LIBDNS_SOURCES) $(PROG_SOURCES) @@ -92,6 +93,8 @@ run-test11: run-test11.o $(LIBDNS_OBJECTS) $(LIBOBJS) $(LINK) ${LIBS} -o $@ $+ run-test18: run-test18.o $(LIBDNS_OBJECTS) $(LIBOBJS) $(LINK) ${LIBS} -lssl -o $@ $+ +run-test19: run-test19.o $(LIBDNS_OBJECTS) $(LIBOBJS) + $(LINK) ${LIBS} -lssl -o $@ $+ doc: doxygen libdns.doxygen diff --git a/dnssec.c b/dnssec.c index 5b20946e..8e00ebe6 100644 --- a/dnssec.c +++ b/dnssec.c @@ -948,6 +948,7 @@ ldns_sign_public_rsasha1(ldns_buffer *to_sign, RSA *key) ldns_rdf *sigdata_rdf; ldns_buffer *b64sig; + siglen = 0; b64sig = ldns_buffer_new(MAX_PACKETLEN); if (!b64sig) { return NULL; diff --git a/ldns/ldns.h b/ldns/ldns.h index eb85fc14..df54e5f0 100644 --- a/ldns/ldns.h +++ b/ldns/ldns.h @@ -26,5 +26,6 @@ #include #include #include +#include #endif /* _LDNS_H */ diff --git a/ldns/parse.h b/ldns/parse.h new file mode 100644 index 00000000..56ea1b98 --- /dev/null +++ b/ldns/parse.h @@ -0,0 +1,45 @@ +/* + * parse.h + * + * a Net::DNS like library for C + * LibDNS Team @ NLnet Labs + * (c) NLnet Labs, 2005 + * See the file LICENSE for the license + */ + +#ifndef _PARSE_H_ +#define _PARSE_H_ + +#include + + +#define MAXTOKEN_LEN 1024 +#define LDNS_EAT_SPACE true + + +/* what we can parse */ +enum ldns_enum_parse +{ + LDNS_SPACE_STR, /* str with spaces */ + LDNS_STR, /* str without spaces */ + LDNS_QUOTE_STR, /* str with \ in it */ + LDNS_QUOTE_SPACE_STR /* str with \ in it and spaces */ +}; +typedef enum ldns_enum_parse ldns_parse; + +/* + * get a token/char from the stream F + * return 0 on error of EOF of F + * return >0 length of what is read. + * This function deals with ( and ) in the stream + * and ignore \n when it finds them + */ +size_t ldns_get_token(FILE *f, char *token, bool eat_space); + +/* + * get the next string and supply the type we want + * return 0 on error, otherwise the length + */ +size_t ldns_get_str(FILE *f, char *word, ldns_parse type); + +#endif /* _PARSE_H_ */ diff --git a/parse.c b/parse.c new file mode 100644 index 00000000..cfc8ba6d --- /dev/null +++ b/parse.c @@ -0,0 +1,139 @@ +/* + * a generic (simple) parser. Use to parse rr's, private key + * information and /etc/resolv.conf files + * + * a Net::DNS like library for C + * LibDNS Team @ NLnet Labs + * (c) NLnet Labs, 2005 + * See the file LICENSE for the license + */ + + +#include + +#include +#include + +#include + +#include +#include +#include "util.h" + + + + +/* + * get the next string and supply the type we + * want + */ +size_t +ldns_get_str(FILE *f, char *word, ldns_parse type) +{ + size_t i; + + i = 0; + switch (type) { + case LDNS_SPACE_STR: + i = ldns_get_token(f, word, LDNS_EAT_SPACE); + return i; + case LDNS_STR: + i = ldns_get_token(f, word, false); + return i; + case LDNS_QUOTE_STR: + i = ldns_get_token(f, word, false); + break; + case LDNS_QUOTE_SPACE_STR: + i = ldns_get_token(f, word, LDNS_EAT_SPACE); + break; + } + /* only reach this spot if the str was quoted */ + /* mangle the quoted string and return what we got */ + return i; +} + + + + + + + + + + + + + + + + + + + + + +/* + * get a token/char from the stream F + * return 0 on error of EOF of F + * return >0 length of what is read. + * This function deals with ( and ) in the stream + * and ignore \n when it finds them + */ +size_t +ldns_get_token(FILE *f, char *token, bool eat_space) +{ + int c; + int p; /* 0 -> no parenthese seen, >0 nr of ( seen */ + char *t; + uint8_t i; + + p = 0; + i = 0; + t = token; + while ((c = getc(f)) != EOF) { + if (c == '(') { + p++; + continue; + } + + if (c == ')') { + p--; + continue; + } + + if (p < 0) { + /* more ) then ( */ + token = NULL; + return 0; + } + + if (c == '\n' && p != 0) { + /* in parentheses */ + continue; + } + + if (isspace(c)) { + if (isblank(c) && eat_space) { + /* ordered to keep eating */ + *t++ = c; + i++; + continue; + } + goto tokenread; + } + + *t++ = c; + i++; + } + +tokenread: + if (p != 0 || c == EOF) { + /* ( count doesn't match ) count or EOF reached */ + token = NULL; + return 0; + } else { + *t = '\0'; + return i; + } + +} diff --git a/run-test18.c b/run-test18.c index e7bb7fab..fbaf4178 100644 --- a/run-test18.c +++ b/run-test18.c @@ -84,8 +84,6 @@ main() ldns_rr_list_print(stdout, signatures); - return 0; - /* END */ printf("Now we are going to verify\n"); printf("\n[%d]\n", ldns_verify(rrs, signatures, dnskeys)); diff --git a/run-test19.c b/run-test19.c new file mode 100644 index 00000000..184a293a --- /dev/null +++ b/run-test19.c @@ -0,0 +1,37 @@ +/* + * mx is a small programs that prints out the mx records + * for a particulary domain + */ + +#include +#include +#include + +int +usage(FILE *fp, char *prog) { + fprintf(fp, "%s keygen\n", prog); + fprintf(fp, " generate a DNSKEY RR \n"); + return 0; +} + +int +main() +{ + FILE *f; + char *tok; + size_t b; + + if (!(f = fopen("blaat", "r"))) { + exit(1); + } + + tok = XMALLOC(char, 1024); + + while ((b = ldns_get_str(f, tok, true)) != 0) { + fprintf(stdout, "%d: %s\n", (int)b, tok); + } + + + fclose(f); + return 0; +}