[IOMultiplexerV2] alpha
[NextIRCd.git] / src / IOHandler / IODNSLookup.c
1 /* IODNSLookup.c - IOMultiplexer
2  * Copyright (C) 2014  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20 #include "IODNSLookup.h"
21 #include "IOLog.h"
22 #include "IOSockets.h"
23
24 #include <string.h>
25
26 struct _IODNSQuery *iodnsquery_first = NULL;
27 struct _IODNSQuery *iodnsquery_last = NULL;
28
29 struct IODNSEngine *dnsengine = NULL;
30
31 static void iodns_init_engine() {
32         if(dnsengine)
33                 return;
34         //try DNS engines
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;
39         else {
40                 iolog_trigger(IOLOG_FATAL, "found no useable IO DNS engine");
41                 return;
42         }
43         iolog_trigger(IOLOG_DEBUG, "using %s IODNS engine", dnsengine->name);
44 }
45
46 void _init_iodns() {
47         iodns_init_engine();
48 }
49
50 struct _IODNSQuery *_create_dnsquery() {
51         struct _IODNSQuery *query = calloc(1, sizeof(*query));
52         if(!query) {
53                 iolog_trigger(IOLOG_ERROR, "could not allocate memory for _IODNSQuery in %s:%d", __FILE__, __LINE__);
54                 return NULL;
55         }
56         if(iodnsquery_last)
57                 iodnsquery_last->next = query;
58         else
59                 iodnsquery_first = query;
60         query->prev = iodnsquery_last;
61         iodnsquery_last = query;
62         return query;
63 }
64
65 void _start_dnsquery(struct _IODNSQuery *query) {
66         query->flags |= IODNSFLAG_RUNNING;
67         dnsengine->add(query);
68 }
69
70 void _free_dnsquery(struct _IODNSQuery *query) {
71         if(query->prev)
72                 query->prev->next = query->next;
73         else
74                 iodnsquery_first = query->next;
75         if(query->next)
76                 query->next->prev = query->prev;
77         else
78                 iodnsquery_last = query->prev;
79         if((query->type & IODNS_REVERSE) && query->request.addr.address)
80                 free(query->request.addr.address);
81         free(query);
82 }
83
84 void _stop_dnsquery(struct _IODNSQuery *query) {
85         if((query->flags & IODNSFLAG_RUNNING)) {
86                 query->flags &= ~IODNSFLAG_RUNNING;
87                 dnsengine->remove(query);
88         }
89         if(!(query->flags & IODNSFLAG_PROCESSING))
90                 _free_dnsquery(query);
91 }
92
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;
97                 event.type = state;
98                 event.query = descriptor;
99                 event.result = query->result;
100                 
101                 descriptor->query = NULL;
102                 _stop_dnsquery(query);
103                 
104                 if(descriptor->callback)
105                         descriptor->callback(&event);
106                 
107                 iogc_add(descriptor);
108         } else if((query->flags & IODNSFLAG_PARENT_SOCKET)) {
109                 struct IODNSEvent event;
110                 event.type = state;
111                 event.query = NULL;
112                 event.result = query->result;
113                 void *parent = query->parent;
114                 
115                 _stop_dnsquery(query);
116                 iosocket_lookup_callback(parent, &event);
117                 
118         }
119 }
120
121 void iodns_poll() {
122         if(dnsengine)
123                 dnsengine->loop();
124 }
125
126 /* public functions */
127
128 struct IODNSQuery *iodns_getaddrinfo(char *hostname, int records, iodns_callback *callback) {
129         if(!(records & IODNS_FORWARD) || !hostname || !callback)
130                 return NULL;
131         
132         struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
133         if(!descriptor) {
134                 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
135                 return NULL;
136         }
137         
138         struct _IODNSQuery *query = _create_dnsquery();
139         if(!query) {
140                 free(descriptor);
141                 return NULL;
142         }
143         
144         query->parent = descriptor;
145         query->flags |= IODNSFLAG_PARENT_PUBLIC;
146         descriptor->query = query;
147         
148         query->request.host = strdup(hostname);
149         query->type = (records & IODNS_FORWARD);
150         
151         descriptor->callback = callback;
152         
153         _start_dnsquery(query);
154         return descriptor;
155 }
156
157 struct IODNSQuery *iodns_getnameinfo(const struct sockaddr *addr, size_t addrlen, iodns_callback *callback) {
158         if(!addr || !callback)
159                 return NULL;
160         
161         struct IODNSQuery *descriptor = calloc(1, sizeof(*descriptor));
162         if(!descriptor) {
163                 iolog_trigger(IOLOG_ERROR, "could not allocate memory for IODNSQuery in %s:%d", __FILE__, __LINE__);
164                 return NULL;
165         }
166         
167         struct _IODNSQuery *query = _create_dnsquery();
168         if(!query) {
169                 free(descriptor);
170                 return NULL;
171         }
172         
173         query->parent = descriptor;
174         query->flags |= IODNSFLAG_PARENT_PUBLIC;
175         descriptor->query = query;
176         
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);
183                 free(descriptor);
184                 return NULL;
185         }
186         memcpy(query->request.addr.address, addr, addrlen);
187         
188         descriptor->callback = callback;
189         
190         _start_dnsquery(query);
191         return descriptor;
192 }
193
194 void iodns_abort(struct IODNSQuery *descriptor) {
195         if(!descriptor)
196                 return;
197         
198         struct _IODNSQuery *query = descriptor->query;
199         if(!query) {
200                 iolog_trigger(IOLOG_WARNING, "called iodns_abort for destroyed IODNSQuery in %s:%d", __FILE__, __LINE__);
201                 return;
202         }
203         
204         _stop_dnsquery(query);
205 }
206
207 void iodns_free_result(struct IODNSResult *result) {
208         struct IODNSResult *next;
209         for(;result;result = next) {
210                 next = result->next;
211                 
212                 if((result->type & IODNS_FORWARD)) {
213                         if(result->result.addr.address)
214                                 free(result->result.addr.address);
215                 }
216                 if((result->type & IODNS_REVERSE)) {
217                         if(result->result.host)
218                                 free(result->result.host);
219                 }
220                 free(result);
221         }
222 }
223