summaryrefslogtreecommitdiff
path: root/src/packet/packet.c
blob: 83dc5c34dd45163d6c30fab2a7fde03bf25f9379 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "packet.h"
#include "buffer.h"
#include "header.h"
#include "question.h"
#include "record.h"

void read_packet(PacketBuffer* buffer, Packet* packet) {
    read_header(buffer, &packet->header);
    
    packet->questions = malloc(sizeof(Question) * packet->header.questions);
    for(uint16_t i = 0; i < packet->header.questions; i++) {
        if (!read_question(buffer, &packet->questions[i])) {
            i--;
            packet->header.questions--;
        }
    }

    packet->answers = malloc(sizeof(Record) * packet->header.answers);
    for(uint16_t i = 0; i < packet->header.answers; i++) {
        if (!read_record(buffer, &packet->answers[i])) {
            i--;
            packet->header.answers--;
        }
    }

    packet->authorities = malloc(sizeof(Record) * packet->header.authoritative_entries);
    for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
        if (!read_record(buffer, &packet->authorities[i])) {
            i--;
            packet->header.authoritative_entries--;
        }
    }

    packet->resources = malloc(sizeof(Record) * packet->header.resource_entries);
    for(uint16_t i = 0; i < packet->header.resource_entries; i++) {
        if (!read_record(buffer, &packet->resources[i])) {
            i--;
            packet->header.resource_entries--;
        }
    }
}

void write_packet(PacketBuffer* buffer, Packet* packet) {
    write_header(buffer, &packet->header);
    
    for(uint16_t i = 0; i < packet->header.questions; i++) {
        write_question(buffer, &packet->questions[i]);
    }

    for(uint16_t i = 0; i < packet->header.answers; i++) {
        write_record(buffer, &packet->answers[i]);
    }

    for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
        write_record(buffer, &packet->authorities[i]);
    }

    for(uint16_t i = 0; i < packet->header.resource_entries; i++) {
        write_record(buffer, &packet->resources[i]);
    }
}

void free_packet(Packet* packet) {
    
    for(uint16_t i = 0; i < packet->header.questions; i++) {
        free_question(&packet->questions[i]);
    }
    free(packet->questions);

    for(uint16_t i = 0; i < packet->header.answers; i++) {
        free_record(&packet->answers[i]);
    }
    free(packet->answers);

    for(uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
        free_record(&packet->authorities[i]);
    }
    free(packet->authorities);

    for(uint16_t i = 0; i < packet->header.resource_entries; i++) {
        free_record(&packet->resources[i]);
    }
    free(packet->resources);
}

bool get_random_a(Packet* packet, IpAddr* addr) {
    for (uint16_t i = 0; i < packet->header.answers; i++) {
        Record record = packet->answers[i];
        if (record.type == A) {
            create_ip_addr(record.data.a.addr, addr);
            return true;
        } else if (record.type == AAAA) {
            create_ip_addr6(record.data.aaaa.addr, addr);
            return true;
        }
    }
    return false;
}

static bool ends_with(uint8_t* full, uint8_t* end) {
    uint8_t check = end[0];
    uint8_t len = full[0];

    if (check > len) {
        return false;
    }

    for(uint8_t i = 0; i < check; i++) {
        if (end[check - 1 - i] != full[len - 1 - i]) {
            return false;
        }
    }
    
    return true;
}

static bool equals(uint8_t* a, uint8_t* b) {
    if (a[0] != b[0]) {
        return false;
    }

    for(uint8_t i = 1; i < a[0] + 1; i++) {
        if(a[i] != b[i]) {
            return false;
        }
    }

    return true;
}

bool get_resolved_ns(Packet* packet, uint8_t* qname, IpAddr* addr) {
    for (uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
        Record record = packet->authorities[i];
        if (record.type != NS) {
            continue;
        }

        if(!ends_with(qname, record.domain)) {
            continue;
        }

        for (uint16_t i = 0; i < packet->header.resource_entries; i++) {
            Record resource = packet->resources[i];
            if (!equals(record.data.ns.host, resource.domain)) {
                continue;
            }

            if (resource.type == A) {
                create_ip_addr(record.data.a.addr, addr);
                return true;
            } else if (resource.type == AAAA) {
                create_ip_addr6(record.data.aaaa.addr, addr);
                return true;
            }
        }
    }
    return false;
}

bool get_unresoled_ns(Packet* packet, uint8_t* qname, Question* question) {
    for (uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
        Record record = packet->authorities[i];
        if (record.type != NS) {
            continue;
        }

        if(!ends_with(qname, record.domain)) {
            continue;
        }

        uint8_t* host = record.data.ns.host;

        question->qtype = NS;
        question->domain = malloc(host[0] + 1);
        memcpy(question->domain, host, host[0] + 1);
        return true;
    }
    return false;
}