]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: spoe: Add a function to validate a version is supported
authorChristopher Faulet <cfaulet@haproxy.com>
Mon, 22 Jul 2024 16:55:28 +0000 (18:55 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 24 Jul 2024 12:19:10 +0000 (14:19 +0200)
spoe_check_vsn() function can now be used to check if a version, converted
to an integer, via spoe_str_to_vsn() for instance, is supported. To do so,
the list of all supported version is now exported.

include/haproxy/spoe-t.h
include/haproxy/spoe.h
src/mux_spop.c

index 26332ff964ceb8fa1dd82587438fd9067484a6cf..e2db8a1d81f3aa1fe7676bd62e498cec32c1b129 100644 (file)
@@ -95,6 +95,12 @@ enum spop_vars_scope {
 #define SPOP_DATA_FL_FALSE 0x00
 #define SPOP_DATA_FL_TRUE  0x10
 
+struct spop_version {
+       char *str;
+       int   min;
+       int   max;
+};
+
 /* All supported SPOP data types */
 enum spop_data_type {
        SPOP_DATA_T_NULL = 0,
index d397d4008a68c81dfa64e1f1e1f01e965c099ca8..37f00ef68e3583a201887c034508c6d7bb017ba1 100644 (file)
@@ -29,6 +29,8 @@
 
 struct appctx;
 
+extern const struct spop_version spop_supported_versions[];
+
 struct spoe_agent *spoe_appctx_agent(struct appctx *appctx);
 
 /* Encode a buffer. Its length <len> is encoded as a varint, followed by a copy
@@ -344,5 +346,21 @@ out:
        return vsn;
 }
 
+/* Check if vsn, converted into an integer, is supported by looping on the list
+ * of supported versions. It return -1 on error and 0 on success.
+ */
+static inline int spoe_check_vsn(int vsn)
+{
+       int i;
+
+       for (i = 0; spop_supported_versions[i].str != NULL; ++i) {
+               if (vsn >= spop_supported_versions[i].min &&
+                   vsn <= spop_supported_versions[i].max)
+                       break;
+       }
+       if (spop_supported_versions[i].str == NULL)
+               return -1;
+       return 0;
+}
 
 #endif /* _HAPROXY_SPOE_H */
index 901695769cf8afc7d170abab7646ad7339181dc2..e660f473367c2b9c3abe8d8ee0f78dc2ab48a039 100644 (file)
@@ -245,14 +245,8 @@ const char *spop_err_reasons[SPOP_ERR_ENTRIES] = {
 #define SPOP_STATUS_CODE_KEY            "status-code"
 #define SPOP_MSG_KEY                    "message"
 
-struct spop_version {
-       char *str;
-       int   min;
-       int   max;
-};
-
 /* All supported versions */
-static struct spop_version spop_supported_versions[] = {
+const struct spop_version spop_supported_versions[] = {
        /* 1.0 is now unsupported because of a bug about frame's flags*/
        {"2.0", 2000, 2000},
        {NULL,  0, 0}
@@ -1619,7 +1613,7 @@ static int spop_conn_handle_hello(struct spop_conn *spop_conn)
 
                /* Check "version" K/V item */
                if (sz >= strlen(SPOP_VERSION_KEY) && !memcmp(str, SPOP_VERSION_KEY, strlen(SPOP_VERSION_KEY))) {
-                       int i, type = *p++;
+                       int type = *p++;
 
                        /* The value must be a string */
                        if ((type & SPOP_DATA_T_MASK) != SPOP_DATA_T_STR) {
@@ -1633,15 +1627,10 @@ static int spop_conn_handle_hello(struct spop_conn *spop_conn)
 
                        vsn = spoe_str_to_vsn(str, sz);
                        if (vsn == -1) {
-                               spop_conn_error(spop_conn, SPOP_ERR_BAD_VSN);
+                               spop_conn_error(spop_conn, SPOP_ERR_INVALID);
                                goto fail;
                        }
-                       for (i = 0; spop_supported_versions[i].str != NULL; ++i) {
-                               if (vsn >= spop_supported_versions[i].min &&
-                                   vsn <= spop_supported_versions[i].max)
-                                       break;
-                       }
-                       if (spop_supported_versions[i].str == NULL) {
+                       if (spoe_check_vsn(vsn) == -1) {
                                spop_conn_error(spop_conn, SPOP_ERR_BAD_VSN);
                                goto fail;
                        }