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_socket_callback(struct _IOSocket *iosock, int wantread, int wantwrite) {
94 if(dnsengine && dnsengine->socket_callback)
95 dnsengine->socket_callback(iosock, wantread, wantwrite);
98 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state) {
99 if((query->flags & IODNSFLAG_PARENT_PUBLIC)) {
100 struct IODNSQuery *descriptor = query->parent;
101 struct IODNSEvent event;
103 event.query = descriptor;
104 event.result = query->result;
106 descriptor->query = NULL;
107 _stop_dnsquery(query);
109 if(descriptor->callback)
110 descriptor->callback(&event);
112 iogc_add(descriptor);
113 } else if((query->flags & IODNSFLAG_PARENT_SOCKET)) {
114 struct IODNSEvent event;
117 event.result = query->result;
118 void *parent = query->parent;
120 _stop_dnsquery(query);
121 iosocket_lookup_callback(parent, &event);
131 /* public functions */
133 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback) {
134 if(!(records & IODNS_FORWARD) || !hostname || !callback)
137 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
139 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
143 struct _IODNSQuery *query = _create_dnsquery();
149 query->parent = descriptor;
150 query->flags |= IODNSFLAG_PARENT_PUBLIC;
151 descriptor->query = query;
153 query->request.host = strdup(hostname);
154 query->type = (records & IODNS_FORWARD);
156 descriptor->callback = callback;
158 _start_dnsquery(query);
162 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback) {
163 if(!addr || !callback)
166 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
168 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
172 struct _IODNSQuery *query = _create_dnsquery();
178 query->parent = descriptor;
179 query->flags |= IODNSFLAG_PARENT_PUBLIC;
180 descriptor->query = query;
182 query->type = IODNS_RECORD_PTR;
183 query->request.addr.addresslen = addrlen;
184 query->request.addr.address = malloc(addrlen);
185 if(!query->request.addr.address) {
186 iolog_trigger(IOLOG_ERROR, "could not allocate memory for sockaddr in %s:%d", __FILE__, __LINE__);
187 _free_dnsquery(query);
191 memcpy(query->request.addr.address, addr, addrlen);
193 descriptor->callback = callback;
195 _start_dnsquery(query);
199 void iodns_abort(struct IODNSQuery *descriptor) {
203 struct _IODNSQuery *query = descriptor->query;
205 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
209 _stop_dnsquery(query);
212 void iodns_free_result(struct IODNSResult *result) {
213 struct IODNSResult *next;
214 for(;result;result = next) {
217 if((result->type & IODNS_FORWARD)) {
218 if(result->result.addr.address)
219 free(result->result.addr.address);
221 if((result->type & IODNS_REVERSE)) {
222 if(result->result.host)
223 free(result->result.host);