]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: dns: extract options
authorThierry Fournier <tfournier@arpalert.org>
Wed, 17 Feb 2016 20:25:09 +0000 (21:25 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 19 Feb 2016 13:37:46 +0000 (14:37 +0100)
DNS selection preferences are actually declared inline in the
struct server. There are copied from the server struct to the
dns_resolution struct for each resolution.

Next patchs adds new preferences options, and it is not a good
way to copy all the configuration information before each dns
resolution.

This patch extract the configuration preference from the struct
server and declares a new dedicated struct. Only a pointer to this
new striuict will be copied before each dns resolution.

include/proto/dns.h
include/types/dns.h
include/types/server.h
src/checks.c
src/dns.c
src/server.c

index 4ccbfa0c7b05402df3133236e39ff66dd80cfa5e..170eefa522c40ce191372bd899269e9918cd7fc2 100644 (file)
@@ -34,8 +34,8 @@ int dns_init_resolvers(void);
 uint16_t dns_rnd16(void);
 int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, char *dn_name, int dn_name_len);
 int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end,
-                             char *dn_name, int dn_name_len, void *currentip,
-                             short currentip_sin_family, int family_priority,
+                             struct dns_resolution *resol, void *currentip,
+                             short currentip_sin_family,
                              void **newip, short *newip_sin_family);
 void dns_resolve_send(struct dgram_conn *dgram);
 void dns_resolve_recv(struct dgram_conn *dgram);
index ea1a9f901abdb86716f6dfae151a9339abaf3fc4..cf784cd1d6b8528fdad16f186311edb9576d7039 100644 (file)
@@ -140,6 +140,10 @@ struct dns_nameserver {
        } counters;
 };
 
+struct dns_options {
+       int family_prio;        /* which IP family should the resolver use when both are returned */
+};
+
 /*
  * resolution structure associated to single server and used to manage name resolution for
  * this server.
@@ -155,7 +159,7 @@ struct dns_resolution {
                                        /* requester callback, for error management */
        char *hostname_dn;              /* server hostname in domain name label format */
        int hostname_dn_len;            /* server domain name label len */
-       int resolver_family_priority;   /* which IP family should the resolver use when both are returned */
+       struct dns_options *opts;       /* IP selection options inherited from the configuration file. */
        unsigned int last_resolution;   /* time of the lastest valid resolution */
        unsigned int last_sent_packet;  /* time of the latest DNS packet sent */
        unsigned int last_status_change;        /* time of the latest DNS resolution status change */
index 3e25c344c017d2c8fd8d306264eef3472663e843..c04af9c3e10ad414f6f8b2624baa5b93a8a1abf0 100644 (file)
@@ -35,6 +35,7 @@
 
 #include <types/connection.h>
 #include <types/counters.h>
+#include <types/dns.h>
 #include <types/freq_ctr.h>
 #include <types/obj_type.h>
 #include <types/proxy.h>
@@ -224,7 +225,7 @@ struct server {
        char *resolvers_id;                     /* resolvers section used by this server */
        char *hostname;                         /* server hostname */
        struct dns_resolution *resolution;      /* server name resolution */
-       int resolver_family_priority;           /* which IP family should the resolver use when both are returned */
+       struct dns_options dns_opts;
 
 #ifdef USE_OPENSSL
        int use_ssl;                            /* ssl enabled */
index 2cfb01f6a59e9d998f5d3077ab912f14752ec4ea..4d3b39345ce1539a7a75031672f78d89710c1d43 100644 (file)
@@ -2218,8 +2218,8 @@ int trigger_resolution(struct server *s)
        resolution->query_id = query_id;
        resolution->qid.key = query_id;
        resolution->step = RSLV_STEP_RUNNING;
-       resolution->resolver_family_priority = s->resolver_family_priority;
-       if (resolution->resolver_family_priority == AF_INET) {
+       resolution->opts = &s->dns_opts;
+       if (resolution->opts->family_prio == AF_INET) {
                resolution->query_type = DNS_RTYPE_A;
        } else {
                resolution->query_type = DNS_RTYPE_AAAA;
index 0d8d305c6dacd2d2e17ca4f34dd57e5f98ae1faf..f43a675832f406da564805dc6ac1fe914f68efa8 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -102,7 +102,7 @@ void dns_reset_resolution(struct dns_resolution *resolution)
        resolution->qid.key = 0;
 
        /* default values */
-       if (resolution->resolver_family_priority == AF_INET) {
+       if (resolution->opts->family_prio == AF_INET) {
                resolution->query_type = DNS_RTYPE_A;
        } else {
                resolution->query_type = DNS_RTYPE_AAAA;
@@ -593,12 +593,20 @@ int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, char *
  * returns one of the DNS_UPD_* code
  */
 int dns_get_ip_from_response(unsigned char *resp, unsigned char *resp_end,
-               char *dn_name, int dn_name_len, void *currentip, short currentip_sin_family,
-               int family_priority, void **newip, short *newip_sin_family)
+                             struct dns_resolution *resol, void *currentip,
+                             short currentip_sin_family,
+                             void **newip, short *newip_sin_family)
 {
+       int family_priority;
+       char *dn_name;
+       int dn_name_len;
        int i, ancount, cnamelen, type, data_len, currentip_found;
        unsigned char *reader, *cname, *ptr, *newip4, *newip6;
 
+       family_priority = resol->opts->family_prio;
+       dn_name = resol->hostname_dn;
+       dn_name_len = resol->hostname_dn_len;
+
        cname = *newip = newip4 = newip6 = NULL;
        cnamelen = currentip_found = 0;
        *newip_sin_family = AF_UNSPEC;
index 3a04d22f6abea09c3ef75dde57bb4511a367a7f5..84dad38e182b84484b6f95a4c6c98bb32dfbba8b 100644 (file)
@@ -1017,15 +1017,15 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr
                        newsrv->agent.fall      = curproxy->defsrv.agent.fall;
                        newsrv->agent.health    = newsrv->agent.rise;   /* up, but will fall down at first failure */
                        newsrv->agent.server    = newsrv;
-                       newsrv->resolver_family_priority = curproxy->defsrv.resolver_family_priority;
-                       if (newsrv->resolver_family_priority == AF_UNSPEC)
-                               newsrv->resolver_family_priority = AF_INET6;
+                       newsrv->dns_opts.family_prio = curproxy->defsrv.dns_opts.family_prio;
+                       if (newsrv->dns_opts.family_prio == AF_UNSPEC)
+                               newsrv->dns_opts.family_prio = AF_INET6;
 
                        cur_arg = 3;
                } else {
                        newsrv = &curproxy->defsrv;
                        cur_arg = 1;
-                       newsrv->resolver_family_priority = AF_INET6;
+                       newsrv->dns_opts.family_prio = AF_INET6;
                }
 
                while (*args[cur_arg]) {
@@ -1079,9 +1079,9 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr
                        }
                        else if (!strcmp(args[cur_arg], "resolve-prefer")) {
                                if (!strcmp(args[cur_arg + 1], "ipv4"))
-                                       newsrv->resolver_family_priority = AF_INET;
+                                       newsrv->dns_opts.family_prio = AF_INET;
                                else if (!strcmp(args[cur_arg + 1], "ipv6"))
-                                       newsrv->resolver_family_priority = AF_INET6;
+                                       newsrv->dns_opts.family_prio = AF_INET6;
                                else {
                                        Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
                                                file, linenum, args[cur_arg]);
@@ -1746,7 +1746,7 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr
                        }
 
                        if (newsrv->resolution)
-                               newsrv->resolution->resolver_family_priority = newsrv->resolver_family_priority;
+                               newsrv->resolution->opts = &newsrv->dns_opts;
 
                        newsrv->check.state |= CHK_ST_CONFIGURED | CHK_ST_ENABLED;
                }
@@ -2625,9 +2625,9 @@ int snr_resolution_cb(struct dns_resolution *resolution, struct dns_nameserver *
                        goto invalid;
        }
 
-       ret = dns_get_ip_from_response(response, response_end, resolution->hostname_dn, resolution->hostname_dn_len,
-                       serverip, server_sin_family, resolution->resolver_family_priority, &firstip,
-                       &firstip_sin_family);
+       ret = dns_get_ip_from_response(response, response_end, resolution,
+                                      serverip, server_sin_family, &firstip,
+                                      &firstip_sin_family);
 
        switch (ret) {
                case DNS_UPD_NO:
@@ -2737,8 +2737,8 @@ int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
                case DNS_RESP_TRUNCATED:
                case DNS_RESP_ERROR:
                case DNS_RESP_NO_EXPECTED_RECORD:
-                       res_preferred_afinet = resolution->resolver_family_priority == AF_INET && resolution->query_type == DNS_RTYPE_A;
-                       res_preferred_afinet6 = resolution->resolver_family_priority == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
+                       res_preferred_afinet = resolution->opts->family_prio == AF_INET && resolution->query_type == DNS_RTYPE_A;
+                       res_preferred_afinet6 = resolution->opts->family_prio == AF_INET6 && resolution->query_type == DNS_RTYPE_AAAA;
 
                        if ((res_preferred_afinet || res_preferred_afinet6)
                                       || (resolution->try > 0)) {
@@ -2753,7 +2753,7 @@ int snr_resolution_error_cb(struct dns_resolution *resolution, int error_code)
                                }
                                else {
                                        resolution->try -= 1;
-                                       if (resolution->resolver_family_priority == AF_INET) {
+                                       if (resolution->opts->family_prio == AF_INET) {
                                                resolution->query_type = DNS_RTYPE_A;
                                        } else {
                                                resolution->query_type = DNS_RTYPE_AAAA;