aboutsummaryrefslogtreecommitdiff
path: root/client/libacron/apps/acronc/async_dns.c
diff options
context:
space:
mode:
Diffstat (limited to 'client/libacron/apps/acronc/async_dns.c')
-rw-r--r--client/libacron/apps/acronc/async_dns.c65
1 files changed, 65 insertions, 0 deletions
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 <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;
+}