From 8b07bf593e54dd876e30d0cb1c7c7226d0d1b1e2 Mon Sep 17 00:00:00 2001 From: Trumeet Date: Tue, 26 Jul 2022 17:17:09 -0700 Subject: feat(acronc): add acronc(1), the Acron cli Still in early development, Windows support is incomplete. Signed-off-by: Trumeet --- client/libacron/apps/acronc/async_dns.c | 65 +++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 client/libacron/apps/acronc/async_dns.c (limited to 'client/libacron/apps/acronc/async_dns.c') diff --git a/client/libacron/apps/acronc/async_dns.c b/client/libacron/apps/acronc/async_dns.c new file mode 100644 index 0000000..2e221cf --- /dev/null +++ b/client/libacron/apps/acronc/async_dns.c @@ -0,0 +1,65 @@ +/* + * Created by yuuta on 7/24/22. + */ + +#include "handler.h" +#include "log.h" + +#include + +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; +} -- cgit v1.2.3