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