DNS, refactored and compiling. Needs struct-building action written still.
This commit is contained in:
parent
40be28fb7e
commit
4226d67c26
7 changed files with 354 additions and 69 deletions
71
examples/dns_common.c
Normal file
71
examples/dns_common.c
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#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(parse_result_t *p) {
|
||||
if (TT_SEQUENCE != p->ast->token_type)
|
||||
return false;
|
||||
return (64 > p->ast->seq->used);
|
||||
}
|
||||
|
||||
const parser_t* init_domain() {
|
||||
static const parser_t *domain = NULL;
|
||||
if (domain)
|
||||
return domain;
|
||||
|
||||
const parser_t *letter = choice(ch_range('a', 'z'),
|
||||
ch_range('A', 'Z'),
|
||||
NULL);
|
||||
|
||||
const parser_t *let_dig = choice(letter,
|
||||
ch_range('0', '9'),
|
||||
NULL);
|
||||
|
||||
const parser_t *ldh_str = many1(choice(let_dig,
|
||||
ch('-'),
|
||||
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 ...
|
||||
* parser_t *indirect_subdomain = indirect();
|
||||
* const parser_t *subdomain = choice(label,
|
||||
* sequence(indirect_subdomain,
|
||||
* ch('.'),
|
||||
* label,
|
||||
* NULL),
|
||||
* NULL);
|
||||
* bind_indirect(indirect_subdomain, subdomain);
|
||||
*
|
||||
* ... but this is easier and equivalent
|
||||
*/
|
||||
|
||||
const parser_t *subdomain = sepBy1(label, ch('.'));
|
||||
|
||||
domain = choice(subdomain,
|
||||
ch(' '),
|
||||
NULL);
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
const parser_t* init_character_string() {
|
||||
static const parser_t *cstr = NULL;
|
||||
if (cstr)
|
||||
return cstr;
|
||||
|
||||
cstr = length_value(uint8(), uint8());
|
||||
|
||||
return cstr;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue