diff options
Diffstat (limited to 'src/packet/question.c')
-rw-r--r-- | src/packet/question.c | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/packet/question.c b/src/packet/question.c new file mode 100644 index 0000000..c2807d0 --- /dev/null +++ b/src/packet/question.c @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "question.h" +#include "buffer.h" +#include "record.h" +#include "../io/log.h" + +void read_question(PacketBuffer* buffer, Question* question) { + buffer_read_qname(buffer, &question->domain); + + uint16_t qtype_num = buffer_read_short(buffer); + record_from_id(qtype_num, &question->qtype); + question->cls = buffer_read_short(buffer); + + INIT_LOG_BUFFER(log) + LOGONLY(print_question(question, log);) + TRACE("Reading question: %s", log); +} + +void write_question(PacketBuffer* buffer, Question* question) { + buffer_write_qname(buffer, question->domain); + + uint16_t id = record_to_id(question->qtype); + buffer_write_short(buffer, id); + + buffer_write_short(buffer, question->cls); + + INIT_LOG_BUFFER(log) + LOGONLY(print_question(question, log);) + TRACE("Writing question: %s", log); +} + +void free_question(Question* question) { + free(question->domain); +} + +void print_question(Question* question, char* buffer) { + INIT_LOG_BOUNDS + switch (question->cls) { + case 1: + APPEND(buffer, "IN ");; + break; + case 3: + APPEND(buffer, "CH "); + break; + case 4: + APPEND(buffer, "HS "); + break; + default: + APPEND(buffer, "?? "); + break; + } + switch(question->qtype) { + case UNKOWN: + APPEND(buffer, "UNKOWN "); + break; + case A: + APPEND(buffer, "A "); + break; + case NS: + APPEND(buffer, "NS "); + break; + case CNAME: + APPEND(buffer, "CNAME "); + break; + case SOA: + APPEND(buffer, "SOA "); + break; + case PTR: + APPEND(buffer, "PTR "); + break; + case MX: + APPEND(buffer, "MX "); + break; + case TXT: + APPEND(buffer, "TXT "); + break; + case AAAA: + APPEND(buffer, "AAAA "); + break; + case SRV: + APPEND(buffer, "SRV "); + break; + case CAA: + APPEND(buffer, "CAA "); + break; + break; + } + APPEND(buffer, "%.*s", + question->domain[0], + question->domain + 1 + ); +}
\ No newline at end of file |