blob: 3067cc75825a074697283cb308a7d596c1860928 [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
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100242static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
243 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
244 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
245 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
246 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
247 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
248 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
249 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
250 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
251 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
252 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
253 [SMP_CKP_BE_SRV_CON] = "server source selection",
254 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
255 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
256 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
257 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
258 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
259 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
260 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
261 [SMP_CKP_FE_LOG_END] = "logs",
262};
263
Willy Tarreau80aca902013-01-07 15:42:20 +0100264/* fill the trash with a comma-delimited list of source names for the <use> bit
265 * field which must be composed of a non-null set of SMP_USE_* flags. The return
266 * value is the pointer to the string in the trash buffer.
267 */
268const char *sample_src_names(unsigned int use)
269{
270 int bit;
271
272 trash.len = 0;
273 trash.str[0] = '\0';
274 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
275 if (!(use & ~((1 << bit) - 1)))
276 break; /* no more bits */
277
278 if (!(use & (1 << bit)))
279 continue; /* bit not set */
280
281 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
282 (use & ((1 << bit) - 1)) ? "," : "",
283 fetch_src_names[bit]);
284 }
285 return trash.str;
286}
287
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100288/* return a pointer to the correct sample checkpoint name, or "unknown" when
289 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
290 * if set.
291 */
292const char *sample_ckp_names(unsigned int use)
293{
294 int bit;
295
296 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
297 if (use & (1 << bit))
298 return fetch_ckp_names[bit];
299 return "unknown sample check place, please report this bug";
300}
301
Emeric Brun107ca302010-01-04 16:16:05 +0100302/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100303 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
304 * for next parsing sessions. The fetch keywords capabilities are also computed
305 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100306 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100307void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100308{
Willy Tarreau80aca902013-01-07 15:42:20 +0100309 struct sample_fetch *sf;
310 int bit;
311
312 for (sf = kwl->kw; sf->kw != NULL; sf++) {
313 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
314 if (sf->use & (1 << bit))
315 sf->val |= fetch_cap[bit];
316 }
317 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100318}
319
320/*
Willy Tarreau12785782012-04-27 21:37:17 +0200321 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100322 * parsing sessions.
323 */
Willy Tarreau12785782012-04-27 21:37:17 +0200324void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100325{
Willy Tarreau12785782012-04-27 21:37:17 +0200326 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100327}
328
329/*
Willy Tarreau12785782012-04-27 21:37:17 +0200330 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100331 * string of <len> in buffer <kw>.
332 *
333 */
Willy Tarreau12785782012-04-27 21:37:17 +0200334struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100335{
336 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200337 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100338
Willy Tarreau12785782012-04-27 21:37:17 +0200339 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100340 for (index = 0; kwl->kw[index].kw != NULL; index++) {
341 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
342 kwl->kw[index].kw[len] == '\0')
343 return &kwl->kw[index];
344 }
345 }
346 return NULL;
347}
348
349/*
Willy Tarreau12785782012-04-27 21:37:17 +0200350 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100351 * string of <len> in buffer <kw>.
352 *
353 */
Willy Tarreau12785782012-04-27 21:37:17 +0200354struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100355{
356 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200357 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100358
Willy Tarreau12785782012-04-27 21:37:17 +0200359 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100360 for (index = 0; kwl->kw[index].kw != NULL; index++) {
361 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
362 kwl->kw[index].kw[len] == '\0')
363 return &kwl->kw[index];
364 }
365 }
366 return NULL;
367}
368
Emeric Brun107ca302010-01-04 16:16:05 +0100369/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200370/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200371/* Note: these functions do *NOT* set the output type on the */
372/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100373/******************************************************************/
374
Willy Tarreau342acb42012-04-23 22:03:39 +0200375static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100376{
Willy Tarreau342acb42012-04-23 22:03:39 +0200377 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Emeric Brun107ca302010-01-04 16:16:05 +0100378 return 1;
379}
380
Willy Tarreau342acb42012-04-23 22:03:39 +0200381static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100382{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100383 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100384
Willy Tarreau342acb42012-04-23 22:03:39 +0200385 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100386 return 0;
387
388 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200389 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100390
391 return 1;
392}
393
Willy Tarreau342acb42012-04-23 22:03:39 +0200394static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100395{
Willy Tarreau342acb42012-04-23 22:03:39 +0200396 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
David du Colombier4f92d322011-03-24 11:09:31 +0100397 return 1;
398}
399
Willy Tarreau342acb42012-04-23 22:03:39 +0200400static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100401{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100402 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100403
Willy Tarreau342acb42012-04-23 22:03:39 +0200404 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100405 return 0;
406
407 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200408 smp->data.str = *trash;
David du Colombier4f92d322011-03-24 11:09:31 +0100409 return 1;
410}
411
412/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200413static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100414{
Willy Tarreau342acb42012-04-23 22:03:39 +0200415 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100416}
417*/
418
Willy Tarreau342acb42012-04-23 22:03:39 +0200419static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100420{
Willy Tarreau342acb42012-04-23 22:03:39 +0200421 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Emeric Brun107ca302010-01-04 16:16:05 +0100422 return 1;
423}
424
Willy Tarreau342acb42012-04-23 22:03:39 +0200425static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100426{
Willy Tarreau342acb42012-04-23 22:03:39 +0200427 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100428 return 0;
429 return 1;
430}
431
Willy Tarreau342acb42012-04-23 22:03:39 +0200432static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100433{
Willy Tarreau342acb42012-04-23 22:03:39 +0200434 return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100435}
436
Emeric Brun8ac33d92012-10-17 13:36:06 +0200437static int c_bin2str(struct sample *smp)
438{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100439 struct chunk *trash = get_trash_chunk();
Emeric Brun8ac33d92012-10-17 13:36:06 +0200440 unsigned char c;
441 int ptr = 0;
442
443 trash->len = 0;
444 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
445 c = smp->data.str.str[ptr++];
446 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
447 trash->str[trash->len++] = hextab[c & 0xF];
448 }
449 smp->data.str = *trash;
450 return 1;
451}
452
Willy Tarreau342acb42012-04-23 22:03:39 +0200453static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100454{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100455 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100456 char *pos;
457
Willy Tarreau342acb42012-04-23 22:03:39 +0200458 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100459
460 if (!pos)
461 return 0;
462
Emeric Brun485479d2010-09-23 18:02:19 +0200463 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100464 trash->str = pos;
465 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200466 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100467 return 1;
468}
469
Willy Tarreau342acb42012-04-23 22:03:39 +0200470static int c_datadup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200471{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100472 struct chunk *trash = get_trash_chunk();
Emeric Brun485479d2010-09-23 18:02:19 +0200473
Willy Tarreau342acb42012-04-23 22:03:39 +0200474 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
475 memcpy(trash->str, smp->data.str.str, trash->len);
476 smp->data.str = *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200477 return 1;
478}
479
480
Willy Tarreau342acb42012-04-23 22:03:39 +0200481static int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100482{
483 return 1;
484}
485
Willy Tarreau342acb42012-04-23 22:03:39 +0200486static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100487{
488 int i;
489 uint32_t ret = 0;
490
Willy Tarreau342acb42012-04-23 22:03:39 +0200491 for (i = 0; i < smp->data.str.len; i++) {
492 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100493
494 if (val > 9)
495 break;
496
497 ret = ret * 10 + val;
498 }
499
Willy Tarreau342acb42012-04-23 22:03:39 +0200500 smp->data.uint = ret;
Emeric Brun107ca302010-01-04 16:16:05 +0100501 return 1;
502}
503
504/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200505/* Sample casts matrix: */
506/* sample_casts[from type][to type] */
507/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100508/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100509
Willy Tarreau12785782012-04-27 21:37:17 +0200510typedef int (*sample_cast_fct)(struct sample *smp);
511static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Willy Tarreau422aa072012-04-20 20:49:27 +0200512/* to: BOOL UINT SINT IPV4 IPV6 STR BIN CSTR CBIN */
Willy Tarreaud167e6d2012-12-21 17:38:05 +0100513/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, c_int2str, NULL, c_int2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200514/* UINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
515/* SINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
516/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL },
517/* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL },
518/* 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 +0200519/* BIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none },
Willy Tarreau422aa072012-04-20 20:49:27 +0200520/* 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 +0200521/* CBIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200522};
Emeric Brun107ca302010-01-04 16:16:05 +0100523
Emeric Brun107ca302010-01-04 16:16:05 +0100524/*
Willy Tarreau12785782012-04-27 21:37:17 +0200525 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100526 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200527 * Returns a pointer on allocated sample expression structure.
Emeric Brun107ca302010-01-04 16:16:05 +0100528 */
Willy Tarreau12785782012-04-27 21:37:17 +0200529struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_size)
Emeric Brun107ca302010-01-04 16:16:05 +0100530{
531 const char *endw;
532 const char *end;
Willy Tarreau12785782012-04-27 21:37:17 +0200533 struct sample_expr *expr;
534 struct sample_fetch *fetch;
535 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100536 unsigned long prev_type;
Emeric Brun485479d2010-09-23 18:02:19 +0200537 char *p;
Emeric Brun107ca302010-01-04 16:16:05 +0100538
Emeric Brun485479d2010-09-23 18:02:19 +0200539 snprintf(err, err_size, "memory error.");
540 if (!str[*idx]) {
541
542 snprintf(err, err_size, "missing fetch method.");
Emeric Brun107ca302010-01-04 16:16:05 +0100543 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200544 }
Emeric Brun107ca302010-01-04 16:16:05 +0100545
546 end = str[*idx] + strlen(str[*idx]);
547 endw = strchr(str[*idx], '(');
548
549 if (!endw)
550 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200551 else if ((end-1)[0] != ')') {
552 p = my_strndup(str[*idx], endw - str[*idx]);
553 if (p) {
554 snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p);
555 free(p);
556 }
Emeric Brun107ca302010-01-04 16:16:05 +0100557 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200558 }
Emeric Brun107ca302010-01-04 16:16:05 +0100559
Willy Tarreau12785782012-04-27 21:37:17 +0200560 fetch = find_sample_fetch(str[*idx], endw - str[*idx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200561 if (!fetch) {
562 p = my_strndup(str[*idx], endw - str[*idx]);
563 if (p) {
564 snprintf(err, err_size, "unknown fetch method '%s'.", p);
565 free(p);
566 }
Emeric Brun107ca302010-01-04 16:16:05 +0100567 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200568 }
Willy Tarreau422aa072012-04-20 20:49:27 +0200569 if (fetch->out_type >= SMP_TYPES) {
Emeric Brun107ca302010-01-04 16:16:05 +0100570
Emeric Brun485479d2010-09-23 18:02:19 +0200571 p = my_strndup(str[*idx], endw - str[*idx]);
572 if (p) {
573 snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p);
574 free(p);
575 }
Emeric Brun107ca302010-01-04 16:16:05 +0100576 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200577 }
Emeric Brun107ca302010-01-04 16:16:05 +0100578
579 prev_type = fetch->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200580 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200581 if (!expr)
582 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100583
584 LIST_INIT(&(expr->conv_exprs));
585 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200586 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100587
588 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200589 char *err_msg = NULL;
590 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200591
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200592 if (!fetch->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200593 p = my_strndup(str[*idx], endw - str[*idx]);
594 if (p) {
595 snprintf(err, err_size, "fetch method '%s' does not support any args.", p);
596 free(p);
597 }
598 goto out_error;
599 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200600
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200601 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 +0200602 p = my_strndup(str[*idx], endw - str[*idx]);
603 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200604 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 +0200605 free(p);
606 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200607 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200608 goto out_error;
609 }
610
Willy Tarreau2e845be2012-10-19 19:49:09 +0200611 if (!expr->arg_p)
612 expr->arg_p = empty_arg_list;
613
Willy Tarreau21d68a62012-04-20 15:52:36 +0200614 if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) {
615 p = my_strndup(str[*idx], endw - str[*idx]);
616 if (p) {
617 snprintf(err, err_size, "invalid args in fetch method '%s' : %s.", p, err_msg);
618 free(p);
619 }
620 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200621 goto out_error;
622 }
623 }
Willy Tarreauf22a5082012-10-19 16:47:23 +0200624 else if (ARGM(fetch->arg_mask)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200625 p = my_strndup(str[*idx], endw - str[*idx]);
626 if (p) {
627 snprintf(err, err_size, "missing args for fetch method '%s'.", p);
628 free(p);
629 }
630 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100631 }
632
633 for (*idx += 1; *(str[*idx]); (*idx)++) {
Willy Tarreau12785782012-04-27 21:37:17 +0200634 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100635
636 end = str[*idx] + strlen(str[*idx]);
637 endw = strchr(str[*idx], '(');
638
639 if (!endw)
640 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200641 else if ((end-1)[0] != ')') {
642 p = my_strndup(str[*idx], endw - str[*idx]);
643 if (p) {
644 snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p);
645 free(p);
646 }
Emeric Brun107ca302010-01-04 16:16:05 +0100647 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200648 }
Emeric Brun107ca302010-01-04 16:16:05 +0100649
Willy Tarreau12785782012-04-27 21:37:17 +0200650 conv = find_sample_conv(str[*idx], endw - str[*idx]);
Emeric Brun107ca302010-01-04 16:16:05 +0100651 if (!conv)
652 break;
653
Willy Tarreau422aa072012-04-20 20:49:27 +0200654 if (conv->in_type >= SMP_TYPES ||
655 conv->out_type >= SMP_TYPES) {
Emeric Brun485479d2010-09-23 18:02:19 +0200656 p = my_strndup(str[*idx], endw - str[*idx]);
657 if (p) {
658 snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p);
659 free(p);
660 }
Emeric Brun107ca302010-01-04 16:16:05 +0100661 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200662 }
Emeric Brun107ca302010-01-04 16:16:05 +0100663
664 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200665 if (!sample_casts[prev_type][conv->in_type]) {
Emeric Brun485479d2010-09-23 18:02:19 +0200666 p = my_strndup(str[*idx], endw - str[*idx]);
667 if (p) {
668 snprintf(err, err_size, "conv method '%s' cannot be applied.", p);
669 free(p);
670 }
Emeric Brun107ca302010-01-04 16:16:05 +0100671 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200672 }
Emeric Brun107ca302010-01-04 16:16:05 +0100673
674 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200675 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200676 if (!conv_expr)
677 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100678
679 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
680 conv_expr->conv = conv;
681
682 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200683 char *err_msg = NULL;
684 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200685
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200686 if (!conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200687 p = my_strndup(str[*idx], endw - str[*idx]);
688
689 if (p) {
690 snprintf(err, err_size, "conv method '%s' does not support any args.", p);
691 free(p);
692 }
693 goto out_error;
694 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100695
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200696 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 +0200697 p = my_strndup(str[*idx], endw - str[*idx]);
698 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200699 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 +0200700 free(p);
701 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200702 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200703 goto out_error;
704 }
705
Willy Tarreau2e845be2012-10-19 19:49:09 +0200706 if (!conv_expr->arg_p)
707 conv_expr->arg_p = empty_arg_list;
708
Willy Tarreau21d68a62012-04-20 15:52:36 +0200709 if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) {
710 p = my_strndup(str[*idx], endw - str[*idx]);
711 if (p) {
712 snprintf(err, err_size, "invalid args in conv method '%s' : %s.", p, err_msg);
713 free(p);
714 }
715 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200716 goto out_error;
717 }
718 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200719 else if (conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200720 p = my_strndup(str[*idx], endw - str[*idx]);
721 if (p) {
722 snprintf(err, err_size, "missing args for conv method '%s'.", p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100723 free(p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100724 }
Emeric Brun485479d2010-09-23 18:02:19 +0200725 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100726 }
Emeric Brun485479d2010-09-23 18:02:19 +0200727
Emeric Brun107ca302010-01-04 16:16:05 +0100728 }
Emeric Brun485479d2010-09-23 18:02:19 +0200729
Emeric Brun107ca302010-01-04 16:16:05 +0100730 return expr;
731
732out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200733 /* TODO: prune_sample_expr(expr); */
Emeric Brun107ca302010-01-04 16:16:05 +0100734 return NULL;
735}
736
737/*
Willy Tarreau12785782012-04-27 21:37:17 +0200738 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200739 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200740 * Returns a pointer on a typed sample structure containing the result or NULL if
741 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100742 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200743 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200744 *
745 * Note: the fetch functions are required to properly set the return type. The
746 * conversion functions must do so too. However the cast functions do not need
747 * to since they're made to cast mutiple types according to what is required.
Emeric Brun107ca302010-01-04 16:16:05 +0100748 */
Willy Tarreau12785782012-04-27 21:37:17 +0200749struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
750 unsigned int opt,
751 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100752{
Willy Tarreau12785782012-04-27 21:37:17 +0200753 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100754
755 if (p == NULL)
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200756 p = &temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +0100757
Willy Tarreau342acb42012-04-23 22:03:39 +0200758 p->flags = 0;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200759 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100760 return NULL;
761
Willy Tarreau40aebd92012-04-26 11:05:50 +0200762 if (p->flags & SMP_F_MAY_CHANGE)
Willy Tarreau12785782012-04-27 21:37:17 +0200763 return NULL; /* we can only use stable samples */
Willy Tarreau40aebd92012-04-26 11:05:50 +0200764
Emeric Brun107ca302010-01-04 16:16:05 +0100765 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200766 /* we want to ensure that p->type can be casted into
767 * conv_expr->conv->in_type. We have 3 possibilities :
768 * - NULL => not castable.
769 * - c_none => nothing to do (let's optimize it)
770 * - other => apply cast and prepare to fail
771 */
Willy Tarreau12785782012-04-27 21:37:17 +0200772 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200773 return NULL;
774
Willy Tarreau12785782012-04-27 21:37:17 +0200775 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
776 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100777 return NULL;
778
Willy Tarreau12e50112012-04-25 17:21:49 +0200779 /* OK cast succeeded */
780
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200781 /* force the output type after a cast */
Emeric Brun107ca302010-01-04 16:16:05 +0100782 p->type = conv_expr->conv->in_type;
Willy Tarreau342acb42012-04-23 22:03:39 +0200783 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100784 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100785 }
786 return p;
787}
788
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100789/*
790 * Process a fetch + format conversion as defined by the sample expression <expr>
791 * on request or response considering the <opt> parameter. The output is always of
792 * type string. Returns either NULL if no sample could be extracted, or a pointer
793 * to the converted result stored in static temp_smp in format string.
794 */
795struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
796 unsigned int opt, struct sample_expr *expr)
797{
798 struct sample *smp;
799
800 smp = sample_process(px, l4, l7, opt, expr, NULL);
801 if (!smp)
802 return NULL;
803
804 if (!sample_casts[smp->type][SMP_T_CSTR])
805 return NULL;
806
807 if (!sample_casts[smp->type][SMP_T_CSTR](smp))
808 return NULL;
809
810 smp->type = SMP_T_CSTR;
811 return smp;
812}
813
Emeric Brun107ca302010-01-04 16:16:05 +0100814/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200815/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200816/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100817/*****************************************************************/
818
Willy Tarreau12785782012-04-27 21:37:17 +0200819static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100820{
821 int i;
822
Willy Tarreau342acb42012-04-23 22:03:39 +0200823 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +0200824 return 0;
825
Willy Tarreau342acb42012-04-23 22:03:39 +0200826 for (i = 0; i < smp->data.str.len; i++) {
827 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
828 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +0100829 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200830 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100831 return 1;
832}
833
Willy Tarreau12785782012-04-27 21:37:17 +0200834static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100835{
836 int i;
837
Willy Tarreau342acb42012-04-23 22:03:39 +0200838 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +0200839 return 0;
840
Willy Tarreau342acb42012-04-23 22:03:39 +0200841 for (i = 0; i < smp->data.str.len; i++) {
842 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
843 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +0100844 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200845 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100846 return 1;
847}
848
Willy Tarreauf9954102012-04-20 14:03:29 +0200849/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +0200850static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100851{
Willy Tarreau342acb42012-04-23 22:03:39 +0200852 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200853 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +0100854 return 1;
855}
856
Emeric Brun107ca302010-01-04 16:16:05 +0100857/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +0200858static struct sample_conv_kw_list sample_conv_kws = {{ },{
859 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
860 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
861 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200862 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +0100863}};
864
865__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +0200866static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +0100867{
Willy Tarreau12785782012-04-27 21:37:17 +0200868 /* register sample format convert keywords */
869 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +0100870}