Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * Sample management functions. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <string.h> |
| 15 | #include <arpa/inet.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 16 | |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 17 | #include <common/standard.h> |
| 18 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 19 | #include <proto/arg.h> |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 20 | #include <proto/buffers.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 21 | #include <proto/sample.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 22 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 23 | /* static sample used in sample_process() when <p> is NULL */ |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 24 | static struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 26 | /* trash chunk used for sample conversions */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 27 | static struct chunk trash_chunk; |
| 28 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 29 | /* trash buffers used or sample conversions */ |
| 30 | static char sample_trash_buf1[BUFSIZE]; |
| 31 | static char sample_trash_buf2[BUFSIZE]; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 33 | /* sample_trash_buf point on used buffer*/ |
| 34 | static char *sample_trash_buf = sample_trash_buf1; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 36 | /* list head of all known sample fetch keywords */ |
| 37 | static struct sample_fetch_kw_list sample_fetches = { |
| 38 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 41 | /* list head of all known sample format conversion keywords */ |
| 42 | static struct sample_conv_kw_list sample_convs = { |
| 43 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 47 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords for next |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 48 | * parsing sessions. |
| 49 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 50 | void sample_register_fetches(struct sample_fetch_kw_list *pfkl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 51 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 52 | LIST_ADDQ(&sample_fetches.list, &pfkl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 56 | * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 57 | * parsing sessions. |
| 58 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 59 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 60 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 61 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 65 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 66 | * string of <len> in buffer <kw>. |
| 67 | * |
| 68 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 69 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 70 | { |
| 71 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 72 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 73 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 74 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 75 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 76 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 77 | kwl->kw[index].kw[len] == '\0') |
| 78 | return &kwl->kw[index]; |
| 79 | } |
| 80 | } |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 85 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 86 | * string of <len> in buffer <kw>. |
| 87 | * |
| 88 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 89 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 90 | { |
| 91 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 92 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 93 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 94 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 95 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 96 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 97 | kwl->kw[index].kw[len] == '\0') |
| 98 | return &kwl->kw[index]; |
| 99 | } |
| 100 | } |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 106 | * Returns a static trash struct chunk to use in sample casts or format conversions |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 107 | * Swiths the 2 available trash buffers to protect data during convert |
| 108 | */ |
| 109 | static struct chunk *get_trash_chunk(void) |
| 110 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 111 | if (sample_trash_buf == sample_trash_buf1) |
| 112 | sample_trash_buf = sample_trash_buf2; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 113 | else |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 114 | sample_trash_buf = sample_trash_buf1; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 115 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 116 | chunk_init(&trash_chunk, sample_trash_buf, BUFSIZE); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 117 | |
| 118 | return &trash_chunk; |
| 119 | } |
| 120 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 121 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 122 | /* Sample casts functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 123 | /* Note: these functions do *NOT* set the output type on the */ |
| 124 | /* sample, the caller is responsible for doing this on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 125 | /******************************************************************/ |
| 126 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 127 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 128 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 129 | smp->data.uint = ntohl(smp->data.ipv4.s_addr); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 130 | return 1; |
| 131 | } |
| 132 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 133 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 134 | { |
| 135 | struct chunk *trash = get_trash_chunk(); |
| 136 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 137 | if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 138 | return 0; |
| 139 | |
| 140 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 141 | smp->data.str = *trash; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 142 | |
| 143 | return 1; |
| 144 | } |
| 145 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 146 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 147 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 148 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 149 | return 1; |
| 150 | } |
| 151 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 152 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 153 | { |
| 154 | struct chunk *trash = get_trash_chunk(); |
| 155 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 156 | if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 157 | return 0; |
| 158 | |
| 159 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 160 | smp->data.str = *trash; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 165 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 166 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 167 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 168 | } |
| 169 | */ |
| 170 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 171 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 172 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 173 | smp->data.ipv4.s_addr = htonl(smp->data.uint); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 174 | return 1; |
| 175 | } |
| 176 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 177 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 178 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 179 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 180 | return 0; |
| 181 | return 1; |
| 182 | } |
| 183 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 184 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 185 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 186 | return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 187 | } |
| 188 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 189 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 190 | { |
| 191 | struct chunk *trash = get_trash_chunk(); |
| 192 | char *pos; |
| 193 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 194 | pos = ultoa_r(smp->data.uint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 195 | |
| 196 | if (!pos) |
| 197 | return 0; |
| 198 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 199 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 200 | trash->str = pos; |
| 201 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 202 | smp->data.str = *trash; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 203 | return 1; |
| 204 | } |
| 205 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 206 | static int c_datadup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 207 | { |
| 208 | struct chunk *trash = get_trash_chunk(); |
| 209 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 210 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 211 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 212 | smp->data.str = *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 217 | static int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 218 | { |
| 219 | return 1; |
| 220 | } |
| 221 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 222 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 223 | { |
| 224 | int i; |
| 225 | uint32_t ret = 0; |
| 226 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 227 | for (i = 0; i < smp->data.str.len; i++) { |
| 228 | uint32_t val = smp->data.str.str[i] - '0'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 229 | |
| 230 | if (val > 9) |
| 231 | break; |
| 232 | |
| 233 | ret = ret * 10 + val; |
| 234 | } |
| 235 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 236 | smp->data.uint = ret; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 241 | /* Sample casts matrix: */ |
| 242 | /* sample_casts[from type][to type] */ |
| 243 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 244 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 245 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 246 | typedef int (*sample_cast_fct)(struct sample *smp); |
| 247 | static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 248 | /* to: BOOL UINT SINT IPV4 IPV6 STR BIN CSTR CBIN */ |
| 249 | /* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, NULL, NULL, NULL }, |
| 250 | /* UINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL }, |
| 251 | /* SINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL }, |
| 252 | /* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL }, |
| 253 | /* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL }, |
| 254 | /* STR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_none, c_none, c_none, c_none }, |
| 255 | /* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_none, NULL, c_none }, |
| 256 | /* CSTR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_datadup, c_datadup, c_none, c_none }, |
| 257 | /* CBIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_datadup, NULL, c_none }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 258 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 259 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 260 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 261 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 262 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 263 | * Returns a pointer on allocated sample expression structure. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 264 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 265 | struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_size) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 266 | { |
| 267 | const char *endw; |
| 268 | const char *end; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 269 | struct sample_expr *expr; |
| 270 | struct sample_fetch *fetch; |
| 271 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 272 | unsigned long prev_type; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 273 | char *p; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 274 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 275 | snprintf(err, err_size, "memory error."); |
| 276 | if (!str[*idx]) { |
| 277 | |
| 278 | snprintf(err, err_size, "missing fetch method."); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 279 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 280 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 281 | |
| 282 | end = str[*idx] + strlen(str[*idx]); |
| 283 | endw = strchr(str[*idx], '('); |
| 284 | |
| 285 | if (!endw) |
| 286 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 287 | else if ((end-1)[0] != ')') { |
| 288 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 289 | if (p) { |
| 290 | snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p); |
| 291 | free(p); |
| 292 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 293 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 294 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 295 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 296 | fetch = find_sample_fetch(str[*idx], endw - str[*idx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 297 | if (!fetch) { |
| 298 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 299 | if (p) { |
| 300 | snprintf(err, err_size, "unknown fetch method '%s'.", p); |
| 301 | free(p); |
| 302 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 303 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 304 | } |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 305 | if (fetch->out_type >= SMP_TYPES) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 306 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 307 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 308 | if (p) { |
| 309 | snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p); |
| 310 | free(p); |
| 311 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 312 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 313 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 314 | |
| 315 | prev_type = fetch->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 316 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 317 | if (!expr) |
| 318 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 319 | |
| 320 | LIST_INIT(&(expr->conv_exprs)); |
| 321 | expr->fetch = fetch; |
| 322 | |
| 323 | if (end != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 324 | char *err_msg = NULL; |
| 325 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 326 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 327 | if (!fetch->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 328 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 329 | if (p) { |
| 330 | snprintf(err, err_size, "fetch method '%s' does not support any args.", p); |
| 331 | free(p); |
| 332 | } |
| 333 | goto out_error; |
| 334 | } |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 335 | |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 336 | if (make_arg_list(endw + 1, end - endw - 2, fetch->arg_mask, &expr->arg_p, &err_msg, NULL, &err_arg) < 0) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 337 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 338 | if (p) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 339 | snprintf(err, err_size, "invalid arg %d in fetch method '%s' : %s.", err_arg+1, p, err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 340 | free(p); |
| 341 | } |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 342 | free(err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 343 | goto out_error; |
| 344 | } |
| 345 | |
| 346 | if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) { |
| 347 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 348 | if (p) { |
| 349 | snprintf(err, err_size, "invalid args in fetch method '%s' : %s.", p, err_msg); |
| 350 | free(p); |
| 351 | } |
| 352 | free(err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 353 | goto out_error; |
| 354 | } |
| 355 | } |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 356 | else if (fetch->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 357 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 358 | if (p) { |
| 359 | snprintf(err, err_size, "missing args for fetch method '%s'.", p); |
| 360 | free(p); |
| 361 | } |
| 362 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | for (*idx += 1; *(str[*idx]); (*idx)++) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 366 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 367 | |
| 368 | end = str[*idx] + strlen(str[*idx]); |
| 369 | endw = strchr(str[*idx], '('); |
| 370 | |
| 371 | if (!endw) |
| 372 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 373 | else if ((end-1)[0] != ')') { |
| 374 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 375 | if (p) { |
| 376 | snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p); |
| 377 | free(p); |
| 378 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 379 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 380 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 382 | conv = find_sample_conv(str[*idx], endw - str[*idx]); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 383 | if (!conv) |
| 384 | break; |
| 385 | |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 386 | if (conv->in_type >= SMP_TYPES || |
| 387 | conv->out_type >= SMP_TYPES) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 388 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 389 | if (p) { |
| 390 | snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p); |
| 391 | free(p); |
| 392 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 393 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 394 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 395 | |
| 396 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 397 | if (!sample_casts[prev_type][conv->in_type]) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 398 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 399 | if (p) { |
| 400 | snprintf(err, err_size, "conv method '%s' cannot be applied.", p); |
| 401 | free(p); |
| 402 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 403 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 404 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 405 | |
| 406 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 407 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 408 | if (!conv_expr) |
| 409 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 410 | |
| 411 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 412 | conv_expr->conv = conv; |
| 413 | |
| 414 | if (end != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 415 | char *err_msg = NULL; |
| 416 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 417 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 418 | if (!conv->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 419 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 420 | |
| 421 | if (p) { |
| 422 | snprintf(err, err_size, "conv method '%s' does not support any args.", p); |
| 423 | free(p); |
| 424 | } |
| 425 | goto out_error; |
| 426 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 427 | |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 428 | if (make_arg_list(endw + 1, end - endw - 2, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg) < 0) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 429 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 430 | if (p) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 431 | snprintf(err, err_size, "invalid arg %d in conv method '%s' : %s.", err_arg+1, p, err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 432 | free(p); |
| 433 | } |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 434 | free(err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 435 | goto out_error; |
| 436 | } |
| 437 | |
| 438 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) { |
| 439 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 440 | if (p) { |
| 441 | snprintf(err, err_size, "invalid args in conv method '%s' : %s.", p, err_msg); |
| 442 | free(p); |
| 443 | } |
| 444 | free(err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 445 | goto out_error; |
| 446 | } |
| 447 | } |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 448 | else if (conv->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 449 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 450 | if (p) { |
| 451 | snprintf(err, err_size, "missing args for conv method '%s'.", p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 452 | free(p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 453 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 454 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 455 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 456 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 457 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 458 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 459 | return expr; |
| 460 | |
| 461 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 462 | /* TODO: prune_sample_expr(expr); */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 467 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 468 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 469 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 470 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 471 | * If <p> is not null, function returns results in structure pointed by <p>. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 472 | * If <p> is null, functions returns a pointer on a static sample structure. |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 473 | * |
| 474 | * Note: the fetch functions are required to properly set the return type. The |
| 475 | * conversion functions must do so too. However the cast functions do not need |
| 476 | * to since they're made to cast mutiple types according to what is required. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 477 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 478 | struct sample *sample_process(struct proxy *px, struct session *l4, void *l7, |
| 479 | unsigned int opt, |
| 480 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 481 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 482 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 483 | |
| 484 | if (p == NULL) |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 485 | p = &temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 486 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 487 | p->flags = 0; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 488 | if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 489 | return NULL; |
| 490 | |
Willy Tarreau | 40aebd9 | 2012-04-26 11:05:50 +0200 | [diff] [blame] | 491 | if (p->flags & SMP_F_MAY_CHANGE) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 492 | return NULL; /* we can only use stable samples */ |
Willy Tarreau | 40aebd9 | 2012-04-26 11:05:50 +0200 | [diff] [blame] | 493 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 494 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 495 | /* we want to ensure that p->type can be casted into |
| 496 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 497 | * - NULL => not castable. |
| 498 | * - c_none => nothing to do (let's optimize it) |
| 499 | * - other => apply cast and prepare to fail |
| 500 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 501 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 502 | return NULL; |
| 503 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 504 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 505 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 506 | return NULL; |
| 507 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 508 | /* OK cast succeeded */ |
| 509 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 510 | /* force the output type after a cast */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 511 | p->type = conv_expr->conv->in_type; |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 512 | if (!conv_expr->conv->process(conv_expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 513 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 514 | } |
| 515 | return p; |
| 516 | } |
| 517 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 518 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 519 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 520 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 521 | /*****************************************************************/ |
| 522 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 523 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 524 | { |
| 525 | int i; |
| 526 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 527 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 528 | return 0; |
| 529 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 530 | for (i = 0; i < smp->data.str.len; i++) { |
| 531 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 532 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 533 | } |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 534 | smp->type = SMP_T_STR; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 538 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 539 | { |
| 540 | int i; |
| 541 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 542 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 543 | return 0; |
| 544 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 545 | for (i = 0; i < smp->data.str.len; i++) { |
| 546 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 547 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 548 | } |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 549 | smp->type = SMP_T_STR; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 550 | return 1; |
| 551 | } |
| 552 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 553 | /* takes the netmask in arg_p */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 554 | static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 555 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 556 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 557 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 558 | return 1; |
| 559 | } |
| 560 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 561 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 562 | static struct sample_conv_kw_list sample_conv_kws = {{ },{ |
| 563 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 564 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 565 | { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 566 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 567 | }}; |
| 568 | |
| 569 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 570 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 571 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 572 | /* register sample format convert keywords */ |
| 573 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 574 | } |