Skip to content
Browse files

Continuing with DNS. Added some checks to attr_bool, and explained ch…

…ecks in docs.
  • Loading branch information...
1 parent 05ef58b commit 947b67b5a5e08cfdd2556e2e5eb19180a80a2b05 @abiggerhammer committed May 24, 2012
Showing with 85 additions and 22 deletions.
  1. +49 −14 examples/dns.c
  2. +23 −7 src/hammer.c
  3. +13 −1 src/hammer.h
View
63 examples/dns.c
@@ -1,16 +1,48 @@
#include "../hammer.h"
bool is_zero(parse_result_t *p) {
+ if (TT_UINT != p->ast->token_type)
+ return 0;
return (0 == p->ast->uint);
}
+/**
+ * A label can't be more than 63 characters.
+ */
+bool validate_label(parse_result_t *p) {
+ if (TT_SEQ != p->ast->token_type)
+ return 0;
+ return (64 > p->ast->seq->used);
+}
+
+/**
+ * Every DNS message should have QDCOUNT entries in the question
+ * section, and ANCOUNT+NSCOUNT+ARCOUNT resource records.
+ *
+ */
bool validate_dns(parse_result_t *p) {
-
+ if (TT_SEQ != p->ast->token_type)
+ return 0;
+ // The header holds the counts as its last 4 elements.
+ parsed_token_t *header = p->ast->seq->elements[0];
+ size_t qd = header->seq->elements[8];
+ size_t an = header->seq->elements[9];
+ size_t ns = header->seq->elements[10];
+ size_t ar = header->seq->elements[11];
+ parsed_token_t *questions = p->ast->seq->elements[1];
+ if (questions->seq->used != qd)
+ return false;
+ parsed_token_t *rrs = p->ast->seq->elements[2];
+ if (an+ns+ar != rrs->seq->used)
+ return false;
}
-int main(int argc, char **argv) {
+parser_t init_parser() {
+ static parser_t *dns_message = NULL;
+ if (dns_message)
+ return dns_message;
- const parser_t dns_header = sequence(bits(16, false), // ID
+ const parser_t *dns_header = sequence(bits(16, false), // ID
bits(1, false), // QR
bits(4, false), // opcode
bits(1, false), // AA
@@ -42,11 +74,12 @@ int main(int argc, char **argv) {
ch('-'),
NULL));
- const parser_t *label = sequence(letter,
- optional(sequence(optional(ldh_str),
- let_dig,
- NULL)),
- NULL);
+ const parser_t *label = attr_bool(sequence(letter,
+ optional(sequence(optional(ldh_str),
+ let_dig,
+ NULL)),
+ NULL),
+ validate_label);
/**
* You could write it like this ...
@@ -76,10 +109,12 @@ int main(int argc, char **argv) {
NULL);
- const parser_t *dns_message = attr_bool(sequence(dns_header,
- dns_question,
- many(dns_rr),
- end_p(),
- NULL),
- validate_dns);
+ dns_message = attr_bool(sequence(dns_header,
+ many(dns_question),
+ many(dns_rr),
+ end_p(),
+ NULL),
+ validate_dns);
+
+ return dns_message;
}
View
30 src/hammer.c
@@ -311,11 +311,6 @@ const parser_t* ch(const uint8_t c) {
return (const parser_t*)ret;
}
-typedef struct {
- uint8_t lower;
- uint8_t upper;
-} range_t;
-
static parse_result_t* parse_whitespace(void* env, parse_state_t *state) {
char c;
input_stream_t bak;
@@ -374,7 +369,7 @@ static parse_result_t* parse_charset(void *env, parse_state_t *state) {
return NULL;
}
-const parser_t* range(const uint8_t lower, const uint8_t upper) {
+const parser_t* ch_range(const uint8_t lower, const uint8_t upper) {
parser_t *ret = g_new(parser_t, 1);
charset cs = new_charset();
for (int i = 0; i < 256; i++)
@@ -383,6 +378,27 @@ const parser_t* range(const uint8_t lower, const uint8_t upper) {
return (const parser_t*)ret;
}
+typedef struct {
+ int64_t lower;
+ int64_t upper;
+} range_t;
+
+const parser_t* int_range(const int64_t lower, const int64_t upper) {
+ struct bits_env env = p->env;
+ // p must be an integer parser, which means it's using parse_bits;
+ // if it's a uint parser, it can't be uint64
+ assert_message(p->fn == parse_bits, "int_range requires an integer parser");
+ assert_message(!(env->signed) ? (env->length < 64) : true, "int_range can't use a uint64 parser");
+
+ range_t *env = g_new(range_t, 1);
+ env->lower = lower;
+ env->upper = upper;
+ parser_t *ret = g_new(parser_t, 1);
+ ret->fn = parse_int_range;
+ ret->env = (void*)env;
+ return ret;
+}
+
const parser_t* not_in(const uint8_t *options, int count) {
parser_t *ret = g_new(parser_t, 1);
charset cs = new_charset();
@@ -829,7 +845,7 @@ typedef struct {
static parse_result_t* parse_attr_bool(void *env, parse_state_t *state) {
attr_bool_t *a = (attr_bool_t*)env;
parse_result_t *res = do_parse(a->p, state);
- if (res) {
+ if (res && res->ast) {
if (a->pred(res))
return res;
else
View
14 src/hammer.h
@@ -129,7 +129,14 @@ const parser_t* ch(const uint8_t c);
*
* Result token type: TT_UINT
*/
-const parser_t* range(const uint8_t lower, const uint8_t upper);
+const parser_t* ch_range(const uint8_t lower, const uint8_t upper);
+
+/**
+ * Given an integer parser, p, and two integer bounds, lower and upper,
+ * returns a parser that parses an integral value within the range
+ * [lower, upper] (inclusive).
+ */
+const parser_t* int_range(const parser_t *p, const int64_t lower, const int64_t upper);
/**
* Returns a parser that parses the specified number of bits. sign ==
@@ -359,8 +366,13 @@ const parser_t* length_value(const parser_t* length, const parser_t* value);
* This parser attaches a predicate function, which returns true or
* false, to a parser. The function is evaluated over the parser's
* result.
+ *
* The parse only succeeds if the attribute function returns true.
*
+ * attr_bool will check whether p's result exists and whether p's
+ * result AST exists; you do not need to check for this in your
+ * predicate function.
+ *
* Result token type: p's result type if pred succeeded, NULL otherwise.
*/
const parser_t* attr_bool(const parser_t* p, predicate_t pred);

0 comments on commit 947b67b

Please sign in to comment.
Something went wrong with that request. Please try again.