]> git.meshlink.io Git - meshlink/blob - src/adns.c
Add debugging for async DNS errors.
[meshlink] / src / adns.c
1 /*
2     dns.c -- hostname resolving functions
3     Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include <pthread.h>
23
24 #include "adns.h"
25 #include "devtools.h"
26 #include "logger.h"
27 #include "xalloc.h"
28
29 typedef struct adns_item {
30         adns_cb_t cb;
31         void *data;
32         time_t deadline;
33         struct addrinfo *ai;
34         int err;
35         char *host;
36         char *serv;
37 } adns_item_t;
38
39 static void *adns_loop(void *data) {
40         meshlink_handle_t *mesh = data;
41
42         while(true) {
43                 adns_item_t *item = meshlink_queue_pop_cond(&mesh->adns_queue, &mesh->adns_cond);
44
45                 if(!item) {
46                         break;
47                 }
48
49                 if(time(NULL) < item->deadline) {
50                         logger(mesh, MESHLINK_DEBUG, "Resolving %s port %s", item->host, item->serv);
51                         devtool_adns_resolve_probe();
52                         int result = getaddrinfo(item->host, item->serv, NULL, &item->ai);
53
54                         if(result) {
55                                 item->ai = NULL;
56                                 item->err = errno;
57                         }
58                 } else {
59                         logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", item->host, item->serv);
60                         item->ai = NULL;
61                         item->err = ETIMEDOUT;
62                 }
63
64                 if(meshlink_queue_push(&mesh->adns_done_queue, item)) {
65                         signal_trigger(&mesh->loop, &mesh->adns_signal);
66                 } else {
67                         free(item->host);
68                         free(item->serv);
69                         free(item);
70                 }
71         }
72
73         return NULL;
74 }
75
76 static void adns_cb_handler(event_loop_t *loop, void *data) {
77         (void)loop;
78         meshlink_handle_t *mesh = data;
79
80         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_done_queue));) {
81                 item->cb(mesh, item->host, item->serv, item->data, item->ai, item->err);
82                 free(item);
83         }
84 }
85
86 void init_adns(meshlink_handle_t *mesh) {
87         meshlink_queue_init(&mesh->adns_queue);
88         meshlink_queue_init(&mesh->adns_done_queue);
89         signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
90         pthread_create(&mesh->adns_thread, NULL, adns_loop, mesh);
91 }
92
93 void exit_adns(meshlink_handle_t *mesh) {
94         if(!mesh->adns_signal.cb) {
95                 return;
96         }
97
98         /* Drain the queue of any pending ADNS requests */
99         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_queue));) {
100                 free(item->host);
101                 free(item->serv);
102                 free(item);
103         }
104
105         /* Signal the ADNS thread to stop */
106         if(!meshlink_queue_push(&mesh->adns_queue, NULL)) {
107                 abort();
108         }
109
110         pthread_cond_signal(&mesh->adns_cond);
111
112         pthread_join(mesh->adns_thread, NULL);
113         meshlink_queue_exit(&mesh->adns_queue);
114         signal_del(&mesh->loop, &mesh->adns_signal);
115 }
116
117 void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, void *data, int timeout) {
118         adns_item_t *item = xmalloc(sizeof(*item));
119         item->cb = cb;
120         item->data = data;
121         item->deadline = time(NULL) + timeout;
122         item->host = host;
123         item->serv = serv;
124
125         logger(mesh, MESHLINK_DEBUG, "Enqueueing DNS request for %s port %s", item->host, item->serv);
126
127         if(!meshlink_queue_push(&mesh->adns_queue, item)) {
128                 abort();
129         }
130
131         pthread_cond_signal(&mesh->adns_cond);
132 }
133
134 struct adns_blocking_info {
135         meshlink_handle_t *mesh;
136         pthread_mutex_t mutex;
137         pthread_cond_t cond;
138         char *host;
139         char *serv;
140         struct addrinfo *ai;
141         int socktype;
142         bool done;
143 };
144
145 static void *adns_blocking_handler(void *data) {
146         struct adns_blocking_info *info = data;
147
148         logger(info->mesh, MESHLINK_WARNING, "Resolving %s port %s", info->host, info->serv);
149         devtool_adns_resolve_probe();
150
151         struct addrinfo hint = {
152                 .ai_family = AF_UNSPEC,
153                 .ai_socktype = info->socktype,
154         };
155
156         int result = getaddrinfo(info->host, info->serv, &hint, &info->ai);
157
158         if(result) {
159                 logger(info->mesh, MESHLINK_ERROR, "getaddrinfo(%s, %s) returned an error: %s", info->host, info->serv, gai_strerror(result));
160                 info->ai = NULL;
161         }
162
163         if(pthread_mutex_lock(&info->mutex) != 0) {
164                 abort();
165         }
166
167         bool cleanup = info->done;
168
169         if(!info->done) {
170                 logger(info->mesh, MESHLINK_WARNING, "getaddrinfo(%s, %s) returned before waiter timed out", info->host, info->serv);
171                 info->done = true;
172                 pthread_cond_signal(&info->cond);
173         } else {
174                 logger(info->mesh, MESHLINK_WARNING, "getaddrinfo(%s, %s) returned after waiter timed out", info->host, info->serv);
175         }
176
177         pthread_mutex_unlock(&info->mutex);
178
179         if(cleanup) {
180                 free(info->host);
181                 free(info->serv);
182                 free(info);
183         }
184
185         return NULL;
186 }
187
188 struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout) {
189         struct adns_blocking_info *info = xzalloc(sizeof(*info));
190
191         info->mesh = mesh;
192         info->host = host;
193         info->serv = serv;
194         info->socktype = socktype;
195         pthread_mutex_init(&info->mutex, NULL);
196         pthread_cond_init(&info->cond, NULL);
197
198         struct timespec deadline;
199         clock_gettime(CLOCK_REALTIME, &deadline);
200         deadline.tv_sec += timeout;
201
202         logger(mesh, MESHLINK_WARNING, "Starting blocking DNS request for %s port %s", host, serv);
203
204         pthread_t thread;
205
206         if(pthread_create(&thread, NULL, adns_blocking_handler, info)) {
207                 free(info->host);
208                 free(info->serv);
209                 free(info);
210                 return NULL;
211         } else {
212                 pthread_detach(thread);
213         }
214
215         if(pthread_mutex_lock(&info->mutex) != 0) {
216                 abort();
217         }
218
219         pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
220
221         struct addrinfo *result = NULL;
222         bool cleanup = info->done;
223
224         if(info->done) {
225                 logger(mesh, MESHLINK_WARNING, "DNS request for %s port %s fulfilled in time, ai = %p", host, serv, (void *)info->ai);
226                 result = info->ai;
227         } else {
228                 logger(mesh, MESHLINK_ERROR, "Deadline passed for DNS request %s port %s, ai = %p", host, serv, (void *)info->ai);
229                 info->done = true;
230         }
231
232         pthread_mutex_unlock(&info->mutex);
233
234         if(cleanup) {
235                 free(info->host);
236                 free(info->serv);
237                 free(info);
238         }
239
240         return result;
241 }