1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
|
* |
4 |
michael |
9101 |
* Copyright (c) 1997-2020 ircd-hybrid development team |
5 |
michael |
2916 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
michael |
4565 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
michael |
2916 |
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
3322 |
/*! \file res.c |
23 |
michael |
2916 |
* \brief ircd resolver functions |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
/* |
28 |
michael |
4311 |
* A rewrite of Darren Reed's original res.c As there is nothing |
29 |
|
|
* left of Darren's original code, this is now licensed by the hybrid group. |
30 |
adx |
30 |
* (Well, some of the function names are the same, and bits of the structs..) |
31 |
|
|
* You can use it where it is useful, free even. Buy us a beer and stuff. |
32 |
|
|
* |
33 |
|
|
* The authors takes no responsibility for any damage or loss |
34 |
|
|
* of property which results from the use of this software. |
35 |
|
|
* |
36 |
|
|
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
37 |
|
|
* added callbacks and reference counting of returned hostents. |
38 |
|
|
* --Bleep (Thomas Helvey <tomh@inxpress.net>) |
39 |
|
|
* |
40 |
|
|
* This was all needlessly complicated for irc. Simplified. No more hostent |
41 |
michael |
4311 |
* All we really care about is the IP -> hostname mappings. That's all. |
42 |
adx |
30 |
* |
43 |
|
|
* Apr 28, 2003 --cryogen and Dianora |
44 |
|
|
*/ |
45 |
|
|
|
46 |
|
|
#include "stdinc.h" |
47 |
michael |
1011 |
#include "list.h" |
48 |
adx |
30 |
#include "event.h" |
49 |
|
|
#include "irc_string.h" |
50 |
|
|
#include "ircd.h" |
51 |
michael |
982 |
#include "rng_mt.h" |
52 |
adx |
30 |
#include "fdlist.h" |
53 |
|
|
#include "s_bsd.h" |
54 |
michael |
3347 |
#include "misc.h" |
55 |
michael |
3322 |
#include "res.h" |
56 |
|
|
#include "reslib.h" |
57 |
michael |
8385 |
#include "memory.h" |
58 |
adx |
30 |
|
59 |
|
|
#if (CHAR_BIT != 8) |
60 |
michael |
2916 |
#error this code needs to be able to address individual octets |
61 |
adx |
30 |
#endif |
62 |
|
|
|
63 |
michael |
4461 |
static void res_readreply(fde_t *, void *); |
64 |
adx |
30 |
|
65 |
michael |
4311 |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
66 |
|
|
#define AR_TTL 600 /**< TTL in seconds for dns cache entries */ |
67 |
adx |
30 |
|
68 |
michael |
4311 |
/* |
69 |
|
|
* RFC 1104/1105 wasn't very helpful about what these fields |
70 |
adx |
30 |
* should be named, so for now, we'll just name them this way. |
71 |
michael |
4311 |
* We probably should look at what named calls them or something. |
72 |
adx |
30 |
*/ |
73 |
|
|
#define TYPE_SIZE (size_t)2 |
74 |
|
|
#define CLASS_SIZE (size_t)2 |
75 |
|
|
#define TTL_SIZE (size_t)4 |
76 |
|
|
#define RDLENGTH_SIZE (size_t)2 |
77 |
|
|
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
78 |
|
|
|
79 |
michael |
2916 |
struct reslist |
80 |
adx |
30 |
{ |
81 |
michael |
4423 |
dlink_node node; /**< Doubly linked list node. */ |
82 |
michael |
7595 |
unsigned int id; /**< Request ID (from request header). */ |
83 |
michael |
4423 |
char type; /**< Current request type. */ |
84 |
|
|
char retries; /**< Retry counter */ |
85 |
|
|
unsigned int sends; /**< Number of sends (>1 means resent). */ |
86 |
michael |
7330 |
uintmax_t sentat; /**< Timestamp we last sent this request. */ |
87 |
|
|
uintmax_t timeout; /**< When this request times out. */ |
88 |
michael |
4423 |
struct irc_ssaddr addr; /**< Address for this request. */ |
89 |
|
|
char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */ |
90 |
|
|
size_t namelength; /**< Actual hostname length. */ |
91 |
|
|
dns_callback_fnc callback; /**< Callback function on completion. */ |
92 |
|
|
void *callback_ctx; /**< Context pointer for callback. */ |
93 |
adx |
30 |
}; |
94 |
|
|
|
95 |
michael |
8339 |
static fde_t *ResolverFileDescriptor; |
96 |
michael |
3235 |
static dlink_list request_list; |
97 |
adx |
30 |
|
98 |
|
|
|
99 |
michael |
4311 |
/* |
100 |
|
|
* rem_request - remove a request from the list. |
101 |
|
|
* This must also free any memory that has been allocated for |
102 |
|
|
* temporary storage of DNS results. |
103 |
|
|
*/ |
104 |
|
|
static void |
105 |
|
|
rem_request(struct reslist *request) |
106 |
|
|
{ |
107 |
|
|
dlinkDelete(&request->node, &request_list); |
108 |
michael |
8385 |
xfree(request); |
109 |
michael |
4311 |
} |
110 |
|
|
|
111 |
adx |
30 |
/* |
112 |
michael |
4311 |
* make_request - Create a DNS request record for the server. |
113 |
|
|
*/ |
114 |
|
|
static struct reslist * |
115 |
|
|
make_request(dns_callback_fnc callback, void *ctx) |
116 |
|
|
{ |
117 |
michael |
8385 |
struct reslist *request = xcalloc(sizeof(*request)); |
118 |
michael |
4311 |
|
119 |
michael |
8900 |
request->sentat = event_base->time.sec_monotonic; |
120 |
michael |
7597 |
request->retries = 2; |
121 |
|
|
request->timeout = 4; /* Start at 4 and exponential inc. */ |
122 |
|
|
request->callback = callback; |
123 |
michael |
4311 |
request->callback_ctx = ctx; |
124 |
|
|
|
125 |
|
|
dlinkAdd(request, &request->node, &request_list); |
126 |
|
|
return request; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
/* |
130 |
adx |
30 |
* int |
131 |
|
|
* res_ourserver(inp) |
132 |
|
|
* looks up "inp" in irc_nsaddr_list[] |
133 |
|
|
* returns: |
134 |
|
|
* 0 : not found |
135 |
|
|
* >0 : found |
136 |
|
|
* author: |
137 |
|
|
* paul vixie, 29may94 |
138 |
|
|
* revised for ircd, cryogen(stu) may03 |
139 |
|
|
*/ |
140 |
michael |
8658 |
static bool |
141 |
michael |
2916 |
res_ourserver(const struct irc_ssaddr *inp) |
142 |
adx |
30 |
{ |
143 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
144 |
michael |
8881 |
if (address_compare(inp, &irc_nsaddr_list[i], true) == true) |
145 |
|
|
return true; |
146 |
adx |
30 |
|
147 |
michael |
8658 |
return false; |
148 |
adx |
30 |
} |
149 |
|
|
|
150 |
|
|
/* |
151 |
|
|
* start_resolver - do everything we need to read the resolv.conf file |
152 |
|
|
* and initialize the resolver file descriptor if needed |
153 |
|
|
*/ |
154 |
|
|
static void |
155 |
|
|
start_resolver(void) |
156 |
|
|
{ |
157 |
|
|
irc_res_init(); |
158 |
|
|
|
159 |
michael |
8522 |
if (ResolverFileDescriptor == NULL) |
160 |
adx |
30 |
{ |
161 |
michael |
8339 |
int fd = comm_socket(irc_nsaddr_list[0].ss.ss_family, SOCK_DGRAM, 0); |
162 |
|
|
if (fd == -1) |
163 |
adx |
30 |
return; |
164 |
|
|
|
165 |
michael |
8658 |
ResolverFileDescriptor = fd_open(fd, true, "UDP resolver socket"); |
166 |
michael |
8339 |
|
167 |
adx |
30 |
/* At the moment, the resolver FD data is global .. */ |
168 |
michael |
8339 |
comm_setselect(ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
169 |
adx |
30 |
} |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
/* |
173 |
|
|
* restart_resolver - reread resolv.conf, reopen socket |
174 |
|
|
*/ |
175 |
|
|
void |
176 |
|
|
restart_resolver(void) |
177 |
|
|
{ |
178 |
michael |
8363 |
if (ResolverFileDescriptor) |
179 |
|
|
{ |
180 |
|
|
fd_close(ResolverFileDescriptor); |
181 |
|
|
ResolverFileDescriptor = NULL; |
182 |
|
|
} |
183 |
michael |
8339 |
|
184 |
adx |
30 |
start_resolver(); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
/* |
188 |
michael |
2916 |
* delete_resolver_queries - cleanup outstanding queries |
189 |
adx |
30 |
* for which there no longer exist clients or conf lines. |
190 |
|
|
*/ |
191 |
|
|
void |
192 |
michael |
992 |
delete_resolver_queries(const void *vptr) |
193 |
adx |
30 |
{ |
194 |
michael |
7914 |
dlink_node *node, *node_next; |
195 |
adx |
30 |
|
196 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
197 |
adx |
30 |
{ |
198 |
michael |
4815 |
struct reslist *request = node->data; |
199 |
michael |
997 |
|
200 |
|
|
if (request->callback_ctx == vptr) |
201 |
|
|
rem_request(request); |
202 |
adx |
30 |
} |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
/* |
206 |
|
|
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
207 |
|
|
* This should reflect /etc/resolv.conf. We will get responses |
208 |
|
|
* which arent needed but is easier than checking to see if nameserver |
209 |
michael |
4298 |
* isn't present. Returns number of messages successfully sent to |
210 |
adx |
30 |
* nameservers or -1 if no successful sends. |
211 |
|
|
*/ |
212 |
michael |
7595 |
static void |
213 |
michael |
4468 |
send_res_msg(const unsigned char *msg, int len, unsigned int rcount) |
214 |
adx |
30 |
{ |
215 |
michael |
3297 |
unsigned int max_queries = IRCD_MIN(irc_nscount, rcount); |
216 |
adx |
30 |
|
217 |
|
|
/* RES_PRIMARY option is not implemented |
218 |
|
|
* if (res.options & RES_PRIMARY || 0 == max_queries) |
219 |
|
|
*/ |
220 |
|
|
if (max_queries == 0) |
221 |
|
|
max_queries = 1; |
222 |
|
|
|
223 |
michael |
3297 |
for (unsigned int i = 0; i < max_queries; ++i) |
224 |
michael |
8339 |
sendto(ResolverFileDescriptor->fd, msg, len, 0, |
225 |
michael |
7595 |
(struct sockaddr *)&irc_nsaddr_list[i], irc_nsaddr_list[i].ss_len); |
226 |
adx |
30 |
} |
227 |
|
|
|
228 |
|
|
/* |
229 |
|
|
* find_id - find a dns request id (id is determined by dn_mkquery) |
230 |
|
|
*/ |
231 |
|
|
static struct reslist * |
232 |
michael |
7595 |
find_id(unsigned int id) |
233 |
adx |
30 |
{ |
234 |
michael |
7914 |
dlink_node *node; |
235 |
adx |
30 |
|
236 |
michael |
4815 |
DLINK_FOREACH(node, request_list.head) |
237 |
adx |
30 |
{ |
238 |
michael |
4815 |
struct reslist *request = node->data; |
239 |
adx |
30 |
|
240 |
|
|
if (request->id == id) |
241 |
michael |
1124 |
return request; |
242 |
adx |
30 |
} |
243 |
|
|
|
244 |
michael |
1124 |
return NULL; |
245 |
adx |
30 |
} |
246 |
|
|
|
247 |
michael |
2916 |
/* |
248 |
michael |
4311 |
* query_name - generate a query based on class, type and name. |
249 |
adx |
30 |
*/ |
250 |
michael |
4311 |
static void |
251 |
michael |
4468 |
query_name(const char *name, int query_class, int type, struct reslist *request) |
252 |
adx |
30 |
{ |
253 |
michael |
4468 |
unsigned char buf[MAXPACKET]; |
254 |
adx |
30 |
|
255 |
michael |
4311 |
memset(buf, 0, sizeof(buf)); |
256 |
adx |
30 |
|
257 |
michael |
9202 |
int request_len = irc_res_mkquery(name, query_class, type, buf, sizeof(buf)); |
258 |
|
|
if (request_len > 0) |
259 |
michael |
4311 |
{ |
260 |
|
|
HEADER *header = (HEADER *)buf; |
261 |
|
|
|
262 |
|
|
/* |
263 |
|
|
* Generate an unique id. |
264 |
|
|
* NOTE: we don't have to worry about converting this to and from |
265 |
|
|
* network byte order, the nameserver does not interpret this value |
266 |
|
|
* and returns it unchanged. |
267 |
|
|
*/ |
268 |
|
|
do |
269 |
|
|
header->id = (header->id + genrand_int32()) & 0xFFFF; |
270 |
|
|
while (find_id(header->id)); |
271 |
|
|
|
272 |
|
|
request->id = header->id; |
273 |
|
|
++request->sends; |
274 |
|
|
|
275 |
michael |
7595 |
send_res_msg(buf, request_len, request->sends); |
276 |
michael |
4311 |
} |
277 |
adx |
30 |
} |
278 |
|
|
|
279 |
|
|
/* |
280 |
|
|
* do_query_name - nameserver lookup name |
281 |
|
|
*/ |
282 |
|
|
static void |
283 |
michael |
992 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
284 |
adx |
30 |
struct reslist *request, int type) |
285 |
|
|
{ |
286 |
michael |
4408 |
char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1]; |
287 |
adx |
30 |
|
288 |
michael |
998 |
strlcpy(host_name, name, sizeof(host_name)); |
289 |
adx |
30 |
|
290 |
michael |
8522 |
if (request == NULL) |
291 |
adx |
30 |
{ |
292 |
michael |
7597 |
request = make_request(callback, ctx); |
293 |
|
|
request->type = type; |
294 |
michael |
4408 |
request->namelength = strlcpy(request->name, host_name, sizeof(request->name)); |
295 |
adx |
30 |
} |
296 |
|
|
|
297 |
|
|
request->type = type; |
298 |
|
|
query_name(host_name, C_IN, type, request); |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
/* |
302 |
|
|
* do_query_number - Use this to do reverse IP# lookups. |
303 |
|
|
*/ |
304 |
|
|
static void |
305 |
michael |
992 |
do_query_number(dns_callback_fnc callback, void *ctx, |
306 |
|
|
const struct irc_ssaddr *addr, |
307 |
adx |
30 |
struct reslist *request) |
308 |
|
|
{ |
309 |
michael |
3298 |
char ipbuf[128] = ""; |
310 |
michael |
985 |
|
311 |
adx |
30 |
if (addr->ss.ss_family == AF_INET) |
312 |
|
|
{ |
313 |
michael |
992 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
314 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
315 |
adx |
30 |
|
316 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
317 |
|
|
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
318 |
|
|
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
319 |
adx |
30 |
} |
320 |
|
|
else if (addr->ss.ss_family == AF_INET6) |
321 |
|
|
{ |
322 |
michael |
992 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
323 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
324 |
adx |
30 |
|
325 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), |
326 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
327 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
328 |
|
|
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
329 |
|
|
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
330 |
|
|
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
331 |
|
|
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
332 |
|
|
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
333 |
|
|
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
334 |
|
|
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
335 |
|
|
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
336 |
|
|
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
337 |
|
|
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
338 |
|
|
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
339 |
|
|
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
340 |
|
|
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
341 |
|
|
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
342 |
|
|
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
343 |
|
|
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
344 |
adx |
30 |
} |
345 |
michael |
4415 |
|
346 |
michael |
8522 |
if (request == NULL) |
347 |
adx |
30 |
{ |
348 |
michael |
7597 |
request = make_request(callback, ctx); |
349 |
adx |
30 |
request->type = T_PTR; |
350 |
michael |
9005 |
request->addr = *addr; |
351 |
adx |
30 |
} |
352 |
|
|
|
353 |
|
|
query_name(ipbuf, C_IN, T_PTR, request); |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
/* |
357 |
michael |
4311 |
* gethost_byname_type - get host address from name |
358 |
|
|
* |
359 |
adx |
30 |
*/ |
360 |
michael |
4311 |
void |
361 |
|
|
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
362 |
adx |
30 |
{ |
363 |
michael |
4311 |
assert(name); |
364 |
|
|
do_query_name(callback, ctx, name, NULL, type); |
365 |
|
|
} |
366 |
adx |
30 |
|
367 |
michael |
4311 |
/* |
368 |
|
|
* gethost_byaddr - get host name from address |
369 |
|
|
*/ |
370 |
|
|
void |
371 |
|
|
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
372 |
|
|
{ |
373 |
|
|
do_query_number(callback, ctx, addr, NULL); |
374 |
adx |
30 |
} |
375 |
|
|
|
376 |
|
|
static void |
377 |
|
|
resend_query(struct reslist *request) |
378 |
|
|
{ |
379 |
michael |
982 |
switch (request->type) |
380 |
adx |
30 |
{ |
381 |
|
|
case T_PTR: |
382 |
michael |
992 |
do_query_number(NULL, NULL, &request->addr, request); |
383 |
adx |
30 |
break; |
384 |
|
|
case T_A: |
385 |
michael |
4449 |
case T_AAAA: |
386 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, request->type); |
387 |
adx |
30 |
break; |
388 |
|
|
default: |
389 |
|
|
break; |
390 |
|
|
} |
391 |
|
|
} |
392 |
|
|
|
393 |
|
|
/* |
394 |
|
|
* proc_answer - process name server reply |
395 |
|
|
*/ |
396 |
michael |
8658 |
static bool |
397 |
michael |
4457 |
proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob) |
398 |
adx |
30 |
{ |
399 |
michael |
4408 |
char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */ |
400 |
michael |
4457 |
unsigned char *current = buf + sizeof(HEADER); /* current position in buf */ |
401 |
michael |
4456 |
unsigned int type = 0; /* answer type */ |
402 |
|
|
unsigned int rd_length = 0; |
403 |
adx |
30 |
struct sockaddr_in *v4; /* conversion */ |
404 |
|
|
struct sockaddr_in6 *v6; |
405 |
michael |
4415 |
|
406 |
adx |
30 |
for (; header->qdcount > 0; --header->qdcount) |
407 |
|
|
{ |
408 |
michael |
9202 |
int n = irc_dn_skipname(current, eob); |
409 |
|
|
if (n < 0) |
410 |
adx |
30 |
break; |
411 |
|
|
|
412 |
michael |
1124 |
current += (size_t)n + QFIXEDSZ; |
413 |
adx |
30 |
} |
414 |
|
|
|
415 |
|
|
/* |
416 |
michael |
4298 |
* Process each answer sent to us blech. |
417 |
adx |
30 |
*/ |
418 |
michael |
4457 |
while (header->ancount > 0 && current < eob) |
419 |
adx |
30 |
{ |
420 |
michael |
7870 |
--header->ancount; |
421 |
adx |
30 |
|
422 |
michael |
9202 |
int n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
423 |
michael |
4298 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
424 |
michael |
8658 |
return false; |
425 |
adx |
30 |
|
426 |
michael |
4408 |
hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0'; |
427 |
adx |
30 |
|
428 |
michael |
4298 |
/* |
429 |
|
|
* With Address arithmetic you have to be very anal |
430 |
adx |
30 |
* this code was not working on alpha due to that |
431 |
|
|
* (spotted by rodder/jailbird/dianora) |
432 |
|
|
*/ |
433 |
michael |
3298 |
current += (size_t)n; |
434 |
adx |
30 |
|
435 |
michael |
4457 |
if (!((current + ANSWER_FIXED_SIZE) < eob)) |
436 |
adx |
30 |
break; |
437 |
|
|
|
438 |
|
|
type = irc_ns_get16(current); |
439 |
|
|
current += TYPE_SIZE; |
440 |
|
|
current += CLASS_SIZE; |
441 |
|
|
current += TTL_SIZE; |
442 |
|
|
rd_length = irc_ns_get16(current); |
443 |
|
|
current += RDLENGTH_SIZE; |
444 |
|
|
|
445 |
michael |
2916 |
/* |
446 |
|
|
* Wait to set request->type until we verify this structure |
447 |
adx |
30 |
*/ |
448 |
|
|
switch (type) |
449 |
|
|
{ |
450 |
|
|
case T_A: |
451 |
|
|
if (request->type != T_A) |
452 |
michael |
8658 |
return false; |
453 |
adx |
30 |
|
454 |
|
|
/* |
455 |
michael |
3298 |
* Check for invalid rd_length or too many addresses |
456 |
adx |
30 |
*/ |
457 |
|
|
if (rd_length != sizeof(struct in_addr)) |
458 |
michael |
8658 |
return false; |
459 |
michael |
1124 |
|
460 |
michael |
4476 |
request->addr.ss_len = sizeof(struct sockaddr_in); |
461 |
adx |
30 |
v4 = (struct sockaddr_in *)&request->addr; |
462 |
|
|
v4->sin_family = AF_INET; |
463 |
|
|
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
464 |
michael |
8658 |
return true; |
465 |
adx |
30 |
break; |
466 |
michael |
8437 |
|
467 |
adx |
30 |
case T_AAAA: |
468 |
|
|
if (request->type != T_AAAA) |
469 |
michael |
8658 |
return false; |
470 |
michael |
1124 |
|
471 |
adx |
30 |
if (rd_length != sizeof(struct in6_addr)) |
472 |
michael |
8658 |
return false; |
473 |
michael |
1124 |
|
474 |
adx |
30 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
475 |
|
|
v6 = (struct sockaddr_in6 *)&request->addr; |
476 |
|
|
v6->sin6_family = AF_INET6; |
477 |
|
|
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
478 |
michael |
8658 |
return true; |
479 |
adx |
30 |
break; |
480 |
michael |
8437 |
|
481 |
adx |
30 |
case T_PTR: |
482 |
|
|
if (request->type != T_PTR) |
483 |
michael |
8658 |
return false; |
484 |
michael |
1124 |
|
485 |
michael |
4457 |
n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
486 |
michael |
4298 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
487 |
michael |
8658 |
return false; |
488 |
adx |
30 |
|
489 |
michael |
4408 |
request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name)); |
490 |
michael |
8658 |
return true; |
491 |
adx |
30 |
break; |
492 |
michael |
8437 |
|
493 |
michael |
4452 |
case T_CNAME: |
494 |
adx |
30 |
current += rd_length; |
495 |
|
|
break; |
496 |
michael |
8437 |
|
497 |
adx |
30 |
default: |
498 |
michael |
8658 |
return false; |
499 |
adx |
30 |
break; |
500 |
|
|
} |
501 |
|
|
} |
502 |
|
|
|
503 |
michael |
8658 |
return false; |
504 |
adx |
30 |
} |
505 |
|
|
|
506 |
|
|
/* |
507 |
|
|
* res_readreply - read a dns reply from the nameserver and process it. |
508 |
|
|
*/ |
509 |
|
|
static void |
510 |
michael |
8339 |
res_readreply(fde_t *F, void *data) |
511 |
adx |
30 |
{ |
512 |
michael |
4457 |
unsigned char buf[sizeof(HEADER) + MAXPACKET]; |
513 |
michael |
9202 |
ssize_t rc; |
514 |
adx |
30 |
socklen_t len = sizeof(struct irc_ssaddr); |
515 |
|
|
struct irc_ssaddr lsin; |
516 |
|
|
|
517 |
michael |
8339 |
while ((rc = recvfrom(F->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1) |
518 |
michael |
4317 |
{ |
519 |
|
|
if (rc <= (ssize_t)sizeof(HEADER)) |
520 |
|
|
continue; |
521 |
adx |
30 |
|
522 |
michael |
4317 |
/* |
523 |
|
|
* Check against possibly fake replies |
524 |
|
|
*/ |
525 |
michael |
8658 |
if (res_ourserver(&lsin) == false) |
526 |
michael |
4317 |
continue; |
527 |
michael |
984 |
|
528 |
michael |
4317 |
/* |
529 |
|
|
* Convert DNS reply reader from Network byte order to CPU byte order. |
530 |
|
|
*/ |
531 |
michael |
7597 |
HEADER *header = (HEADER *)buf; |
532 |
michael |
4317 |
header->ancount = ntohs(header->ancount); |
533 |
|
|
header->qdcount = ntohs(header->qdcount); |
534 |
|
|
header->nscount = ntohs(header->nscount); |
535 |
|
|
header->arcount = ntohs(header->arcount); |
536 |
adx |
30 |
|
537 |
michael |
4317 |
/* |
538 |
|
|
* Response for an id which we have already received an answer for |
539 |
|
|
* just ignore this response. |
540 |
|
|
*/ |
541 |
michael |
9202 |
struct reslist *request = find_id(header->id); |
542 |
|
|
if (request == NULL) |
543 |
michael |
4317 |
continue; |
544 |
michael |
4237 |
|
545 |
michael |
4449 |
if (header->rcode != NO_ERRORS || header->ancount == 0) |
546 |
michael |
4317 |
{ |
547 |
michael |
4449 |
/* |
548 |
michael |
7597 |
* If a bad error was returned, stop here and don't send |
549 |
|
|
* any more (no retries granted). |
550 |
michael |
4449 |
*/ |
551 |
|
|
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
552 |
|
|
rem_request(request); |
553 |
michael |
4317 |
continue; |
554 |
|
|
} |
555 |
adx |
30 |
|
556 |
michael |
4317 |
/* |
557 |
|
|
* If this fails there was an error decoding the received packet. |
558 |
|
|
* We only give it one shot. If it fails, just leave the client |
559 |
|
|
* unresolved. |
560 |
|
|
*/ |
561 |
michael |
8658 |
if (proc_answer(request, header, buf, buf + rc) == false) |
562 |
db |
155 |
{ |
563 |
michael |
4408 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
564 |
michael |
984 |
rem_request(request); |
565 |
michael |
4317 |
continue; |
566 |
michael |
984 |
} |
567 |
michael |
4317 |
|
568 |
|
|
if (request->type == T_PTR) |
569 |
michael |
984 |
{ |
570 |
michael |
4408 |
if (request->namelength == 0) |
571 |
michael |
4317 |
{ |
572 |
|
|
/* |
573 |
|
|
* Got a PTR response with no name, something bogus is happening |
574 |
|
|
* don't bother trying again, the client address doesn't resolve |
575 |
|
|
*/ |
576 |
michael |
4408 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
577 |
michael |
4317 |
rem_request(request); |
578 |
|
|
continue; |
579 |
|
|
} |
580 |
|
|
|
581 |
michael |
2916 |
/* |
582 |
michael |
4317 |
* Lookup the 'authoritative' name that we were given for the ip#. |
583 |
adx |
30 |
*/ |
584 |
michael |
4317 |
if (request->addr.ss.ss_family == AF_INET6) |
585 |
|
|
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
586 |
|
|
else |
587 |
michael |
4415 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
588 |
michael |
7597 |
|
589 |
michael |
4317 |
rem_request(request); |
590 |
michael |
984 |
} |
591 |
michael |
4317 |
else |
592 |
adx |
30 |
{ |
593 |
|
|
/* |
594 |
michael |
4317 |
* Got a name and address response, client resolved |
595 |
adx |
30 |
*/ |
596 |
michael |
4408 |
(*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength); |
597 |
adx |
30 |
rem_request(request); |
598 |
|
|
} |
599 |
michael |
4316 |
|
600 |
michael |
4317 |
continue; |
601 |
adx |
30 |
} |
602 |
michael |
4317 |
|
603 |
michael |
8339 |
comm_setselect(F, COMM_SELECT_READ, res_readreply, NULL, 0); |
604 |
adx |
30 |
} |
605 |
|
|
|
606 |
michael |
4311 |
/* |
607 |
|
|
* timeout_query_list - Remove queries from the list which have been |
608 |
|
|
* there too long without being resolved. |
609 |
|
|
*/ |
610 |
michael |
7330 |
static uintmax_t |
611 |
michael |
4311 |
timeout_query_list(void) |
612 |
|
|
{ |
613 |
michael |
7914 |
dlink_node *node, *node_next; |
614 |
michael |
7330 |
uintmax_t next_time = 0; |
615 |
michael |
4311 |
|
616 |
michael |
4815 |
DLINK_FOREACH_SAFE(node, node_next, request_list.head) |
617 |
michael |
4311 |
{ |
618 |
michael |
7914 |
struct reslist *request = node->data; |
619 |
|
|
uintmax_t timeout = request->sentat + request->timeout; |
620 |
michael |
4311 |
|
621 |
michael |
8900 |
if (event_base->time.sec_monotonic >= timeout) |
622 |
michael |
4311 |
{ |
623 |
|
|
if (--request->retries <= 0) |
624 |
|
|
{ |
625 |
michael |
4408 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
626 |
michael |
4311 |
rem_request(request); |
627 |
|
|
continue; |
628 |
|
|
} |
629 |
|
|
else |
630 |
|
|
{ |
631 |
michael |
8900 |
request->sentat = event_base->time.sec_monotonic; |
632 |
michael |
4311 |
request->timeout += request->timeout; |
633 |
|
|
resend_query(request); |
634 |
|
|
} |
635 |
|
|
} |
636 |
|
|
|
637 |
|
|
if (next_time == 0 || timeout < next_time) |
638 |
|
|
next_time = timeout; |
639 |
|
|
} |
640 |
|
|
|
641 |
michael |
8900 |
return (next_time > event_base->time.sec_monotonic) ? next_time : (event_base->time.sec_monotonic + AR_TTL); |
642 |
michael |
4311 |
} |
643 |
|
|
|
644 |
|
|
/* |
645 |
|
|
* timeout_resolver - check request list |
646 |
|
|
*/ |
647 |
|
|
static void |
648 |
michael |
4439 |
timeout_resolver(void *unused) |
649 |
michael |
4311 |
{ |
650 |
|
|
timeout_query_list(); |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
/* |
654 |
michael |
7569 |
* resolver_init - initialize resolver and resolver library |
655 |
michael |
4311 |
*/ |
656 |
|
|
void |
657 |
michael |
7569 |
resolver_init(void) |
658 |
michael |
4311 |
{ |
659 |
|
|
static struct event event_timeout_resolver = |
660 |
|
|
{ |
661 |
|
|
.name = "timeout_resolver", |
662 |
|
|
.handler = timeout_resolver, |
663 |
|
|
.when = 1 |
664 |
|
|
}; |
665 |
|
|
|
666 |
|
|
start_resolver(); |
667 |
|
|
event_add(&event_timeout_resolver, NULL); |
668 |
|
|
} |