From 66795bd7216fe5d38bdb42511e8126f91b7d38ab Mon Sep 17 00:00:00 2001 From: Aurelien DARRAGON Date: Thu, 19 Oct 2023 16:06:03 +0200 Subject: [PATCH] MINOR: connection: add conn_pr_mode_to_proto_mode() helper func 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 | 19 +++++++++++++++++++ src/cfgparse.c | 4 ++-- src/stream.c | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h index 98b84b0e0..b384f5099 100644 --- a/include/haproxy/connection.h +++ b/include/haproxy/connection.h @@ -605,6 +605,10 @@ void list_mux_proto(FILE *out); * HTTP). can be empty. Will fall back to the first compatible mux * with exactly the same or with an empty name. May return * null if the code improperly registered the default mux to use as a fallback. + * + * 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 */ /* diff --git a/src/cfgparse.c b/src/cfgparse.c index 310214464..ec5e7e4b6 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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) diff --git a/src/stream.c b/src/stream.c index b1e487bef..ef8d46625 100644 --- a/src/stream.c +++ b/src/stream.c @@ -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;