From 6ecb6c150033a2701c25c5ffb64769190b38c485 Mon Sep 17 00:00:00 2001 From: cpq Date: Thu, 21 Jan 2021 10:00:18 +0000 Subject: [PATCH] Fix DNS timeout, add unit test --- examples/http-client/Makefile | 1 + mongoose.c | 3 ++- src/dns.c | 3 ++- test/unit_test.c | 23 +++++++++++++++++------ 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/http-client/Makefile b/examples/http-client/Makefile index b41d4be0..aa649765 100644 --- a/examples/http-client/Makefile +++ b/examples/http-client/Makefile @@ -1,4 +1,5 @@ PROG ?= example +CFLAGS ?= -DMG_ENABLE_LINES $(CFLAGS_EXTRA) MBEDTLS_DIR ?= ifeq "$(MBEDTLS_DIR)" "" diff --git a/mongoose.c b/mongoose.c index e305ae28..440feb28 100644 --- a/mongoose.c +++ b/mongoose.c @@ -261,7 +261,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, struct dns_data *d, *tmp; if (ev == MG_EV_POLL) { unsigned long now = *(unsigned long *) ev_data; - for (d = (struct dns_data *) fn_data; d != NULL; d = tmp) { + for (d = s_reqs; d != NULL; d = tmp) { tmp = d->next; // LOG(LL_DEBUG, ("%lu %lu dns poll", d->expire, now)); if (now > d->expire) mg_error(d->c, "DNS timeout"); @@ -308,6 +308,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, mg_dns_free(d); } } + (void) fn_data; } void mg_dns_send(struct mg_connection *c, const struct mg_str *name, diff --git a/src/dns.c b/src/dns.c index d107ad9d..6220a579 100644 --- a/src/dns.c +++ b/src/dns.c @@ -135,7 +135,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, struct dns_data *d, *tmp; if (ev == MG_EV_POLL) { unsigned long now = *(unsigned long *) ev_data; - for (d = (struct dns_data *) fn_data; d != NULL; d = tmp) { + for (d = s_reqs; d != NULL; d = tmp) { tmp = d->next; // LOG(LL_DEBUG, ("%lu %lu dns poll", d->expire, now)); if (now > d->expire) mg_error(d->c, "DNS timeout"); @@ -182,6 +182,7 @@ static void dns_cb(struct mg_connection *c, int ev, void *ev_data, mg_dns_free(d); } } + (void) fn_data; } void mg_dns_send(struct mg_connection *c, const struct mg_str *name, diff --git a/test/unit_test.c b/test/unit_test.c index 6d21fa1c..cee9010a 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -1002,6 +1002,11 @@ static void test_str(void) { ASSERT(mg_strcmp(mg_str("hi"), mg_strstrip(mg_str(" \thi\r\n"))) == 0); } +static void fn1(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_ERROR) sprintf((char *) fn_data, "%s", (char *) ev_data); + (void) c; +} + static void test_dns(void) { struct mg_dns_message dm; // txid flags numQ numA numAP numOP @@ -1021,14 +1026,20 @@ static void test_dns(void) { ASSERT(mg_dns_parse(data, sizeof(data), &dm) == 1); ASSERT(strcmp(dm.name, "") == 0); -#if 0 { - char *data = mg_file_read("dns.bin"); - ASSERT(data != NULL); - ASSERT(mg_dns_parse((uint8_t *) data, mg_file_size("dns.bin"), &dm) == 0); - free(data); + // Test timeout + struct mg_mgr mgr; + struct mg_connection *c; + char buf[100] = ""; + int i; + mg_mgr_init(&mgr); + mgr.dns4.url = "udp://127.0.0.1:12345"; + mgr.dnstimeout = 10; + c = mg_http_connect(&mgr, "http://google.com", fn1, buf); + for (i = 0; i < 50 && buf[0] == '\0'; i++) mg_mgr_poll(&mgr, 1); + mg_mgr_free(&mgr); + ASSERT(strcmp(buf, "DNS timeout") == 0); } -#endif } static void test_util(void) {