DNS, refactored and compiling. Needs struct-building action written still.

This commit is contained in:
Meredith L. Patterson 2012-05-24 18:44:48 +02:00
parent 40be28fb7e
commit 4226d67c26
7 changed files with 354 additions and 69 deletions

View file

@ -1,4 +1,5 @@
#include "../src/hammer.h"
#include "dns_common.h"
#define false 0
#define true 1
@ -9,15 +10,6 @@ bool is_zero(parse_result_t *p) {
return (0 == p->ast->uint);
}
/**
* 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);
}
/**
* Every DNS message should have QDCOUNT entries in the question
* section, and ANCOUNT+NSCOUNT+ARCOUNT resource records.
@ -40,25 +32,27 @@ bool validate_dns(parse_result_t *p) {
return true;
}
parser_t* init_parser() {
const parser_t* init_parser() {
static parser_t *dns_message = NULL;
if (dns_message)
return dns_message;
const parser_t *domain = init_domain();
const parser_t *dns_header = sequence(bits(16, false), // ID
bits(1, false), // QR
bits(4, false), // opcode
bits(1, false), // AA
bits(1, false), // TC
bits(1, false), // RD
bits(1, false), // RA
ignore(attr_bool(bits(3, false), is_zero)), // Z
bits(4, false), // RCODE
uint16(), // QDCOUNT
uint16(), // ANCOUNT
uint16(), // NSCOUNT
uint16(), // ARCOUNT
NULL);
bits(1, false), // QR
bits(4, false), // opcode
bits(1, false), // AA
bits(1, false), // TC
bits(1, false), // RD
bits(1, false), // RA
ignore(attr_bool(bits(3, false), is_zero)), // Z
bits(4, false), // RCODE
uint16(), // QDCOUNT
uint16(), // ANCOUNT
uint16(), // NSCOUNT
uint16(), // ARCOUNT
NULL);
const parser_t *type = int_range(uint16(), 1, 16);
@ -79,50 +73,12 @@ parser_t* init_parser() {
qtype, // QTYPE
qclass, // QCLASS
NULL);
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('.'));
const parser_t *domain = choice(subdomain,
ch(' '),
NULL);
const parser_t *dns_rr = sequence(domain, // NAME
uint16(), // TYPE
uint16(), // CLASS
uint32(), // TTL
const parser_t *dns_rr = sequence(domain, // NAME
type, // TYPE
class, // CLASS
uint32(), // TTL
length_value(uint16(), uint8()), // RDLENGTH+RDATA
NULL);
@ -136,3 +92,7 @@ parser_t* init_parser() {
return dns_message;
}
int main(int argc, char** argv) {
return 0;
}