]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: acl: add parse and match primitives to use binary type on ACLs
authorEmeric Brun <ebrun@exceliance.fr>
Wed, 17 Oct 2012 11:38:19 +0000 (13:38 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 22 Oct 2012 16:54:24 +0000 (18:54 +0200)
Binary ACL match patterns can now be entered as hex digit strings.

include/proto/acl.h
src/acl.c

index d514076e233798d380c70a3c45e9a67bd7e5b0b1..38ad604e34542e362321fc0729080060710d0005 100644 (file)
@@ -138,6 +138,9 @@ int acl_parse_nothing(const char **text, struct acl_pattern *pattern, int *opaqu
 /* NB: For two strings to be identical, it is required that their lengths match */
 int acl_match_str(struct sample *smp, struct acl_pattern *pattern);
 
+/* NB: For two binary buffers to be identical, it is required that their lengths match */
+int acl_match_bin(struct sample *smp, struct acl_pattern *pattern);
+
 /* Checks that the length of the pattern in <test> is included between min and max */
 int acl_match_len(struct sample *smp, struct acl_pattern *pattern);
 
@@ -158,6 +161,9 @@ int acl_parse_range(const char **text, struct acl_pattern *pattern, int *opaque,
 /* Parse a string. It is allocated and duplicated. */
 int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
 
+/* Parse a hexa binary definition. It is allocated and duplicated. */
+int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
+
 /* Parse and concatenate strings into one. It is allocated and duplicated. */
 int acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err);
 
index f220394d9606492e76c852c986a66aa5abd5af9c..d0ea731d282aeca1d16e2bc8e7b9ae52495cce8d 100644 (file)
--- a/src/acl.c
+++ b/src/acl.c
@@ -492,6 +492,17 @@ int acl_match_str(struct sample *smp, struct acl_pattern *pattern)
        return ACL_PAT_FAIL;
 }
 
+/* NB: For two binaries buf to be identical, it is required that their lengths match */
+int acl_match_bin(struct sample *smp, struct acl_pattern *pattern)
+{
+       if (pattern->len != smp->data.str.len)
+               return ACL_PAT_FAIL;
+
+       if (memcmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) == 0)
+               return ACL_PAT_PASS;
+       return ACL_PAT_FAIL;
+}
+
 /* Lookup a string in the expression's pattern tree. The node is returned if it
  * exists, otherwise NULL.
  */
@@ -823,6 +834,43 @@ int acl_parse_str(const char **text, struct acl_pattern *pattern, int *opaque, c
        return 1;
 }
 
+/* Parse a binary written in hexa. It is allocated. */
+int acl_parse_bin(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
+{
+       int len;
+       const char *p = *text;
+       int i,j;
+
+       len  = strlen(p);
+       if (len%2) {
+               memprintf(err, "an even number of hex digit is expected");
+               return 0;
+       }
+
+       pattern->type = SMP_T_CBIN;
+       pattern->len = len >> 1;
+       pattern->ptr.str = malloc(pattern->len);
+       if (!pattern->ptr.str) {
+               memprintf(err, "out of memory while loading string pattern");
+               return 0;
+       }
+
+       i = j = 0;
+       while (j < pattern->len) {
+               if (!ishex(p[i++]))
+                       goto bad_input;
+               if (!ishex(p[i++]))
+                       goto bad_input;
+               pattern->ptr.str[j++] =  (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
+       }
+       return 1;
+
+bad_input:
+       memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
+       free(pattern->ptr.str);
+       return 0;
+}
+
 /* Parse and concatenate all further strings into one. */
 int
 acl_parse_strcat(const char **text, struct acl_pattern *pattern, int *opaque, char **err)