hammer/examples/dns_common.c
2013-01-16 15:40:10 +01:00

46 lines
1.1 KiB
C

#include "../src/hammer.h"
#include "dns_common.h"
#define false 0
#define true 1
/**
* A label can't be more than 63 characters.
*/
bool validate_label(HParseResult *p) {
if (TT_SEQUENCE != p->ast->token_type)
return false;
return (64 > p->ast->seq->used);
}
const HParser* init_domain() {
static const HParser *ret = NULL;
if (ret)
return ret;
H_RULE (letter, h_choice(h_ch_range('a','z'), h_ch_range('A','Z'), NULL));
H_RULE (let_dig, h_choice(letter, h_ch_range('0','9'), NULL));
H_RULE (ldh_str, h_many1(h_choice(let_dig, h_ch('-'), NULL)));
H_RULE (label, h_attr_bool(h_sequence(letter,
h_optional(h_sequence(h_optional(ldh_str),
let_dig,
NULL)),
NULL),
validate_label));
H_RULE (subdomain, h_sepBy1(label, h_ch('.')));
H_RULE (domain, h_choice(subdomain, h_ch(' '), NULL));
ret = domain;
return ret;
}
const HParser* init_character_string() {
static const HParser *cstr = NULL;
if (cstr)
return cstr;
cstr = h_length_value(h_uint8(), h_uint8());
return cstr;
}