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