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