2020-12-05 11:26:32 +00:00
|
|
|
#include "dns.h"
|
|
|
|
#include "log.h"
|
2023-03-27 14:11:39 -03:00
|
|
|
#include "printf.h"
|
2020-12-05 11:26:32 +00:00
|
|
|
#include "str.h"
|
|
|
|
#include "timer.h"
|
2022-01-07 15:48:09 +00:00
|
|
|
#include "url.h"
|
2020-12-05 11:26:32 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
struct dns_data {
|
|
|
|
struct dns_data *next;
|
|
|
|
struct mg_connection *c;
|
2022-04-07 13:50:25 +01:00
|
|
|
uint64_t expire;
|
2020-12-05 11:26:32 +00:00
|
|
|
uint16_t txnid;
|
|
|
|
};
|
|
|
|
|
2020-12-22 09:44:59 +00:00
|
|
|
static void mg_sendnsreq(struct mg_connection *, struct mg_str *, int,
|
|
|
|
struct mg_dns *, bool);
|
|
|
|
|
2023-09-21 12:29:51 +01:00
|
|
|
static void mg_dns_free(struct dns_data **head, struct dns_data *d) {
|
|
|
|
LIST_DELETE(struct dns_data, head, d);
|
2020-12-05 11:26:32 +00:00
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
2020-12-22 09:44:59 +00:00
|
|
|
void mg_resolve_cancel(struct mg_connection *c) {
|
2023-09-21 12:29:51 +01:00
|
|
|
struct dns_data *tmp, *d;
|
|
|
|
struct dns_data **head = (struct dns_data **) &c->mgr->active_dns_requests;
|
|
|
|
for (d = *head; d != NULL; d = tmp) {
|
2020-12-05 11:26:32 +00:00
|
|
|
tmp = d->next;
|
2023-09-21 12:29:51 +01:00
|
|
|
if (d->c == c) mg_dns_free(head, d);
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-28 05:25:29 +00:00
|
|
|
static size_t mg_dns_parse_name_depth(const uint8_t *s, size_t len, size_t ofs,
|
2022-03-21 16:29:27 +00:00
|
|
|
char *to, size_t tolen, size_t j,
|
|
|
|
int depth) {
|
|
|
|
size_t i = 0;
|
|
|
|
if (tolen > 0 && depth == 0) to[0] = '\0';
|
2020-12-11 14:02:52 +00:00
|
|
|
if (depth > 5) return 0;
|
2022-03-21 16:29:27 +00:00
|
|
|
// MG_INFO(("ofs %lx %x %x", (unsigned long) ofs, s[ofs], s[ofs + 1]));
|
2020-12-28 05:25:29 +00:00
|
|
|
while (ofs + i + 1 < len) {
|
|
|
|
size_t n = s[ofs + i];
|
|
|
|
if (n == 0) {
|
2020-12-05 11:26:32 +00:00
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
}
|
2020-12-28 05:25:29 +00:00
|
|
|
if (n & 0xc0) {
|
|
|
|
size_t ptr = (((n & 0x3f) << 8) | s[ofs + i + 1]); // 12 is hdr len
|
2022-03-21 16:29:27 +00:00
|
|
|
// MG_INFO(("PTR %lx", (unsigned long) ptr));
|
2020-12-28 05:25:29 +00:00
|
|
|
if (ptr + 1 < len && (s[ptr] & 0xc0) == 0 &&
|
2022-03-21 16:29:27 +00:00
|
|
|
mg_dns_parse_name_depth(s, len, ptr, to, tolen, j, depth + 1) == 0)
|
2020-12-28 05:25:29 +00:00
|
|
|
return 0;
|
|
|
|
i += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ofs + i + n + 1 >= len) return 0;
|
|
|
|
if (j > 0) {
|
|
|
|
if (j < tolen) to[j] = '.';
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
if (j + n < tolen) memcpy(&to[j], &s[ofs + i + 1], n);
|
2020-12-05 11:26:32 +00:00
|
|
|
j += n;
|
|
|
|
i += n + 1;
|
2020-12-28 05:25:29 +00:00
|
|
|
if (j < tolen) to[j] = '\0'; // Zero-terminate this chunk
|
2022-03-21 16:29:27 +00:00
|
|
|
// MG_INFO(("--> [%s]", to));
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
2020-12-17 19:14:17 +00:00
|
|
|
if (tolen > 0) to[tolen - 1] = '\0'; // Make sure make sure it is nul-term
|
2020-12-05 11:26:32 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2021-10-22 19:41:26 +01:00
|
|
|
static size_t mg_dns_parse_name(const uint8_t *s, size_t n, size_t ofs,
|
|
|
|
char *dst, size_t dstlen) {
|
2022-03-21 16:29:27 +00:00
|
|
|
return mg_dns_parse_name_depth(s, n, ofs, dst, dstlen, 0, 0);
|
2020-12-28 05:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t mg_dns_parse_rr(const uint8_t *buf, size_t len, size_t ofs,
|
|
|
|
bool is_question, struct mg_dns_rr *rr) {
|
|
|
|
const uint8_t *s = buf + ofs, *e = &buf[len];
|
|
|
|
|
|
|
|
memset(rr, 0, sizeof(*rr));
|
2021-04-13 19:42:47 +01:00
|
|
|
if (len < sizeof(struct mg_dns_header)) return 0; // Too small
|
2021-10-22 08:56:45 +01:00
|
|
|
if (len > 512) return 0; // Too large, we don't expect that
|
|
|
|
if (s >= e) return 0; // Overflow
|
2020-12-28 05:25:29 +00:00
|
|
|
|
2020-12-28 06:32:55 +00:00
|
|
|
if ((rr->nlen = (uint16_t) mg_dns_parse_name(buf, len, ofs, NULL, 0)) == 0)
|
|
|
|
return 0;
|
2020-12-28 05:25:29 +00:00
|
|
|
s += rr->nlen + 4;
|
|
|
|
if (s > e) return 0;
|
2021-10-22 08:56:45 +01:00
|
|
|
rr->atype = (uint16_t) (((uint16_t) s[-4] << 8) | s[-3]);
|
|
|
|
rr->aclass = (uint16_t) (((uint16_t) s[-2] << 8) | s[-1]);
|
|
|
|
if (is_question) return (size_t) (rr->nlen + 4);
|
2020-12-28 05:25:29 +00:00
|
|
|
|
|
|
|
s += 6;
|
|
|
|
if (s > e) return 0;
|
2021-10-22 08:56:45 +01:00
|
|
|
rr->alen = (uint16_t) (((uint16_t) s[-2] << 8) | s[-1]);
|
2020-12-28 05:25:29 +00:00
|
|
|
if (s + rr->alen > e) return 0;
|
2021-10-22 08:56:45 +01:00
|
|
|
return (size_t) (rr->nlen + rr->alen + 10);
|
2020-12-28 05:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool mg_dns_parse(const uint8_t *buf, size_t len, struct mg_dns_message *dm) {
|
|
|
|
const struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
|
|
|
struct mg_dns_rr rr;
|
|
|
|
size_t i, n, ofs = sizeof(*h);
|
|
|
|
memset(dm, 0, sizeof(*dm));
|
|
|
|
|
|
|
|
if (len < sizeof(*h)) return 0; // Too small, headers dont fit
|
2020-12-25 04:32:56 +00:00
|
|
|
if (mg_ntohs(h->num_questions) > 1) return 0; // Sanity
|
2023-10-08 15:30:18 +01:00
|
|
|
if (mg_ntohs(h->num_answers) > 15) return 0; // Sanity
|
2020-12-28 05:25:29 +00:00
|
|
|
dm->txnid = mg_ntohs(h->txnid);
|
|
|
|
|
2020-12-11 14:02:52 +00:00
|
|
|
for (i = 0; i < mg_ntohs(h->num_questions); i++) {
|
2020-12-28 05:25:29 +00:00
|
|
|
if ((n = mg_dns_parse_rr(buf, len, ofs, true, &rr)) == 0) return false;
|
2022-03-21 16:29:27 +00:00
|
|
|
// MG_INFO(("Q %lu %lu %hu/%hu", ofs, n, rr.atype, rr.aclass));
|
2020-12-28 05:25:29 +00:00
|
|
|
ofs += n;
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
for (i = 0; i < mg_ntohs(h->num_answers); i++) {
|
2020-12-28 05:25:29 +00:00
|
|
|
if ((n = mg_dns_parse_rr(buf, len, ofs, false, &rr)) == 0) return false;
|
2022-03-21 16:29:27 +00:00
|
|
|
// MG_INFO(("A -- %lu %lu %hu/%hu %s", ofs, n, rr.atype, rr.aclass,
|
|
|
|
// dm->name));
|
2020-12-28 05:25:29 +00:00
|
|
|
mg_dns_parse_name(buf, len, ofs, dm->name, sizeof(dm->name));
|
|
|
|
ofs += n;
|
|
|
|
|
|
|
|
if (rr.alen == 4 && rr.atype == 1 && rr.aclass == 1) {
|
2020-12-22 09:44:59 +00:00
|
|
|
dm->addr.is_ip6 = false;
|
2020-12-28 05:25:29 +00:00
|
|
|
memcpy(&dm->addr.ip, &buf[ofs - 4], 4);
|
2020-12-22 09:44:59 +00:00
|
|
|
dm->resolved = true;
|
|
|
|
break; // Return success
|
2020-12-28 05:25:29 +00:00
|
|
|
} else if (rr.alen == 16 && rr.atype == 28 && rr.aclass == 1) {
|
2020-12-22 09:44:59 +00:00
|
|
|
dm->addr.is_ip6 = true;
|
2023-06-09 08:56:08 -04:00
|
|
|
memcpy(&dm->addr.ip, &buf[ofs - 16], 16);
|
2020-12-20 16:55:33 +00:00
|
|
|
dm->resolved = true;
|
|
|
|
break; // Return success
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-20 16:55:33 +00:00
|
|
|
return true;
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
|
2024-01-08 17:34:34 -03:00
|
|
|
static void dns_cb(struct mg_connection *c, int ev, void *ev_data) {
|
2020-12-22 09:44:59 +00:00
|
|
|
struct dns_data *d, *tmp;
|
2023-09-21 12:29:51 +01:00
|
|
|
struct dns_data **head = (struct dns_data **) &c->mgr->active_dns_requests;
|
2020-12-05 11:26:32 +00:00
|
|
|
if (ev == MG_EV_POLL) {
|
2022-04-07 13:50:25 +01:00
|
|
|
uint64_t now = *(uint64_t *) ev_data;
|
2023-09-21 12:29:51 +01:00
|
|
|
for (d = *head; d != NULL; d = tmp) {
|
2020-12-05 11:26:32 +00:00
|
|
|
tmp = d->next;
|
2022-02-12 18:17:25 +00:00
|
|
|
// MG_DEBUG ("%lu %lu dns poll", d->expire, now));
|
2020-12-05 11:26:32 +00:00
|
|
|
if (now > d->expire) mg_error(d->c, "DNS timeout");
|
|
|
|
}
|
|
|
|
} else if (ev == MG_EV_READ) {
|
|
|
|
struct mg_dns_message dm;
|
|
|
|
int resolved = 0;
|
2020-12-20 16:55:33 +00:00
|
|
|
if (mg_dns_parse(c->recv.buf, c->recv.len, &dm) == false) {
|
2022-05-06 21:09:13 +01:00
|
|
|
MG_ERROR(("Unexpected DNS response:"));
|
|
|
|
mg_hexdump(c->recv.buf, c->recv.len);
|
2020-12-20 16:55:33 +00:00
|
|
|
} else {
|
2022-09-09 11:11:53 +01:00
|
|
|
// MG_VERBOSE(("%s %d", dm.name, dm.resolved));
|
2023-09-21 12:29:51 +01:00
|
|
|
for (d = *head; d != NULL; d = tmp) {
|
2020-12-05 11:26:32 +00:00
|
|
|
tmp = d->next;
|
2022-02-12 18:17:25 +00:00
|
|
|
// MG_INFO(("d %p %hu %hu", d, d->txnid, dm.txnid));
|
2020-12-05 11:26:32 +00:00
|
|
|
if (dm.txnid != d->txnid) continue;
|
|
|
|
if (d->c->is_resolving) {
|
2020-12-20 16:55:33 +00:00
|
|
|
if (dm.resolved) {
|
2022-02-22 22:00:55 +00:00
|
|
|
dm.addr.port = d->c->rem.port; // Save port
|
|
|
|
d->c->rem = dm.addr; // Copy resolved address
|
2023-02-02 10:33:00 +00:00
|
|
|
MG_DEBUG(
|
|
|
|
("%lu %s is %M", d->c->id, dm.name, mg_print_ip, &d->c->rem));
|
2020-12-20 16:55:33 +00:00
|
|
|
mg_connect_resolved(d->c);
|
2020-12-22 09:44:59 +00:00
|
|
|
#if MG_ENABLE_IPV6
|
2022-04-22 20:44:53 +01:00
|
|
|
} else if (dm.addr.is_ip6 == false && dm.name[0] != '\0' &&
|
|
|
|
c->mgr->use_dns6 == false) {
|
2020-12-22 09:44:59 +00:00
|
|
|
struct mg_str x = mg_str(dm.name);
|
|
|
|
mg_sendnsreq(d->c, &x, c->mgr->dnstimeout, &c->mgr->dns6, true);
|
|
|
|
#endif
|
2020-12-20 16:55:33 +00:00
|
|
|
} else {
|
|
|
|
mg_error(d->c, "%s DNS lookup failed", dm.name);
|
|
|
|
}
|
2020-12-05 11:26:32 +00:00
|
|
|
} else {
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_ERROR(("%lu already resolved", d->c->id));
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
2023-09-21 12:29:51 +01:00
|
|
|
mg_dns_free(head, d);
|
2020-12-05 11:26:32 +00:00
|
|
|
resolved = 1;
|
|
|
|
}
|
|
|
|
}
|
2022-02-12 18:17:25 +00:00
|
|
|
if (!resolved) MG_ERROR(("stray DNS reply"));
|
2020-12-05 11:26:32 +00:00
|
|
|
c->recv.len = 0;
|
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
2023-09-21 12:29:51 +01:00
|
|
|
for (d = *head; d != NULL; d = tmp) {
|
2020-12-05 11:26:32 +00:00
|
|
|
tmp = d->next;
|
2022-01-14 08:50:01 +00:00
|
|
|
mg_error(d->c, "DNS error");
|
2023-09-21 12:29:51 +01:00
|
|
|
mg_dns_free(head, d);
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 15:41:23 +01:00
|
|
|
static bool mg_dns_send(struct mg_connection *c, const struct mg_str *name,
|
2021-10-22 19:41:26 +01:00
|
|
|
uint16_t txnid, bool ipv6) {
|
2020-12-05 11:26:32 +00:00
|
|
|
struct {
|
|
|
|
struct mg_dns_header header;
|
|
|
|
uint8_t data[256];
|
|
|
|
} pkt;
|
|
|
|
size_t i, n;
|
|
|
|
memset(&pkt, 0, sizeof(pkt));
|
2020-12-28 05:25:29 +00:00
|
|
|
pkt.header.txnid = mg_htons(txnid);
|
2020-12-05 11:26:32 +00:00
|
|
|
pkt.header.flags = mg_htons(0x100);
|
2020-12-22 09:44:59 +00:00
|
|
|
pkt.header.num_questions = mg_htons(1);
|
2020-12-05 11:26:32 +00:00
|
|
|
for (i = n = 0; i < sizeof(pkt.data) - 5; i++) {
|
|
|
|
if (name->ptr[i] == '.' || i >= name->len) {
|
2021-10-22 08:56:45 +01:00
|
|
|
pkt.data[n] = (uint8_t) (i - n);
|
2020-12-05 11:26:32 +00:00
|
|
|
memcpy(&pkt.data[n + 1], name->ptr + n, i - n);
|
|
|
|
n = i + 1;
|
|
|
|
}
|
|
|
|
if (i >= name->len) break;
|
|
|
|
}
|
|
|
|
memcpy(&pkt.data[n], "\x00\x00\x01\x00\x01", 5); // A query
|
|
|
|
n += 5;
|
2020-12-22 09:44:59 +00:00
|
|
|
if (ipv6) pkt.data[n - 3] = 0x1c; // AAAA query
|
|
|
|
// memcpy(&pkt.data[n], "\xc0\x0c\x00\x1c\x00\x01", 6); // AAAA query
|
|
|
|
// n += 6;
|
2022-04-01 15:41:23 +01:00
|
|
|
return mg_send(c, &pkt, sizeof(pkt.header) + n);
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 09:44:59 +00:00
|
|
|
static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
|
|
|
struct mg_dns *dnsc, bool ipv6) {
|
2020-12-05 11:26:32 +00:00
|
|
|
struct dns_data *d = NULL;
|
2020-12-22 09:44:59 +00:00
|
|
|
if (dnsc->url == NULL) {
|
|
|
|
mg_error(c, "DNS server URL is NULL. Call mg_mgr_init()");
|
|
|
|
} else if (dnsc->c == NULL) {
|
|
|
|
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
|
|
|
|
if (dnsc->c != NULL) {
|
|
|
|
dnsc->c->pfn = dns_cb;
|
2022-06-03 13:57:57 +01:00
|
|
|
// dnsc->c->is_hexdumping = 1;
|
2020-12-22 09:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dnsc->c == NULL) {
|
|
|
|
mg_error(c, "resolver");
|
|
|
|
} else if ((d = (struct dns_data *) calloc(1, sizeof(*d))) == NULL) {
|
|
|
|
mg_error(c, "resolve OOM");
|
|
|
|
} else {
|
2022-04-12 14:14:55 +01:00
|
|
|
struct dns_data *reqs = (struct dns_data *) c->mgr->active_dns_requests;
|
|
|
|
d->txnid = reqs ? (uint16_t) (reqs->txnid + 1) : 1;
|
|
|
|
d->next = (struct dns_data *) c->mgr->active_dns_requests;
|
|
|
|
c->mgr->active_dns_requests = d;
|
2022-04-07 13:50:25 +01:00
|
|
|
d->expire = mg_millis() + (uint64_t) ms;
|
2020-12-22 09:44:59 +00:00
|
|
|
d->c = c;
|
|
|
|
c->is_resolving = 1;
|
2022-02-12 18:17:25 +00:00
|
|
|
MG_VERBOSE(("%lu resolving %.*s @ %s, txnid %hu", c->id, (int) name->len,
|
2023-02-17 10:20:33 +00:00
|
|
|
name->ptr, dnsc->url, d->txnid));
|
2022-04-01 15:41:23 +01:00
|
|
|
if (!mg_dns_send(dnsc->c, name, d->txnid, ipv6)) {
|
|
|
|
mg_error(dnsc->c, "DNS send");
|
|
|
|
}
|
2020-12-22 09:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 15:00:10 +00:00
|
|
|
void mg_resolve(struct mg_connection *c, const char *url) {
|
|
|
|
struct mg_str host = mg_url_host(url);
|
2022-02-22 22:00:55 +00:00
|
|
|
c->rem.port = mg_htons(mg_url_port(url));
|
|
|
|
if (mg_aton(host, &c->rem)) {
|
2022-01-07 15:00:10 +00:00
|
|
|
// host is an IP address, do not fire name resolution
|
2020-12-05 11:26:32 +00:00
|
|
|
mg_connect_resolved(c);
|
|
|
|
} else {
|
2022-01-07 15:00:10 +00:00
|
|
|
// host is not an IP, send DNS resolution request
|
2022-04-22 20:44:53 +01:00
|
|
|
struct mg_dns *dns = c->mgr->use_dns6 ? &c->mgr->dns6 : &c->mgr->dns4;
|
|
|
|
mg_sendnsreq(c, &host, c->mgr->dnstimeout, dns, c->mgr->use_dns6);
|
2020-12-05 11:26:32 +00:00
|
|
|
}
|
|
|
|
}
|