2 dns.c -- hostname resolving functions
3 Copyright (C) 2019 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
29 typedef struct adns_item {
39 static void *adns_loop(void *data) {
40 meshlink_handle_t *mesh = data;
43 adns_item_t *item = meshlink_queue_pop_cond(&mesh->adns_queue, &mesh->adns_cond);
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);
59 logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", item->host, item->serv);
61 item->err = ETIMEDOUT;
64 if(meshlink_queue_push(&mesh->adns_done_queue, item)) {
65 signal_trigger(&mesh->loop, &mesh->adns_signal);
76 static void adns_cb_handler(event_loop_t *loop, void *data) {
78 meshlink_handle_t *mesh = data;
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);
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);
93 void exit_adns(meshlink_handle_t *mesh) {
94 if(!mesh->adns_signal.cb) {
98 /* Drain the queue of any pending ADNS requests */
99 for(adns_item_t *item; (item = meshlink_queue_pop(&mesh->adns_queue));) {
105 /* Signal the ADNS thread to stop */
106 if(!meshlink_queue_push(&mesh->adns_queue, NULL)) {
110 pthread_cond_signal(&mesh->adns_cond);
112 pthread_join(mesh->adns_thread, NULL);
113 meshlink_queue_exit(&mesh->adns_queue);
114 signal_del(&mesh->loop, &mesh->adns_signal);
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));
121 item->deadline = time(NULL) + timeout;
125 logger(mesh, MESHLINK_DEBUG, "Enqueueing DNS request for %s port %s", item->host, item->serv);
127 if(!meshlink_queue_push(&mesh->adns_queue, item)) {
131 pthread_cond_signal(&mesh->adns_cond);
134 struct adns_blocking_info {
135 meshlink_handle_t *mesh;
136 pthread_mutex_t mutex;
145 static void *adns_blocking_handler(void *data) {
146 struct adns_blocking_info *info = data;
148 logger(info->mesh, MESHLINK_DEBUG, "Resolving %s port %s", info->host, info->serv);
149 devtool_adns_resolve_probe();
151 struct addrinfo hint = {
152 .ai_family = AF_UNSPEC,
153 .ai_socktype = info->socktype,
156 if(getaddrinfo(info->host, info->serv, &hint, &info->ai)) {
160 if(pthread_mutex_lock(&info->mutex) != 0) {
164 bool cleanup = info->done;
168 pthread_cond_signal(&info->cond);
171 pthread_mutex_unlock(&info->mutex);
182 struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout) {
183 struct adns_blocking_info *info = xzalloc(sizeof(*info));
188 info->socktype = socktype;
189 pthread_mutex_init(&info->mutex, NULL);
190 pthread_cond_init(&info->cond, NULL);
192 struct timespec deadline;
193 clock_gettime(CLOCK_REALTIME, &deadline);
194 deadline.tv_sec += timeout;
198 if(pthread_create(&thread, NULL, adns_blocking_handler, info)) {
204 pthread_detach(thread);
207 if(pthread_mutex_lock(&info->mutex) != 0) {
211 pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
213 struct addrinfo *result = NULL;
214 bool cleanup = info->done;
219 logger(mesh, MESHLINK_WARNING, "Deadline passed for DNS request %s port %s", host, serv);
223 pthread_mutex_unlock(&info->mutex);