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"
26 struct _IODNSQuery *iodnsquery_first = NULL;
27 struct _IODNSQuery *iodnsquery_last = NULL;
29 struct IODNSEngine *dnsengine = NULL;
31 static void iodns_init_engine() {
35 if(dnsengine_cares.init && dnsengine_cares.init())
36 dnsengine = &dnsengine_cares;
37 else if(dnsengine_default.init && dnsengine_default.init())
38 dnsengine = &dnsengine_default;
40 iolog_trigger(IOLOG_FATAL, "found no useable IO DNS engine");
43 iolog_trigger(IOLOG_DEBUG, "using %s IODNS engine", dnsengine->name);
50 struct _IODNSQuery *_create_dnsquery() {
51 struct _IODNSQuery *query = calloc(1, sizeof(*query));
53 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IODNSQuery in %s:%d", __FILE__, __LINE__);
57 iodnsquery_last->next = query;
59 iodnsquery_first = query;
60 query->prev = iodnsquery_last;
61 iodnsquery_last = query;
65 void _start_dnsquery(struct _IODNSQuery *query) {
66 query->flags |= IODNSFLAG_RUNNING;
67 dnsengine->add(query);
70 void _free_dnsquery(struct _IODNSQuery *query) {
72 query->prev->next = query->next;
74 iodnsquery_first = query->next;
76 query->next->prev = query->prev;
78 iodnsquery_last = query->prev;
79 if((query->type & IODNS_REVERSE) && query->request.addr.address)
80 free(query->request.addr.address);
84 void _stop_dnsquery(struct _IODNSQuery *query) {
85 if((query->flags & IODNSFLAG_RUNNING)) {
86 query->flags &= ~IODNSFLAG_RUNNING;
87 dnsengine->remove(query);
89 if(!(query->flags & IODNSFLAG_PROCESSING))
90 _free_dnsquery(query);
93 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state) {
94 if((query->flags & IODNSFLAG_PARENT_PUBLIC)) {
95 struct IODNSQuery *descriptor = query->parent;
96 struct IODNSEvent event;
98 event.query = descriptor;
99 event.result = query->result;
101 descriptor->query = NULL;
102 _stop_dnsquery(query);
104 if(descriptor->callback)
105 descriptor->callback(&event);
107 iogc_add(descriptor);
108 } else if((query->flags & IODNSFLAG_PARENT_SOCKET)) {
109 struct IODNSEvent event;
112 event.result = query->result;
113 void *parent = query->parent;
115 _stop_dnsquery(query);
116 iosocket_lookup_callback(parent, &event);
126 /* public functions */
128 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback) {
129 if(!(records & IODNS_FORWARD) || !hostname || !callback)
132 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
134 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
138 struct _IODNSQuery *query = _create_dnsquery();
144 query->parent = descriptor;
145 query->flags |= IODNSFLAG_PARENT_PUBLIC;
146 descriptor->query = query;
148 query->request.host = strdup(hostname);
149 query->type = (records & IODNS_FORWARD);
151 descriptor->callback = callback;
153 _start_dnsquery(query);
157 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback) {
158 if(!addr || !callback)
161 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
163 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
167 struct _IODNSQuery *query = _create_dnsquery();
173 query->parent = descriptor;
174 query->flags |= IODNSFLAG_PARENT_PUBLIC;
175 descriptor->query = query;
177 query->type = IODNS_RECORD_PTR;
178 query->request.addr.addresslen = addrlen;
179 query->request.addr.address = malloc(addrlen);
180 if(!query->request.addr.address) {
181 iolog_trigger(IOLOG_ERROR, "could not allocate memory for sockaddr in %s:%d", __FILE__, __LINE__);
182 _free_dnsquery(query);
186 memcpy(query->request.addr.address, addr, addrlen);
188 descriptor->callback = callback;
190 _start_dnsquery(query);
194 void iodns_abort(struct IODNSQuery *descriptor) {
198 struct _IODNSQuery *query = descriptor->query;
200 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
204 _stop_dnsquery(query);
207 void iodns_free_result(struct IODNSResult *result) {
208 struct IODNSResult *next;
209 for(;result;result = next) {
212 if((result->type & IODNS_FORWARD)) {
213 if(result->result.addr.address)
214 free(result->result.addr.address);
216 if((result->type & IODNS_REVERSE)) {
217 if(result->result.host)
218 free(result->result.host);