diff --git a/include/haproxy/spoe-t.h b/include/haproxy/spoe-t.h index 26332ff96..e2db8a1d8 100644 --- a/include/haproxy/spoe-t.h +++ b/include/haproxy/spoe-t.h @@ -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, diff --git a/include/haproxy/spoe.h b/include/haproxy/spoe.h index d397d4008..37f00ef68 100644 --- a/include/haproxy/spoe.h +++ b/include/haproxy/spoe.h @@ -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 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 */ diff --git a/src/mux_spop.c b/src/mux_spop.c index 901695769..e660f4733 100644 --- a/src/mux_spop.c +++ b/src/mux_spop.c @@ -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; }