Continuing with DNS. Added some checks to attr_bool, and explained checks in docs.

This commit is contained in:
Meredith L. Patterson 2012-05-24 11:01:18 +02:00
parent 05ef58b958
commit 947b67b5a5
3 changed files with 86 additions and 23 deletions

View file

@ -1,16 +1,48 @@
#include "../hammer.h" #include "../hammer.h"
bool is_zero(parse_result_t *p) { bool is_zero(parse_result_t *p) {
if (TT_UINT != p->ast->token_type)
return 0;
return (0 == p->ast->uint); return (0 == p->ast->uint);
} }
bool validate_dns(parse_result_t *p) { /**
* 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);
} }
int main(int argc, char **argv) { /**
* 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;
}
const parser_t dns_header = sequence(bits(16, false), // ID 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
bits(1, false), // QR bits(1, false), // QR
bits(4, false), // opcode bits(4, false), // opcode
bits(1, false), // AA bits(1, false), // AA
@ -42,11 +74,12 @@ int main(int argc, char **argv) {
ch('-'), ch('-'),
NULL)); NULL));
const parser_t *label = sequence(letter, const parser_t *label = attr_bool(sequence(letter,
optional(sequence(optional(ldh_str), optional(sequence(optional(ldh_str),
let_dig, let_dig,
NULL)), NULL)),
NULL); NULL),
validate_label);
/** /**
* You could write it like this ... * You could write it like this ...
@ -76,10 +109,12 @@ int main(int argc, char **argv) {
NULL); NULL);
const parser_t *dns_message = attr_bool(sequence(dns_header, dns_message = attr_bool(sequence(dns_header,
dns_question, many(dns_question),
many(dns_rr), many(dns_rr),
end_p(), end_p(),
NULL), NULL),
validate_dns); validate_dns);
return dns_message;
} }

View file

@ -311,11 +311,6 @@ const parser_t* ch(const uint8_t c) {
return (const parser_t*)ret; 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) { static parse_result_t* parse_whitespace(void* env, parse_state_t *state) {
char c; char c;
input_stream_t bak; input_stream_t bak;
@ -374,7 +369,7 @@ static parse_result_t* parse_charset(void *env, parse_state_t *state) {
return NULL; 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); parser_t *ret = g_new(parser_t, 1);
charset cs = new_charset(); charset cs = new_charset();
for (int i = 0; i < 256; i++) 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; 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) { const parser_t* not_in(const uint8_t *options, int count) {
parser_t *ret = g_new(parser_t, 1); parser_t *ret = g_new(parser_t, 1);
charset cs = new_charset(); charset cs = new_charset();
@ -829,7 +845,7 @@ typedef struct {
static parse_result_t* parse_attr_bool(void *env, parse_state_t *state) { static parse_result_t* parse_attr_bool(void *env, parse_state_t *state) {
attr_bool_t *a = (attr_bool_t*)env; attr_bool_t *a = (attr_bool_t*)env;
parse_result_t *res = do_parse(a->p, state); parse_result_t *res = do_parse(a->p, state);
if (res) { if (res && res->ast) {
if (a->pred(res)) if (a->pred(res))
return res; return res;
else else

View file

@ -129,7 +129,14 @@ const parser_t* ch(const uint8_t c);
* *
* Result token type: TT_UINT * 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 == * 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 * This parser attaches a predicate function, which returns true or
* false, to a parser. The function is evaluated over the parser's * false, to a parser. The function is evaluated over the parser's
* result. * result.
*
* The parse only succeeds if the attribute function returns true. * 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. * Result token type: p's result type if pred succeeded, NULL otherwise.
*/ */
const parser_t* attr_bool(const parser_t* p, predicate_t pred); const parser_t* attr_bool(const parser_t* p, predicate_t pred);