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"
24 pthread_mutex_t iodns_sync;
27 struct _IODNSQuery *iodnsquery_first = NULL;
28 struct _IODNSQuery *iodnsquery_last = NULL;
30 struct IODNSEngine *dnsengine = NULL;
32 static void iodns_init_engine() {
36 if(dnsengine_cares.init && dnsengine_cares.init())
37 dnsengine = &dnsengine_cares;
38 else if(dnsengine_default.init && dnsengine_default.init())
39 dnsengine = &dnsengine_default;
41 iohandler_log(IOLOG_FATAL, "found no useable IO DNS engine");
47 IOTHREAD_MUTEX_INIT(iodns_sync);
51 struct _IODNSQuery *_create_dnsquery() {
52 struct _IODNSQuery *query = calloc(1, sizeof(*query));
54 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IODNSQuery in %s:%d", __FILE__, __LINE__);
57 IOSYNCHRONIZE(iodns_sync);
59 iodnsquery_last->next = query;
61 iodnsquery_first = query;
62 query->prev = iodnsquery_last;
63 iodnsquery_last = query;
64 IODESYNCHRONIZE(iodns_sync);
68 void _start_dnsquery(struct _IODNSQuery *query) {
69 IOSYNCHRONIZE(iodns_sync);
70 query->flags |= IODNSFLAG_RUNNING;
71 dnsengine->add(query);
72 IODESYNCHRONIZE(iodns_sync);
75 void _free_dnsquery(struct _IODNSQuery *query) {
76 IOSYNCHRONIZE(iodns_sync);
78 query->prev->next = query->next;
80 iodnsquery_first = query->next;
82 query->next->prev = query->prev;
84 iodnsquery_last = query->prev;
85 IODESYNCHRONIZE(iodns_sync);
86 if((query->type & IODNS_REVERSE) && query->request.addr.address)
87 free(query->request.addr.address);
91 void _stop_dnsquery(struct _IODNSQuery *query) {
92 IOSYNCHRONIZE(iodns_sync);
93 if((query->flags & IODNSFLAG_RUNNING)) {
94 query->flags &= ~IODNSFLAG_RUNNING;
95 dnsengine->remove(query);
97 if(!(query->flags & IODNSFLAG_PROCESSING))
98 _free_dnsquery(query);
99 IODESYNCHRONIZE(iodns_sync);
102 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state) {
103 if((query->flags & IODNSFLAG_PARENT_PUBLIC)) {
104 struct IODNSQuery *descriptor = query->parent;
105 struct IODNSEvent event;
107 event.query = descriptor;
109 descriptor->parent = NULL;
110 _stop_dnsquery(query);
112 if(descriptor->callback)
113 descriptor->callback(&event);
115 iogc_add(descriptor);
124 /* public functions */
126 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback) {
127 if(!(records & IODNS_FORWARD) || !hostname || !callback)
130 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
132 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
136 struct _IODNSQuery *query = _create_dnsquery();
142 query->parent = descriptor;
143 query->flags |= IODNSFLAG_PARENT_PUBLIC;
144 descriptor->query = query;
146 query->request.host = strdup(hostname);
147 query->type = (records & IODNS_FORWARD);
149 descriptor->callback = callback;
151 _start_dnsquery(query);
155 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, socklen_t addrlen, iodns_callback *callback) {
156 if(!addr || !callback)
159 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
161 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
165 struct _IODNSQuery *query = _create_dnsquery();
171 query->parent = descriptor;
172 query->flags |= IODNSFLAG_PARENT_PUBLIC;
173 descriptor->query = query;
175 query->type = IODNS_RECORD_PTR;
176 query->request.addr.addresslen = addrlen;
177 query->request.addr.address = malloc(addrlen);
178 if(!query->request.addr.address) {
179 iolog_trigger(IOLOG_ERROR, "could not allocate memory for sockaddr in %s:%d", __FILE__, __LINE__);
180 _free_dnsquery(query);
184 memcpy(query->request.addr.address, addr, addrlen);
186 descriptor->callback = callback;
188 _start_dnsquery(query);
192 void iodns_abort(struct IODNSQuery *descriptor) {
196 struct _IODNSQuery *query = descriptor->query;
198 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
202 _stop_dnsquery(query)
205 void iodns_free_result(struct IODNSResult *result) {
206 struct IODNSResult *next;
207 for(;result;result = next) {
210 if((result->type & IODNS_FORWARD)) {
211 if(result->result.addr.address)
212 free(result->result.addr.address);
214 if((result->type & IODNS_REVERSE)) {
215 if(result->result.host)
216 free(result->result.host);