]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: connection: add conn_pr_mode_to_proto_mode() helper func
authorAurelien DARRAGON <adarragon@haproxy.com>
Thu, 19 Oct 2023 14:06:03 +0000 (16:06 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 25 Oct 2023 09:59:27 +0000 (11:59 +0200)
This function allows to safely map proxy mode to corresponding proto_mode

This will allow for easier code maintenance and prevent mixups between
proxy mode and proto mode.

include/haproxy/connection.h
src/cfgparse.c
src/stream.c

index 98b84b0e0d9a479fbf900ffb9edcd9a20de3ff67..b384f5099d60eaecab9b91592e1af18e4206b2cd 100644 (file)
@@ -605,6 +605,10 @@ void list_mux_proto(FILE *out);
  * HTTP). <mux_proto> can be empty. Will fall back to the first compatible mux
  * with exactly the same <proto_mode> or with an empty name. May return
  * null if the code improperly registered the default mux to use as a fallback.
+ *
+ * <proto_mode> expects PROTO_MODE_* value only: PROXY_MODE_* values should
+ * never be used directly here (but you may use conn_pr_mode_to_proto_mode()
+ * to map proxy mode to corresponding proto mode before calling the function).
  */
 static inline const struct mux_proto_list *conn_get_best_mux_entry(
         const struct ist mux_proto,
@@ -733,6 +737,21 @@ static inline void set_tlv_arg(int tlv_type, struct arg *tlv_arg)
        tlv_arg->data.sint = tlv_type;
 }
 
+/*
+ * Map proxy mode (PR_MODE_*) to equivalent proto_proxy_mode (PROTO_MODE_*)
+ */
+static inline int conn_pr_mode_to_proto_mode(int proxy_mode)
+{
+       int mode;
+
+       /* for now we only support TCP and HTTP proto_modes, so we
+        * consider that if it's not HTTP, then it's TCP
+        */
+       mode = 1 << (proxy_mode == PR_MODE_HTTP);
+
+       return mode;
+}
+
 #endif /* _HAPROXY_CONNECTION_H */
 
 /*
index 310214464c716be9a03afbcf2fc87a290931b860..ec5e7e4b6f6cf717ec0614d94643acd213d8785e 100644 (file)
@@ -4007,7 +4007,7 @@ out_uri_auth_compat:
                /* Check the mux protocols, if any, for each listener and server
                 * attached to the current proxy */
                list_for_each_entry(bind_conf, &curproxy->conf.bind, by_fe) {
-                       int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
+                       int mode = conn_pr_mode_to_proto_mode(curproxy->mode);
                        const struct mux_proto_list *mux_ent;
 
                        if (!bind_conf->mux_proto) {
@@ -4058,7 +4058,7 @@ out_uri_auth_compat:
                        bind_conf->mux_proto = mux_ent;
                }
                for (newsrv = curproxy->srv; newsrv; newsrv = newsrv->next) {
-                       int mode = (1 << (curproxy->mode == PR_MODE_HTTP));
+                       int mode = conn_pr_mode_to_proto_mode(curproxy->mode);
                        const struct mux_proto_list *mux_ent;
 
                        if (!newsrv->mux_proto)
index b1e487bef1dfa36b966779ed4e5d61c4cdb72f0d..ef8d46625ea7970ed92643cbaa701dc0ab3a7f83 100644 (file)
@@ -2947,7 +2947,7 @@ static int check_tcp_switch_stream_mode(struct act_rule *rule, struct proxy *px,
        const struct mux_proto_list *mux_ent;
        const struct mux_proto_list *mux_proto = rule->arg.act.p[1];
        enum pr_mode pr_mode = (uintptr_t)rule->arg.act.p[0];
-       enum proto_proxy_mode mode = (1 << (pr_mode == PR_MODE_HTTP));
+       enum proto_proxy_mode mode = conn_pr_mode_to_proto_mode(pr_mode);
 
        if (pr_mode == PR_MODE_HTTP)
                px->options |= PR_O_HTTP_UPG;