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