From ec032181c654edd227cdb26bbbd5ff12908cb176 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Wed, 25 Feb 2026 11:35:30 -0300 Subject: [PATCH] add entropy to DNS txnid --- mongoose.c | 8 ++++++-- src/dns.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index ca465739..83910894 100644 --- a/mongoose.c +++ b/mongoose.c @@ -376,8 +376,12 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms, mg_error(c, "resolve OOM"); } else { 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; + uint16_t id; + mg_random(&id, sizeof(uint16_t)); + // TODO(): traverse reqs and check id != reqs->txnid; repeat otherwise + if (reqs != NULL) id = (uint16_t) (reqs->txnid + 1); // no collision + d->txnid = id; + d->next = reqs; c->mgr->active_dns_requests = d; d->expire = mg_millis() + (uint64_t) ms; d->c = c; diff --git a/src/dns.c b/src/dns.c index 468db3c8..ab337e5f 100644 --- a/src/dns.c +++ b/src/dns.c @@ -255,8 +255,12 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms, mg_error(c, "resolve OOM"); } else { 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; + uint16_t id; + mg_random(&id, sizeof(uint16_t)); + // TODO(): traverse reqs and check id != reqs->txnid; repeat otherwise + if (reqs != NULL) id = (uint16_t) (reqs->txnid + 1); // no collision + d->txnid = id; + d->next = reqs; c->mgr->active_dns_requests = d; d->expire = mg_millis() + (uint64_t) ms; d->c = c;