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"
28 #define _WIN32_WINNT 0x501
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <arpa/inet.h>
43 struct _IODNSQuery *iodnsquery_first = NULL;
44 struct _IODNSQuery *iodnsquery_last = NULL;
46 struct IODNSEngine *dnsengine = NULL;
48 static void iodns_init_engine() {
52 if(dnsengine_cares.init && dnsengine_cares.init())
53 dnsengine = &dnsengine_cares;
54 else if(dnsengine_default.init && dnsengine_default.init())
55 dnsengine = &dnsengine_default;
57 iolog_trigger(IOLOG_FATAL, "found no useable IO DNS engine");
60 iolog_trigger(IOLOG_DEBUG, "using %s IODNS engine", dnsengine->name);
67 struct _IODNSQuery *_create_dnsquery() {
68 struct _IODNSQuery *query = calloc(1, sizeof(*query));
70 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IODNSQuery in %s:%d", __FILE__, __LINE__);
74 iodnsquery_last->next = query;
76 iodnsquery_first = query;
77 query->prev = iodnsquery_last;
78 iodnsquery_last = query;
82 void _start_dnsquery(struct _IODNSQuery *query) {
83 query->flags |= IODNSFLAG_RUNNING;
84 dnsengine->add(query);
87 void _free_dnsquery(struct _IODNSQuery *query) {
89 query->prev->next = query->next;
91 iodnsquery_first = query->next;
93 query->next->prev = query->prev;
95 iodnsquery_last = query->prev;
96 if((query->type & IODNS_REVERSE) && query->request.addr.address)
97 free(query->request.addr.address);
101 void _stop_dnsquery(struct _IODNSQuery *query) {
102 if((query->flags & IODNSFLAG_RUNNING)) {
103 query->flags &= ~IODNSFLAG_RUNNING;
104 dnsengine->remove(query);
106 if(!(query->flags & IODNSFLAG_PROCESSING))
107 _free_dnsquery(query);
110 void iodns_socket_callback(struct _IOSocket *iosock, int wantread, int wantwrite) {
111 if(dnsengine && dnsengine->socket_callback)
112 dnsengine->socket_callback(iosock, wantread, wantwrite);
115 void iodns_event_callback(struct _IODNSQuery *query, enum IODNSEventType state) {
116 if((query->flags & IODNSFLAG_PARENT_PUBLIC)) {
117 struct IODNSQuery *descriptor = query->parent;
118 struct IODNSEvent event;
120 event.query = descriptor;
121 event.result = query->result;
123 descriptor->query = NULL;
124 _stop_dnsquery(query);
126 if(descriptor->callback)
127 descriptor->callback(&event);
129 iogc_add(descriptor);
130 } else if((query->flags & IODNSFLAG_PARENT_SOCKET)) {
131 struct IODNSEvent event;
134 event.result = query->result;
135 void *parent = query->parent;
137 _stop_dnsquery(query);
138 iosocket_lookup_callback(parent, &event);
148 /* public functions */
150 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback, void *arg) {
151 if(!(records & IODNS_FORWARD) || !hostname || !callback)
154 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
156 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
160 struct _IODNSQuery *query = _create_dnsquery();
166 query->parent = descriptor;
167 query->flags |= IODNSFLAG_PARENT_PUBLIC;
168 descriptor->query = query;
169 descriptor->data = arg;
171 query->request.host = strdup(hostname);
172 query->type = (records & IODNS_FORWARD);
174 descriptor->callback = callback;
176 _start_dnsquery(query);
180 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback, void *arg) {
181 if(!addr || !callback)
184 struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
186 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
190 struct _IODNSQuery *query = _create_dnsquery();
196 query->parent = descriptor;
197 query->flags |= IODNSFLAG_PARENT_PUBLIC;
198 descriptor->query = query;
199 descriptor->data = arg;
201 query->type = IODNS_RECORD_PTR;
202 query->request.addr.addresslen = addrlen;
203 query->request.addr.address = malloc(addrlen);
204 if(!query->request.addr.address) {
205 iolog_trigger(IOLOG_ERROR, "could not allocate memory for sockaddr in %s:%d", __FILE__, __LINE__);
206 _free_dnsquery(query);
210 memcpy(query->request.addr.address, addr, addrlen);
212 descriptor->callback = callback;
214 _start_dnsquery(query);
218 void iodns_abort(struct IODNSQuery *descriptor) {
222 struct _IODNSQuery *query = descriptor->query;
224 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
228 _stop_dnsquery(query);
231 int iodns_print_address(struct IODNSAddress *address, int ipv6, char *buffer, int length) {
236 addr = (void *)(&((struct sockaddr_in6 *)address->address)->sin6_addr);
239 addr = (void *)(&((struct sockaddr_in *)address->address)->sin_addr);
241 buffer = inet_ntop(af, addr, buffer, length);
245 return strlen(buffer);
248 void iodns_free_result(struct IODNSResult *result) {
249 struct IODNSResult *next;
250 for(;result;result = next) {
253 if((result->type & IODNS_FORWARD)) {
254 if(result->result.addr.address)
255 free(result->result.addr.address);
257 if((result->type & IODNS_REVERSE)) {
258 if(result->result.host)
259 free(result->result.host);