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 |
adx |
30 |
* A rewrite of Darren Reeds original res.c As there is nothing |
29 |
|
|
* left of Darrens original code, this is now licensed by the hybrid group. |
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 |
knight |
31 |
* $Id$ |
37 |
adx |
30 |
* |
38 |
|
|
* July 1999 - Rewrote a bunch of stuff here. Change hostent builder code, |
39 |
|
|
* added callbacks and reference counting of returned hostents. |
40 |
|
|
* --Bleep (Thomas Helvey <tomh@inxpress.net>) |
41 |
|
|
* |
42 |
|
|
* This was all needlessly complicated for irc. Simplified. No more hostent |
43 |
michael |
2916 |
* All we really care about is the IP -> hostname mappings. Thats all. |
44 |
adx |
30 |
* |
45 |
|
|
* Apr 28, 2003 --cryogen and Dianora |
46 |
|
|
*/ |
47 |
|
|
|
48 |
|
|
#include "stdinc.h" |
49 |
michael |
1011 |
#include "list.h" |
50 |
adx |
30 |
#include "client.h" |
51 |
|
|
#include "event.h" |
52 |
|
|
#include "irc_string.h" |
53 |
|
|
#include "ircd.h" |
54 |
|
|
#include "numeric.h" |
55 |
michael |
982 |
#include "rng_mt.h" |
56 |
adx |
30 |
#include "fdlist.h" |
57 |
|
|
#include "s_bsd.h" |
58 |
michael |
1309 |
#include "log.h" |
59 |
michael |
3347 |
#include "misc.h" |
60 |
adx |
30 |
#include "send.h" |
61 |
|
|
#include "memory.h" |
62 |
michael |
1654 |
#include "mempool.h" |
63 |
michael |
3322 |
#include "res.h" |
64 |
|
|
#include "reslib.h" |
65 |
adx |
30 |
|
66 |
|
|
#if (CHAR_BIT != 8) |
67 |
michael |
2916 |
#error this code needs to be able to address individual octets |
68 |
adx |
30 |
#endif |
69 |
|
|
|
70 |
|
|
static PF res_readreply; |
71 |
|
|
|
72 |
|
|
#define MAXPACKET 1024 /* rfc sez 512 but we expand names so ... */ |
73 |
|
|
#define AR_TTL 600 /* TTL in seconds for dns cache entries */ |
74 |
|
|
|
75 |
|
|
/* RFC 1104/1105 wasn't very helpful about what these fields |
76 |
|
|
* should be named, so for now, we'll just name them this way. |
77 |
|
|
* we probably should look at what named calls them or something. |
78 |
|
|
*/ |
79 |
|
|
#define TYPE_SIZE (size_t)2 |
80 |
|
|
#define CLASS_SIZE (size_t)2 |
81 |
|
|
#define TTL_SIZE (size_t)4 |
82 |
|
|
#define RDLENGTH_SIZE (size_t)2 |
83 |
|
|
#define ANSWER_FIXED_SIZE (TYPE_SIZE + CLASS_SIZE + TTL_SIZE + RDLENGTH_SIZE) |
84 |
|
|
|
85 |
michael |
2916 |
typedef enum |
86 |
adx |
30 |
{ |
87 |
|
|
REQ_IDLE, /* We're doing not much at all */ |
88 |
|
|
REQ_PTR, /* Looking up a PTR */ |
89 |
|
|
REQ_A, /* Looking up an A, possibly because AAAA failed */ |
90 |
|
|
#ifdef IPV6 |
91 |
|
|
REQ_AAAA, /* Looking up an AAAA */ |
92 |
|
|
#endif |
93 |
michael |
985 |
REQ_CNAME /* We got a CNAME in response, we better get a real answer next */ |
94 |
adx |
30 |
} request_state; |
95 |
|
|
|
96 |
michael |
2916 |
struct reslist |
97 |
adx |
30 |
{ |
98 |
|
|
dlink_node node; |
99 |
|
|
int id; |
100 |
|
|
int sent; /* number of requests sent */ |
101 |
|
|
request_state state; /* State the resolver machine is in */ |
102 |
|
|
time_t ttl; |
103 |
|
|
char type; |
104 |
|
|
char retries; /* retry counter */ |
105 |
michael |
3297 |
unsigned int sends; /* number of sends (>1 means resent) */ |
106 |
adx |
30 |
char resend; /* send flag. 0 == dont resend */ |
107 |
|
|
time_t sentat; |
108 |
|
|
time_t timeout; |
109 |
|
|
struct irc_ssaddr addr; |
110 |
|
|
char *name; |
111 |
michael |
992 |
dns_callback_fnc callback; |
112 |
|
|
void *callback_ctx; |
113 |
adx |
30 |
}; |
114 |
|
|
|
115 |
|
|
static fde_t ResolverFileDescriptor; |
116 |
michael |
3235 |
static dlink_list request_list; |
117 |
|
|
static mp_pool_t *dns_pool; |
118 |
adx |
30 |
|
119 |
michael |
1124 |
static void rem_request(struct reslist *); |
120 |
|
|
static struct reslist *make_request(dns_callback_fnc, void *); |
121 |
|
|
static void do_query_name(dns_callback_fnc, void *, |
122 |
michael |
992 |
const char *, struct reslist *, int); |
123 |
michael |
1124 |
static void do_query_number(dns_callback_fnc, void *, |
124 |
adx |
30 |
const struct irc_ssaddr *, |
125 |
michael |
1124 |
struct reslist *); |
126 |
|
|
static void query_name(const char *, int, int, struct reslist *); |
127 |
michael |
3297 |
static int send_res_msg(const char *, int, unsigned int); |
128 |
michael |
1124 |
static void resend_query(struct reslist *); |
129 |
|
|
static int proc_answer(struct reslist *, HEADER *, char *, char *); |
130 |
|
|
static struct reslist *find_id(int); |
131 |
adx |
30 |
|
132 |
|
|
|
133 |
|
|
/* |
134 |
|
|
* int |
135 |
|
|
* res_ourserver(inp) |
136 |
|
|
* looks up "inp" in irc_nsaddr_list[] |
137 |
|
|
* returns: |
138 |
|
|
* 0 : not found |
139 |
|
|
* >0 : found |
140 |
|
|
* author: |
141 |
|
|
* paul vixie, 29may94 |
142 |
|
|
* revised for ircd, cryogen(stu) may03 |
143 |
|
|
*/ |
144 |
|
|
static int |
145 |
michael |
2916 |
res_ourserver(const struct irc_ssaddr *inp) |
146 |
adx |
30 |
{ |
147 |
|
|
#ifdef IPV6 |
148 |
michael |
992 |
const struct sockaddr_in6 *v6; |
149 |
|
|
const struct sockaddr_in6 *v6in = (const struct sockaddr_in6 *)inp; |
150 |
adx |
30 |
#endif |
151 |
michael |
992 |
const struct sockaddr_in *v4; |
152 |
michael |
2916 |
const struct sockaddr_in *v4in = (const struct sockaddr_in *)inp; |
153 |
adx |
30 |
|
154 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
155 |
adx |
30 |
{ |
156 |
michael |
3235 |
const struct irc_ssaddr *srv = &irc_nsaddr_list[i]; |
157 |
adx |
30 |
#ifdef IPV6 |
158 |
michael |
992 |
v6 = (const struct sockaddr_in6 *)srv; |
159 |
adx |
30 |
#endif |
160 |
michael |
992 |
v4 = (const struct sockaddr_in *)srv; |
161 |
adx |
30 |
|
162 |
|
|
/* could probably just memcmp(srv, inp, srv.ss_len) here |
163 |
|
|
* but we'll air on the side of caution - stu |
164 |
|
|
* |
165 |
|
|
*/ |
166 |
|
|
switch (srv->ss.ss_family) |
167 |
|
|
{ |
168 |
|
|
#ifdef IPV6 |
169 |
|
|
case AF_INET6: |
170 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
171 |
|
|
if (v6->sin6_port == v6in->sin6_port) |
172 |
michael |
1346 |
if (!memcmp(&v6->sin6_addr.s6_addr, &v6in->sin6_addr.s6_addr, |
173 |
|
|
sizeof(struct in6_addr))) |
174 |
michael |
1124 |
return 1; |
175 |
adx |
30 |
break; |
176 |
|
|
#endif |
177 |
|
|
case AF_INET: |
178 |
|
|
if (srv->ss.ss_family == inp->ss.ss_family) |
179 |
|
|
if (v4->sin_port == v4in->sin_port) |
180 |
michael |
1346 |
if (v4->sin_addr.s_addr == v4in->sin_addr.s_addr) |
181 |
michael |
1124 |
return 1; |
182 |
adx |
30 |
break; |
183 |
|
|
default: |
184 |
|
|
break; |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
|
188 |
michael |
1124 |
return 0; |
189 |
adx |
30 |
} |
190 |
|
|
|
191 |
|
|
/* |
192 |
michael |
2916 |
* timeout_query_list - Remove queries from the list which have been |
193 |
adx |
30 |
* there too long without being resolved. |
194 |
|
|
*/ |
195 |
|
|
static time_t |
196 |
michael |
4234 |
timeout_query_list(void) |
197 |
adx |
30 |
{ |
198 |
michael |
3235 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
199 |
michael |
4234 |
struct reslist *request = NULL; |
200 |
adx |
30 |
time_t next_time = 0; |
201 |
|
|
time_t timeout = 0; |
202 |
|
|
|
203 |
michael |
3235 |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
204 |
adx |
30 |
{ |
205 |
|
|
request = ptr->data; |
206 |
|
|
timeout = request->sentat + request->timeout; |
207 |
|
|
|
208 |
michael |
4234 |
if (CurrentTime >= timeout) |
209 |
adx |
30 |
{ |
210 |
|
|
if (--request->retries <= 0) |
211 |
|
|
{ |
212 |
michael |
992 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
213 |
adx |
30 |
rem_request(request); |
214 |
|
|
continue; |
215 |
|
|
} |
216 |
|
|
else |
217 |
|
|
{ |
218 |
michael |
4234 |
request->sentat = CurrentTime; |
219 |
adx |
30 |
request->timeout += request->timeout; |
220 |
|
|
resend_query(request); |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
if ((next_time == 0) || timeout < next_time) |
225 |
|
|
next_time = timeout; |
226 |
|
|
} |
227 |
|
|
|
228 |
michael |
4234 |
return (next_time > CurrentTime) ? next_time : (CurrentTime + AR_TTL); |
229 |
adx |
30 |
} |
230 |
|
|
|
231 |
|
|
/* |
232 |
|
|
* timeout_resolver - check request list |
233 |
|
|
*/ |
234 |
|
|
static void |
235 |
|
|
timeout_resolver(void *notused) |
236 |
|
|
{ |
237 |
michael |
4234 |
timeout_query_list(); |
238 |
adx |
30 |
} |
239 |
|
|
|
240 |
|
|
/* |
241 |
|
|
* start_resolver - do everything we need to read the resolv.conf file |
242 |
|
|
* and initialize the resolver file descriptor if needed |
243 |
|
|
*/ |
244 |
|
|
static void |
245 |
|
|
start_resolver(void) |
246 |
|
|
{ |
247 |
|
|
irc_res_init(); |
248 |
|
|
|
249 |
|
|
if (!ResolverFileDescriptor.flags.open) |
250 |
|
|
{ |
251 |
|
|
if (comm_open(&ResolverFileDescriptor, irc_nsaddr_list[0].ss.ss_family, |
252 |
|
|
SOCK_DGRAM, 0, "Resolver socket") == -1) |
253 |
|
|
return; |
254 |
|
|
|
255 |
|
|
/* At the moment, the resolver FD data is global .. */ |
256 |
michael |
3298 |
comm_setselect(&ResolverFileDescriptor, COMM_SELECT_READ, res_readreply, NULL, 0); |
257 |
adx |
30 |
} |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
/* |
261 |
|
|
* init_resolver - initialize resolver and resolver library |
262 |
|
|
*/ |
263 |
|
|
void |
264 |
|
|
init_resolver(void) |
265 |
|
|
{ |
266 |
michael |
4095 |
static struct event event_timeout_resolver = |
267 |
|
|
{ |
268 |
|
|
.name = "timeout_resolver", |
269 |
|
|
.handler = timeout_resolver, |
270 |
|
|
.when = 1 |
271 |
|
|
}; |
272 |
|
|
|
273 |
michael |
1654 |
dns_pool = mp_pool_new(sizeof(struct reslist), MP_CHUNK_SIZE_DNS); |
274 |
adx |
30 |
memset(&ResolverFileDescriptor, 0, sizeof(fde_t)); |
275 |
|
|
start_resolver(); |
276 |
michael |
4095 |
|
277 |
|
|
event_add(&event_timeout_resolver, NULL); |
278 |
adx |
30 |
} |
279 |
|
|
|
280 |
|
|
/* |
281 |
|
|
* restart_resolver - reread resolv.conf, reopen socket |
282 |
|
|
*/ |
283 |
|
|
void |
284 |
|
|
restart_resolver(void) |
285 |
|
|
{ |
286 |
|
|
fd_close(&ResolverFileDescriptor); |
287 |
|
|
start_resolver(); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
/* |
291 |
michael |
2916 |
* rem_request - remove a request from the list. |
292 |
|
|
* This must also free any memory that has been allocated for |
293 |
adx |
30 |
* temporary storage of DNS results. |
294 |
|
|
*/ |
295 |
|
|
static void |
296 |
|
|
rem_request(struct reslist *request) |
297 |
|
|
{ |
298 |
|
|
dlinkDelete(&request->node, &request_list); |
299 |
michael |
998 |
|
300 |
adx |
30 |
MyFree(request->name); |
301 |
michael |
1654 |
mp_pool_release(request); |
302 |
adx |
30 |
} |
303 |
|
|
|
304 |
|
|
/* |
305 |
|
|
* make_request - Create a DNS request record for the server. |
306 |
|
|
*/ |
307 |
|
|
static struct reslist * |
308 |
michael |
992 |
make_request(dns_callback_fnc callback, void *ctx) |
309 |
adx |
30 |
{ |
310 |
michael |
1654 |
struct reslist *request = mp_pool_get(dns_pool); |
311 |
adx |
30 |
|
312 |
michael |
992 |
request->sentat = CurrentTime; |
313 |
michael |
3268 |
request->retries = 2; |
314 |
michael |
992 |
request->resend = 1; |
315 |
michael |
3298 |
request->timeout = 4; /* Start at 4 and exponential inc. */ |
316 |
michael |
992 |
request->state = REQ_IDLE; |
317 |
|
|
request->callback = callback; |
318 |
|
|
request->callback_ctx = ctx; |
319 |
adx |
30 |
|
320 |
|
|
dlinkAdd(request, &request->node, &request_list); |
321 |
michael |
998 |
return request; |
322 |
adx |
30 |
} |
323 |
|
|
|
324 |
|
|
/* |
325 |
michael |
2916 |
* delete_resolver_queries - cleanup outstanding queries |
326 |
adx |
30 |
* for which there no longer exist clients or conf lines. |
327 |
|
|
*/ |
328 |
|
|
void |
329 |
michael |
992 |
delete_resolver_queries(const void *vptr) |
330 |
adx |
30 |
{ |
331 |
michael |
3235 |
dlink_node *ptr = NULL, *ptr_next = NULL; |
332 |
adx |
30 |
|
333 |
michael |
3235 |
DLINK_FOREACH_SAFE(ptr, ptr_next, request_list.head) |
334 |
adx |
30 |
{ |
335 |
michael |
997 |
struct reslist *request = ptr->data; |
336 |
|
|
|
337 |
|
|
if (request->callback_ctx == vptr) |
338 |
|
|
rem_request(request); |
339 |
adx |
30 |
} |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
/* |
343 |
|
|
* send_res_msg - sends msg to all nameservers found in the "_res" structure. |
344 |
|
|
* This should reflect /etc/resolv.conf. We will get responses |
345 |
|
|
* which arent needed but is easier than checking to see if nameserver |
346 |
michael |
2916 |
* isnt present. Returns number of messages successfully sent to |
347 |
adx |
30 |
* nameservers or -1 if no successful sends. |
348 |
|
|
*/ |
349 |
|
|
static int |
350 |
michael |
3297 |
send_res_msg(const char *msg, int len, unsigned int rcount) |
351 |
adx |
30 |
{ |
352 |
|
|
int sent = 0; |
353 |
michael |
3297 |
unsigned int max_queries = IRCD_MIN(irc_nscount, rcount); |
354 |
adx |
30 |
|
355 |
|
|
/* RES_PRIMARY option is not implemented |
356 |
|
|
* if (res.options & RES_PRIMARY || 0 == max_queries) |
357 |
|
|
*/ |
358 |
|
|
if (max_queries == 0) |
359 |
|
|
max_queries = 1; |
360 |
|
|
|
361 |
michael |
3297 |
for (unsigned int i = 0; i < max_queries; ++i) |
362 |
adx |
30 |
{ |
363 |
michael |
2916 |
if (sendto(ResolverFileDescriptor.fd, msg, len, 0, |
364 |
|
|
(struct sockaddr*)&(irc_nsaddr_list[i]), |
365 |
|
|
irc_nsaddr_list[i].ss_len) == len) |
366 |
adx |
30 |
++sent; |
367 |
|
|
} |
368 |
|
|
|
369 |
michael |
1124 |
return sent; |
370 |
adx |
30 |
} |
371 |
|
|
|
372 |
|
|
/* |
373 |
|
|
* find_id - find a dns request id (id is determined by dn_mkquery) |
374 |
|
|
*/ |
375 |
|
|
static struct reslist * |
376 |
|
|
find_id(int id) |
377 |
|
|
{ |
378 |
michael |
1124 |
dlink_node *ptr = NULL; |
379 |
adx |
30 |
|
380 |
|
|
DLINK_FOREACH(ptr, request_list.head) |
381 |
|
|
{ |
382 |
michael |
1124 |
struct reslist *request = ptr->data; |
383 |
adx |
30 |
|
384 |
|
|
if (request->id == id) |
385 |
michael |
1124 |
return request; |
386 |
adx |
30 |
} |
387 |
|
|
|
388 |
michael |
1124 |
return NULL; |
389 |
adx |
30 |
} |
390 |
|
|
|
391 |
michael |
2916 |
/* |
392 |
adx |
30 |
* gethost_byname_type - get host address from name |
393 |
|
|
* |
394 |
|
|
*/ |
395 |
|
|
void |
396 |
michael |
992 |
gethost_byname_type(dns_callback_fnc callback, void *ctx, const char *name, int type) |
397 |
adx |
30 |
{ |
398 |
michael |
3235 |
assert(name); |
399 |
michael |
992 |
do_query_name(callback, ctx, name, NULL, type); |
400 |
adx |
30 |
} |
401 |
|
|
|
402 |
|
|
/* |
403 |
|
|
* gethost_byname - wrapper for _type - send T_AAAA first if IPV6 supported |
404 |
|
|
*/ |
405 |
|
|
void |
406 |
michael |
992 |
gethost_byname(dns_callback_fnc callback, void *ctx, const char *name) |
407 |
adx |
30 |
{ |
408 |
|
|
#ifdef IPV6 |
409 |
michael |
992 |
gethost_byname_type(callback, ctx, name, T_AAAA); |
410 |
adx |
30 |
#else |
411 |
michael |
992 |
gethost_byname_type(callback, ctx, name, T_A); |
412 |
adx |
30 |
#endif |
413 |
|
|
} |
414 |
|
|
|
415 |
|
|
/* |
416 |
|
|
* gethost_byaddr - get host name from address |
417 |
|
|
*/ |
418 |
|
|
void |
419 |
michael |
992 |
gethost_byaddr(dns_callback_fnc callback, void *ctx, const struct irc_ssaddr *addr) |
420 |
adx |
30 |
{ |
421 |
michael |
992 |
do_query_number(callback, ctx, addr, NULL); |
422 |
adx |
30 |
} |
423 |
|
|
|
424 |
|
|
/* |
425 |
|
|
* do_query_name - nameserver lookup name |
426 |
|
|
*/ |
427 |
|
|
static void |
428 |
michael |
992 |
do_query_name(dns_callback_fnc callback, void *ctx, const char *name, |
429 |
adx |
30 |
struct reslist *request, int type) |
430 |
|
|
{ |
431 |
|
|
char host_name[HOSTLEN + 1]; |
432 |
|
|
|
433 |
michael |
998 |
strlcpy(host_name, name, sizeof(host_name)); |
434 |
adx |
30 |
|
435 |
|
|
if (request == NULL) |
436 |
|
|
{ |
437 |
michael |
992 |
request = make_request(callback, ctx); |
438 |
michael |
3505 |
request->name = MyCalloc(strlen(host_name) + 1); |
439 |
adx |
30 |
request->type = type; |
440 |
|
|
strcpy(request->name, host_name); |
441 |
|
|
#ifdef IPV6 |
442 |
michael |
984 |
if (type != T_A) |
443 |
|
|
request->state = REQ_AAAA; |
444 |
adx |
30 |
else |
445 |
michael |
984 |
#endif |
446 |
adx |
30 |
request->state = REQ_A; |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
request->type = type; |
450 |
|
|
query_name(host_name, C_IN, type, request); |
451 |
|
|
} |
452 |
|
|
|
453 |
|
|
/* |
454 |
|
|
* do_query_number - Use this to do reverse IP# lookups. |
455 |
|
|
*/ |
456 |
|
|
static void |
457 |
michael |
992 |
do_query_number(dns_callback_fnc callback, void *ctx, |
458 |
|
|
const struct irc_ssaddr *addr, |
459 |
adx |
30 |
struct reslist *request) |
460 |
|
|
{ |
461 |
michael |
3298 |
char ipbuf[128] = ""; |
462 |
michael |
985 |
|
463 |
adx |
30 |
if (addr->ss.ss_family == AF_INET) |
464 |
|
|
{ |
465 |
michael |
992 |
const struct sockaddr_in *v4 = (const struct sockaddr_in *)addr; |
466 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v4->sin_addr.s_addr; |
467 |
adx |
30 |
|
468 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), "%u.%u.%u.%u.in-addr.arpa.", |
469 |
|
|
(unsigned int)(cp[3]), (unsigned int)(cp[2]), |
470 |
|
|
(unsigned int)(cp[1]), (unsigned int)(cp[0])); |
471 |
adx |
30 |
} |
472 |
|
|
#ifdef IPV6 |
473 |
|
|
else if (addr->ss.ss_family == AF_INET6) |
474 |
|
|
{ |
475 |
michael |
992 |
const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)addr; |
476 |
michael |
3298 |
const unsigned char *cp = (const unsigned char *)&v6->sin6_addr.s6_addr; |
477 |
adx |
30 |
|
478 |
michael |
1124 |
snprintf(ipbuf, sizeof(ipbuf), |
479 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x." |
480 |
|
|
"%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.%x.ip6.arpa.", |
481 |
|
|
(unsigned int)(cp[15] & 0xf), (unsigned int)(cp[15] >> 4), |
482 |
|
|
(unsigned int)(cp[14] & 0xf), (unsigned int)(cp[14] >> 4), |
483 |
|
|
(unsigned int)(cp[13] & 0xf), (unsigned int)(cp[13] >> 4), |
484 |
|
|
(unsigned int)(cp[12] & 0xf), (unsigned int)(cp[12] >> 4), |
485 |
|
|
(unsigned int)(cp[11] & 0xf), (unsigned int)(cp[11] >> 4), |
486 |
|
|
(unsigned int)(cp[10] & 0xf), (unsigned int)(cp[10] >> 4), |
487 |
|
|
(unsigned int)(cp[9] & 0xf), (unsigned int)(cp[9] >> 4), |
488 |
|
|
(unsigned int)(cp[8] & 0xf), (unsigned int)(cp[8] >> 4), |
489 |
|
|
(unsigned int)(cp[7] & 0xf), (unsigned int)(cp[7] >> 4), |
490 |
|
|
(unsigned int)(cp[6] & 0xf), (unsigned int)(cp[6] >> 4), |
491 |
|
|
(unsigned int)(cp[5] & 0xf), (unsigned int)(cp[5] >> 4), |
492 |
|
|
(unsigned int)(cp[4] & 0xf), (unsigned int)(cp[4] >> 4), |
493 |
|
|
(unsigned int)(cp[3] & 0xf), (unsigned int)(cp[3] >> 4), |
494 |
|
|
(unsigned int)(cp[2] & 0xf), (unsigned int)(cp[2] >> 4), |
495 |
|
|
(unsigned int)(cp[1] & 0xf), (unsigned int)(cp[1] >> 4), |
496 |
|
|
(unsigned int)(cp[0] & 0xf), (unsigned int)(cp[0] >> 4)); |
497 |
adx |
30 |
} |
498 |
|
|
#endif |
499 |
|
|
if (request == NULL) |
500 |
|
|
{ |
501 |
michael |
992 |
request = make_request(callback, ctx); |
502 |
adx |
30 |
request->type = T_PTR; |
503 |
|
|
memcpy(&request->addr, addr, sizeof(struct irc_ssaddr)); |
504 |
michael |
3505 |
request->name = MyCalloc(HOSTLEN + 1); |
505 |
adx |
30 |
} |
506 |
|
|
|
507 |
|
|
query_name(ipbuf, C_IN, T_PTR, request); |
508 |
|
|
} |
509 |
|
|
|
510 |
|
|
/* |
511 |
|
|
* query_name - generate a query based on class, type and name. |
512 |
|
|
*/ |
513 |
|
|
static void |
514 |
|
|
query_name(const char *name, int query_class, int type, |
515 |
|
|
struct reslist *request) |
516 |
|
|
{ |
517 |
|
|
char buf[MAXPACKET]; |
518 |
|
|
int request_len = 0; |
519 |
|
|
|
520 |
|
|
memset(buf, 0, sizeof(buf)); |
521 |
|
|
|
522 |
michael |
2916 |
if ((request_len = irc_res_mkquery(name, query_class, type, |
523 |
adx |
30 |
(unsigned char *)buf, sizeof(buf))) > 0) |
524 |
|
|
{ |
525 |
|
|
HEADER *header = (HEADER *)buf; |
526 |
michael |
982 |
|
527 |
adx |
30 |
/* |
528 |
|
|
* generate an unique id |
529 |
|
|
* NOTE: we don't have to worry about converting this to and from |
530 |
|
|
* network byte order, the nameserver does not interpret this value |
531 |
|
|
* and returns it unchanged |
532 |
|
|
*/ |
533 |
|
|
do |
534 |
michael |
982 |
header->id = (header->id + genrand_int32()) & 0xffff; |
535 |
|
|
while (find_id(header->id)); |
536 |
|
|
|
537 |
adx |
30 |
request->id = header->id; |
538 |
|
|
++request->sends; |
539 |
|
|
|
540 |
|
|
request->sent += send_res_msg(buf, request_len, request->sends); |
541 |
|
|
} |
542 |
|
|
} |
543 |
|
|
|
544 |
|
|
static void |
545 |
|
|
resend_query(struct reslist *request) |
546 |
|
|
{ |
547 |
|
|
if (request->resend == 0) |
548 |
|
|
return; |
549 |
|
|
|
550 |
michael |
982 |
switch (request->type) |
551 |
adx |
30 |
{ |
552 |
|
|
case T_PTR: |
553 |
michael |
992 |
do_query_number(NULL, NULL, &request->addr, request); |
554 |
adx |
30 |
break; |
555 |
|
|
case T_A: |
556 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, request->type); |
557 |
adx |
30 |
break; |
558 |
|
|
#ifdef IPV6 |
559 |
|
|
case T_AAAA: |
560 |
|
|
/* didnt work, try A */ |
561 |
|
|
if (request->state == REQ_AAAA) |
562 |
michael |
992 |
do_query_name(NULL, NULL, request->name, request, T_A); |
563 |
adx |
30 |
#endif |
564 |
|
|
default: |
565 |
|
|
break; |
566 |
|
|
} |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
/* |
570 |
|
|
* proc_answer - process name server reply |
571 |
|
|
*/ |
572 |
|
|
static int |
573 |
michael |
1124 |
proc_answer(struct reslist *request, HEADER *header, char *buf, char *eob) |
574 |
adx |
30 |
{ |
575 |
|
|
char hostbuf[HOSTLEN + 100]; /* working buffer */ |
576 |
|
|
unsigned char *current; /* current position in buf */ |
577 |
|
|
int query_class; /* answer class */ |
578 |
|
|
int type; /* answer type */ |
579 |
|
|
int n; /* temp count */ |
580 |
|
|
int rd_length; |
581 |
|
|
struct sockaddr_in *v4; /* conversion */ |
582 |
|
|
#ifdef IPV6 |
583 |
|
|
struct sockaddr_in6 *v6; |
584 |
|
|
#endif |
585 |
|
|
current = (unsigned char *)buf + sizeof(HEADER); |
586 |
|
|
|
587 |
|
|
for (; header->qdcount > 0; --header->qdcount) |
588 |
|
|
{ |
589 |
|
|
if ((n = irc_dn_skipname(current, (unsigned char *)eob)) < 0) |
590 |
|
|
break; |
591 |
|
|
|
592 |
michael |
1124 |
current += (size_t)n + QFIXEDSZ; |
593 |
adx |
30 |
} |
594 |
|
|
|
595 |
|
|
/* |
596 |
|
|
* process each answer sent to us blech. |
597 |
|
|
*/ |
598 |
|
|
while (header->ancount > 0 && (char *)current < eob) |
599 |
|
|
{ |
600 |
|
|
header->ancount--; |
601 |
|
|
|
602 |
|
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, current, |
603 |
|
|
hostbuf, sizeof(hostbuf)); |
604 |
|
|
|
605 |
michael |
3298 |
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
606 |
michael |
1124 |
return 0; |
607 |
adx |
30 |
|
608 |
|
|
hostbuf[HOSTLEN] = '\0'; |
609 |
|
|
|
610 |
|
|
/* With Address arithmetic you have to be very anal |
611 |
|
|
* this code was not working on alpha due to that |
612 |
|
|
* (spotted by rodder/jailbird/dianora) |
613 |
|
|
*/ |
614 |
michael |
3298 |
current += (size_t)n; |
615 |
adx |
30 |
|
616 |
|
|
if (!(((char *)current + ANSWER_FIXED_SIZE) < eob)) |
617 |
|
|
break; |
618 |
|
|
|
619 |
|
|
type = irc_ns_get16(current); |
620 |
|
|
current += TYPE_SIZE; |
621 |
|
|
|
622 |
|
|
query_class = irc_ns_get16(current); |
623 |
|
|
current += CLASS_SIZE; |
624 |
|
|
|
625 |
|
|
request->ttl = irc_ns_get32(current); |
626 |
|
|
current += TTL_SIZE; |
627 |
|
|
|
628 |
|
|
rd_length = irc_ns_get16(current); |
629 |
|
|
current += RDLENGTH_SIZE; |
630 |
|
|
|
631 |
michael |
2916 |
/* |
632 |
|
|
* Wait to set request->type until we verify this structure |
633 |
adx |
30 |
*/ |
634 |
|
|
switch (type) |
635 |
|
|
{ |
636 |
|
|
case T_A: |
637 |
|
|
if (request->type != T_A) |
638 |
michael |
1124 |
return 0; |
639 |
adx |
30 |
|
640 |
|
|
/* |
641 |
michael |
3298 |
* Check for invalid rd_length or too many addresses |
642 |
adx |
30 |
*/ |
643 |
|
|
if (rd_length != sizeof(struct in_addr)) |
644 |
michael |
1124 |
return 0; |
645 |
|
|
|
646 |
adx |
30 |
v4 = (struct sockaddr_in *)&request->addr; |
647 |
|
|
request->addr.ss_len = sizeof(struct sockaddr_in); |
648 |
|
|
v4->sin_family = AF_INET; |
649 |
|
|
memcpy(&v4->sin_addr, current, sizeof(struct in_addr)); |
650 |
michael |
1124 |
return 1; |
651 |
adx |
30 |
break; |
652 |
|
|
#ifdef IPV6 |
653 |
|
|
case T_AAAA: |
654 |
|
|
if (request->type != T_AAAA) |
655 |
michael |
1124 |
return 0; |
656 |
|
|
|
657 |
adx |
30 |
if (rd_length != sizeof(struct in6_addr)) |
658 |
michael |
1124 |
return 0; |
659 |
|
|
|
660 |
adx |
30 |
request->addr.ss_len = sizeof(struct sockaddr_in6); |
661 |
|
|
v6 = (struct sockaddr_in6 *)&request->addr; |
662 |
|
|
v6->sin6_family = AF_INET6; |
663 |
|
|
memcpy(&v6->sin6_addr, current, sizeof(struct in6_addr)); |
664 |
michael |
1124 |
return 1; |
665 |
adx |
30 |
break; |
666 |
|
|
#endif |
667 |
|
|
case T_PTR: |
668 |
|
|
if (request->type != T_PTR) |
669 |
michael |
1124 |
return 0; |
670 |
|
|
|
671 |
adx |
30 |
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
672 |
michael |
3298 |
current, hostbuf, sizeof(hostbuf)); |
673 |
|
|
if (n < 0 /* Broken message */ || n == 0 /* No more answers left */) |
674 |
michael |
1124 |
return 0; |
675 |
adx |
30 |
|
676 |
michael |
986 |
strlcpy(request->name, hostbuf, HOSTLEN + 1); |
677 |
michael |
1124 |
return 1; |
678 |
adx |
30 |
break; |
679 |
michael |
2916 |
case T_CNAME: /* first check we already havent started looking |
680 |
adx |
30 |
into a cname */ |
681 |
michael |
2916 |
if (request->type != T_PTR) |
682 |
michael |
1124 |
return 0; |
683 |
adx |
30 |
|
684 |
|
|
if (request->state == REQ_CNAME) |
685 |
|
|
{ |
686 |
|
|
n = irc_dn_expand((unsigned char *)buf, (unsigned char *)eob, |
687 |
|
|
current, hostbuf, sizeof(hostbuf)); |
688 |
|
|
|
689 |
|
|
if (n < 0) |
690 |
michael |
1124 |
return 0; |
691 |
|
|
return 1; |
692 |
adx |
30 |
} |
693 |
|
|
|
694 |
|
|
request->state = REQ_CNAME; |
695 |
|
|
current += rd_length; |
696 |
|
|
break; |
697 |
michael |
2916 |
|
698 |
adx |
30 |
default: |
699 |
|
|
/* XXX I'd rather just throw away the entire bogus thing |
700 |
|
|
* but its possible its just a broken nameserver with still |
701 |
|
|
* valid answers. But lets do some rudimentary logging for now... |
702 |
|
|
*/ |
703 |
michael |
1247 |
ilog(LOG_TYPE_IRCD, "irc_res.c bogus type %d", type); |
704 |
adx |
30 |
break; |
705 |
|
|
} |
706 |
|
|
} |
707 |
|
|
|
708 |
michael |
1124 |
return 1; |
709 |
adx |
30 |
} |
710 |
|
|
|
711 |
|
|
/* |
712 |
|
|
* res_readreply - read a dns reply from the nameserver and process it. |
713 |
|
|
*/ |
714 |
|
|
static void |
715 |
|
|
res_readreply(fde_t *fd, void *data) |
716 |
|
|
{ |
717 |
adx |
411 |
char buf[sizeof(HEADER) + MAXPACKET] |
718 |
michael |
2916 |
/* Sparc and alpha need 16bit-alignment for accessing header->id |
719 |
|
|
* (which is uint16_t). Because of the header = (HEADER*) buf; |
720 |
adx |
411 |
* lateron, this is neeeded. --FaUl |
721 |
|
|
*/ |
722 |
michael |
2916 |
#if defined(__sparc__) || defined(__alpha__) |
723 |
adx |
411 |
__attribute__((aligned (16))) |
724 |
michael |
2916 |
#endif |
725 |
adx |
411 |
; |
726 |
adx |
30 |
HEADER *header; |
727 |
|
|
struct reslist *request = NULL; |
728 |
|
|
int rc; |
729 |
|
|
socklen_t len = sizeof(struct irc_ssaddr); |
730 |
|
|
struct irc_ssaddr lsin; |
731 |
|
|
|
732 |
|
|
rc = recvfrom(fd->fd, buf, sizeof(buf), 0, (struct sockaddr *)&lsin, &len); |
733 |
|
|
|
734 |
|
|
/* Re-schedule a read *after* recvfrom, or we'll be registering |
735 |
|
|
* interest where it'll instantly be ready for read :-) -- adrian |
736 |
|
|
*/ |
737 |
|
|
comm_setselect(fd, COMM_SELECT_READ, res_readreply, NULL, 0); |
738 |
michael |
984 |
|
739 |
adx |
30 |
/* Better to cast the sizeof instead of rc */ |
740 |
|
|
if (rc <= (int)(sizeof(HEADER))) |
741 |
|
|
return; |
742 |
|
|
|
743 |
|
|
/* |
744 |
michael |
3298 |
* Convert DNS reply reader from Network byte order to CPU byte order. |
745 |
adx |
30 |
*/ |
746 |
|
|
header = (HEADER *)buf; |
747 |
|
|
header->ancount = ntohs(header->ancount); |
748 |
|
|
header->qdcount = ntohs(header->qdcount); |
749 |
|
|
header->nscount = ntohs(header->nscount); |
750 |
|
|
header->arcount = ntohs(header->arcount); |
751 |
|
|
|
752 |
|
|
/* |
753 |
michael |
3298 |
* Check against possibly fake replies |
754 |
adx |
30 |
*/ |
755 |
michael |
994 |
if (!res_ourserver(&lsin)) |
756 |
adx |
30 |
return; |
757 |
|
|
|
758 |
|
|
/* |
759 |
michael |
3298 |
* Response for an id which we have already received an answer for |
760 |
michael |
994 |
* just ignore this response. |
761 |
adx |
30 |
*/ |
762 |
michael |
994 |
if (!(request = find_id(header->id))) |
763 |
adx |
30 |
return; |
764 |
|
|
|
765 |
|
|
if ((header->rcode != NO_ERRORS) || (header->ancount == 0)) |
766 |
|
|
{ |
767 |
michael |
984 |
if (header->rcode == SERVFAIL || header->rcode == NXDOMAIN) |
768 |
db |
155 |
{ |
769 |
michael |
984 |
/* |
770 |
|
|
* If a bad error was returned, stop here and don't |
771 |
|
|
* send any more (no retries granted). |
772 |
|
|
*/ |
773 |
michael |
992 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
774 |
michael |
984 |
rem_request(request); |
775 |
|
|
} |
776 |
|
|
#ifdef IPV6 |
777 |
|
|
else |
778 |
|
|
{ |
779 |
michael |
2916 |
/* |
780 |
adx |
30 |
* If we havent already tried this, and we're looking up AAAA, try A |
781 |
|
|
* now |
782 |
|
|
*/ |
783 |
|
|
if (request->state == REQ_AAAA && request->type == T_AAAA) |
784 |
|
|
{ |
785 |
|
|
request->timeout += 4; |
786 |
|
|
resend_query(request); |
787 |
|
|
} |
788 |
michael |
984 |
} |
789 |
adx |
30 |
#endif |
790 |
michael |
984 |
|
791 |
db |
468 |
return; |
792 |
adx |
30 |
} |
793 |
michael |
984 |
|
794 |
adx |
30 |
/* |
795 |
michael |
2916 |
* If this fails there was an error decoding the received packet, |
796 |
adx |
30 |
* try it again and hope it works the next time. |
797 |
|
|
*/ |
798 |
michael |
994 |
if (proc_answer(request, header, buf, buf + rc)) |
799 |
adx |
30 |
{ |
800 |
|
|
if (request->type == T_PTR) |
801 |
|
|
{ |
802 |
|
|
if (request->name == NULL) |
803 |
|
|
{ |
804 |
|
|
/* |
805 |
michael |
3298 |
* Got a PTR response with no name, something bogus is happening |
806 |
adx |
30 |
* don't bother trying again, the client address doesn't resolve |
807 |
|
|
*/ |
808 |
michael |
992 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
809 |
adx |
30 |
rem_request(request); |
810 |
|
|
return; |
811 |
|
|
} |
812 |
|
|
|
813 |
|
|
/* |
814 |
michael |
3298 |
* Lookup the 'authoritative' name that we were given for the ip#. |
815 |
adx |
30 |
*/ |
816 |
|
|
#ifdef IPV6 |
817 |
|
|
if (request->addr.ss.ss_family == AF_INET6) |
818 |
michael |
992 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_AAAA); |
819 |
adx |
30 |
else |
820 |
|
|
#endif |
821 |
michael |
992 |
gethost_byname_type(request->callback, request->callback_ctx, request->name, T_A); |
822 |
adx |
30 |
rem_request(request); |
823 |
|
|
} |
824 |
|
|
else |
825 |
|
|
{ |
826 |
|
|
/* |
827 |
michael |
3298 |
* Got a name and address response, client resolved |
828 |
adx |
30 |
*/ |
829 |
michael |
992 |
(*request->callback)(request->callback_ctx, &request->addr, request->name); |
830 |
adx |
30 |
rem_request(request); |
831 |
|
|
} |
832 |
|
|
} |
833 |
|
|
else if (!request->sent) |
834 |
|
|
{ |
835 |
|
|
/* XXX - we got a response for a query we didn't send with a valid id? |
836 |
|
|
* this should never happen, bail here and leave the client unresolved |
837 |
|
|
*/ |
838 |
|
|
assert(0); |
839 |
|
|
|
840 |
michael |
4235 |
(*request->callback)(request->callback_ctx, NULL, NULL); |
841 |
adx |
30 |
/* XXX don't leak it */ |
842 |
|
|
rem_request(request); |
843 |
|
|
} |
844 |
|
|
} |
845 |
|
|
|
846 |
|
|
void |
847 |
|
|
report_dns_servers(struct Client *source_p) |
848 |
|
|
{ |
849 |
michael |
3235 |
char ipaddr[HOSTIPLEN + 1] = ""; |
850 |
adx |
30 |
|
851 |
michael |
3297 |
for (unsigned int i = 0; i < irc_nscount; ++i) |
852 |
adx |
30 |
{ |
853 |
michael |
1123 |
getnameinfo((struct sockaddr *)&(irc_nsaddr_list[i]), |
854 |
|
|
irc_nsaddr_list[i].ss_len, ipaddr, |
855 |
|
|
sizeof(ipaddr), NULL, 0, NI_NUMERICHOST); |
856 |
michael |
3109 |
sendto_one_numeric(source_p, &me, RPL_STATSALINE, ipaddr); |
857 |
adx |
30 |
} |
858 |
|
|
} |