-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve.c
executable file
·206 lines (177 loc) · 4.1 KB
/
resolve.c
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>
#include "resolve.h"
#include "dns.h"
#include "manager.h"
#include "misc.h"
#include "label.h"
dns_node *root_node;
dns_node *find_node (dns_node *root, dns_label *name, uint8_t mode, dns_label **partial)
{
int i;
dns_node *res = NULL;
for (i = 0; i < root->n_down; i++) {
if (!strcasecmp (root->down[i]->label, name->label)) {
if (!name->down) {
res = root->down[i];
break;
}
else {
res = find_node (root->down[i], name->down, mode, partial);
if (partial && !res) {
*partial = name;
res = root->down[i];
break;
}
}
}
}
return res;
}
dns_resource_record *resolve_name (dns_label *name, dns_answer *answer, uint16_t *n)
{
dns_node *find = find_node (root_node, dns_label_head (name), FIND_TOTAL, NULL);
if (find) {
*n = find->n_records;
return find->records;
}
*n = 0;
return NULL;
}
void free_record (dns_resource_record *r)
{
if (r->name)
free (r->name);
if (r->body) {
if (r->body->rdata)
free (r->body->rdata);
free (r->body);
}
}
void free_all_nodes (dns_node *root)
{
int i;
if (root) {
if (root->label)
free (root->label);
if (root->down) {
for (i = 0; i < root->n_down; i++)
free_all_nodes (root->down[i]);
free (root->down);
}
if (root->records) {
for (i = 0; i < root->n_records; i++)
free_record (&root->records[i]);
free (root->records);
}
free (root);
}
}
void add_record (char *name, int type, int class, char *content)
{
dns_label *partial = NULL;
dns_label *label = dns_label_from_string (name);
dns_node *x = find_node (root_node, label->down, FIND_PARTIAL, &partial);
dns_node *new = NULL;
dns_resource_record *rr;
if (!x)
x = root_node;
if (!partial)
partial = label->down;
else if (partial->down)
partial = partial->down;
while (partial) {
new = create_node (partial->label);
add_node (x, new);
partial = partial->down;
x = new;
}
rr = new_rr (name, type, class, content);
insert_rr_in_node (x, rr);
free (rr);
free_dns_name (label);
}
void add_node (dns_node *parent, dns_node *child)
{
if (!parent)
return;
parent->down = realloc (parent->down, (++parent->n_down + 1) * sizeof (dns_node));
parent->down[parent->n_down - 1] = child;
parent->down[parent->n_down] = NULL;
child->up = parent;
return;
}
dns_node *create_node (char *label)
{
dns_node *new = malloc (sizeof (dns_node));
new->up = NULL;
new->down = NULL;
new->n_down = 0;
new->n_records = 0;
new->records = NULL;
if (label) {
new->label = malloc (strlen (label) + 1);
strcpy (new->label, label);
}
else
new->label = NULL;
return new;
}
dns_resource_record *new_rr (char *name, int type, int class, char *content)
{
dns_resource_record *rr;
unsigned int len = 0;
rr = malloc (sizeof (dns_resource_record));
rr->body = malloc (sizeof (dns_resource_record_body));
rr->name = dns_string_from_char (name);
rr->body->class = class;
rr->body->type = type;
rr->body->ttl = htons (400);
if (content) {
switch (type) {
case DNS_TYPE_A:
rr->body->rdata = str2ip (content);
len = 4;
break;
case DNS_TYPE_CNAME:
rr->body->rdata = (char *)dns_string_from_char (content);
len = strlen (rr->body->rdata)+1;
break;
default:
printf ("Not implemented\n");
}
}
else
rr->body->rdata = NULL;
rr->body->rdlength = htons (len);
return rr;
}
dns_string *dns_string_from_char (char *str)
{
char *res = NULL;
uint8_t len = 0;
uint32_t total = 0;
uint32_t s_len = strlen (str), i = 0, j = 0;
while (i < s_len) {
while (i < s_len && str[i] && str[i] != '.')
i++;
len = i - j;
res = realloc (res, total + len + 1);
((dns_string *) (res + total))->len = len;
memcpy (&res[total+1], &str[j], len);
total += len + 1;
j = ++i;
}
res = realloc (res,++total);
res[total-1] = 0x00;
return (dns_string *) res;
}
void insert_rr_in_node (dns_node *node, dns_resource_record *rr)
{
if (!node || !rr)
return;
node->records = realloc (node->records, ++node->n_records * sizeof (dns_resource_record));
memcpy (&node->records[node->n_records - 1], rr, sizeof (dns_resource_record));
}