1 /* IODNSLookup.c - IOMultiplexer
2 * Copyright (C) 2014 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IODNSLookup.h"
22 #include "IOSockets.h"
25 pthread_mutex_t iodns_sync;
28 struct _IODNSQuery *iodnsquery_first = NULL;
29 struct _IODNSQuery *iodnsquery_last = NULL;
31 struct IODNSEngine *dnsengine = NULL;
33 static void iodns_init_engine() {
37 if(dnsengine_cares.init && dnsengine_cares.init())
38 dnsengine = &dnsengine_cares;
39 else if(dnsengine_default.init && dnsengine_default.init())
40 dnsengine = &dnsengine_default;
42 iohandler_log(IOLOG_FATAL, "found no useable IO DNS engine");
45 iohandler_log(IOLOG_DEBUG, "using %s IODNS engine", dnsengine->name);
49 IOTHREAD_MUTEX_INIT(iodns_sync);
53 struct _IODNSQuery *_create_dnsquery() {
54 struct _IODNSQuery *query = calloc(1, sizeof(*query));
56 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IODNSQuery in %s:%d", __FILE__, __LINE__);
59 IOSYNCHRONIZE(iodns_sync);
61 iodnsquery_last->next = query;
63 iodnsquery_first = query;
64 query->prev = iodnsquery_last;
65 iodnsquery_last = query;
66 IODESYNCHRONIZE(iodns_sync);
70 void _start_dnsquery(struct _IODNSQuery *query) {
71 IOSYNCHRONIZE(iodns_sync);
72 query->flags |= IODNSFLAG_RUNNING;
73 dnsengine->add(query);
74 IODESYNCHRONIZE(iodns_sync);
77 void _free_dnsquery(struct _IODNSQuery *query) {
78 IOSYNCHRONIZE(iodns_sync);
80 query->prev->next = query->next;
82 iodnsquery_first = query->next;
84 query->next->prev = query->prev;
86 iodnsquery_last = query->prev;
87 IODESYNCHRONIZE(iodns_sync);
88 if((query->type & IODNS_REVERSE) && query->request.addr.address)
89 free(query->request.addr.address);
93 void _stop_dnsquery(struct _IODNSQuery *query) {
94 IOSYNCHRONIZE(iodns_sync);
95 if((query->flags & IODNSFLAG_RUNNING)) {
96 query->flags &= ~IODNSFLAG_RUNNING;
97 dnsengine->remove(query);
99 if(!(query->flags & IODNSFLAG_PROCESSING))
100 _free_dnsquery(query);
101 IODESYNCHRONIZE(iodns_sync);
104 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state) {
105 if((query->flags & IODNSFLAG_PARENT_PUBLIC)) {
106 struct IODNSQuery *descriptor = query->parent;
107 struct IODNSEvent event;
109 event.query = descriptor;
110 event.result = query->result;
112 descriptor->parent = NULL;
113 _stop_dnsquery(query);
115 if(descriptor->callback)
116 descriptor->callback(&event);
118 iogc_add(descriptor);
119 } else if((query->flags & IODNSFLAG_PARENT_SOCKET)) {
120 struct IODNSEvent event;
123 event.result = query->result
124 void *parent = query->parent;
126 _stop_dnsquery(query);
127 iosocket_lookup_callback(parent, &event);
137 /* public functions */
139 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback) {
140 if(!(records & IODNS_FORWARD) || !hostname || !callback)
143 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
145 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
149 struct _IODNSQuery *query = _create_dnsquery();
155 query->parent = descriptor;
156 query->flags |= IODNSFLAG_PARENT_PUBLIC;
157 descriptor->query = query;
159 query->request.host = strdup(hostname);
160 query->type = (records & IODNS_FORWARD);
162 descriptor->callback = callback;
164 _start_dnsquery(query);
168 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, socklen_t addrlen, iodns_callback *callback) {
169 if(!addr || !callback)
172 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
174 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
178 struct _IODNSQuery *query = _create_dnsquery();
184 query->parent = descriptor;
185 query->flags |= IODNSFLAG_PARENT_PUBLIC;
186 descriptor->query = query;
188 query->type = IODNS_RECORD_PTR;
189 query->request.addr.addresslen = addrlen;
190 query->request.addr.address = malloc(addrlen);
191 if(!query->request.addr.address) {
192 iolog_trigger(IOLOG_ERROR, "could not allocate memory for sockaddr in %s:%d", __FILE__, __LINE__);
193 _free_dnsquery(query);
197 memcpy(query->request.addr.address, addr, addrlen);
199 descriptor->callback = callback;
201 _start_dnsquery(query);
205 void iodns_abort(struct IODNSQuery *descriptor) {
209 struct _IODNSQuery *query = descriptor->query;
211 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
215 _stop_dnsquery(query)
218 void iodns_free_result(struct IODNSResult *result) {
219 struct IODNSResult *next;
220 for(;result;result = next) {
223 if((result->type & IODNS_FORWARD)) {
224 if(result->result.addr.address)
225 free(result->result.addr.address);
227 if((result->type & IODNS_REVERSE)) {
228 if(result->result.host)
229 free(result->result.host);