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
|
#include "client.h"
#include "../packet/question.h"
#include "addr.h"
#include "binding.h"
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <sys/time.h>
void resolve_default_server(IpAddr* addr) {
res_init();
addr->data.v4 = _res.nsaddr_list[0].sin_addr;
addr->type = V4;
}
static void push_question(Question** buf, Question q, uint8_t* amount, uint8_t* capacity) {
if (*amount >= *capacity) {
*capacity *= 2;
*buf = realloc(*buf, sizeof(Question) * *capacity);
}
(*buf)[*amount] = q;
(*amount)++;
}
void resolve_questions(int argc, char** argv, uint8_t* len, Question** questions) {
assert(argc > 0);
uint8_t amount = 0;
uint8_t capacity = 1;
Question* buf = malloc(sizeof(Question) * capacity);
Question q;
RecordType t;
if (argc == 1 && str_to_qtype(argv[0], &t)) {
create_question("", NS, &q);
push_question(&buf, q, &amount, &capacity);
*questions = buf;
*len = 1;
return;
}
for (int i = 0; i < argc; i++) {
if (str_to_qtype(argv[i], &t)) {
if (i + 1 == argc) break;
create_question(argv[i+1], t, &q);
i++;
} else if (i + 1 < argc && str_to_qtype(argv[i+1], &t)){
create_question(argv[i], t, &q);
i++;
} else {
create_question(argv[i], A, &q);
}
push_question(&buf, q, &amount, &capacity);
}
*questions = buf;
*len = amount;
}
static void print_result(Packet* packet, const char* type, uint32_t ms) {
printf(">> Recieved response\n");
printf("Id: %hu, Code: %s\n",
packet->header.id,
str_from_code(packet->header.rescode)
);
printf("Questions: %hu, Answers: %hu, Authorities: %hu, Resources: %hu\n",
packet->header.questions,
packet->header.answers,
packet->header.authoritative_entries,
packet->header.resource_entries
);
if (packet->header.questions > 0) {
printf("\n>> Question Section\n");
for (uint16_t i = 0; i < packet->header.questions; i++) {
print_question(&packet->questions[i]);
}
}
if (packet->header.answers > 0) {
printf("\n>> Answer Section\n");
for (uint16_t i = 0; i < packet->header.answers; i++) {
print_record(&packet->answers[i]);
}
}
if (packet->header.authoritative_entries > 0) {
printf("\n>> Authority Section\n");
for (uint16_t i = 0; i < packet->header.authoritative_entries; i++) {
print_record(&packet->authorities[i]);
}
}
if (packet->header.resource_entries > 0) {
printf("\n>> Resource Section\n");
for (uint16_t i = 0; i < packet->header.resource_entries; i++) {
print_record(&packet->resources[i]);
}
}
printf("\n>> Query time: %ums (%s)\n", ms, type);
}
void resolve(IpAddr server, Options options, Question* questions, uint8_t len) {
struct timeval stop, start;
gettimeofday(&start, NULL);
SocketAddr addr;
create_socket_addr(options.port, server, &addr);
Packet req;
memset(&req, 0, sizeof(Packet));
srand(time(NULL));
req.header.id = rand() % 65536;
req.header.questions = len;
req.header.recursion_desired = true;
req.questions = questions;
Connection conn;
if (options.force_tcp) goto tcp;
if (!create_request(UDP, &addr, &conn)) {
printf("error: failed to create udp request: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
Packet res;
if (!request_packet(&conn, &req, &res)) {
free_request(&conn);
free_packet(&req);
printf("error: failed to request udp packet: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
free_request(&conn);
if (!res.header.truncated_message) {
gettimeofday(&stop, NULL);
uint32_t ms = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;
print_result(&res, "UDP", ms / 1000);
free_packet(&req);
free_packet(&res);
return;
}
free_packet(&res);
printf("Response truncated, retrying in TCP...\n");
tcp:
if (!create_request(TCP, &addr, &conn)) {
printf("error: failed to create tcp request: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
if (!request_packet(&conn, &req, &res)) {
free_request(&conn);
printf("error: failed to request tcp packet: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
gettimeofday(&stop, NULL);
uint32_t ms = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;
print_result(&res, "TCP", ms / 1000);
free_request(&conn);
free_packet(&req);
free_packet(&res);
}
|