]> git.meshlink.io Git - meshlink/blobdiff - src/adns.c
Initialize the adns_done_queue.
[meshlink] / src / adns.c
index a04310149f07a76cf3ff7ae2d05db369fb921d23..56aed8b31c7d97ce79aef759349c0c43294b2f1b 100644 (file)
@@ -85,8 +85,9 @@ static void adns_cb_handler(event_loop_t *loop, void *data) {
 }
 
 void init_adns(meshlink_handle_t *mesh) {
-       signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
        meshlink_queue_init(&mesh->adns_queue);
+       meshlink_queue_init(&mesh->adns_done_queue);
+       signal_add(&mesh->loop, &mesh->adns_signal, adns_cb_handler, mesh, 1);
        pthread_create(&mesh->adns_thread, NULL, adns_loop, mesh);
 }
 
@@ -107,7 +108,7 @@ void exit_adns(meshlink_handle_t *mesh) {
                abort();
        }
 
-       pthread_cond_signal(&mesh->adns_cond);
+       assert(pthread_cond_signal(&mesh->adns_cond) == 0);
 
        pthread_join(mesh->adns_thread, NULL);
        meshlink_queue_exit(&mesh->adns_queue);
@@ -128,7 +129,7 @@ void adns_queue(meshlink_handle_t *mesh, char *host, char *serv, adns_cb_t cb, v
                abort();
        }
 
-       pthread_cond_signal(&mesh->adns_cond);
+       assert(pthread_cond_signal(&mesh->adns_cond) == 0);
 }
 
 struct adns_blocking_info {
@@ -138,6 +139,7 @@ struct adns_blocking_info {
        char *host;
        char *serv;
        struct addrinfo *ai;
+       int socktype;
        bool done;
 };
 
@@ -147,20 +149,25 @@ static void *adns_blocking_handler(void *data) {
        logger(info->mesh, MESHLINK_DEBUG, "Resolving %s port %s", info->host, info->serv);
        devtool_adns_resolve_probe();
 
-       if(getaddrinfo(info->host, info->serv, NULL, &info->ai)) {
+       struct addrinfo hint = {
+               .ai_family = AF_UNSPEC,
+               .ai_socktype = info->socktype,
+       };
+
+       if(getaddrinfo(info->host, info->serv, &hint, &info->ai)) {
                info->ai = NULL;
        }
 
-       pthread_mutex_lock(&info->mutex);
+       assert(pthread_mutex_lock(&info->mutex) == 0);
 
        bool cleanup = info->done;
 
        if(!info->done) {
                info->done = true;
-               pthread_cond_signal(&info->cond);
+               assert(pthread_cond_signal(&info->cond) == 0);
        }
 
-       pthread_mutex_unlock(&info->mutex);
+       assert(pthread_mutex_unlock(&info->mutex) == 0);
 
        if(cleanup) {
                free(info->host);
@@ -171,12 +178,15 @@ static void *adns_blocking_handler(void *data) {
        return NULL;
 }
 
-struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int timeout) {
+struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char *serv, int socktype, int timeout) {
        struct adns_blocking_info *info = xzalloc(sizeof(*info));
 
        info->mesh = mesh;
        info->host = host;
        info->serv = serv;
+       info->socktype = socktype;
+       assert(pthread_mutex_init(&info->mutex, NULL) == 0);
+       assert(pthread_cond_init(&info->cond, NULL) == 0);
 
        struct timespec deadline;
        clock_gettime(CLOCK_REALTIME, &deadline);
@@ -190,11 +200,11 @@ struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char
                free(info);
                return NULL;
        } else {
-               pthread_detach(thread);
+               assert(pthread_detach(thread) == 0);
        }
 
-       pthread_mutex_lock(&info->mutex);
-       pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
+       assert(pthread_mutex_lock(&info->mutex) == 0);
+       assert(pthread_cond_timedwait(&info->cond, &info->mutex, &deadline) == 0);
 
        struct addrinfo *result = NULL;
        bool cleanup = info->done;
@@ -206,7 +216,7 @@ struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char
                info->done = true;
        }
 
-       pthread_mutex_unlock(&info->mutex);
+       assert(pthread_mutex_unlock(&info->mutex) == 0);
 
        if(cleanup) {
                free(info->host);