diff --git a/mongoose.c b/mongoose.c index 7227e85a..00cb5795 100644 --- a/mongoose.c +++ b/mongoose.c @@ -366,13 +366,15 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms, } } -void mg_resolve(struct mg_connection *c, struct mg_str *name, int ms) { - if (mg_aton(*name, &c->peer)) { - // name is an IP address, do not fire name resolution +void mg_resolve(struct mg_connection *c, const char *url) { + struct mg_str host = mg_url_host(url); + c->peer.port = mg_htons(mg_url_port(url)); + if (mg_aton(host, &c->peer)) { + // host is an IP address, do not fire name resolution mg_connect_resolved(c); } else { - // name is not an IP, send DNS resolution request - mg_sendnsreq(c, name, ms, &c->mgr->dns4, false); + // host is not an IP, send DNS resolution request + mg_sendnsreq(c, &host, c->mgr->dnstimeout, &c->mgr->dns4, false); } } @@ -3228,15 +3230,13 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) { LOG(LL_ERROR, ("OOM")); } else { - struct mg_str host = mg_url_host(url); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->is_udp = (strncmp(url, "udp:", 4) == 0); - c->peer.port = mg_htons(mg_url_port(url)); c->fn = fn; c->fn_data = fn_data; LOG(LL_DEBUG, ("%lu -> %s", c->id, url)); mg_call(c, MG_EV_OPEN, NULL); - mg_resolve(c, &host, mgr->dnstimeout); + mg_resolve(c, url); } return c; } diff --git a/mongoose.h b/mongoose.h index 1a95babb..9298148f 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1121,7 +1121,7 @@ struct mg_dns_rr { uint16_t alen; // Address length }; -void mg_resolve(struct mg_connection *, struct mg_str *, int); +void mg_resolve(struct mg_connection *, const char *url); void mg_resolve_cancel(struct mg_connection *); bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *); size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs, diff --git a/src/dns.c b/src/dns.c index 7a2cb23a..390081e9 100644 --- a/src/dns.c +++ b/src/dns.c @@ -256,12 +256,14 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms, } } -void mg_resolve(struct mg_connection *c, struct mg_str *name, int ms) { - if (mg_aton(*name, &c->peer)) { - // name is an IP address, do not fire name resolution +void mg_resolve(struct mg_connection *c, const char *url) { + struct mg_str host = mg_url_host(url); + c->peer.port = mg_htons(mg_url_port(url)); + if (mg_aton(host, &c->peer)) { + // host is an IP address, do not fire name resolution mg_connect_resolved(c); } else { - // name is not an IP, send DNS resolution request - mg_sendnsreq(c, name, ms, &c->mgr->dns4, false); + // host is not an IP, send DNS resolution request + mg_sendnsreq(c, &host, c->mgr->dnstimeout, &c->mgr->dns4, false); } } diff --git a/src/dns.h b/src/dns.h index 5d6b1790..bc723f46 100644 --- a/src/dns.h +++ b/src/dns.h @@ -31,7 +31,7 @@ struct mg_dns_rr { uint16_t alen; // Address length }; -void mg_resolve(struct mg_connection *, struct mg_str *, int); +void mg_resolve(struct mg_connection *, const char *url); void mg_resolve_cancel(struct mg_connection *); bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *); size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs, diff --git a/src/sock.c b/src/sock.c index 5a73efc2..5fd550ac 100644 --- a/src/sock.c +++ b/src/sock.c @@ -369,15 +369,13 @@ struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *url, if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) { LOG(LL_ERROR, ("OOM")); } else { - struct mg_str host = mg_url_host(url); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); c->is_udp = (strncmp(url, "udp:", 4) == 0); - c->peer.port = mg_htons(mg_url_port(url)); c->fn = fn; c->fn_data = fn_data; LOG(LL_DEBUG, ("%lu -> %s", c->id, url)); mg_call(c, MG_EV_OPEN, NULL); - mg_resolve(c, &host, mgr->dnstimeout); + mg_resolve(c, url); } return c; }