2 * dns.c: An interface to the resolver module in authd
3 * (based somewhat on ircd-ratbox dns.c)
5 * Copyright (C) 2005 Aaron Sethman <androsyn@ratbox.org>
6 * Copyright (C) 2005-2012 ircd-ratbox development team
7 * Copyright (C) 2016 William Pitcock <nenolod@dereferenced.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
28 #include "ircd_defs.h"
41 #define DNS_HOST_IPV4 ((char)'4')
42 #define DNS_HOST_IPV6 ((char)'6')
43 #define DNS_REVERSE_IPV4 ((char)'R')
44 #define DNS_REVERSE_IPV6 ((char)'S')
46 static void submit_dns(uint32_t uid
, char type
, const char *addr
);
47 static void submit_dns_stat(uint32_t uid
);
61 /* These serve as a form of sparse array */
62 static rb_dictionary
*query_dict
;
63 static rb_dictionary
*stat_dict
;
65 rb_dlink_list nameservers
;
67 static uint32_t query_id
= 0;
68 static uint32_t stat_id
= 0;
71 static inline uint32_t
72 assign_id(uint32_t *id
)
81 handle_dns_failure(uint32_t xid
)
83 struct dnsreq
*req
= rb_dictionary_retrieve(query_dict
, RB_UINT_TO_POINTER(xid
));
86 if(req
== NULL
|| req
->callback
== NULL
)
89 req
->callback("FAILED", 0, 0, req
->data
);
95 handle_dns_stat_failure(uint32_t xid
)
97 struct dnsstatreq
*req
= rb_dictionary_retrieve(stat_dict
, RB_UINT_TO_POINTER(xid
));
100 if(req
== NULL
|| req
->callback
== NULL
)
103 req
->callback(1, NULL
, 2, req
->data
);
104 req
->callback
= NULL
;
110 cancel_lookup(uint32_t xid
)
112 struct dnsreq
*req
= rb_dictionary_retrieve(query_dict
, RB_UINT_TO_POINTER(xid
));
118 req
->callback
= NULL
;
123 cancel_dns_stats(uint32_t xid
)
125 struct dnsstatreq
*req
= rb_dictionary_retrieve(stat_dict
, RB_UINT_TO_POINTER(xid
));
131 req
->callback
= NULL
;
137 lookup_hostname(const char *hostname
, int aftype
, DNSCB callback
, void *data
)
139 struct dnsreq
*req
= rb_malloc(sizeof(struct dnsreq
));
141 uint32_t rid
= assign_id(&query_id
);
145 rb_dictionary_add(query_dict
, RB_UINT_TO_POINTER(rid
), req
);
147 req
->callback
= callback
;
150 if(aftype
== AF_INET6
)
155 submit_dns(rid
, aft
== 4 ? DNS_HOST_IPV4
: DNS_HOST_IPV6
, hostname
);
160 lookup_ip(const char *addr
, int aftype
, DNSCB callback
, void *data
)
162 struct dnsreq
*req
= rb_malloc(sizeof(struct dnsreq
));
164 uint32_t rid
= assign_id(&query_id
);
168 rb_dictionary_add(query_dict
, RB_UINT_TO_POINTER(rid
), req
);
170 req
->callback
= callback
;
173 if(aftype
== AF_INET6
)
178 submit_dns(rid
, aft
== 4 ? DNS_REVERSE_IPV4
: DNS_REVERSE_IPV6
, addr
);
183 get_nameservers(DNSLISTCB callback
, void *data
)
185 struct dnsstatreq
*req
= rb_malloc(sizeof(struct dnsstatreq
));
186 uint32_t qid
= assign_id(&stat_id
);
190 rb_dictionary_add(stat_dict
, RB_UINT_TO_POINTER(qid
), req
);
192 req
->callback
= callback
;
195 submit_dns_stat(qid
);
201 dns_results_callback(const char *callid
, const char *status
, const char *type
, const char *results
)
207 long lrid
= strtol(callid
, NULL
, 16);
209 if(lrid
> UINT32_MAX
)
212 rid
= (uint32_t)lrid
;
213 req
= rb_dictionary_retrieve(query_dict
, RB_UINT_TO_POINTER(rid
));
217 st
= (*status
== 'O');
218 aft
= *type
== '6' || *type
== 'S' ? 6 : 4;
219 if(req
->callback
== NULL
)
221 /* got cancelled..oh well */
230 req
->callback(results
, st
, aft
, req
->data
);
233 rb_dictionary_delete(query_dict
, RB_UINT_TO_POINTER(rid
));
237 dns_stats_results_callback(const char *callid
, const char *status
, int resc
, const char *resv
[])
239 struct dnsstatreq
*req
;
242 long lqid
= strtol(callid
, NULL
, 16);
244 if(lqid
> UINT32_MAX
)
247 qid
= (uint32_t)lqid
;
248 req
= rb_dictionary_retrieve(stat_dict
, RB_UINT_TO_POINTER(qid
));
254 if(req
->callback
== NULL
)
270 /* Shouldn't happen... */
275 req
->callback(resc
, resv
, st
, req
->data
);
278 rb_dictionary_delete(stat_dict
, RB_UINT_TO_POINTER(qid
));
282 stats_results_callback(int resc
, const char *resv
[], int status
, void *data
)
286 rb_dlink_node
*n
, *tn
;
288 RB_DLINK_FOREACH_SAFE(n
, tn
, nameservers
.head
)
290 /* Clean up old nameservers */
292 rb_dlinkDestroy(n
, &nameservers
);
295 for(int i
= 0; i
< resc
; i
++)
296 rb_dlinkAddAlloc(rb_strdup(resv
[i
]), &nameservers
);
300 const char *error
= resc
? resv
[resc
- 1] : "Unknown error";
301 iwarn("Error getting DNS servers: %s", error
);
309 query_dict
= rb_dictionary_create("dns queries", rb_uint32cmp
);
310 stat_dict
= rb_dictionary_create("dns stat queries", rb_uint32cmp
);
311 (void)get_nameservers(stats_results_callback
, NULL
);
315 reload_nameservers(void)
318 rb_helper_write(authd_helper
, "R D");
319 (void)get_nameservers(stats_results_callback
, NULL
);
324 submit_dns(uint32_t nid
, char type
, const char *addr
)
326 if(authd_helper
== NULL
)
328 handle_dns_failure(nid
);
331 rb_helper_write(authd_helper
, "D %x %c %s", nid
, type
, addr
);
335 submit_dns_stat(uint32_t nid
)
337 if(authd_helper
== NULL
)
339 handle_dns_stat_failure(nid
);
342 rb_helper_write(authd_helper
, "S %x D", nid
);