]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[CLEANUP] stick_table: move pattern to key functions to stick_table.c
authorWilly Tarreau <w@1wt.eu>
Sun, 6 Jun 2010 11:22:23 +0000 (13:22 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 14 Jun 2010 13:10:24 +0000 (15:10 +0200)
pattern.c depended on stick_table while in fact it should be the opposite.
So we move from pattern.c everything related to stick_tables and invert the
dependency. That way the code becomes more logical and intuitive.

include/common/standard.h
include/proto/pattern.h
include/proto/stick_table.h
src/cfgparse.c
src/pattern.c
src/session.c
src/standard.c
src/stick_table.c

index 762fab5624c4c12efa3027de940fef15d3b31078..eff47e749443c9791ac347c5f7330c3841264d6d 100644 (file)
@@ -379,4 +379,9 @@ unsigned int get_next_id(struct eb_root *root, unsigned int key);
  */
 int word_match(const char *sample, int slen, const char *word, int wlen);
 
+/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
+ * or the number of chars read in case of success.
+ */
+int buf2ip(const char *buf, size_t len, struct in_addr *dst);
+
 #endif /* _COMMON_STANDARD_H */
index 41eda06e967a8fc1c240e213645e4d3afd146b02..0bef7e6a27153c7c6e6c792ee35880bbf08b141e 100644 (file)
@@ -29,10 +29,6 @@ struct pattern_expr *pattern_parse_expr(char **str, int *idx);
 struct pattern *pattern_process(struct proxy *px, struct session *l4,
                                 void *l7, int dir, struct pattern_expr *expr,
                                 struct pattern *p);
-struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4,
-                                         void *l7, int dir, struct pattern_expr *expr,
-                                         unsigned long table_type);
-int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type);
 void pattern_register_fetches(struct pattern_fetch_kw_list *psl);
 void pattern_register_convs(struct pattern_conv_kw_list *psl);
 
index ed44cc0c751d98749a5c2d0189b6003f91365df7..f26e002a9000476755de150db8c6e427cb36cce1 100644 (file)
@@ -32,6 +32,10 @@ int stktable_init(struct stktable *t);
 int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size);
 int stktable_store(struct stktable *t, struct stksess *ts, int sid);
 struct stksess *stktable_lookup(struct stktable *t, struct stktable_key *key);
+struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4,
+                                       void *l7, int dir, struct pattern_expr *expr,
+                                       unsigned long table_type);
+int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type);
 
 
 #endif /* _PROTO_STICK_TABLE_H */
index 1abb1b7137435cbab398ef5951ede55673cc103c..d2f79f2200c3ea72b92e35717762f7cd6a8015fd 100644 (file)
@@ -4873,7 +4873,7 @@ int check_config_validity()
                                      curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
                                cfgerr++;
                        }
-                       else if (pattern_notusable_key(mrule->expr,  target->table.type)) {
+                       else if (!stktable_compatible_pattern(mrule->expr,  target->table.type)) {
                                Alert("Proxy '%s': type of pattern not usable with type of stick-table '%s'.\n",
                                      curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
                                cfgerr++;
@@ -4905,7 +4905,7 @@ int check_config_validity()
                                      curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
                                cfgerr++;
                        }
-                       else if (pattern_notusable_key(mrule->expr, target->table.type)) {
+                       else if (!stktable_compatible_pattern(mrule->expr, target->table.type)) {
                                Alert("Proxy '%s': type of pattern not usable with type of stick-table '%s'.\n",
                                      curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
                                cfgerr++;
index 63bae2b8cada375a65e0d2534a3fa63d02663c07..6a12442fc3625d43674277d1024fe95de982e219 100644 (file)
@@ -12,7 +12,6 @@
 
 #include <string.h>
 #include <arpa/inet.h>
-#include <types/stick_table.h>
 
 #include <proto/pattern.h>
 #include <common/standard.h>
@@ -30,9 +29,6 @@ static char pattern_trash_buf2[BUFSIZE];
 /* pattern_trash_buf point on used buffer*/
 static char *pattern_trash_buf = pattern_trash_buf1;
 
-/* static structure used to returns builded table key from a pattern*/
-static struct stktable_key stable_key;
-
 /* list head of all known pattern fetch keywords */
 static struct pattern_fetch_kw_list pattern_fetches = {
        .list = LIST_HEAD_INIT(pattern_fetches.list)
@@ -159,56 +155,6 @@ static int c_int2ip(union pattern_data *data)
        return 1;
 }
 
-/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
- * or the number of chars read in case of success.
- */
-static int buf2ip(const char *buf, size_t len, struct in_addr *dst)
-{
-       const char *addr;
-       int saw_digit, octets, ch;
-       u_char tmp[4], *tp;
-       const char *cp = buf;
-
-       saw_digit = 0;
-       octets = 0;
-       *(tp = tmp) = 0;
-
-       for (addr = buf; addr - buf < len; addr++) {
-               unsigned char digit = (ch = *addr) - '0';
-
-               if (digit > 9 && ch != '.')
-                       break;
-
-               if (digit <= 9) {
-                       u_int new = *tp * 10 + digit;
-
-                       if (new > 255)
-                               return 0;
-
-                       *tp = new;
-
-                       if (!saw_digit) {
-                               if (++octets > 4)
-                                       return 0;
-                               saw_digit = 1;
-                       }
-               } else if (ch == '.' && saw_digit) {
-                       if (octets == 4)
-                               return 0;
-
-                       *++tp = 0;
-                       saw_digit = 0;
-               } else
-                       return 0;
-       }
-
-       if (octets < 4)
-               return 0;
-
-       memcpy(&dst->s_addr, tmp, 4);
-       return addr - cp;
-}
-
 static int c_str2ip(union pattern_data *data)
 {
        if (!buf2ip(data->str.str, data->str.len, &data->ip))
@@ -263,100 +209,14 @@ static int c_str2int(union pattern_data *data)
 /*           NULL pointer used for impossible pattern casts      */
 /*****************************************************************/
 
-typedef int (*pattern_cast)(union pattern_data *data);
-static pattern_cast pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = { { c_donothing, c_ip2int, c_ip2str  },
-                                                                   { c_int2ip, c_donothing, c_int2str },
-                                                                   { c_str2ip, c_str2int, c_donothing } };
-
-
-/*****************************************************************/
-/*    typed pattern to typed table key functions                 */
-/*****************************************************************/
-
-static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       return (void *)&pdata->integer;
-}
-
-static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       return (void *)&pdata->ip.s_addr;
-}
-
-static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       kdata->integer = ntohl(pdata->ip.s_addr);
-       return (void *)&kdata->integer;
-}
-
-static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       kdata->ip.s_addr = htonl(pdata->integer);
-       return (void *)&kdata->ip.s_addr;
-}
-
-static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       *len = pdata->str.len;
-       return (void *)pdata->str.str;
-}
-
-static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
-               return NULL;
-
-       *len = strlen((const char *)kdata->buf);
-       return (void *)kdata->buf;
-}
-
-static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       void *key;
-
-       key = (void *)ultoa_r(pdata->integer,  kdata->buf,  sizeof(kdata->buf));
-       if (!key)
-               return NULL;
-
-       *len = strlen((const char *)key);
-       return key;
-}
-
-static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
-               return NULL;
-
-       return (void *)&kdata->ip.s_addr;
-}
-
-
-static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
-{
-       int i;
-
-       kdata->integer = 0;
-       for (i = 0; i < pdata->str.len; i++) {
-               uint32_t val = pdata->str.str[i] - '0';
-
-               if (val > 9)
-                       break;
-
-               kdata->integer = kdata->integer * 10 + val;
-       }
-       return (void *)&kdata->integer;
-}
+typedef int (*pattern_cast_fct)(union pattern_data *data);
+static pattern_cast_fct pattern_casts[PATTERN_TYPES][PATTERN_TYPES] = {
+       { c_donothing, c_ip2int,    c_ip2str    },
+       { c_int2ip,    c_donothing, c_int2str   },
+       { c_str2ip,    c_str2int,   c_donothing },
+};
 
-/*****************************************************************/
-/*      typed pattern to typed table key matrix:                 */
-/*           pattern_keys[from pattern type][to table key type]  */
-/*           NULL pointer used for impossible pattern casts      */
-/*****************************************************************/
 
-typedef void *(*pattern_key)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
-static pattern_key pattern_keys[PATTERN_TYPES][STKTABLE_TYPES] = { { k_ip2ip,  k_ip2int,  k_ip2str  },
-                                                                   { k_int2ip, k_int2int, k_int2str },
-                                                                   { k_str2ip, k_str2int, k_str2str } };
 /*
  * Parse a pattern expression configuration:
  *        fetch keyword followed by format conversion keywords.
@@ -485,55 +345,6 @@ struct pattern *pattern_process(struct proxy *px, struct session *l4, void *l7,
        return p;
 }
 
-/*
- *  Process a fetch + format conversion of defined by the pattern expression <expr>
- *  on request or response considering the <dir> parameter.
- *  Returns a pointer on a static tablekey  structure of type <table_type> of
- *  the converted result.
- */
-struct stktable_key *pattern_process_key(struct proxy *px, struct session *l4, void *l7, int dir,
-                                         struct pattern_expr *expr, unsigned long table_type)
-{
-       struct pattern *ptrn;
-
-       ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
-       if (!ptrn)
-               return NULL;
-
-       stable_key.key_len = (size_t)-1;
-       stable_key.key = pattern_keys[ptrn->type][table_type](&ptrn->data, &stable_key.data, &stable_key.key_len);
-
-       if (!stable_key.key)
-               return NULL;
-
-       return &stable_key;
-}
-
-/*
- * Returns 1 if pattern expression <expr> result cannot be converted to table key of
- * type <table_type> .
- *
- * Used in configuration check
- */
-int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
-{
-
-       if (table_type >= STKTABLE_TYPES)
-               return 1;
-
-       if (LIST_ISEMPTY(&expr->conv_exprs)) {
-               if (!pattern_keys[expr->fetch->out_type][table_type])
-                       return 1;
-       } else {
-               struct pattern_conv_expr *conv_expr;
-               conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
-
-               if (!pattern_keys[conv_expr->conv->out_type][table_type])
-                       return 1;
-       }
-       return 0;
-}
-
 /* Converts an argument string to an IPv4 mask stored in network byte order in
  * arg_i. Returns non-zero in case of success, 0 on error.
  */
index 137bbd330d161a18c6940ee5a2b2c8931b0cb9ad..63184f389abe8b498b5d5b27d699d6b6cfdbe5c8 100644 (file)
@@ -29,7 +29,6 @@
 #include <proto/hdr_idx.h>
 #include <proto/log.h>
 #include <proto/session.h>
-#include <proto/pattern.h>
 #include <proto/pipe.h>
 #include <proto/proto_http.h>
 #include <proto/proto_tcp.h>
@@ -936,7 +935,7 @@ int process_sticking_rules(struct session *s, struct buffer *req, int an_bit)
                if (ret) {
                        struct stktable_key *key;
 
-                       key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
+                       key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_REQ, rule->expr, rule->table.t->type);
                        if (!key)
                                continue;
 
@@ -1027,7 +1026,7 @@ int process_store_rules(struct session *s, struct buffer *rep, int an_bit)
                if (ret) {
                        struct stktable_key *key;
 
-                       key = pattern_process_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
+                       key = stktable_fetch_key(px, s, &s->txn, PATTERN_FETCH_RTR, rule->expr, rule->table.t->type);
                        if (!key)
                                continue;
 
index c8b0d51b38209d17482db58879d53288b9a0a078..3f3e6bd0e634c692495f4e755fd05b5596dce08a 100644 (file)
@@ -1007,6 +1007,58 @@ unsigned int inetaddr_host_lim_ret(const char *text, char *stop, const char **re
        return ((dig100 * 10) + dig10) * 10 + dig1;
 }
 
+/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
+ * or the number of chars read in case of success. Maybe this could be replaced
+ * by one of the functions above. Also, apparently this function does not support
+ * hosts above 255 and requires exactly 4 octets.
+ */
+int buf2ip(const char *buf, size_t len, struct in_addr *dst)
+{
+       const char *addr;
+       int saw_digit, octets, ch;
+       u_char tmp[4], *tp;
+       const char *cp = buf;
+
+       saw_digit = 0;
+       octets = 0;
+       *(tp = tmp) = 0;
+
+       for (addr = buf; addr - buf < len; addr++) {
+               unsigned char digit = (ch = *addr) - '0';
+
+               if (digit > 9 && ch != '.')
+                       break;
+
+               if (digit <= 9) {
+                       u_int new = *tp * 10 + digit;
+
+                       if (new > 255)
+                               return 0;
+
+                       *tp = new;
+
+                       if (!saw_digit) {
+                               if (++octets > 4)
+                                       return 0;
+                               saw_digit = 1;
+                       }
+               } else if (ch == '.' && saw_digit) {
+                       if (octets == 4)
+                               return 0;
+
+                       *++tp = 0;
+                       saw_digit = 0;
+               } else
+                       return 0;
+       }
+
+       if (octets < 4)
+               return 0;
+
+       memcpy(&dst->s_addr, tmp, 4);
+       return addr - cp;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8
index 099f78301dc3bc1125b2ebbfb6c0446d7081e650..fa469255529a4a59a8c35dd2e11cbaf4c9934c13 100644 (file)
 
 #include <types/stick_table.h>
 
+#include <proto/pattern.h>
 #include <proto/proxy.h>
 #include <proto/session.h>
 #include <proto/task.h>
 
 
+/* static structure used to return a table key built from a pattern */
+static struct stktable_key static_table_key;
+
 /*
  * Free an allocated sticky session <ts>, and decrease sticky sessions counter
  * in table <t>.
@@ -332,4 +336,141 @@ int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *ke
        return 1;
 }
 
+/*****************************************************************/
+/*    typed pattern to typed table key functions                 */
+/*****************************************************************/
+
+static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       return (void *)&pdata->integer;
+}
+
+static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       return (void *)&pdata->ip.s_addr;
+}
+
+static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       kdata->integer = ntohl(pdata->ip.s_addr);
+       return (void *)&kdata->integer;
+}
+
+static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       kdata->ip.s_addr = htonl(pdata->integer);
+       return (void *)&kdata->ip.s_addr;
+}
+
+static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       *len = pdata->str.len;
+       return (void *)pdata->str.str;
+}
+
+static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf)))
+               return NULL;
+
+       *len = strlen((const char *)kdata->buf);
+       return (void *)kdata->buf;
+}
+
+static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       void *key;
+
+       key = (void *)ultoa_r(pdata->integer,  kdata->buf,  sizeof(kdata->buf));
+       if (!key)
+               return NULL;
+
+       *len = strlen((const char *)key);
+       return key;
+}
+
+static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip))
+               return NULL;
+
+       return (void *)&kdata->ip.s_addr;
+}
+
+
+static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len)
+{
+       int i;
+
+       kdata->integer = 0;
+       for (i = 0; i < pdata->str.len; i++) {
+               uint32_t val = pdata->str.str[i] - '0';
+
+               if (val > 9)
+                       break;
+
+               kdata->integer = kdata->integer * 10 + val;
+       }
+       return (void *)&kdata->integer;
+}
+
+/*****************************************************************/
+/*      typed pattern to typed table key matrix:                 */
+/*         pattern_to_key[from pattern type][to table key type]  */
+/*         NULL pointer used for impossible pattern casts        */
+/*****************************************************************/
+
+typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len);
+static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = {
+       { k_ip2ip,  k_ip2int,  k_ip2str  },
+       { k_int2ip, k_int2int, k_int2str },
+       { k_str2ip, k_str2int, k_str2str },
+};
+
+
+/*
+ * Process a fetch + format conversion as defined by the pattern expression <expr>
+ * on request or response considering the <dir> parameter. Returns either NULL if
+ * no key could be extracted, or a pointer to the converted result stored in
+ * static_table_key in format <table_type>.
+ */
+struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir,
+                                       struct pattern_expr *expr, unsigned long table_type)
+{
+       struct pattern *ptrn;
+
+       ptrn = pattern_process(px, l4, l7, dir, expr, NULL);
+       if (!ptrn)
+               return NULL;
+
+       static_table_key.key_len = (size_t)-1;
+       static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len);
+
+       if (!static_table_key.key)
+               return NULL;
+
+       return &static_table_key;
+}
+
+/*
+ * Returns 1 if pattern expression <expr> result can be converted to table key of
+ * type <table_type>, otherwise zero. Used in configuration check.
+ */
+int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type)
+{
+       if (table_type >= STKTABLE_TYPES)
+               return 0;
+
+       if (LIST_ISEMPTY(&expr->conv_exprs)) {
+               if (!pattern_to_key[expr->fetch->out_type][table_type])
+                       return 0;
+       } else {
+               struct pattern_conv_expr *conv_expr;
+               conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list);
+
+               if (!pattern_to_key[conv_expr->conv->out_type][table_type])
+                       return 0;
+       }
+       return 1;
+}