MINOR: resolvers: add unique numeric id to nameservers

When we want to avoid keeping pointers on a nameserver struct, it's not
always convenient to refer as a nameserver using it's text-based unique
identifier since it's not limited in length thus it cannot be serialized
and deserialized safely.

To address this limitation, we add a new ->puid member in dns_nameserver
struct which is a parent-unique numeric value that can be used to refer
to the dns nameserver within its parent resolver context.

To achieve this, we reused the resolver->nb_nameserver member that wasn't
used. Each time we add a new nameserver to a resolver: we set ns->puid to
the current number of nameservers within the resolver and we increment
this number right away.

Public helper function find_nameserver_by_resolvers_and_id() was added to
help retrieve nameserver pointer from (resolver X nameserver puid)
combination.
This commit is contained in:
Aurelien DARRAGON 2023-12-04 15:34:32 +01:00 committed by Christopher Faulet
parent 4fe0cca305
commit 2f6120d6d4
4 changed files with 22 additions and 1 deletions

View File

@ -136,6 +136,7 @@ struct dns_session {
struct dns_nameserver {
char *id; /* nameserver unique identifier */
void *parent;
unsigned int puid; /* parent-unique numeric id */
struct {
const char *file; /* file where the section appears */
int line; /* line where the section appears */

View File

@ -134,7 +134,7 @@ struct resolv_response {
struct resolvers {
__decl_thread(HA_SPINLOCK_T lock);
unsigned int accepted_payload_size; /* maximum payload size we accept for responses */
int nb_nameservers; /* total number of active nameservers in a resolvers section */
int nb_nameservers; /* total number of nameservers in a resolvers section */
int resolve_retries; /* number of retries before giving up */
struct { /* time to: */
int resolve; /* wait between 2 queries for the same resolution */

View File

@ -34,6 +34,7 @@ extern struct list sec_resolvers;
extern unsigned int resolv_failed_resolutions;
struct resolvers *find_resolvers_by_id(const char *id);
struct dns_nameserver *find_nameserver_by_resolvers_and_id(struct resolvers *parent, unsigned int id);
struct resolv_srvrq *find_srvrq_by_name(const char *name, struct proxy *px);
struct resolv_srvrq *new_resolv_srvrq(struct server *srv, char *fqdn);
struct resolv_answer_item *find_srvrq_answer_record(const struct resolv_requester *requester);

View File

@ -170,6 +170,20 @@ struct resolvers *find_resolvers_by_id(const char *id)
return NULL;
}
/* Returns a pointer to the nameserver matching numerical <id> within <parent>
* resolver section. NULL is returned if no match is found.
*/
struct dns_nameserver *find_nameserver_by_resolvers_and_id(struct resolvers *parent, unsigned int id)
{
struct dns_nameserver *ns;
list_for_each_entry(ns, &parent->nameservers, list) {
if (ns->puid == id)
return ns;
}
return NULL;
}
/* Returns a pointer on the SRV request matching the name <name> for the proxy
* <px>. NULL is returned if no match is found.
*/
@ -3371,7 +3385,9 @@ static int parse_resolve_conf(char **errmsg, char **warnmsg)
newnameserver->parent = curr_resolvers;
newnameserver->process_responses = resolv_process_responses;
newnameserver->conf.line = resolv_linenum;
newnameserver->puid = curr_resolvers->nb_nameservers;
LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
curr_resolvers->nb_nameservers++;
}
resolv_out:
@ -3428,6 +3444,7 @@ static int resolvers_new(struct resolvers **resolvers, const char *id, const cha
r->timeout.resolve = 1000;
r->timeout.retry = 1000;
r->resolve_retries = 3;
r->nb_nameservers = 0;
LIST_INIT(&r->nameservers);
LIST_INIT(&r->resolutions.curr);
LIST_INIT(&r->resolutions.wait);
@ -3572,8 +3589,10 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
newnameserver->parent = curr_resolvers;
newnameserver->process_responses = resolv_process_responses;
newnameserver->conf.line = linenum;
newnameserver->puid = curr_resolvers->nb_nameservers;
/* the nameservers are linked backward first */
LIST_APPEND(&curr_resolvers->nameservers, &newnameserver->list);
curr_resolvers->nb_nameservers++;
}
else if (strcmp(args[0], "parse-resolv-conf") == 0) {
err_code |= parse_resolve_conf(&errmsg, &warnmsg);