]> git.meshlink.io Git - meshlink/blobdiff - src/adns.c
Don't use assert() to check the results of pthread_*() calls.
[meshlink] / src / adns.c
index a04310149f07a76cf3ff7ae2d05db369fb921d23..cae8234f499d506d8dedb2ed02a47c9f3ba1cd59 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);
 }
 
@@ -138,6 +139,7 @@ struct adns_blocking_info {
        char *host;
        char *serv;
        struct addrinfo *ai;
+       int socktype;
        bool done;
 };
 
@@ -147,11 +149,18 @@ 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);
+       if(pthread_mutex_lock(&info->mutex) != 0) {
+               abort();
+       }
 
        bool cleanup = info->done;
 
@@ -171,12 +180,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;
+       pthread_mutex_init(&info->mutex, NULL);
+       pthread_cond_init(&info->cond, NULL);
 
        struct timespec deadline;
        clock_gettime(CLOCK_REALTIME, &deadline);
@@ -193,7 +205,10 @@ struct addrinfo *adns_blocking_request(meshlink_handle_t *mesh, char *host, char
                pthread_detach(thread);
        }
 
-       pthread_mutex_lock(&info->mutex);
+       if(pthread_mutex_lock(&info->mutex) != 0) {
+               abort();
+       }
+
        pthread_cond_timedwait(&info->cond, &info->mutex, &deadline);
 
        struct addrinfo *result = NULL;