]> git.meshlink.io Git - meshlink/blob - src/adns.c
2776a4c5494bfa562ecb4163812c89740d925957
[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 "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                         int result = getaddrinfo(item->host, item->serv, NULL, &item->ai);
52
53                         if(result) {
54                                 item->ai = NULL;
55                                 item->err = errno;
56                         }
57                 } else {
58                         logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", item->host, item->serv);
59                         item->ai = NULL;
60                         item->err = ETIMEDOUT;
61                 }
62
63                 if(meshlink_queue_push(&mesh->adns_done_queue, item)) {
64                         signal_trigger(&mesh->loop, &mesh->adns_signal);
65                 } else {
66                         free(item->host);
67                         free(item->serv);
68                         free(item);
69                 }
70         }
71
72         return NULL;
73 }
74
75 static void adns_cb_handler(event_loop_t *loop, void *data) {
76         (void)loop;
77         meshlink_handle_t *mesh = data;
78
79         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_done_queue));) {
80                 item->cb(mesh, item->host, item->serv, item->data, item->ai, item->err);
81                 free(item);
82         }
83 }
84
85 void init_adns(meshlink_handle_t *mesh) {
86         signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
87         meshlink_queue_init(&mesh->adns_queue);
88         pthread_create(&mesh->adns_thread, NULL, adns_loop, mesh);
89 }
90
91 void exit_adns(meshlink_handle_t *mesh) {
92         if(!mesh->adns_signal.cb) {
93                 return;
94         }
95
96         /* Drain the queue of any pending ADNS requests */
97         for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_queue));) {
98                 free(item->host);
99                 free(item->serv);
100                 free(item);
101         }
102
103         /* Signal the ADNS thread to stop */
104         if(!meshlink_queue_push(&mesh->adns_queue, NULL)) {
105                 abort();
106         }
107
108         pthread_cond_signal(&mesh->adns_cond);
109
110         pthread_join(mesh->adns_thread, NULL);
111         meshlink_queue_exit(&mesh->adns_queue);
112         signal_del(&mesh->loop, &mesh->adns_signal);
113 }
114
115 void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, void *data, int timeout) {
116         adns_item_t *item = xmalloc(sizeof(*item));
117         item->cb = cb;
118         item->data = data;
119         item->deadline = time(NULL) + timeout;
120         item->host = host;
121         item->serv = serv;
122
123         logger(mesh, MESHLINK_DEBUG, "Enqueueing DNS request for %s port %s", item->host, item->serv);
124
125         if(!meshlink_queue_push(&mesh->adns_queue, item)) {
126                 abort();
127         }
128
129         pthread_cond_signal(&mesh->adns_cond);
130 }
131
132 struct adns_blocking_info {
133         meshlink_handle_t *mesh;
134         pthread_mutex_t mutex;
135         pthread_cond_t cond;
136         char *host;
137         char *serv;
138         struct addrinfo *ai;
139         bool done;
140 };
141
142 void *adns_blocking_handler(void *data) {
143         struct adns_blocking_info *info = data;
144
145         logger(info->mesh, MESHLINK_DEBUG, "Resolving %s port %s", info->host, info->serv);
146
147         if(getaddrinfo(info->host, info->serv, NULL, &info->ai)) {
148                 info->ai = NULL;
149         }
150
151         pthread_mutex_lock(&info->mutex);
152
153         bool cleanup = info->done;
154
155         if(!info->done) {
156                 info->done = true;
157                 pthread_cond_signal(&info->cond);
158         }
159
160         pthread_mutex_unlock(&info->mutex);
161
162         if(cleanup) {
163                 free(info->host);
164                 free(info->serv);
165                 free(info);
166         }
167
168         return NULL;
169 }
170
171 struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int timeout) {
172         struct adns_blocking_info *info = xzalloc(sizeof(*info));
173
174         info->mesh = mesh;
175         info->host = host;
176         info->serv = serv;
177
178         struct timespec deadline;
179         clock_gettime(CLOCK_REALTIME, &deadline);
180         deadline.tv_sec += timeout;
181
182         pthread_t thread;
183
184         if(pthread_create(&thread, NULL, adns_blocking_handler, info)) {
185                 free(info->host);
186                 free(info->serv);
187                 free(info);
188                 return NULL;
189         } else {
190                 pthread_detach(thread);
191         }
192
193         pthread_mutex_lock(&info->mutex);
194         pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
195
196         struct addrinfo *result = NULL;
197         bool cleanup = info->done;
198
199         if(info->done) {
200                 result = info->ai;
201         } else {
202                 logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", host, serv);
203                 info->done = true;
204         }
205
206         pthread_mutex_unlock(&info->mutex);
207
208         if(cleanup) {
209                 free(info->host);
210                 free(info->serv);
211                 free(info);
212         }
213
214         return result;
215 }