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