]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: extend str2sa_range to add an alt parameter
authorAperence <anthony.doeraene.dev@gmail.com>
Mon, 26 Aug 2024 09:50:24 +0000 (11:50 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 30 Aug 2024 16:53:49 +0000 (18:53 +0200)
Add a new parameter "alt" that will store wether this configuration
use an alternate protocol.

This alt pointer will contain a value that can be transparently
passed to protocol_lookup to obtain an appropriate protocol structure.

This change is needed to allow for example the servers to know if it
need to use an alternate protocol or not.

14 files changed:
include/haproxy/protocol.h
include/haproxy/tools.h
src/cfgparse-listen.c
src/cfgparse.c
src/check.c
src/cli.c
src/hlua.c
src/http_client.c
src/log.c
src/resolvers.c
src/server.c
src/ssl_ocsp.c
src/tcpcheck.c
src/tools.c

index 475ac5ebb48af6b7403e6e243dd2ea315da35e2b..7b14e1581995f6f8752f397a3539be8feb0520bc 100644 (file)
@@ -95,10 +95,10 @@ int protocol_enable_all(void);
  * supported protocol types, and ctrl_type of either SOCK_STREAM or SOCK_DGRAM
  * depending on the requested values, or NULL if not found.
  */
-static inline struct protocol *protocol_lookup(int family, enum proto_type proto_type, int ctrl_dgram)
+static inline struct protocol *protocol_lookup(int family, enum proto_type proto_type, int alt)
 {
        if (family >= 0 && family < AF_CUST_MAX)
-               return __protocol_by_family[family][proto_type][!!ctrl_dgram];
+               return __protocol_by_family[family][proto_type][!!alt];
        return NULL;
 }
 
index 5c4a81228f20d5565f6a26c2cc0865f1c04f8951..f4af49d513583abff1a1a659cac1238d89aec13c 100644 (file)
@@ -286,7 +286,7 @@ static inline int is_idchar(char c)
  */
 struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd,
                                       struct protocol **proto, struct net_addr_type *sa_type,
-                                      char **err, const char *pfx, char **fqdn, unsigned int opts);
+                                      char **err, const char *pfx, char **fqdn, int *alt, unsigned int opts);
 
 
 /* converts <addr> and <port> into a string representation of the address and port. This is sort
index 2cab44109546ef7ddf4b4ff0fbbd30e2c296be2b..9f2786e8d6abf51573c27dcc818baaf0737fa138 100644 (file)
@@ -2497,7 +2497,7 @@ stats_error_parsing:
                        err_code |= ERR_WARN;
 
                sk = str2sa_range(args[1], NULL, &port1, &port2, NULL, NULL, NULL,
-                                 &errmsg, NULL, NULL,
+                                 &errmsg, NULL, NULL, NULL,
                                  PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
                if (!sk) {
                        ha_alert("parsing [%s:%d] : '%s' : %s\n", file, linenum, args[0], errmsg);
@@ -2786,7 +2786,8 @@ stats_error_parsing:
                curproxy->conn_src.iface_len = 0;
 
                sk = str2sa_range(args[1], NULL, &port1, &port2, NULL, NULL, NULL,
-                                 &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
+                                 &errmsg, NULL, NULL, NULL,
+                                 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
                if (!sk) {
                        ha_alert("parsing [%s:%d] : '%s %s' : %s\n",
                                 file, linenum, args[0], args[1], errmsg);
@@ -2860,7 +2861,8 @@ stats_error_parsing:
                                        struct sockaddr_storage *sk;
 
                                        sk = str2sa_range(args[cur_arg + 1], NULL, &port1, &port2, NULL, NULL, NULL,
-                                                         &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
+                                                         &errmsg, NULL, NULL, NULL,
+                                                         PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
                                        if (!sk) {
                                                ha_alert("parsing [%s:%d] : '%s %s' : %s\n",
                                                         file, linenum, args[cur_arg], args[cur_arg+1], errmsg);
index 4296e47e8f73350bb15d87c23deb8619b7a3e57c..d90fdc5eab5d060d3a930e9189eb452e60ed58b4 100644 (file)
@@ -158,7 +158,7 @@ int str2listener(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
 
                ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, NULL, err,
                                   (curproxy == global.cli_fe || curproxy == mworker_proxy) ? NULL : global.unix_bind.prefix,
-                                  NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
+                                  NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
                                          PA_O_SOCKET_FD | PA_O_STREAM | PA_O_XPRT);
                if (!ss2)
                        goto fail;
@@ -244,7 +244,7 @@ int str2receiver(char *str, struct proxy *curproxy, struct bind_conf *bind_conf,
 
                ss2 = str2sa_range(str, NULL, &port, &end, &fd, &proto, NULL, err,
                                   curproxy == global.cli_fe ? NULL : global.unix_bind.prefix,
-                                  NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
+                                  NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_PORT_RANGE |
                                          PA_O_SOCKET_FD | PA_O_DGRAM | PA_O_XPRT);
                if (!ss2)
                        goto fail;
@@ -1185,7 +1185,7 @@ int cfg_parse_mailers(const char *file, int linenum, char **args, int kwm)
                newmailer->id = strdup(args[1]);
 
                sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto, NULL,
-                                 &errmsg, NULL, NULL,
+                                 &errmsg, NULL, NULL, NULL,
                                  PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
                if (!sk) {
                        ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
index 40d403b2729cd760022f64e9c7e658d7f6c59f66..1474a55f9dfc50ff71db8390914cacc9f54899d5 100644 (file)
@@ -2032,7 +2032,7 @@ static int srv_parse_addr(char **args, int *cur_arg, struct proxy *curpx, struct
                goto error;
        }
 
-       sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, NULL, errmsg, NULL, NULL,
+       sk = str2sa_range(args[*cur_arg+1], NULL, &port1, &port2, NULL, NULL, NULL, errmsg, NULL, NULL, NULL,
                          PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
        if (!sk) {
                memprintf(errmsg, "'%s' : %s", args[*cur_arg], *errmsg);
index 81422e8607f60e850330e6daa5e265f3fe5152e4..3e0ec1e506aeb643da16d5ea4f1de247709b4feb 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -3335,7 +3335,7 @@ int mworker_cli_proxy_create()
 
                memprintf(&msg, "sockpair@%d", child->ipc_fd[0]);
                if ((sk = str2sa_range(msg, &port, &port1, &port2, NULL, &proto, NULL,
-                                      &errmsg, NULL, NULL, PA_O_STREAM)) == 0) {
+                                      &errmsg, NULL, NULL, NULL, PA_O_STREAM)) == 0) {
                        goto error;
                }
                ha_free(&msg);
index 30bd34edd5f5e3ba68e50216ba3d4dcfbab5c206..96773a86a759726c17485c43d7b958f7182193b9 100644 (file)
@@ -3519,7 +3519,8 @@ __LJMP static int hlua_socket_connect(struct lua_State *L)
                csk_ctx->srv = socket_tcp;
 
        /* Parse ip address. */
-       addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM);
+       addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL,
+                           NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM);
        if (!addr) {
                xref_unlock(&socket->xref, peer);
                WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
index 6deff05bd697066540fb43a0fcda33f1331be8b8..5db0e175c80fa7ef55d94a4b413a6555649ccedc 100644 (file)
@@ -455,7 +455,7 @@ int httpclient_set_dst(struct httpclient *hc, const char *dst)
        sockaddr_free(&hc->dst);
        /* 'sk' is statically allocated (no need to be freed). */
        sk = str2sa_range(dst, NULL, NULL, NULL, NULL, NULL, NULL,
-                         &errmsg, NULL, NULL,
+                         &errmsg, NULL, NULL, NULL,
                          PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
        if (!sk) {
                ha_alert("httpclient: Failed to parse destination address in %s\n", errmsg);
index d1f285a396387b2179a5bdb0f87756750bf79a28..993fe08e80b4597a00efc30e531550e2c3c1168a 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1600,7 +1600,7 @@ static int parse_log_target(char *raw, struct log_target *target, char **err)
 
        /* parse the target address */
        sk = str2sa_range(raw, NULL, &port1, &port2, &fd, &proto, NULL,
-                         err, NULL, NULL,
+                         err, NULL, NULL, NULL,
                          PA_O_RESOLVE | PA_O_PORT_OK | PA_O_RAW_FD | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
        if (!sk)
                goto error;
index 33a9bbd7927a97b8bf2b2c0e23485e18de484be7..99b4ef0cf5ea2be9e4ae5bf28c0c84a4e6a15317 100644 (file)
@@ -3616,7 +3616,8 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
                }
 
                sk = str2sa_range(args[2], NULL, &port1, &port2, NULL, &proto, NULL,
-                                 &errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
+                                 &errmsg, NULL, NULL, NULL,
+                                 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_DGRAM | PA_O_STREAM | PA_O_DEFAULT_DGRAM);
                if (!sk) {
                        ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
index c6f39012f538b98a2e5f5cc57d1cdf36a548b85e..a4f78cc661ce4b8cb3e87b16bd45ec45c9b4a725 100644 (file)
@@ -1749,7 +1749,7 @@ static int srv_parse_source(char **args, int *cur_arg,
 
        /* 'sk' is statically allocated (no need to be freed). */
        sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, NULL,
-                         &errmsg, NULL, NULL,
+                         &errmsg, NULL, NULL, NULL,
                          PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
        if (!sk) {
                memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
@@ -1837,7 +1837,7 @@ static int srv_parse_source(char **args, int *cur_arg,
 
                                /* 'sk' is statically allocated (no need to be freed). */
                                sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL, NULL,
-                                                 &errmsg, NULL, NULL,
+                                                 &errmsg, NULL, NULL, NULL,
                                                  PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
                                if (!sk) {
                                        ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
@@ -1927,7 +1927,7 @@ static int srv_parse_socks4(char **args, int *cur_arg,
 
        /* 'sk' is statically allocated (no need to be freed). */
        sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL, NULL,
-                         &errmsg, NULL, NULL,
+                         &errmsg, NULL, NULL, NULL,
                          PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
        if (!sk) {
                memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
@@ -3396,7 +3396,7 @@ static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
                        goto skip_addr;
 
                sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL, &newsrv->addr_type,
-                                 &errmsg, NULL, &fqdn,
+                                 &errmsg, NULL, &fqdn, NULL,
                                  (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK |
                                  (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) |
                                  PA_O_STREAM | PA_O_DGRAM | PA_O_XPRT);
index 7d3a485b8de22b596156a6cbee0891d594089e9c..19bb92ac361a44506d17b0f2c443ca160db1cfa1 100644 (file)
@@ -1970,7 +1970,7 @@ static int ocsp_update_parse_global_http_proxy(char **args, int section_type, st
        sockaddr_free(&ocsp_update_dst);
        /* 'sk' is statically allocated (no need to be freed). */
        sk = str2sa_range(args[1], NULL, NULL, NULL, NULL, NULL, NULL,
-                         &errmsg, NULL, NULL,
+                         &errmsg, NULL, NULL, NULL,
                          PA_O_PORT_OK | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
        if (!sk) {
                ha_alert("ocsp-update: Failed to parse destination address in %s\n", errmsg);
index 5e3775ba6a4ea398fff4e34c28381a328909f31a..d5bd796f35d2e2a7fdd674981d9aca7203e05ea0 100644 (file)
@@ -2558,7 +2558,8 @@ struct tcpcheck_rule *parse_tcpcheck_connect(char **args, int cur_arg, struct pr
                        }
 
                        sk = str2sa_range(args[cur_arg+1], NULL, &port1, &port2, NULL, NULL, NULL,
-                                         errmsg, NULL, NULL, PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
+                                         errmsg, NULL, NULL, NULL,
+                                         PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
                        if (!sk) {
                                memprintf(errmsg, "'%s' : %s.", args[cur_arg], *errmsg);
                                goto error;
index b0937c82b2698c102562f42f59d35523e882cc77..db414600f6a6ecdc6e530a85a18df160abbc5eb3 100644 (file)
@@ -967,7 +967,7 @@ struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, i
  */
 struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd,
                                       struct protocol **proto, struct net_addr_type *sa_type,
-                                      char **err, const char *pfx, char **fqdn, unsigned int opts)
+                                      char **err, const char *pfx, char **fqdn, int *alt, unsigned int opts)
 {
        static THREAD_LOCAL struct sockaddr_storage ss;
        struct sockaddr_storage *ret = NULL;
@@ -979,6 +979,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
        int new_fd = -1;
        enum proto_type proto_type = 0; // to shut gcc warning
        int ctrl_type = 0; // to shut gcc warning
+       int alt_proto = 0;
 
        portl = porth = porta = 0;
        if (fqdn)
@@ -1002,6 +1003,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
            ((opts & (PA_O_STREAM|PA_O_DGRAM)) == (PA_O_DGRAM|PA_O_STREAM) && (opts & PA_O_DEFAULT_DGRAM))) {
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        } else {
                proto_type = PROTO_TYPE_STREAM;
                ctrl_type = SOCK_STREAM;
@@ -1016,6 +1018,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                str2 += 6;
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        }
        else if (strncmp(str2, "quic+", 5) == 0) {
                str2 += 5;
@@ -1034,6 +1037,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                ss.ss_family = AF_UNIX;
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        }
        else if (strncmp(str2, "uxst@", 5) == 0) {
                str2 += 5;
@@ -1070,6 +1074,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                ss.ss_family = AF_INET;
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        }
        else if (strncmp(str2, "tcp6@", 5) == 0) {
                str2 += 5;
@@ -1082,6 +1087,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                ss.ss_family = AF_INET6;
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        }
        else if (strncmp(str2, "tcp@", 4) == 0) {
                str2 += 4;
@@ -1094,6 +1100,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                ss.ss_family = AF_UNSPEC;
                proto_type = PROTO_TYPE_DGRAM;
                ctrl_type = SOCK_DGRAM;
+               alt_proto = 1;
        }
        else if (strncmp(str2, "quic4@", 6) == 0) {
                str2 += 6;
@@ -1367,7 +1374,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                 */
                new_proto = protocol_lookup(ss.ss_family,
                                            proto_type,
-                                           ctrl_type == SOCK_DGRAM);
+                                           alt_proto);
 
                if (!new_proto && (!fqdn || !*fqdn) && (ss.ss_family != AF_CUST_EXISTING_FD)) {
                        memprintf(err, "unsupported %s protocol for %s family %d address '%s'%s",
@@ -1408,6 +1415,8 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int
                sa_type->proto_type = proto_type;
                sa_type->xprt_type = (ctrl_type == SOCK_DGRAM) ? PROTO_TYPE_DGRAM : PROTO_TYPE_STREAM;
        }
+       if (alt)
+               *alt = alt_proto;
        free(back);
        return ret;
 }
@@ -6337,7 +6346,7 @@ const char *hash_ipanon(uint32_t scramble, char *ipstring, int hasport)
                        sa = &ss;
                }
                else {
-                       sa = str2sa_range(ipstring, NULL, NULL, NULL, NULL, NULL, NULL, &errmsg, NULL, NULL,
+                       sa = str2sa_range(ipstring, NULL, NULL, NULL, NULL, NULL, NULL, &errmsg, NULL, NULL, NULL,
                                          PA_O_PORT_OK | PA_O_STREAM | PA_O_DGRAM | PA_O_XPRT | PA_O_CONNECT |
                                          PA_O_PORT_RANGE | PA_O_PORT_OFS | PA_O_RESOLVE);
                        if (sa == NULL) {