summaryrefslogtreecommitdiff
path: root/src/packet/question.c
blob: c2807d0aa25b4c2de4699af9abacaa16128acdba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
    );
}