]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] pattern: add the "ipmask()" converting function
authorWilly Tarreau <w@1wt.eu>
Tue, 26 Jan 2010 17:01:41 +0000 (18:01 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 Jan 2010 17:05:48 +0000 (18:05 +0100)
This converter can be applied on top of an IPv4-type pattern. It
applies a netmask which is suited for IP address storage and matching.
This can be used to make all hosts within a certain mask to share the
same table entries and as such use the same server.

The mask can be passed in dotted form (eg: 255.255.255.0) or in CIDR
form (eg: 24).

doc/configuration.txt
src/pattern.c

index b10714d5b2b69253d641ab1ed77ce573f103566c..40ba6b58b9181eca715ee90d5c37ac6ba74045d8 100644 (file)
@@ -6155,6 +6155,12 @@ The currently available list of transformations include :
                after a string pattern fetch function or after a conversion
                function returning a string type. The result is of type string.
 
+  ipmask(mask) Apply a mask to an IPv4 address, and use the result for lookups
+               and storage. This can be used to make all hosts within a
+               certain mask to share the same table entries and as such use
+               the same server. The mask can be passed in dotted form (eg:
+               255.255.255.0) or in CIDR form (eg: 24).
+
 
 8. Logging
 ----------
index aef151a47e3428f8464bb6830074cb19574bceff..5afd068e9472a14470988a4ddcff4577ec66527e 100644 (file)
@@ -536,6 +536,20 @@ int pattern_notusable_key(struct pattern_expr *expr, unsigned long table_type)
        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.
+ */
+static int pattern_conv_arg_to_ipmask(const char *arg_str, void **arg_p, int *arg_i)
+{
+       struct in_addr mask;
+
+       if (!str2mask(arg_str, &mask))
+               return 0;
+
+       *arg_i = mask.s_addr;
+       return 1;
+}
+
 /*****************************************************************/
 /*    Pattern format convert functions                           */
 /*****************************************************************/
@@ -562,10 +576,18 @@ static int pattern_conv_str2upper(const void *arg_p, int arg_i, union pattern_da
        return 1;
 }
 
+/* takes the netmask in arg_i */
+static int pattern_conv_ipmask(const void *arg_p, int arg_i, union pattern_data *data)
+{
+       data->ip.s_addr &= arg_i;
+       return 1;
+}
+
 /* Note: must not be declared <const> as its list will be overwritten */
 static struct pattern_conv_kw_list pattern_conv_kws = {{ },{
        { "upper",       pattern_conv_str2upper, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
        { "lower",       pattern_conv_str2lower, PATTERN_TYPE_STRING, PATTERN_TYPE_STRING },
+       { "ipmask",      pattern_conv_ipmask, PATTERN_TYPE_IP, PATTERN_TYPE_IP, pattern_conv_arg_to_ipmask },
        { NULL, NULL, 0, 0 },
 }};