]> git.meshlink.io Git - meshlink/blob - src/adns.c
Don't use assert() to check the results of pthread_*() calls.
[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 #include <stdatomic.h>
24
25 #include "adns.h"
26 #include "devtools.h"
27 #include "logger.h"
28 #include "xalloc.h"
29
30 typedef struct adns_item {
31         adns_cb_t cb;
32         void *data;
33         time_t deadline;
34         struct addrinfo *ai;
35         int err;
36         char *host;
37         char *serv;
38 } adns_item_t;
39
40 static void *adns_loop(void *data) {
41         meshlink_handle_t *mesh = data;
42
43         while(true) {
44                 adns_item_t *item = meshlink_queue_pop_cond(&mesh->adns_queue, &mesh->adns_cond);
45
46                 if(!item) {
47                         break;
48                 }
49
50                 if(time(NULL) < item->deadline) {
51                         logger(mesh, MESHLINK_DEBUG, "Resolving %s port %s", item->host, item->serv);
52                         devtool_adns_resolve_probe();
53                         int result = getaddrinfo(item->host, item->serv, NULL, &item->ai);
54
55                         if(result) {
56                                 item->ai = NULL;
57                                 item->err = errno;
58                         }
59                 } else {
60                         logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", item->host, item->serv);
61                         item->ai = NULL;
62                         item->err = ETIMEDOUT;
63                 }
64
65                 if(meshlink_queue_push(&mesh->adns_done_queue, item)) {
66                         signal_trigger(&mesh->loop, &mesh->adns_signal);
67                 } else {
68                         free(item->host);
69                         free(item->serv);
70                         free(item);
71                 }
72         }
73
74         return NULL;
75 }
76
77 static void adns_cb_handler(event_loop_t *loop, void *data) {
78         (void)loop;
79         meshlink_handle_t *mesh = data;
80
81         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_done_queue));) {
82                 item->cb(mesh, item->host, item->serv, item->data, item->ai, item->err);
83                 free(item);
84         }
85 }
86
87 void init_adns(meshlink_handle_t *mesh) {
88         meshlink_queue_init(&mesh->adns_queue);
89         meshlink_queue_init(&mesh->adns_done_queue);
90         signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
91         pthread_create(&mesh->adns_thread, NULL, adns_loop, mesh);
92 }
93
94 void exit_adns(meshlink_handle_t *mesh) {
95         if(!mesh->adns_signal.cb) {
96                 return;
97         }
98
99         /* Drain the queue of any pending ADNS requests */
100         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_queue));) {
101                 free(item->host);
102                 free(item->serv);
103                 free(item);
104         }
105
106         /* Signal the ADNS thread to stop */
107         if(!meshlink_queue_push(&mesh->adns_queue, NULL)) {
108                 abort();
109         }
110
111         pthread_cond_signal(&mesh->adns_cond);
112
113         pthread_join(mesh->adns_thread, NULL);
114         meshlink_queue_exit(&mesh->adns_queue);
115         signal_del(&mesh->loop, &mesh->adns_signal);
116 }
117
118 void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, void *data, int timeout) {
119         adns_item_t *item = xmalloc(sizeof(*item));
120         item->cb = cb;
121         item->data = data;
122         item->deadline = time(NULL) + timeout;
123         item->host = host;
124         item->serv = serv;
125
126         logger(mesh, MESHLINK_DEBUG, "Enqueueing DNS request for %s port %s", item->host, item->serv);
127
128         if(!meshlink_queue_push(&mesh->adns_queue, item)) {
129                 abort();
130         }
131
132         pthread_cond_signal(&mesh->adns_cond);
133 }
134
135 struct adns_blocking_info {
136         meshlink_handle_t *mesh;
137         pthread_mutex_t mutex;
138         pthread_cond_t cond;
139         char *host;
140         char *serv;
141         struct addrinfo *ai;
142         int socktype;
143         bool done;
144 };
145
146 static void *adns_blocking_handler(void *data) {
147         struct adns_blocking_info *info = data;
148
149         logger(info->mesh, MESHLINK_DEBUG, "Resolving %s port %s", info->host, info->serv);
150         devtool_adns_resolve_probe();
151
152         struct addrinfo hint = {
153                 .ai_family = AF_UNSPEC,
154                 .ai_socktype = info->socktype,
155         };
156
157         if(getaddrinfo(info->host, info->serv, &hint, &info->ai)) {
158                 info->ai = NULL;
159         }
160
161         if(pthread_mutex_lock(&info->mutex) != 0) {
162                 abort();
163         }
164
165         bool cleanup = info->done;
166
167         if(!info->done) {
168                 info->done = true;
169                 pthread_cond_signal(&info->cond);
170         }
171
172         pthread_mutex_unlock(&info->mutex);
173
174         if(cleanup) {
175                 free(info->host);
176                 free(info->serv);
177                 free(info);
178         }
179
180         return NULL;
181 }
182
183 struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout) {
184         struct adns_blocking_info *info = xzalloc(sizeof(*info));
185
186         info->mesh = mesh;
187         info->host = host;
188         info->serv = serv;
189         info->socktype = socktype;
190         pthread_mutex_init(&info->mutex, NULL);
191         pthread_cond_init(&info->cond, NULL);
192
193         struct timespec deadline;
194         clock_gettime(CLOCK_REALTIME, &deadline);
195         deadline.tv_sec += timeout;
196
197         pthread_t thread;
198
199         if(pthread_create(&thread, NULL, adns_blocking_handler, info)) {
200                 free(info->host);
201                 free(info->serv);
202                 free(info);
203                 return NULL;
204         } else {
205                 pthread_detach(thread);
206         }
207
208         if(pthread_mutex_lock(&info->mutex) != 0) {
209                 abort();
210         }
211
212         pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
213
214         struct addrinfo *result = NULL;
215         bool cleanup = info->done;
216
217         if(info->done) {
218                 result = info->ai;
219         } else {
220                 logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", host, serv);
221                 info->done = true;
222         }
223
224         pthread_mutex_unlock(&info->mutex);
225
226         if(cleanup) {
227                 free(info->host);
228                 free(info->serv);
229                 free(info);
230         }
231
232         return result;
233 }