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