1 |
adx |
30 |
/* |
2 |
michael |
2916 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 1997-2014 ircd-hybrid development team |
5 |
|
|
* |
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 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
19 |
|
|
* USA |
20 |
|
|
*/ |
21 |
|
|
|
22 |
michael |
3322 |
/*! \file res.c |
23 |
michael |
2916 |
* \brief ircd resolver functions |
24 |
|
|
* \version $Id$ |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
/* |
28 |
michael |
4312 |
* 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 |
4312 |
* 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 "client.h" |
49 |
|
|
#include "event.h" |
50 |
|
|
#include "irc_string.h" |
51 |
|
|
#include "ircd.h" |
52 |
|
|
#include "numeric.h" |
53 |
michael |
982 |
#include "rng_mt.h" |
54 |
adx |
30 |
#include "fdlist.h" |
55 |
|
|
#include "s_bsd.h" |
56 |
michael |
1309 |
#include "log.h" |
57 |
michael |
3347 |
#include "misc.h" |
58 |
adx |
30 |
#include "send.h" |
59 |
|
|
#include "memory.h" |
60 |
michael |
1654 |
#include "mempool.h" |
61 |
michael |
3322 |
#include "res.h" |
62 |
|
|
#include "reslib.h" |
63 |
adx |
30 |
|
64 |
|
|
#if (CHAR_BIT != 8) |
65 |
michael |
2916 |
#error this code needs to be able to address individual octets |
66 |
adx |
30 |
#endif |
67 |
|
|
|
68 |
michael |
4462 |
static void res_readreply(fde_t *, void *); |
69 |
adx |
30 |
|
70 |
michael |
4312 |
#define MAXPACKET 1024 /**< rfc says 512 but we expand names so ... */ |
71 |
|
|
#define AR_TTL 600 /**< TTL in seconds for dns cache entries */ |
72 |
adx |
30 |
|
73 |
michael |
4312 |
/* |
74 |
|
|
* RFC 1104/1105 wasn't very helpful about what these fields |
75 |
adx |
30 |
* should be named, so for now, we'll just name them this way. |
76 |
michael |
4312 |
* We probably should look at what named calls them or something. |
77 |
adx |
30 |
*/ |
78 |
|
|
#define TYPE_SIZE (size_t)2 |
79 |
|
|
#define CLASS_SIZE (size_t)2 |
80 |
|
|
#define TTL_SIZE (size_t)4 |
81 |
|
|
#define RDLENGTH_SIZE (size_t)2 |
82 |
|
|
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
83 |
|
|
|
84 |
michael |
2916 |
struct reslist |
85 |
adx |
30 |
{ |
86 |
michael |
4422 |
dlink_node node; /**< Doubly linked list node. */ |
87 |
|
|
int id; /**< Request ID (from request header). */ |
88 |
|
|
int sent; /**< Number of requests sent */ |
89 |
|
|
char type; /**< Current request type. */ |
90 |
|
|
char retries; /**< Retry counter */ |
91 |
|
|
unsigned int sends; /**< Number of sends (>1 means resent). */ |
92 |
|
|
time_t sentat; /**< Timestamp we last sent this request. */ |
93 |
|
|
time_t timeout; /**< When this request times out. */ |
94 |
|
|
struct irc_ssaddr addr; /**< Address for this request. */ |
95 |
|
|
char name[RFC1035_MAX_DOMAIN_LENGTH + 1]; /**< Hostname for this request. */ |
96 |
|
|
size_t namelength; /**< Actual hostname length. */ |
97 |
|
|
dns_callback_fnc callback; /**< Callback function on completion. */ |
98 |
|
|
void *callback_ctx; /**< Context pointer for callback. */ |
99 |
adx |
30 |
}; |
100 |
|
|
|
101 |
|
|
static fde_t ResolverFileDescriptor; |
102 |
michael |
3235 |
static dlink_list request_list; |
103 |
|
|
static mp_pool_t *dns_pool; |
104 |
adx |
30 |
|
105 |
|
|
|
106 |
michael |
4312 |
/* |
107 |
|
|
* rem_request - remove a request from the list. |
108 |
|
|
* This must also free any memory that has been allocated for |
109 |
|
|
* temporary storage of DNS results. |
110 |
|
|
*/ |
111 |
|
|
static void |
112 |
|
|
rem_request(struct reslist *request) |
113 |
|
|
{ |
114 |
|
|
dlinkDelete(&request->node, &request_list); |
115 |
|
|
mp_pool_release(request); |
116 |
|
|
} |
117 |
|
|
|
118 |
adx |
30 |
/* |
119 |
michael |
4312 |
* make_request - Create a DNS request record for the server. |
120 |
|
|
*/ |
121 |
|
|
static struct reslist * |
122 |
|
|
make_request(dns_callback_fnc callback, void *ctx) |
123 |
|
|
{ |
124 |
|
|
struct reslist *request = mp_pool_get(dns_pool); |
125 |
|
|
|
126 |
|
|
request->sentat = CurrentTime; |
127 |
|
|
request->retries = 2; |
128 |
|
|
request->timeout = 4; /* Start at 4 and exponential inc. */ |
129 |
|
|
request->callback = callback; |
130 |
|
|
request->callback_ctx = ctx; |
131 |
|
|
|
132 |
|
|
dlinkAdd(request, &request->node, &request_list); |
133 |
|
|
return request; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/* |
137 |
adx |
30 |
* int |
138 |
|
|
* res_ourserver(inp) |
139 |
|
|
* looks up "inp" in irc_nsaddr_list[] |
140 |
|
|
* returns: |
141 |
|
|
* 0 : not found |
142 |
|
|
* >0 : found |
143 |
|
|
* author: |
144 |
|
|
* paul vixie, 29may94 |
145 |
|
|
* revised for ircd, cryogen(stu) may03 |
146 |
|
|
*/ |
147 |
|
|
static int |
148 |
michael |
2916 |
res_ourserver(const struct irc_ssaddr *inp) |
149 |
adx |
30 |
{ |
150 |
michael |
992 |
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
151 |
michael |
2916 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
152 |
adx |
30 |
|
153 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
154 |
adx |
30 |
{ |
155 |
michael |
3235 |
const struct irc_ssaddr *srv = &irc_nsaddr_list[i]; |
156 |
michael |
4475 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)srv; |
157 |
|
|
const struct sockaddr_in *v4 = (const struct sockaddr_in *)srv; |
158 |
adx |
30 |
|
159 |
michael |
4312 |
/* |
160 |
|
|
* Could probably just memcmp(srv, inp, srv.ss_len) here |
161 |
adx |
30 |
* but we'll air on the side of caution - stu |
162 |
|
|
*/ |
163 |
|
|
switch (srv->ss.ss_family) |
164 |
|
|
{ |
165 |
|
|
case AF_INET6: |
166 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
167 |
|
|
if (v6->sin6_port == v6in->sin6_port) |
168 |
michael |
1346 |
if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
169 |
|
|
sizeof(struct in6_addr))) |
170 |
michael |
1124 |
return 1; |
171 |
adx |
30 |
break; |
172 |
|
|
case AF_INET: |
173 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
174 |
|
|
if (v4->sin_port == v4in->sin_port) |
175 |
michael |
1346 |
if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr) |
176 |
michael |
1124 |
return 1; |
177 |
adx |
30 |
break; |
178 |
|
|
default: |
179 |
|
|
break; |
180 |
|
|
} |
181 |
|
|
} |
182 |
|
|
|
183 |
michael |
1124 |
return 0; |
184 |
adx |
30 |
} |
185 |
|
|
|
186 |
|
|
/* |
187 |
|
|
* start_resolver - do everything we need to read the resolv.conf file |
188 |
|
|
* and initialize the resolver file descriptor if needed |
189 |
|
|
*/ |
190 |
|
|
static void |
191 |
|
|
start_resolver(void) |
192 |
|
|
{ |
193 |
|
|
irc_res_init(); |
194 |
|
|
|
195 |
|
|
if (!ResolverFileDescriptor.flags.open) |
196 |
|
|
{ |
197 |
|
|
if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family, |
198 |
michael |
4447 |
SOCK_DGRAM, 0, "UDP resolver socket") == -1) |
199 |
adx |
30 |
return; |
200 |
|
|
|
201 |
|
|
/* At the moment, the resolver FD data is global .. */ |
202 |
michael |
3298 |
comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
203 |
adx |
30 |
} |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
/* |
207 |
|
|
* restart_resolver - reread resolv.conf, reopen socket |
208 |
|
|
*/ |
209 |
|
|
void |
210 |
|
|
restart_resolver(void) |
211 |
|
|
{ |
212 |
|
|
fd_close(&ResolverFileDescriptor); |
213 |
|
|
start_resolver(); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
/* |
217 |
michael |
2916 |
* delete_resolver_queries - cleanup outstanding queries |
218 |
adx |
30 |
* for which there no longer exist clients or conf lines. |
219 |
|
|
*/ |
220 |
|
|
void |
221 |
michael |
992 |
delete_resolver_queries(const void *vptr) |
222 |
adx |
30 |
{ |
223 |
michael |
3235 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
224 |
adx |
30 |
|
225 |
michael |
3235 |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
226 |
adx |
30 |
{ |
227 |
michael |
997 |
struct reslist *request = ptr->data; |
228 |
|
|
|
229 |
|
|
if (request->callback_ctx == vptr) |
230 |
|
|
rem_request(request); |
231 |
adx |
30 |
} |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
/* |
235 |
|
|
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
236 |
|
|
* This should reflect /etc/resolv.conf. We will get responses |
237 |
|
|
* which arent needed but is easier than checking to see if nameserver |
238 |
michael |
4299 |
* isn't present. Returns number of messages successfully sent to |
239 |
adx |
30 |
* nameservers or -1 if no successful sends. |
240 |
|
|
*/ |
241 |
|
|
static int |
242 |
michael |
4467 |
send_res_msg(const unsigned char *msg, int len, unsigned int rcount) |
243 |
adx |
30 |
{ |
244 |
|
|
int sent = 0; |
245 |
michael |
3297 |
unsigned int max_queries = IRCD_MIN(irc_nscount, rcount); |
246 |
adx |
30 |
|
247 |
|
|
/* RES_PRIMARY option is not implemented |
248 |
|
|
* if (res.options & RES_PRIMARY || 0 == max_queries) |
249 |
|
|
*/ |
250 |
|
|
if (max_queries == 0) |
251 |
|
|
max_queries = 1; |
252 |
|
|
|
253 |
michael |
3297 |
for (unsigned int i = 0; i < max_queries; ++i) |
254 |
adx |
30 |
{ |
255 |
michael |
2916 |
if (sendto(ResolverFileDescriptor.fd, msg, len, 0, |
256 |
|
|
(struct sockaddr*)&(irc_nsaddr_list[i]), |
257 |
|
|
irc_nsaddr_list[i].ss_len) == len) |
258 |
adx |
30 |
++sent; |
259 |
|
|
} |
260 |
|
|
|
261 |
michael |
1124 |
return sent; |
262 |
adx |
30 |
} |
263 |
|
|
|
264 |
|
|
/* |
265 |
|
|
* find_id - find a dns request id (id is determined by dn_mkquery) |
266 |
|
|
*/ |
267 |
|
|
static struct reslist * |
268 |
|
|
find_id(int id) |
269 |
|
|
{ |
270 |
michael |
1124 |
dlink_node *ptr = NULL; |
271 |
adx |
30 |
|
272 |
|
|
DLINK_FOREACH(ptr, request_list.head) |
273 |
|
|
{ |
274 |
michael |
1124 |
struct reslist *request = ptr->data; |
275 |
adx |
30 |
|
276 |
|
|
if (request->id == id) |
277 |
michael |
1124 |
return request; |
278 |
adx |
30 |
} |
279 |
|
|
|
280 |
michael |
1124 |
return NULL; |
281 |
adx |
30 |
} |
282 |
|
|
|
283 |
michael |
2916 |
/* |
284 |
michael |
4312 |
* query_name - generate a query based on class, type and name. |
285 |
adx |
30 |
*/ |
286 |
michael |
4312 |
static void |
287 |
michael |
4467 |
query_name(const char *name, int query_class, int type, struct reslist *request) |
288 |
adx |
30 |
{ |
289 |
michael |
4467 |
unsigned char buf[MAXPACKET]; |
290 |
michael |
4312 |
int request_len = 0; |
291 |
adx |
30 |
|
292 |
michael |
4312 |
memset(buf, 0, sizeof(buf)); |
293 |
adx |
30 |
|
294 |
michael |
4467 |
if ((request_len = irc_res_mkquery(name, query_class, type, buf, sizeof(buf))) > 0) |
295 |
michael |
4312 |
{ |
296 |
|
|
HEADER *header = (HEADER *)buf; |
297 |
|
|
|
298 |
|
|
/* |
299 |
|
|
* Generate an unique id. |
300 |
|
|
* NOTE: we don't have to worry about converting this to and from |
301 |
|
|
* network byte order, the nameserver does not interpret this value |
302 |
|
|
* and returns it unchanged. |
303 |
|
|
*/ |
304 |
|
|
do |
305 |
|
|
header->id = (header->id + genrand_int32()) & 0xFFFF; |
306 |
|
|
while (find_id(header->id)); |
307 |
|
|
|
308 |
|
|
request->id = header->id; |
309 |
|
|
++request->sends; |
310 |
|
|
|
311 |
|
|
request->sent += send_res_msg(buf, request_len, request->sends); |
312 |
|
|
} |
313 |
adx |
30 |
} |
314 |
|
|
|
315 |
|
|
/* |
316 |
|
|
* do_query_name - nameserver lookup name |
317 |
|
|
*/ |
318 |
|
|
static void |
319 |
michael |
992 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
320 |
adx |
30 |
struct reslist *request, int type) |
321 |
|
|
{ |
322 |
michael |
4409 |
char host_name[RFC1035_MAX_DOMAIN_LENGTH + 1]; |
323 |
adx |
30 |
|
324 |
michael |
998 |
strlcpy(host_name, name, sizeof(host_name)); |
325 |
adx |
30 |
|
326 |
|
|
if (request == NULL) |
327 |
|
|
{ |
328 |
michael |
4409 |
request = make_request(callback, ctx); |
329 |
|
|
request->type = type; |
330 |
|
|
request->namelength = strlcpy(request->name, host_name, sizeof(request->name)); |
331 |
adx |
30 |
} |
332 |
|
|
|
333 |
|
|
request->type = type; |
334 |
|
|
query_name(host_name, C_IN, type, request); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
/* |
338 |
|
|
* do_query_number - Use this to do reverse IP# lookups. |
339 |
|
|
*/ |
340 |
|
|
static void |
341 |
michael |
992 |
do_query_number(dns_callback_fnc callback, void *ctx, |
342 |
|
|
const struct irc_ssaddr *addr, |
343 |
adx |
30 |
struct reslist *request) |
344 |
|
|
{ |
345 |
michael |
3298 |
char ipbuf[128] = ""; |
346 |
michael |
985 |
|
347 |
adx |
30 |
if (addr->ss.ss_family == AF_INET) |
348 |
|
|
{ |
349 |
michael |
992 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
350 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
351 |
adx |
30 |
|
352 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
353 |
|
|
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
354 |
|
|
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
355 |
adx |
30 |
} |
356 |
|
|
else if (addr->ss.ss_family == AF_INET6) |
357 |
|
|
{ |
358 |
michael |
992 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
359 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
360 |
adx |
30 |
|
361 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), |
362 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
363 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
364 |
|
|
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
365 |
|
|
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
366 |
|
|
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
367 |
|
|
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
368 |
|
|
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
369 |
|
|
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
370 |
|
|
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
371 |
|
|
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
372 |
|
|
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
373 |
|
|
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
374 |
|
|
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
375 |
|
|
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
376 |
|
|
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
377 |
|
|
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
378 |
|
|
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
379 |
|
|
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
380 |
adx |
30 |
} |
381 |
michael |
4414 |
|
382 |
adx |
30 |
if (request == NULL) |
383 |
|
|
{ |
384 |
michael |
992 |
request = make_request(callback, ctx); |
385 |
adx |
30 |
request->type = T_PTR; |
386 |
|
|
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
query_name(ipbuf, C_IN, T_PTR, request); |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
/* |
393 |
michael |
4312 |
* gethost_byname_type - get host address from name |
394 |
|
|
* |
395 |
adx |
30 |
*/ |
396 |
michael |
4312 |
void |
397 |
|
|
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
398 |
adx |
30 |
{ |
399 |
michael |
4312 |
assert(name); |
400 |
|
|
do_query_name(callback, ctx, name, NULL, type); |
401 |
|
|
} |
402 |
adx |
30 |
|
403 |
michael |
4312 |
/* |
404 |
|
|
* gethost_byaddr - get host name from address |
405 |
|
|
*/ |
406 |
|
|
void |
407 |
|
|
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
408 |
|
|
{ |
409 |
|
|
do_query_number(callback, ctx, addr, NULL); |
410 |
adx |
30 |
} |
411 |
|
|
|
412 |
|
|
static void |
413 |
|
|
resend_query(struct reslist *request) |
414 |
|
|
{ |
415 |
michael |
982 |
switch (request->type) |
416 |
adx |
30 |
{ |
417 |
|
|
case T_PTR: |
418 |
michael |
992 |
do_query_number(NULL, NULL, &request->addr, request); |
419 |
adx |
30 |
break; |
420 |
|
|
case T_A: |
421 |
michael |
4450 |
case T_AAAA: |
422 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, request->type); |
423 |
adx |
30 |
break; |
424 |
|
|
default: |
425 |
|
|
break; |
426 |
|
|
} |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
/* |
430 |
|
|
* proc_answer - process name server reply |
431 |
|
|
*/ |
432 |
|
|
static int |
433 |
michael |
4458 |
proc_answer(struct reslist *request, HEADER *header, unsigned char *buf, unsigned char *eob) |
434 |
adx |
30 |
{ |
435 |
michael |
4409 |
char hostbuf[RFC1035_MAX_DOMAIN_LENGTH + 100]; /* working buffer */ |
436 |
michael |
4458 |
unsigned char *current = buf + sizeof(HEADER); /* current position in buf */ |
437 |
michael |
4455 |
unsigned int type = 0; /* answer type */ |
438 |
|
|
unsigned int rd_length = 0; |
439 |
adx |
30 |
int n; /* temp count */ |
440 |
|
|
struct sockaddr_in *v4; /* conversion */ |
441 |
|
|
struct sockaddr_in6 *v6; |
442 |
michael |
4414 |
|
443 |
adx |
30 |
for (; header->qdcount > 0; --header->qdcount) |
444 |
|
|
{ |
445 |
michael |
4458 |
if ((n = irc_dn_skipname(current, eob)) < 0) |
446 |
adx |
30 |
break; |
447 |
|
|
|
448 |
michael |
1124 |
current += (size_t)n + QFIXEDSZ; |
449 |
adx |
30 |
} |
450 |
|
|
|
451 |
|
|
/* |
452 |
michael |
4299 |
* Process each answer sent to us blech. |
453 |
adx |
30 |
*/ |
454 |
michael |
4458 |
while (header->ancount > 0 && current < eob) |
455 |
adx |
30 |
{ |
456 |
|
|
header->ancount--; |
457 |
|
|
|
458 |
michael |
4458 |
n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
459 |
adx |
30 |
|
460 |
michael |
4299 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
461 |
michael |
1124 |
return 0; |
462 |
adx |
30 |
|
463 |
michael |
4409 |
hostbuf[RFC1035_MAX_DOMAIN_LENGTH] = '\0'; |
464 |
adx |
30 |
|
465 |
michael |
4299 |
/* |
466 |
|
|
* With Address arithmetic you have to be very anal |
467 |
adx |
30 |
* this code was not working on alpha due to that |
468 |
|
|
* (spotted by rodder/jailbird/dianora) |
469 |
|
|
*/ |
470 |
michael |
3298 |
current += (size_t)n; |
471 |
adx |
30 |
|
472 |
michael |
4458 |
if (!((current + ANSWER_FIXED_SIZE) < eob)) |
473 |
adx |
30 |
break; |
474 |
|
|
|
475 |
|
|
type = irc_ns_get16(current); |
476 |
|
|
current += TYPE_SIZE; |
477 |
|
|
current += CLASS_SIZE; |
478 |
|
|
current += TTL_SIZE; |
479 |
|
|
rd_length = irc_ns_get16(current); |
480 |
|
|
current += RDLENGTH_SIZE; |
481 |
|
|
|
482 |
michael |
2916 |
/* |
483 |
|
|
* Wait to set request->type until we verify this structure |
484 |
adx |
30 |
*/ |
485 |
|
|
switch (type) |
486 |
|
|
{ |
487 |
|
|
case T_A: |
488 |
|
|
if (request->type != T_A) |
489 |
michael |
1124 |
return 0; |
490 |
adx |
30 |
|
491 |
|
|
/* |
492 |
michael |
3298 |
* Check for invalid rd_length or too many addresses |
493 |
adx |
30 |
*/ |
494 |
|
|
if (rd_length != sizeof(struct in_addr)) |
495 |
michael |
1124 |
return 0; |
496 |
|
|
|
497 |
michael |
4475 |
request->addr.ss_len = sizeof(struct sockaddr_in); |
498 |
adx |
30 |
v4 = (struct sockaddr_in *)&request->addr; |
499 |
|
|
v4->sin_family = AF_INET; |
500 |
|
|
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
501 |
michael |
1124 |
return 1; |
502 |
adx |
30 |
break; |
503 |
|
|
case T_AAAA: |
504 |
|
|
if (request->type != T_AAAA) |
505 |
michael |
1124 |
return 0; |
506 |
|
|
|
507 |
adx |
30 |
if (rd_length != sizeof(struct in6_addr)) |
508 |
michael |
1124 |
return 0; |
509 |
|
|
|
510 |
adx |
30 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
511 |
|
|
v6 = (struct sockaddr_in6 *)&request->addr; |
512 |
|
|
v6->sin6_family = AF_INET6; |
513 |
|
|
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
514 |
michael |
1124 |
return 1; |
515 |
adx |
30 |
break; |
516 |
|
|
case T_PTR: |
517 |
|
|
if (request->type != T_PTR) |
518 |
michael |
1124 |
return 0; |
519 |
|
|
|
520 |
michael |
4458 |
n = irc_dn_expand(buf, eob, current, hostbuf, sizeof(hostbuf)); |
521 |
michael |
4299 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
522 |
michael |
1124 |
return 0; |
523 |
adx |
30 |
|
524 |
michael |
4409 |
request->namelength = strlcpy(request->name, hostbuf, sizeof(request->name)); |
525 |
michael |
1124 |
return 1; |
526 |
adx |
30 |
break; |
527 |
michael |
4451 |
case T_CNAME: |
528 |
adx |
30 |
current += rd_length; |
529 |
|
|
break; |
530 |
|
|
default: |
531 |
michael |
4466 |
return 0; |
532 |
adx |
30 |
break; |
533 |
|
|
} |
534 |
|
|
} |
535 |
|
|
|
536 |
michael |
4466 |
return 0; |
537 |
adx |
30 |
} |
538 |
|
|
|
539 |
|
|
/* |
540 |
|
|
* res_readreply - read a dns reply from the nameserver and process it. |
541 |
|
|
*/ |
542 |
|
|
static void |
543 |
|
|
res_readreply(fde_t *fd, void *data) |
544 |
|
|
{ |
545 |
michael |
4458 |
unsigned char buf[sizeof(HEADER) + MAXPACKET]; |
546 |
adx |
30 |
HEADER *header; |
547 |
|
|
struct reslist *request = NULL; |
548 |
michael |
4302 |
ssize_t rc = 0; |
549 |
adx |
30 |
socklen_t len = sizeof(struct irc_ssaddr); |
550 |
|
|
struct irc_ssaddr lsin; |
551 |
|
|
|
552 |
michael |
4318 |
while ((rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len)) != -1) |
553 |
|
|
{ |
554 |
|
|
if (rc <= (ssize_t)sizeof(HEADER)) |
555 |
|
|
continue; |
556 |
adx |
30 |
|
557 |
michael |
4318 |
/* |
558 |
|
|
* Check against possibly fake replies |
559 |
|
|
*/ |
560 |
|
|
if (!res_ourserver(&lsin)) |
561 |
|
|
continue; |
562 |
michael |
984 |
|
563 |
michael |
4318 |
/* |
564 |
|
|
* Convert DNS reply reader from Network byte order to CPU byte order. |
565 |
|
|
*/ |
566 |
|
|
header = (HEADER *)buf; |
567 |
|
|
header->ancount = ntohs(header->ancount); |
568 |
|
|
header->qdcount = ntohs(header->qdcount); |
569 |
|
|
header->nscount = ntohs(header->nscount); |
570 |
|
|
header->arcount = ntohs(header->arcount); |
571 |
adx |
30 |
|
572 |
michael |
4318 |
/* |
573 |
|
|
* Response for an id which we have already received an answer for |
574 |
|
|
* just ignore this response. |
575 |
|
|
*/ |
576 |
|
|
if ((request = find_id(header->id)) == NULL) |
577 |
|
|
continue; |
578 |
michael |
4238 |
|
579 |
michael |
4450 |
if (header->rcode != NO_ERRORS || header->ancount == 0) |
580 |
michael |
4318 |
{ |
581 |
michael |
4450 |
/* |
582 |
|
|
* If a bad error was returned, stop here and don't |
583 |
|
|
* send any more (no retries granted). |
584 |
|
|
*/ |
585 |
|
|
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
586 |
|
|
rem_request(request); |
587 |
michael |
4318 |
continue; |
588 |
|
|
} |
589 |
adx |
30 |
|
590 |
michael |
4318 |
/* |
591 |
|
|
* If this fails there was an error decoding the received packet. |
592 |
|
|
* We only give it one shot. If it fails, just leave the client |
593 |
|
|
* unresolved. |
594 |
|
|
*/ |
595 |
|
|
if (!proc_answer(request, header, buf, buf + rc)) |
596 |
db |
155 |
{ |
597 |
michael |
4409 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
598 |
michael |
984 |
rem_request(request); |
599 |
michael |
4318 |
continue; |
600 |
michael |
984 |
} |
601 |
michael |
4318 |
|
602 |
|
|
if (request->type == T_PTR) |
603 |
michael |
984 |
{ |
604 |
michael |
4409 |
if (request->namelength == 0) |
605 |
michael |
4318 |
{ |
606 |
|
|
/* |
607 |
|
|
* Got a PTR response with no name, something bogus is happening |
608 |
|
|
* don't bother trying again, the client address doesn't resolve |
609 |
|
|
*/ |
610 |
michael |
4409 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
611 |
michael |
4318 |
rem_request(request); |
612 |
|
|
continue; |
613 |
|
|
} |
614 |
|
|
|
615 |
michael |
2916 |
/* |
616 |
michael |
4318 |
* Lookup the 'authoritative' name that we were given for the ip#. |
617 |
adx |
30 |
*/ |
618 |
michael |
4318 |
if (request->addr.ss.ss_family == AF_INET6) |
619 |
|
|
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
620 |
|
|
else |
621 |
michael |
4414 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
622 |
michael |
4318 |
rem_request(request); |
623 |
michael |
984 |
} |
624 |
michael |
4318 |
else |
625 |
adx |
30 |
{ |
626 |
|
|
/* |
627 |
michael |
4318 |
* Got a name and address response, client resolved |
628 |
adx |
30 |
*/ |
629 |
michael |
4409 |
(*request->callback)(request->callback_ctx, &request->addr, request->name, request->namelength); |
630 |
adx |
30 |
rem_request(request); |
631 |
|
|
} |
632 |
michael |
4315 |
|
633 |
michael |
4318 |
continue; |
634 |
adx |
30 |
} |
635 |
michael |
4318 |
|
636 |
|
|
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
637 |
adx |
30 |
} |
638 |
|
|
|
639 |
|
|
void |
640 |
|
|
report_dns_servers(struct Client *source_p) |
641 |
|
|
{ |
642 |
michael |
3235 |
char ipaddr[HOSTIPLEN + 1] = ""; |
643 |
adx |
30 |
|
644 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
645 |
adx |
30 |
{ |
646 |
michael |
1123 |
getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]), |
647 |
|
|
irc_nsaddr_list[i].ss_len, ipaddr, |
648 |
|
|
sizeof(ipaddr), NULL, 0, NI_NUMERICHOST); |
649 |
michael |
3109 |
sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr); |
650 |
adx |
30 |
} |
651 |
|
|
} |
652 |
michael |
4312 |
|
653 |
|
|
/* |
654 |
|
|
* timeout_query_list - Remove queries from the list which have been |
655 |
|
|
* there too long without being resolved. |
656 |
|
|
*/ |
657 |
|
|
static time_t |
658 |
|
|
timeout_query_list(void) |
659 |
|
|
{ |
660 |
|
|
dlink_node *ptr = NULL, *ptr_next = NULL; |
661 |
|
|
struct reslist *request = NULL; |
662 |
|
|
time_t next_time = 0; |
663 |
|
|
time_t timeout = 0; |
664 |
|
|
|
665 |
|
|
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
666 |
|
|
{ |
667 |
|
|
request = ptr->data; |
668 |
|
|
timeout = request->sentat + request->timeout; |
669 |
|
|
|
670 |
|
|
if (CurrentTime >= timeout) |
671 |
|
|
{ |
672 |
|
|
if (--request->retries <= 0) |
673 |
|
|
{ |
674 |
michael |
4409 |
(*request->callback)(request->callback_ctx, NULL, NULL, 0); |
675 |
michael |
4312 |
rem_request(request); |
676 |
|
|
continue; |
677 |
|
|
} |
678 |
|
|
else |
679 |
|
|
{ |
680 |
|
|
request->sentat = CurrentTime; |
681 |
|
|
request->timeout += request->timeout; |
682 |
|
|
resend_query(request); |
683 |
|
|
} |
684 |
|
|
} |
685 |
|
|
|
686 |
|
|
if (next_time == 0 || timeout < next_time) |
687 |
|
|
next_time = timeout; |
688 |
|
|
} |
689 |
|
|
|
690 |
|
|
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
691 |
|
|
} |
692 |
|
|
|
693 |
|
|
/* |
694 |
|
|
* timeout_resolver - check request list |
695 |
|
|
*/ |
696 |
|
|
static void |
697 |
michael |
4438 |
timeout_resolver(void *unused) |
698 |
michael |
4312 |
{ |
699 |
|
|
timeout_query_list(); |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
/* |
703 |
|
|
* init_resolver - initialize resolver and resolver library |
704 |
|
|
*/ |
705 |
|
|
void |
706 |
|
|
init_resolver(void) |
707 |
|
|
{ |
708 |
|
|
static struct event event_timeout_resolver = |
709 |
|
|
{ |
710 |
|
|
.name = "timeout_resolver", |
711 |
|
|
.handler = timeout_resolver, |
712 |
|
|
.when = 1 |
713 |
|
|
}; |
714 |
|
|
|
715 |
|
|
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
716 |
michael |
4421 |
|
717 |
michael |
4312 |
start_resolver(); |
718 |
|
|
event_add(&event_timeout_resolver, NULL); |
719 |
|
|
} |