diff options
author | Trumeet <yuuta@yuuta.moe> | 2022-07-26 17:36:20 -0700 |
---|---|---|
committer | Trumeet <yuuta@yuuta.moe> | 2022-07-26 17:36:20 -0700 |
commit | 7099a86ca74fa637f26af38674f80fb8efd5f6fa (patch) | |
tree | 33315c65e82170476cf3c80d976dfa4d16a304a3 /client/acronc/async_dns.c | |
parent | 8b07bf593e54dd876e30d0cb1c7c7226d0d1b1e2 (diff) | |
download | acron-7099a86ca74fa637f26af38674f80fb8efd5f6fa.tar acron-7099a86ca74fa637f26af38674f80fb8efd5f6fa.tar.gz acron-7099a86ca74fa637f26af38674f80fb8efd5f6fa.tar.bz2 acron-7099a86ca74fa637f26af38674f80fb8efd5f6fa.zip |
refactor(libacron/acronc/helloworld): move to separate directories
The corresponding CMakeLists.txt files are still rough.
Diffstat (limited to 'client/acronc/async_dns.c')
-rw-r--r-- | client/acronc/async_dns.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/client/acronc/async_dns.c b/client/acronc/async_dns.c new file mode 100644 index 0000000..2e221cf --- /dev/null +++ b/client/acronc/async_dns.c @@ -0,0 +1,65 @@ +/* + * Created by yuuta on 7/24/22. + */ + +#include "handler.h" +#include "log.h" + +#include <uv.h> + +static uv_getaddrinfo_t resolv; + +static struct addrinfo *ai = NULL; +static struct addrinfo *ai_current = NULL; +static void (*cb)(int status, + const struct addrinfo *, + void (*on_connect_result)(bool)) = NULL; + +static void on_conn(bool res) { + if (res) { + uv_freeaddrinfo(ai); + ai = NULL; + ai_current = NULL; + cb = NULL; + return; + } + ai_current = ai_current->ai_next; + if (!ai_current) { + /* No more result available. */ + cb(1, NULL, NULL); + uv_freeaddrinfo(ai); + ai = NULL; + ai_current = NULL; + cb = NULL; + return; + } + cb(0, ai_current, &on_conn); +} + +static void on_resolv(uv_getaddrinfo_t *req, int status, struct addrinfo *res) { + LOGDV("on_resolv(req = %p): status = %d, res = %p", + req, + status, + res); + if (status) { + LOGEV("Cannot resolve host: %s", uv_strerror(status)); + cb(status, NULL, NULL); + return; + } + ai = res; + ai_current = res; + cb(0, ai_current, &on_conn); +} + +int a_dns(const char *host, + uint16_t port, + void (*c)(int status, + const struct addrinfo *, + void (*on_connect_result)(bool))) { + int r; + char service[6]; + snprintf(service, 6, "%u", port); + cb = c; + if ((r = uv_getaddrinfo(loop, &resolv, on_resolv, host, service, NULL))) return r; + return 0; +} |