blob: c35e833d2a95b5bbeff50aa252af3c25768005d2 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
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 Tarreaudd2f85e2012-09-02 22:34:23 +020016#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010017
Willy Tarreau7e2c6472012-10-29 20:44:36 +010018#include <types/global.h>
19
Willy Tarreauc7e42382012-08-24 19:22:53 +020020#include <common/chunk.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020021#include <common/standard.h>
22
Willy Tarreau9fcb9842012-04-20 14:45:49 +020023#include <proto/arg.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020024#include <proto/sample.h>
Emeric Brun107ca302010-01-04 16:16:05 +010025
Willy Tarreau12785782012-04-27 21:37:17 +020026/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020027static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010028
Willy Tarreau12785782012-04-27 21:37:17 +020029/* list head of all known sample fetch keywords */
30static struct sample_fetch_kw_list sample_fetches = {
31 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010032};
33
Willy Tarreau12785782012-04-27 21:37:17 +020034/* list head of all known sample format conversion keywords */
35static struct sample_conv_kw_list sample_convs = {
36 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010037};
38
Willy Tarreau80aca902013-01-07 15:42:20 +010039const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
40 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
41 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
42 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
43 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
44 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
45 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
46 SMP_VAL_FE_LOG_END),
47
48 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
49 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
50 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
51 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
52 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
53 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
54 SMP_VAL_FE_LOG_END),
55
56 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
57 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
58 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
59 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
60 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
61 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
62 SMP_VAL_FE_LOG_END),
63
64 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
65 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
66 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
67 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
68 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
69 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
70 SMP_VAL_FE_LOG_END),
71
72 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
73 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
74 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
75 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
76 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
77 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
78 SMP_VAL_FE_LOG_END),
79
80 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
81 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
82 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
83 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
84 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
85 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
86 SMP_VAL_FE_LOG_END),
87
88 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
89 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
90 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
91 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
92 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
93 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
94 SMP_VAL___________),
95
96 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
97 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
98 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
99 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
100 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
101 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
102 SMP_VAL___________),
103
104 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
105 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
106 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
107 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
108 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
109 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
110 SMP_VAL_FE_LOG_END),
111
112 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
113 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
114 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
115 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
116 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
117 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
118 SMP_VAL___________),
119
120 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
123 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
124 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
125 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
126 SMP_VAL_FE_LOG_END),
127
128 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
132 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
133 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
134 SMP_VAL_FE_LOG_END),
135
136 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
137 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
140 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
141 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
142 SMP_VAL_FE_LOG_END),
143
144 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
148 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
149 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
150 SMP_VAL_FE_LOG_END),
151
152 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
153 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
154 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
155 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
156 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
157 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
158 SMP_VAL___________),
159
160 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
162 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
163 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
164 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
165 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
166 SMP_VAL___________),
167
168 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
170 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
171 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
172 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
173 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
174 SMP_VAL_FE_LOG_END),
175
176 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
178 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
179 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
180 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
181 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
182 SMP_VAL___________),
183
184 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
186 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
187 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
188 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL_FE_LOG_END),
191
192 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
196 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL_FE_LOG_END),
199
200 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
204 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL_FE_LOG_END),
207
208 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
212 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL_FE_LOG_END),
215};
216
217static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
218 [SMP_SRC_INTRN] = "internal state",
219 [SMP_SRC_LISTN] = "listener",
220 [SMP_SRC_FTEND] = "frontend",
221 [SMP_SRC_L4CLI] = "client address",
222 [SMP_SRC_L5CLI] = "client-side connection",
223 [SMP_SRC_TRACK] = "track counters",
224 [SMP_SRC_L6REQ] = "request buffer",
225 [SMP_SRC_HRQHV] = "HTTP request headers",
226 [SMP_SRC_HRQHP] = "HTTP request",
227 [SMP_SRC_HRQBO] = "HTTP request body",
228 [SMP_SRC_BKEND] = "backend",
229 [SMP_SRC_SERVR] = "server",
230 [SMP_SRC_L4SRV] = "server address",
231 [SMP_SRC_L5SRV] = "server-side connection",
232 [SMP_SRC_L6RES] = "response buffer",
233 [SMP_SRC_HRSHV] = "HTTP response headers",
234 [SMP_SRC_HRSHP] = "HTTP response",
235 [SMP_SRC_HRSBO] = "HTTP response body",
236 [SMP_SRC_RQFIN] = "request buffer statistics",
237 [SMP_SRC_RSFIN] = "response buffer statistics",
238 [SMP_SRC_TXFIN] = "transaction statistics",
239 [SMP_SRC_SSFIN] = "session statistics",
240};
241
242/* fill the trash with a comma-delimited list of source names for the <use> bit
243 * field which must be composed of a non-null set of SMP_USE_* flags. The return
244 * value is the pointer to the string in the trash buffer.
245 */
246const char *sample_src_names(unsigned int use)
247{
248 int bit;
249
250 trash.len = 0;
251 trash.str[0] = '\0';
252 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
253 if (!(use & ~((1 << bit) - 1)))
254 break; /* no more bits */
255
256 if (!(use & (1 << bit)))
257 continue; /* bit not set */
258
259 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
260 (use & ((1 << bit) - 1)) ? "," : "",
261 fetch_src_names[bit]);
262 }
263 return trash.str;
264}
265
Emeric Brun107ca302010-01-04 16:16:05 +0100266/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100267 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
268 * for next parsing sessions. The fetch keywords capabilities are also computed
269 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100270 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100271void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100272{
Willy Tarreau80aca902013-01-07 15:42:20 +0100273 struct sample_fetch *sf;
274 int bit;
275
276 for (sf = kwl->kw; sf->kw != NULL; sf++) {
277 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
278 if (sf->use & (1 << bit))
279 sf->val |= fetch_cap[bit];
280 }
281 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100282}
283
284/*
Willy Tarreau12785782012-04-27 21:37:17 +0200285 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100286 * parsing sessions.
287 */
Willy Tarreau12785782012-04-27 21:37:17 +0200288void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100289{
Willy Tarreau12785782012-04-27 21:37:17 +0200290 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100291}
292
293/*
Willy Tarreau12785782012-04-27 21:37:17 +0200294 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100295 * string of <len> in buffer <kw>.
296 *
297 */
Willy Tarreau12785782012-04-27 21:37:17 +0200298struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100299{
300 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200301 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100302
Willy Tarreau12785782012-04-27 21:37:17 +0200303 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100304 for (index = 0; kwl->kw[index].kw != NULL; index++) {
305 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
306 kwl->kw[index].kw[len] == '\0')
307 return &kwl->kw[index];
308 }
309 }
310 return NULL;
311}
312
313/*
Willy Tarreau12785782012-04-27 21:37:17 +0200314 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100315 * string of <len> in buffer <kw>.
316 *
317 */
Willy Tarreau12785782012-04-27 21:37:17 +0200318struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100319{
320 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200321 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100322
Willy Tarreau12785782012-04-27 21:37:17 +0200323 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100324 for (index = 0; kwl->kw[index].kw != NULL; index++) {
325 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
326 kwl->kw[index].kw[len] == '\0')
327 return &kwl->kw[index];
328 }
329 }
330 return NULL;
331}
332
Emeric Brun107ca302010-01-04 16:16:05 +0100333/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200334/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200335/* Note: these functions do *NOT* set the output type on the */
336/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100337/******************************************************************/
338
Willy Tarreau342acb42012-04-23 22:03:39 +0200339static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100340{
Willy Tarreau342acb42012-04-23 22:03:39 +0200341 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Emeric Brun107ca302010-01-04 16:16:05 +0100342 return 1;
343}
344
Willy Tarreau342acb42012-04-23 22:03:39 +0200345static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100346{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100347 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100348
Willy Tarreau342acb42012-04-23 22:03:39 +0200349 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100350 return 0;
351
352 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200353 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100354
355 return 1;
356}
357
Willy Tarreau342acb42012-04-23 22:03:39 +0200358static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100359{
Willy Tarreau342acb42012-04-23 22:03:39 +0200360 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
David du Colombier4f92d322011-03-24 11:09:31 +0100361 return 1;
362}
363
Willy Tarreau342acb42012-04-23 22:03:39 +0200364static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100365{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100366 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100367
Willy Tarreau342acb42012-04-23 22:03:39 +0200368 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100369 return 0;
370
371 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200372 smp->data.str = *trash;
David du Colombier4f92d322011-03-24 11:09:31 +0100373 return 1;
374}
375
376/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200377static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100378{
Willy Tarreau342acb42012-04-23 22:03:39 +0200379 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100380}
381*/
382
Willy Tarreau342acb42012-04-23 22:03:39 +0200383static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100384{
Willy Tarreau342acb42012-04-23 22:03:39 +0200385 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Emeric Brun107ca302010-01-04 16:16:05 +0100386 return 1;
387}
388
Willy Tarreau342acb42012-04-23 22:03:39 +0200389static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100390{
Willy Tarreau342acb42012-04-23 22:03:39 +0200391 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100392 return 0;
393 return 1;
394}
395
Willy Tarreau342acb42012-04-23 22:03:39 +0200396static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100397{
Willy Tarreau342acb42012-04-23 22:03:39 +0200398 return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100399}
400
Emeric Brun8ac33d92012-10-17 13:36:06 +0200401static int c_bin2str(struct sample *smp)
402{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100403 struct chunk *trash = get_trash_chunk();
Emeric Brun8ac33d92012-10-17 13:36:06 +0200404 unsigned char c;
405 int ptr = 0;
406
407 trash->len = 0;
408 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
409 c = smp->data.str.str[ptr++];
410 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
411 trash->str[trash->len++] = hextab[c & 0xF];
412 }
413 smp->data.str = *trash;
414 return 1;
415}
416
Willy Tarreau342acb42012-04-23 22:03:39 +0200417static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100418{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100419 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100420 char *pos;
421
Willy Tarreau342acb42012-04-23 22:03:39 +0200422 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100423
424 if (!pos)
425 return 0;
426
Emeric Brun485479d2010-09-23 18:02:19 +0200427 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100428 trash->str = pos;
429 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200430 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100431 return 1;
432}
433
Willy Tarreau342acb42012-04-23 22:03:39 +0200434static int c_datadup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200435{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100436 struct chunk *trash = get_trash_chunk();
Emeric Brun485479d2010-09-23 18:02:19 +0200437
Willy Tarreau342acb42012-04-23 22:03:39 +0200438 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
439 memcpy(trash->str, smp->data.str.str, trash->len);
440 smp->data.str = *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200441 return 1;
442}
443
444
Willy Tarreau342acb42012-04-23 22:03:39 +0200445static int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100446{
447 return 1;
448}
449
Willy Tarreau342acb42012-04-23 22:03:39 +0200450static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100451{
452 int i;
453 uint32_t ret = 0;
454
Willy Tarreau342acb42012-04-23 22:03:39 +0200455 for (i = 0; i < smp->data.str.len; i++) {
456 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100457
458 if (val > 9)
459 break;
460
461 ret = ret * 10 + val;
462 }
463
Willy Tarreau342acb42012-04-23 22:03:39 +0200464 smp->data.uint = ret;
Emeric Brun107ca302010-01-04 16:16:05 +0100465 return 1;
466}
467
468/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200469/* Sample casts matrix: */
470/* sample_casts[from type][to type] */
471/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100472/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100473
Willy Tarreau12785782012-04-27 21:37:17 +0200474typedef int (*sample_cast_fct)(struct sample *smp);
475static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Willy Tarreau422aa072012-04-20 20:49:27 +0200476/* to: BOOL UINT SINT IPV4 IPV6 STR BIN CSTR CBIN */
Willy Tarreaud167e6d2012-12-21 17:38:05 +0100477/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, c_int2str, NULL, c_int2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200478/* UINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
479/* SINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
480/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL },
481/* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL },
482/* STR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_none, c_none, c_none, c_none },
Emeric Brun8ac33d92012-10-17 13:36:06 +0200483/* BIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none },
Willy Tarreau422aa072012-04-20 20:49:27 +0200484/* CSTR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_datadup, c_datadup, c_none, c_none },
Emeric Brun8ac33d92012-10-17 13:36:06 +0200485/* CBIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200486};
Emeric Brun107ca302010-01-04 16:16:05 +0100487
Emeric Brun107ca302010-01-04 16:16:05 +0100488/*
Willy Tarreau12785782012-04-27 21:37:17 +0200489 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100490 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200491 * Returns a pointer on allocated sample expression structure.
Emeric Brun107ca302010-01-04 16:16:05 +0100492 */
Willy Tarreau12785782012-04-27 21:37:17 +0200493struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_size)
Emeric Brun107ca302010-01-04 16:16:05 +0100494{
495 const char *endw;
496 const char *end;
Willy Tarreau12785782012-04-27 21:37:17 +0200497 struct sample_expr *expr;
498 struct sample_fetch *fetch;
499 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100500 unsigned long prev_type;
Emeric Brun485479d2010-09-23 18:02:19 +0200501 char *p;
Emeric Brun107ca302010-01-04 16:16:05 +0100502
Emeric Brun485479d2010-09-23 18:02:19 +0200503 snprintf(err, err_size, "memory error.");
504 if (!str[*idx]) {
505
506 snprintf(err, err_size, "missing fetch method.");
Emeric Brun107ca302010-01-04 16:16:05 +0100507 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200508 }
Emeric Brun107ca302010-01-04 16:16:05 +0100509
510 end = str[*idx] + strlen(str[*idx]);
511 endw = strchr(str[*idx], '(');
512
513 if (!endw)
514 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200515 else if ((end-1)[0] != ')') {
516 p = my_strndup(str[*idx], endw - str[*idx]);
517 if (p) {
518 snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p);
519 free(p);
520 }
Emeric Brun107ca302010-01-04 16:16:05 +0100521 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200522 }
Emeric Brun107ca302010-01-04 16:16:05 +0100523
Willy Tarreau12785782012-04-27 21:37:17 +0200524 fetch = find_sample_fetch(str[*idx], endw - str[*idx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200525 if (!fetch) {
526 p = my_strndup(str[*idx], endw - str[*idx]);
527 if (p) {
528 snprintf(err, err_size, "unknown fetch method '%s'.", p);
529 free(p);
530 }
Emeric Brun107ca302010-01-04 16:16:05 +0100531 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200532 }
Willy Tarreau422aa072012-04-20 20:49:27 +0200533 if (fetch->out_type >= SMP_TYPES) {
Emeric Brun107ca302010-01-04 16:16:05 +0100534
Emeric Brun485479d2010-09-23 18:02:19 +0200535 p = my_strndup(str[*idx], endw - str[*idx]);
536 if (p) {
537 snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p);
538 free(p);
539 }
Emeric Brun107ca302010-01-04 16:16:05 +0100540 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200541 }
Emeric Brun107ca302010-01-04 16:16:05 +0100542
543 prev_type = fetch->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200544 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200545 if (!expr)
546 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100547
548 LIST_INIT(&(expr->conv_exprs));
549 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200550 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100551
552 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200553 char *err_msg = NULL;
554 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200555
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200556 if (!fetch->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200557 p = my_strndup(str[*idx], endw - str[*idx]);
558 if (p) {
559 snprintf(err, err_size, "fetch method '%s' does not support any args.", p);
560 free(p);
561 }
562 goto out_error;
563 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200564
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200565 if (make_arg_list(endw + 1, end - endw - 2, fetch->arg_mask, &expr->arg_p, &err_msg, NULL, &err_arg) < 0) {
Emeric Brun485479d2010-09-23 18:02:19 +0200566 p = my_strndup(str[*idx], endw - str[*idx]);
567 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200568 snprintf(err, err_size, "invalid arg %d in fetch method '%s' : %s.", err_arg+1, p, err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200569 free(p);
570 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200571 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200572 goto out_error;
573 }
574
Willy Tarreau2e845be2012-10-19 19:49:09 +0200575 if (!expr->arg_p)
576 expr->arg_p = empty_arg_list;
577
Willy Tarreau21d68a62012-04-20 15:52:36 +0200578 if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) {
579 p = my_strndup(str[*idx], endw - str[*idx]);
580 if (p) {
581 snprintf(err, err_size, "invalid args in fetch method '%s' : %s.", p, err_msg);
582 free(p);
583 }
584 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200585 goto out_error;
586 }
587 }
Willy Tarreauf22a5082012-10-19 16:47:23 +0200588 else if (ARGM(fetch->arg_mask)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200589 p = my_strndup(str[*idx], endw - str[*idx]);
590 if (p) {
591 snprintf(err, err_size, "missing args for fetch method '%s'.", p);
592 free(p);
593 }
594 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100595 }
596
597 for (*idx += 1; *(str[*idx]); (*idx)++) {
Willy Tarreau12785782012-04-27 21:37:17 +0200598 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100599
600 end = str[*idx] + strlen(str[*idx]);
601 endw = strchr(str[*idx], '(');
602
603 if (!endw)
604 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200605 else if ((end-1)[0] != ')') {
606 p = my_strndup(str[*idx], endw - str[*idx]);
607 if (p) {
608 snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p);
609 free(p);
610 }
Emeric Brun107ca302010-01-04 16:16:05 +0100611 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200612 }
Emeric Brun107ca302010-01-04 16:16:05 +0100613
Willy Tarreau12785782012-04-27 21:37:17 +0200614 conv = find_sample_conv(str[*idx], endw - str[*idx]);
Emeric Brun107ca302010-01-04 16:16:05 +0100615 if (!conv)
616 break;
617
Willy Tarreau422aa072012-04-20 20:49:27 +0200618 if (conv->in_type >= SMP_TYPES ||
619 conv->out_type >= SMP_TYPES) {
Emeric Brun485479d2010-09-23 18:02:19 +0200620 p = my_strndup(str[*idx], endw - str[*idx]);
621 if (p) {
622 snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p);
623 free(p);
624 }
Emeric Brun107ca302010-01-04 16:16:05 +0100625 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200626 }
Emeric Brun107ca302010-01-04 16:16:05 +0100627
628 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200629 if (!sample_casts[prev_type][conv->in_type]) {
Emeric Brun485479d2010-09-23 18:02:19 +0200630 p = my_strndup(str[*idx], endw - str[*idx]);
631 if (p) {
632 snprintf(err, err_size, "conv method '%s' cannot be applied.", p);
633 free(p);
634 }
Emeric Brun107ca302010-01-04 16:16:05 +0100635 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200636 }
Emeric Brun107ca302010-01-04 16:16:05 +0100637
638 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200639 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200640 if (!conv_expr)
641 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100642
643 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
644 conv_expr->conv = conv;
645
646 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200647 char *err_msg = NULL;
648 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200649
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200650 if (!conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200651 p = my_strndup(str[*idx], endw - str[*idx]);
652
653 if (p) {
654 snprintf(err, err_size, "conv method '%s' does not support any args.", p);
655 free(p);
656 }
657 goto out_error;
658 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100659
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200660 if (make_arg_list(endw + 1, end - endw - 2, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg) < 0) {
Emeric Brun485479d2010-09-23 18:02:19 +0200661 p = my_strndup(str[*idx], endw - str[*idx]);
662 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200663 snprintf(err, err_size, "invalid arg %d in conv method '%s' : %s.", err_arg+1, p, err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200664 free(p);
665 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200666 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200667 goto out_error;
668 }
669
Willy Tarreau2e845be2012-10-19 19:49:09 +0200670 if (!conv_expr->arg_p)
671 conv_expr->arg_p = empty_arg_list;
672
Willy Tarreau21d68a62012-04-20 15:52:36 +0200673 if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) {
674 p = my_strndup(str[*idx], endw - str[*idx]);
675 if (p) {
676 snprintf(err, err_size, "invalid args in conv method '%s' : %s.", p, err_msg);
677 free(p);
678 }
679 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200680 goto out_error;
681 }
682 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200683 else if (conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200684 p = my_strndup(str[*idx], endw - str[*idx]);
685 if (p) {
686 snprintf(err, err_size, "missing args for conv method '%s'.", p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100687 free(p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100688 }
Emeric Brun485479d2010-09-23 18:02:19 +0200689 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100690 }
Emeric Brun485479d2010-09-23 18:02:19 +0200691
Emeric Brun107ca302010-01-04 16:16:05 +0100692 }
Emeric Brun485479d2010-09-23 18:02:19 +0200693
Emeric Brun107ca302010-01-04 16:16:05 +0100694 return expr;
695
696out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200697 /* TODO: prune_sample_expr(expr); */
Emeric Brun107ca302010-01-04 16:16:05 +0100698 return NULL;
699}
700
701/*
Willy Tarreau12785782012-04-27 21:37:17 +0200702 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200703 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200704 * Returns a pointer on a typed sample structure containing the result or NULL if
705 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100706 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200707 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200708 *
709 * Note: the fetch functions are required to properly set the return type. The
710 * conversion functions must do so too. However the cast functions do not need
711 * to since they're made to cast mutiple types according to what is required.
Emeric Brun107ca302010-01-04 16:16:05 +0100712 */
Willy Tarreau12785782012-04-27 21:37:17 +0200713struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
714 unsigned int opt,
715 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100716{
Willy Tarreau12785782012-04-27 21:37:17 +0200717 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
719 if (p == NULL)
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200720 p = &temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +0100721
Willy Tarreau342acb42012-04-23 22:03:39 +0200722 p->flags = 0;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200723 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100724 return NULL;
725
Willy Tarreau40aebd92012-04-26 11:05:50 +0200726 if (p->flags & SMP_F_MAY_CHANGE)
Willy Tarreau12785782012-04-27 21:37:17 +0200727 return NULL; /* we can only use stable samples */
Willy Tarreau40aebd92012-04-26 11:05:50 +0200728
Emeric Brun107ca302010-01-04 16:16:05 +0100729 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200730 /* we want to ensure that p->type can be casted into
731 * conv_expr->conv->in_type. We have 3 possibilities :
732 * - NULL => not castable.
733 * - c_none => nothing to do (let's optimize it)
734 * - other => apply cast and prepare to fail
735 */
Willy Tarreau12785782012-04-27 21:37:17 +0200736 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200737 return NULL;
738
Willy Tarreau12785782012-04-27 21:37:17 +0200739 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
740 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100741 return NULL;
742
Willy Tarreau12e50112012-04-25 17:21:49 +0200743 /* OK cast succeeded */
744
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200745 /* force the output type after a cast */
Emeric Brun107ca302010-01-04 16:16:05 +0100746 p->type = conv_expr->conv->in_type;
Willy Tarreau342acb42012-04-23 22:03:39 +0200747 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100748 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100749 }
750 return p;
751}
752
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100753/*
754 * Process a fetch + format conversion as defined by the sample expression <expr>
755 * on request or response considering the <opt> parameter. The output is always of
756 * type string. Returns either NULL if no sample could be extracted, or a pointer
757 * to the converted result stored in static temp_smp in format string.
758 */
759struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
760 unsigned int opt, struct sample_expr *expr)
761{
762 struct sample *smp;
763
764 smp = sample_process(px, l4, l7, opt, expr, NULL);
765 if (!smp)
766 return NULL;
767
768 if (!sample_casts[smp->type][SMP_T_CSTR])
769 return NULL;
770
771 if (!sample_casts[smp->type][SMP_T_CSTR](smp))
772 return NULL;
773
774 smp->type = SMP_T_CSTR;
775 return smp;
776}
777
Emeric Brun107ca302010-01-04 16:16:05 +0100778/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200779/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200780/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100781/*****************************************************************/
782
Willy Tarreau12785782012-04-27 21:37:17 +0200783static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100784{
785 int i;
786
Willy Tarreau342acb42012-04-23 22:03:39 +0200787 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +0200788 return 0;
789
Willy Tarreau342acb42012-04-23 22:03:39 +0200790 for (i = 0; i < smp->data.str.len; i++) {
791 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
792 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +0100793 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200794 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100795 return 1;
796}
797
Willy Tarreau12785782012-04-27 21:37:17 +0200798static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100799{
800 int i;
801
Willy Tarreau342acb42012-04-23 22:03:39 +0200802 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +0200803 return 0;
804
Willy Tarreau342acb42012-04-23 22:03:39 +0200805 for (i = 0; i < smp->data.str.len; i++) {
806 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
807 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +0100808 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200809 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100810 return 1;
811}
812
Willy Tarreauf9954102012-04-20 14:03:29 +0200813/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +0200814static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100815{
Willy Tarreau342acb42012-04-23 22:03:39 +0200816 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200817 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100818 return 1;
819}
820
Emeric Brun107ca302010-01-04 16:16:05 +0100821/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +0200822static struct sample_conv_kw_list sample_conv_kws = {{ },{
823 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
824 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
825 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200826 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +0100827}};
828
829__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +0200830static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +0100831{
Willy Tarreau12785782012-04-27 21:37:17 +0200832 /* register sample format convert keywords */
833 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +0100834}