From ab59e8fbca7d65ebf63c3f1d2f0ea9c8dd52f49c Mon Sep 17 00:00:00 2001 From: Wei Li Jiang Date: Fri, 11 Oct 2024 23:47:04 +1300 Subject: [PATCH] dns: Fallback to A/AAAA lookup when getaddrinfo is used without any DNS servers --- include/re_dns.h | 1 + src/dns/client.c | 30 +++++++++++++++++++++++++++--- src/sip/request.c | 4 +++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/include/re_dns.h b/include/re_dns.h index 4b33915be..5f43e63b8 100644 --- a/include/re_dns.h +++ b/include/re_dns.h @@ -224,6 +224,7 @@ void dnsc_cache_flush(struct dnsc *dnsc); void dnsc_cache_max(struct dnsc *dnsc, uint32_t max); void dnsc_getaddrinfo(struct dnsc *dnsc, bool active); bool dnsc_getaddrinfo_enabled(struct dnsc *dnsc); +bool dnsc_getaddrinfo_only(const struct dnsc *dnsc); /* DNS System functions */ diff --git a/src/dns/client.c b/src/dns/client.c index 716ca3302..7ebd109f6 100644 --- a/src/dns/client.c +++ b/src/dns/client.c @@ -974,8 +974,16 @@ static int query(struct dns_query **qp, struct dnsc *dnsc, uint8_t opcode, struct dns_query *q = NULL; struct dnshdr hdr; int err = 0; + bool use_getaddrinfo = false; + bool srv_available = srvv && srvc && *srvc != 0; - if (!dnsc || !name || !srvv || !srvc || !(*srvc)) + if (!dnsc || !name) + return EINVAL; + + use_getaddrinfo = dnsc->conf.getaddrinfo && + (type == DNS_TYPE_A || type == DNS_TYPE_AAAA); + + if (!srv_available && !use_getaddrinfo) return EINVAL; if (DNS_QTYPE_AXFR == type) @@ -1029,8 +1037,7 @@ static int query(struct dns_query **qp, struct dnsc *dnsc, uint8_t opcode, list_init(q->rrlv[i]); } - if (dnsc->conf.getaddrinfo && - (q->type == DNS_TYPE_A || q->type == DNS_TYPE_AAAA)) { + if (use_getaddrinfo) { err = query_getaddrinfo(q); if (err) goto error; @@ -1401,3 +1408,20 @@ bool dnsc_getaddrinfo_enabled(struct dnsc *dnsc) return dnsc->conf.getaddrinfo; } + + +/** + * Return if getaddrinfo usage is enabled and exclusive, + * i.e. there are no DNS servers known explicitly + * + * @param dnsc DNS Client + * + * @return true if DNS servers are available, false otherwise + */ +bool dnsc_getaddrinfo_only(const struct dnsc *dnsc) +{ + if (!dnsc) + return false; + + return dnsc->conf.getaddrinfo && dnsc->srvc == 0; +} diff --git a/src/sip/request.c b/src/sip/request.c index 017db0e7c..074f4454e 100644 --- a/src/sip/request.c +++ b/src/sip/request.c @@ -727,6 +727,8 @@ static int sip_request_send(struct sip_request *req, struct sip *sip, { struct sa dst; int err; + bool addr_only = req->tp_selected && + dnsc_getaddrinfo_only(req->sip->dnsc); if (!sa_set_str(&dst, req->host, sip_transp_port(req->tp, route->port))) { @@ -737,7 +739,7 @@ static int sip_request_send(struct sip_request *req, struct sip *sip, return err; } } - else if (route->port) { + else if (route->port || addr_only){ req->port = sip_transp_port(req->tp, route->port); err = addr_lookup(req, req->host);