Change mg_resolve()

This commit is contained in:
Sergey Lyubka 2022-01-07 15:00:10 +00:00
parent 2f981f9383
commit 3b0a509cae
5 changed files with 18 additions and 18 deletions

View File

@ -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) { void mg_resolve(struct mg_connection *c, const char *url) {
if (mg_aton(*name, &c->peer)) { struct mg_str host = mg_url_host(url);
// name is an IP address, do not fire name resolution 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); mg_connect_resolved(c);
} else { } else {
// name is not an IP, send DNS resolution request // host is not an IP, send DNS resolution request
mg_sendnsreq(c, name, ms, &c->mgr->dns4, false); 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) { if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
LOG(LL_ERROR, ("OOM")); LOG(LL_ERROR, ("OOM"));
} else { } else {
struct mg_str host = mg_url_host(url);
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
c->is_udp = (strncmp(url, "udp:", 4) == 0); c->is_udp = (strncmp(url, "udp:", 4) == 0);
c->peer.port = mg_htons(mg_url_port(url));
c->fn = fn; c->fn = fn;
c->fn_data = fn_data; c->fn_data = fn_data;
LOG(LL_DEBUG, ("%lu -> %s", c->id, url)); LOG(LL_DEBUG, ("%lu -> %s", c->id, url));
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_resolve(c, &host, mgr->dnstimeout); mg_resolve(c, url);
} }
return c; return c;
} }

View File

@ -1121,7 +1121,7 @@ struct mg_dns_rr {
uint16_t alen; // Address length 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 *); void mg_resolve_cancel(struct mg_connection *);
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *); 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, size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,

View File

@ -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) { void mg_resolve(struct mg_connection *c, const char *url) {
if (mg_aton(*name, &c->peer)) { struct mg_str host = mg_url_host(url);
// name is an IP address, do not fire name resolution 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); mg_connect_resolved(c);
} else { } else {
// name is not an IP, send DNS resolution request // host is not an IP, send DNS resolution request
mg_sendnsreq(c, name, ms, &c->mgr->dns4, false); mg_sendnsreq(c, &host, c->mgr->dnstimeout, &c->mgr->dns4, false);
} }
} }

View File

@ -31,7 +31,7 @@ struct mg_dns_rr {
uint16_t alen; // Address length 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 *); void mg_resolve_cancel(struct mg_connection *);
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *); 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, size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,

View File

@ -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) { if ((c = alloc_conn(mgr, 1, INVALID_SOCKET)) == NULL) {
LOG(LL_ERROR, ("OOM")); LOG(LL_ERROR, ("OOM"));
} else { } else {
struct mg_str host = mg_url_host(url);
LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c); LIST_ADD_HEAD(struct mg_connection, &mgr->conns, c);
c->is_udp = (strncmp(url, "udp:", 4) == 0); c->is_udp = (strncmp(url, "udp:", 4) == 0);
c->peer.port = mg_htons(mg_url_port(url));
c->fn = fn; c->fn = fn;
c->fn_data = fn_data; c->fn_data = fn_data;
LOG(LL_DEBUG, ("%lu -> %s", c->id, url)); LOG(LL_DEBUG, ("%lu -> %s", c->id, url));
mg_call(c, MG_EV_OPEN, NULL); mg_call(c, MG_EV_OPEN, NULL);
mg_resolve(c, &host, mgr->dnstimeout); mg_resolve(c, url);
} }
return c; return c;
} }