move get_domain logic into an action on the domain parser

This commit is contained in:
Sven M. Hallberg 2013-01-16 16:08:30 +01:00
parent 16b1e02baa
commit 0083031d6f
3 changed files with 35 additions and 26 deletions

View file

@ -43,30 +43,9 @@ bool validate_dns(HParseResult *p) {
} }
char* get_domain(const HParsedToken *t) { char* get_domain(const HParsedToken *t) {
switch(t->token_type) { assert(t != NULL);
case TT_UINT: assert(t->token_type == (HTokenType)TT_dns_domain);
return " "; return t->user;
case TT_SEQUENCE:
{
// Sequence of subdomains separated by "."
// Each subdomain is a label, which can be no more than 63 chars.
char *ret = h_arena_malloc(t->seq->arena, 64*t->seq->used);
size_t count = 0;
for (size_t i=0; i<t->seq->used; ++i) {
HParsedToken *tmp = t->seq->elements[i];
for (size_t j=0; j<tmp->seq->used; ++j) {
ret[count] = tmp->seq->elements[i]->uint;
++count;
}
ret[count] = '.';
++count;
}
ret[count-1] = '\x00';
return ret;
}
default:
return NULL;
}
} }
uint8_t* get_cs(const HCountedArray *arr) { uint8_t* get_cs(const HCountedArray *arr) {

View file

@ -6,7 +6,8 @@ enum DNSTokenType_ {
TT_dns_label, TT_dns_label,
TT_dns_qname, TT_dns_qname,
TT_dns_question, TT_dns_question,
TT_dns_rr TT_dns_rr,
TT_dns_domain
}; };
typedef struct dns_header { typedef struct dns_header {
@ -98,6 +99,8 @@ typedef struct dns_rr {
}; };
} dns_rr_t; } dns_rr_t;
typedef char *dns_domain_t;
typedef struct dns_message { typedef struct dns_message {
dns_header_t header; dns_header_t header;
dns_question_t *questions; dns_question_t *questions;

View file

@ -1,5 +1,6 @@
#include "../src/hammer.h" #include "../src/hammer.h"
#include "dns_common.h" #include "dns_common.h"
#include "dns.h"
#define false 0 #define false 0
#define true 1 #define true 1
@ -13,6 +14,32 @@ bool validate_label(HParseResult *p) {
return (64 > p->ast->seq->used); return (64 > p->ast->seq->used);
} }
const HParsedToken* act_domain(const HParseResult *p) {
switch(p->ast->token_type) {
case TT_UINT:
return H_MAKE_TOKEN(dns_domain, " ");
case TT_SEQUENCE:
{
// Sequence of subdomains separated by "."
// Each subdomain is a label, which can be no more than 63 chars.
char *ret = h_arena_malloc(p->arena, 64*p->ast->seq->used);
size_t count = 0;
for (size_t i=0; i<p->ast->seq->used; ++i) {
HParsedToken *tmp = p->ast->seq->elements[i];
for (size_t j=0; j<tmp->seq->used; ++j) {
ret[count] = tmp->seq->elements[i]->uint;
++count;
}
ret[count] = '.';
++count;
}
ret[count-1] = '\x00';
return H_MAKE_TOKEN(dns_domain, ret);
}
default:
return NULL;
}
}
const HParser* init_domain() { const HParser* init_domain() {
static const HParser *ret = NULL; static const HParser *ret = NULL;
@ -29,7 +56,7 @@ const HParser* init_domain() {
NULL), NULL),
validate_label)); validate_label));
H_RULE (subdomain, h_sepBy1(label, h_ch('.'))); H_RULE (subdomain, h_sepBy1(label, h_ch('.')));
H_RULE (domain, h_choice(subdomain, h_ch(' '), NULL)); H_ARULE(domain, h_choice(subdomain, h_ch(' '), NULL));
ret = domain; ret = domain;
return ret; return ret;