blob: 97e9b6e6996f17e550e9e6bf0e9ec98298c1f31b [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>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020022#include <common/uri_auth.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023
Willy Tarreau9fcb9842012-04-20 14:45:49 +020024#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020025#include <proto/auth.h>
26#include <proto/log.h>
27#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020029#include <proto/stick_table.h>
Emeric Brun107ca302010-01-04 16:16:05 +010030
Willy Tarreau12785782012-04-27 21:37:17 +020031/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020032static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010033
Willy Tarreau12785782012-04-27 21:37:17 +020034/* list head of all known sample fetch keywords */
35static struct sample_fetch_kw_list sample_fetches = {
36 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010037};
38
Willy Tarreau12785782012-04-27 21:37:17 +020039/* list head of all known sample format conversion keywords */
40static struct sample_conv_kw_list sample_convs = {
41 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010042};
43
Willy Tarreau80aca902013-01-07 15:42:20 +010044const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
45 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
46 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
47 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
48 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
49 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
50 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
51 SMP_VAL_FE_LOG_END),
52
53 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
54 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
55 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
56 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
57 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
58 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
59 SMP_VAL_FE_LOG_END),
60
61 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
62 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
63 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
64 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
65 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
66 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
67 SMP_VAL_FE_LOG_END),
68
69 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
97 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
98 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
99 SMP_VAL___________),
100
101 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
105 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
106 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
107 SMP_VAL___________),
108
109 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
115 SMP_VAL_FE_LOG_END),
116
117 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
118 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
126 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
129 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
130 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
131 SMP_VAL_FE_LOG_END),
132
133 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
135 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
136 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
137 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
138 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
139 SMP_VAL_FE_LOG_END),
140
141 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
145 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
146 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
147 SMP_VAL_FE_LOG_END),
148
149 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL___________),
164
165 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL___________),
172
173 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL_FE_LOG_END),
180
181 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
194 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
195 SMP_VAL_FE_LOG_END),
196
197 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
202 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
203 SMP_VAL_FE_LOG_END),
204
205 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
211 SMP_VAL_FE_LOG_END),
212
213 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220};
221
222static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
223 [SMP_SRC_INTRN] = "internal state",
224 [SMP_SRC_LISTN] = "listener",
225 [SMP_SRC_FTEND] = "frontend",
226 [SMP_SRC_L4CLI] = "client address",
227 [SMP_SRC_L5CLI] = "client-side connection",
228 [SMP_SRC_TRACK] = "track counters",
229 [SMP_SRC_L6REQ] = "request buffer",
230 [SMP_SRC_HRQHV] = "HTTP request headers",
231 [SMP_SRC_HRQHP] = "HTTP request",
232 [SMP_SRC_HRQBO] = "HTTP request body",
233 [SMP_SRC_BKEND] = "backend",
234 [SMP_SRC_SERVR] = "server",
235 [SMP_SRC_L4SRV] = "server address",
236 [SMP_SRC_L5SRV] = "server-side connection",
237 [SMP_SRC_L6RES] = "response buffer",
238 [SMP_SRC_HRSHV] = "HTTP response headers",
239 [SMP_SRC_HRSHP] = "HTTP response",
240 [SMP_SRC_HRSBO] = "HTTP response body",
241 [SMP_SRC_RQFIN] = "request buffer statistics",
242 [SMP_SRC_RSFIN] = "response buffer statistics",
243 [SMP_SRC_TXFIN] = "transaction statistics",
244 [SMP_SRC_SSFIN] = "session statistics",
245};
246
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100247static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
248 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
249 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
250 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
251 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
252 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
253 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
254 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
255 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
256 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
257 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
258 [SMP_CKP_BE_SRV_CON] = "server source selection",
259 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
260 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
261 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
262 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
263 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
264 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
265 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
266 [SMP_CKP_FE_LOG_END] = "logs",
267};
268
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100269/* This function returns the type of the data returned by the sample_expr.
270 * It assumes that the <expr> and all of its converters are properly
271 * initialized.
272 */
273inline
274int smp_expr_output_type(struct sample_expr *expr)
275{
276 struct sample_conv_expr *smp_expr;
277
278 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
279 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
280 return smp_expr->conv->out_type;
281 }
282 return expr->fetch->out_type;
283}
284
285
Willy Tarreau80aca902013-01-07 15:42:20 +0100286/* fill the trash with a comma-delimited list of source names for the <use> bit
287 * field which must be composed of a non-null set of SMP_USE_* flags. The return
288 * value is the pointer to the string in the trash buffer.
289 */
290const char *sample_src_names(unsigned int use)
291{
292 int bit;
293
294 trash.len = 0;
295 trash.str[0] = '\0';
296 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
297 if (!(use & ~((1 << bit) - 1)))
298 break; /* no more bits */
299
300 if (!(use & (1 << bit)))
301 continue; /* bit not set */
302
303 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
304 (use & ((1 << bit) - 1)) ? "," : "",
305 fetch_src_names[bit]);
306 }
307 return trash.str;
308}
309
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100310/* return a pointer to the correct sample checkpoint name, or "unknown" when
311 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
312 * if set.
313 */
314const char *sample_ckp_names(unsigned int use)
315{
316 int bit;
317
318 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
319 if (use & (1 << bit))
320 return fetch_ckp_names[bit];
321 return "unknown sample check place, please report this bug";
322}
323
Emeric Brun107ca302010-01-04 16:16:05 +0100324/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100325 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
326 * for next parsing sessions. The fetch keywords capabilities are also computed
327 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100328 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100329void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100330{
Willy Tarreau80aca902013-01-07 15:42:20 +0100331 struct sample_fetch *sf;
332 int bit;
333
334 for (sf = kwl->kw; sf->kw != NULL; sf++) {
335 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
336 if (sf->use & (1 << bit))
337 sf->val |= fetch_cap[bit];
338 }
339 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100340}
341
342/*
Willy Tarreau12785782012-04-27 21:37:17 +0200343 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100344 * parsing sessions.
345 */
Willy Tarreau12785782012-04-27 21:37:17 +0200346void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100347{
Willy Tarreau12785782012-04-27 21:37:17 +0200348 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100349}
350
351/*
Willy Tarreau12785782012-04-27 21:37:17 +0200352 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100353 * string of <len> in buffer <kw>.
354 *
355 */
Willy Tarreau12785782012-04-27 21:37:17 +0200356struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100357{
358 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200359 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100360
Willy Tarreau12785782012-04-27 21:37:17 +0200361 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100362 for (index = 0; kwl->kw[index].kw != NULL; index++) {
363 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
364 kwl->kw[index].kw[len] == '\0')
365 return &kwl->kw[index];
366 }
367 }
368 return NULL;
369}
370
371/*
Willy Tarreau12785782012-04-27 21:37:17 +0200372 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100373 * string of <len> in buffer <kw>.
374 *
375 */
Willy Tarreau12785782012-04-27 21:37:17 +0200376struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100377{
378 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200379 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100380
Willy Tarreau12785782012-04-27 21:37:17 +0200381 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100382 for (index = 0; kwl->kw[index].kw != NULL; index++) {
383 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
384 kwl->kw[index].kw[len] == '\0')
385 return &kwl->kw[index];
386 }
387 }
388 return NULL;
389}
390
Emeric Brun107ca302010-01-04 16:16:05 +0100391/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200392/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200393/* Note: these functions do *NOT* set the output type on the */
394/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100395/******************************************************************/
396
Willy Tarreau342acb42012-04-23 22:03:39 +0200397static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100398{
Willy Tarreau342acb42012-04-23 22:03:39 +0200399 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100400 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100401 return 1;
402}
403
Willy Tarreau342acb42012-04-23 22:03:39 +0200404static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100405{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100406 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100407
Willy Tarreau342acb42012-04-23 22:03:39 +0200408 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100409 return 0;
410
411 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200412 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100413 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100414
415 return 1;
416}
417
Willy Tarreau342acb42012-04-23 22:03:39 +0200418static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100419{
Willy Tarreau342acb42012-04-23 22:03:39 +0200420 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100421 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100422 return 1;
423}
424
Willy Tarreau342acb42012-04-23 22:03:39 +0200425static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100426{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100427 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100428
Willy Tarreau342acb42012-04-23 22:03:39 +0200429 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100430 return 0;
431
432 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200433 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100434 smp->type = SMP_T_STR;
David du Colombier4f92d322011-03-24 11:09:31 +0100435 return 1;
436}
437
438/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200439static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100440{
Willy Tarreau342acb42012-04-23 22:03:39 +0200441 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100442}
443*/
444
Willy Tarreau342acb42012-04-23 22:03:39 +0200445static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100446{
Willy Tarreau342acb42012-04-23 22:03:39 +0200447 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100448 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100449 return 1;
450}
451
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100452static int c_str2addr(struct sample *smp)
453{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100454 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100455 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
456 return 0;
457 smp->type = SMP_T_IPV6;
458 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100459 }
460 smp->type = SMP_T_IPV4;
461 return 1;
462}
463
Willy Tarreau342acb42012-04-23 22:03:39 +0200464static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100465{
Willy Tarreau342acb42012-04-23 22:03:39 +0200466 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100467 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100468 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100469 return 1;
470}
471
Willy Tarreau342acb42012-04-23 22:03:39 +0200472static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100473{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100474 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
475 return 0;
476 smp->type = SMP_T_IPV6;
477 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100478}
479
Emeric Brun8ac33d92012-10-17 13:36:06 +0200480static int c_bin2str(struct sample *smp)
481{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100482 struct chunk *trash = get_trash_chunk();
Emeric Brun8ac33d92012-10-17 13:36:06 +0200483 unsigned char c;
484 int ptr = 0;
485
486 trash->len = 0;
487 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
488 c = smp->data.str.str[ptr++];
489 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
490 trash->str[trash->len++] = hextab[c & 0xF];
491 }
492 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100493 smp->type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200494 return 1;
495}
496
Willy Tarreau342acb42012-04-23 22:03:39 +0200497static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100498{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100499 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100500 char *pos;
501
Willy Tarreau342acb42012-04-23 22:03:39 +0200502 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100503
504 if (!pos)
505 return 0;
506
Emeric Brun485479d2010-09-23 18:02:19 +0200507 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100508 trash->str = pos;
509 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200510 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100511 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +0100512 return 1;
513}
514
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100515static inline void _c_datadup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200516{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100517 struct chunk *trash = get_trash_chunk();
Emeric Brun485479d2010-09-23 18:02:19 +0200518
Willy Tarreau342acb42012-04-23 22:03:39 +0200519 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
520 memcpy(trash->str, smp->data.str.str, trash->len);
521 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100522}
523
524static int c_datadup(struct sample *smp)
525{
526 _c_datadup(smp);
527 if (smp->type == SMP_T_CSTR)
528 smp->type = SMP_T_STR;
529 else
530 smp->type = SMP_T_BIN;
531 return 1;
532}
533
534static int c_bindup(struct sample *smp)
535{
536 _c_datadup(smp);
537 smp->type = SMP_T_BIN;
Emeric Brun485479d2010-09-23 18:02:19 +0200538 return 1;
539}
540
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100543{
544 return 1;
545}
546
Willy Tarreau342acb42012-04-23 22:03:39 +0200547static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100548{
549 int i;
550 uint32_t ret = 0;
551
Willy Tarreau342acb42012-04-23 22:03:39 +0200552 for (i = 0; i < smp->data.str.len; i++) {
553 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100554
555 if (val > 9)
556 break;
557
558 ret = ret * 10 + val;
559 }
560
Willy Tarreau342acb42012-04-23 22:03:39 +0200561 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100562 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100563 return 1;
564}
565
566/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200567/* Sample casts matrix: */
568/* sample_casts[from type][to type] */
569/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100570/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100571
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100572sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100573/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN CSTR CBIN */
574/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, c_int2str, NULL, },
575/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL, },
576/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL, },
577/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
578/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL, },
579/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL, },
580/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_none, c_none, },
581/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none, },
582/* CSTR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_datadup, c_bindup, c_none, c_none, },
583/* CBIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200584};
Emeric Brun107ca302010-01-04 16:16:05 +0100585
Emeric Brun107ca302010-01-04 16:16:05 +0100586/*
Willy Tarreau12785782012-04-27 21:37:17 +0200587 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100588 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200589 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200590 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100591 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200592struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_size, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100593{
Willy Tarreau833cc792013-07-24 15:34:19 +0200594 const char *begw; /* beginning of word */
595 const char *endw; /* end of word */
596 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200597 struct sample_expr *expr;
598 struct sample_fetch *fetch;
599 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100600 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200601 char *fkw = NULL;
602 char *ckw = NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100603
Willy Tarreau833cc792013-07-24 15:34:19 +0200604 /* prepare a generic message if any further snprintf() fails */
Emeric Brun485479d2010-09-23 18:02:19 +0200605 snprintf(err, err_size, "memory error.");
Emeric Brun485479d2010-09-23 18:02:19 +0200606
Willy Tarreau833cc792013-07-24 15:34:19 +0200607 begw = str[*idx];
608 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
609
610 if (endw == begw) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100611 snprintf(err, err_size, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100612 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200613 }
Emeric Brun107ca302010-01-04 16:16:05 +0100614
Willy Tarreau833cc792013-07-24 15:34:19 +0200615 /* keep a copy of the current fetch keyword for error reporting */
616 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100617
Willy Tarreau833cc792013-07-24 15:34:19 +0200618 fetch = find_sample_fetch(begw, endw - begw);
619 if (!fetch) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100620 snprintf(err, err_size, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100621 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200622 }
Emeric Brun107ca302010-01-04 16:16:05 +0100623
Willy Tarreau833cc792013-07-24 15:34:19 +0200624 endt = endw;
625 if (*endt == '(') {
626 /* look for the end of this term */
627 while (*endt && *endt != ')')
628 endt++;
629 if (*endt != ')') {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100630 snprintf(err, err_size, "syntax error: missing ')' after fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200631 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200632 }
Emeric Brun485479d2010-09-23 18:02:19 +0200633 }
Emeric Brun107ca302010-01-04 16:16:05 +0100634
Willy Tarreau833cc792013-07-24 15:34:19 +0200635 /* At this point, we have :
636 * - begw : beginning of the keyword
637 * - endw : end of the keyword (points to next delimiter or '(')
638 * - endt : end of the term (=endw or last parenthesis if args are present)
639 */
640
641 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100642 snprintf(err, err_size, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100643 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200644 }
Emeric Brun107ca302010-01-04 16:16:05 +0100645 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200646
Willy Tarreau12785782012-04-27 21:37:17 +0200647 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200648 if (!expr)
649 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100650
651 LIST_INIT(&(expr->conv_exprs));
652 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200653 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100654
Willy Tarreau833cc792013-07-24 15:34:19 +0200655 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200656 char *err_msg = NULL;
657 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200658
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200659 if (!fetch->arg_mask) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100660 snprintf(err, err_size, "fetch method '%s' does not support any args", fkw);
Emeric Brun485479d2010-09-23 18:02:19 +0200661 goto out_error;
662 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200663
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200664 al->kw = expr->fetch->kw;
665 al->conv = NULL;
Willy Tarreau833cc792013-07-24 15:34:19 +0200666 if (make_arg_list(endw + 1, endt - endw - 1, fetch->arg_mask, &expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100667 snprintf(err, err_size, "invalid arg %d in fetch method '%s' : %s", err_arg+1, fkw, err_msg);
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200668 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200669 goto out_error;
670 }
671
Willy Tarreau2e845be2012-10-19 19:49:09 +0200672 if (!expr->arg_p)
673 expr->arg_p = empty_arg_list;
674
Willy Tarreau21d68a62012-04-20 15:52:36 +0200675 if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100676 snprintf(err, err_size, "invalid args in fetch method '%s' : %s", fkw, err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200677 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200678 goto out_error;
679 }
680 }
Willy Tarreauf22a5082012-10-19 16:47:23 +0200681 else if (ARGM(fetch->arg_mask)) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100682 snprintf(err, err_size, "missing args for fetch method '%s'", fkw);
Emeric Brun485479d2010-09-23 18:02:19 +0200683 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100684 }
685
Willy Tarreau833cc792013-07-24 15:34:19 +0200686 /* Now process the converters if any. We have two supported syntaxes
687 * for the converters, which can be combined :
688 * - comma-delimited list of converters just after the keyword and args ;
689 * - one converter per keyword
690 * The combination allows to have each keyword being a comma-delimited
691 * series of converters.
692 *
693 * We want to process the former first, then the latter. For this we start
694 * from the beginning of the supposed place in the exiting conv chain, which
695 * starts at the last comma (endt).
696 */
697
698 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200699 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100700
Willy Tarreau833cc792013-07-24 15:34:19 +0200701 if (*endt == ')') /* skip last closing parenthesis */
702 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100703
Willy Tarreau833cc792013-07-24 15:34:19 +0200704 if (*endt && *endt != ',') {
705 if (ckw)
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100706 snprintf(err, err_size, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200707 else
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100708 snprintf(err, err_size, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100709 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200710 }
Emeric Brun107ca302010-01-04 16:16:05 +0100711
Willy Tarreau833cc792013-07-24 15:34:19 +0200712 while (*endt == ',') /* then trailing commas */
713 endt++;
714
715 begw = endt; /* start of conv keyword */
716
717 if (!*begw) {
718 /* none ? skip to next string */
719 (*idx)++;
720 begw = str[*idx];
721 if (!begw || !*begw)
722 break;
723 }
724
725 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
726
727 free(ckw);
728 ckw = my_strndup(begw, endw - begw);
729
730 conv = find_sample_conv(begw, endw - begw);
731 if (!conv) {
732 /* we found an isolated keyword that we don't know, it's not ours */
733 if (begw == str[*idx])
734 break;
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100735 snprintf(err, err_size, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200736 goto out_error;
737 }
Emeric Brun107ca302010-01-04 16:16:05 +0100738
Willy Tarreau833cc792013-07-24 15:34:19 +0200739 endt = endw;
740 if (*endt == '(') {
741 /* look for the end of this term */
742 while (*endt && *endt != ')')
743 endt++;
744 if (*endt != ')') {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100745 snprintf(err, err_size, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200746 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200747 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200748 }
749
750 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100751 snprintf(err, err_size, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100752 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200753 }
Emeric Brun107ca302010-01-04 16:16:05 +0100754
755 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200756 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100757 snprintf(err, err_size, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100758 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200759 }
Emeric Brun107ca302010-01-04 16:16:05 +0100760
761 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200762 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200763 if (!conv_expr)
764 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100765
766 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
767 conv_expr->conv = conv;
768
Willy Tarreau833cc792013-07-24 15:34:19 +0200769 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200770 char *err_msg = NULL;
771 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200772
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200773 if (!conv->arg_mask) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100774 snprintf(err, err_size, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200775 goto out_error;
776 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100777
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200778 al->kw = expr->fetch->kw;
779 al->conv = conv_expr->conv->kw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200780 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100781 snprintf(err, err_size, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, err_msg);
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200782 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200783 goto out_error;
784 }
785
Willy Tarreau2e845be2012-10-19 19:49:09 +0200786 if (!conv_expr->arg_p)
787 conv_expr->arg_p = empty_arg_list;
788
Thierry FOURNIER9c1d67e2013-11-21 13:37:41 +0100789 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, &err_msg)) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100790 snprintf(err, err_size, "invalid args in conv method '%s' : %s", ckw, err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200791 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200792 goto out_error;
793 }
794 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200795 else if (ARGM(conv->arg_mask)) {
Willy Tarreau3d536ac2013-12-06 16:02:46 +0100796 snprintf(err, err_size, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200797 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100798 }
799 }
Emeric Brun485479d2010-09-23 18:02:19 +0200800
Willy Tarreau833cc792013-07-24 15:34:19 +0200801 out:
802 free(fkw);
803 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100804 return expr;
805
806out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200807 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200808 expr = NULL;
809 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100810}
811
812/*
Willy Tarreau12785782012-04-27 21:37:17 +0200813 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200814 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200815 * Returns a pointer on a typed sample structure containing the result or NULL if
816 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100817 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200818 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200819 *
820 * Note: the fetch functions are required to properly set the return type. The
821 * conversion functions must do so too. However the cast functions do not need
822 * to since they're made to cast mutiple types according to what is required.
Emeric Brun107ca302010-01-04 16:16:05 +0100823 */
Willy Tarreau12785782012-04-27 21:37:17 +0200824struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
825 unsigned int opt,
826 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100827{
Willy Tarreau12785782012-04-27 21:37:17 +0200828 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100829
Willy Tarreau18387e22013-07-25 12:02:38 +0200830 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200831 p = &temp_smp;
Willy Tarreau18387e22013-07-25 12:02:38 +0200832 p->flags = 0;
833 }
Emeric Brun107ca302010-01-04 16:16:05 +0100834
Willy Tarreauef38c392013-07-22 16:29:32 +0200835 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw))
Emeric Brun107ca302010-01-04 16:16:05 +0100836 return NULL;
837
Emeric Brun107ca302010-01-04 16:16:05 +0100838 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200839 /* we want to ensure that p->type can be casted into
840 * conv_expr->conv->in_type. We have 3 possibilities :
841 * - NULL => not castable.
842 * - c_none => nothing to do (let's optimize it)
843 * - other => apply cast and prepare to fail
844 */
Willy Tarreau12785782012-04-27 21:37:17 +0200845 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200846 return NULL;
847
Willy Tarreau12785782012-04-27 21:37:17 +0200848 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
849 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100850 return NULL;
851
Willy Tarreau12e50112012-04-25 17:21:49 +0200852 /* OK cast succeeded */
853
Willy Tarreau342acb42012-04-23 22:03:39 +0200854 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100855 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100856 }
857 return p;
858}
859
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100860/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200861 * Resolve all remaining arguments in proxy <p>. Returns the number of
862 * errors or 0 if everything is fine.
863 */
864int smp_resolve_args(struct proxy *p)
865{
866 struct arg_list *cur, *bak;
867 const char *ctx, *where;
868 const char *conv_ctx, *conv_pre, *conv_pos;
869 struct userlist *ul;
870 struct arg *arg;
871 int cfgerr = 0;
872
873 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
874 struct proxy *px;
875 struct server *srv;
876 char *pname, *sname;
877
878 arg = cur->arg;
879
880 /* prepare output messages */
881 conv_pre = conv_pos = conv_ctx = "";
882 if (cur->conv) {
883 conv_ctx = cur->conv;
884 conv_pre = "conversion keyword '";
885 conv_pos = "' for ";
886 }
887
888 where = "in";
889 ctx = "sample fetch keyword";
890 switch (cur->ctx) {
891 case ARGC_STK:where = "in stick rule in"; break;
892 case ARGC_TRK: where = "in tracking rule in"; break;
893 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +0100894 case ARGC_HRQ: where = "in http-request header format string in"; break;
895 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200896 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +0100897 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200898 case ARGC_ACL: ctx = "ACL keyword"; break;
899 }
900
901 /* set a few default settings */
902 px = p;
903 pname = p->id;
904
905 switch (arg->type) {
906 case ARGT_SRV:
907 if (!arg->data.str.len) {
908 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
909 cur->file, cur->line,
910 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
911 cfgerr++;
912 continue;
913 }
914
915 /* we support two formats : "bck/srv" and "srv" */
916 sname = strrchr(arg->data.str.str, '/');
917
918 if (sname) {
919 *sname++ = '\0';
920 pname = arg->data.str.str;
921
922 px = findproxy(pname, PR_CAP_BE);
923 if (!px) {
924 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
925 cur->file, cur->line, pname,
926 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
927 cfgerr++;
928 break;
929 }
930 }
931 else
932 sname = arg->data.str.str;
933
934 srv = findserver(px, sname);
935 if (!srv) {
936 Alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
937 cur->file, cur->line, sname, pname,
938 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
939 cfgerr++;
940 break;
941 }
942
943 free(arg->data.str.str);
944 arg->data.str.str = NULL;
945 arg->unresolved = 0;
946 arg->data.srv = srv;
947 break;
948
949 case ARGT_FE:
950 if (arg->data.str.len) {
951 pname = arg->data.str.str;
952 px = findproxy(pname, PR_CAP_FE);
953 }
954
955 if (!px) {
956 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
957 cur->file, cur->line, pname,
958 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
959 cfgerr++;
960 break;
961 }
962
963 if (!(px->cap & PR_CAP_FE)) {
964 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
965 cur->file, cur->line, pname,
966 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
967 cfgerr++;
968 break;
969 }
970
971 free(arg->data.str.str);
972 arg->data.str.str = NULL;
973 arg->unresolved = 0;
974 arg->data.prx = px;
975 break;
976
977 case ARGT_BE:
978 if (arg->data.str.len) {
979 pname = arg->data.str.str;
980 px = findproxy(pname, PR_CAP_BE);
981 }
982
983 if (!px) {
984 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
985 cur->file, cur->line, pname,
986 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
987 cfgerr++;
988 break;
989 }
990
991 if (!(px->cap & PR_CAP_BE)) {
992 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
993 cur->file, cur->line, pname,
994 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
995 cfgerr++;
996 break;
997 }
998
999 free(arg->data.str.str);
1000 arg->data.str.str = NULL;
1001 arg->unresolved = 0;
1002 arg->data.prx = px;
1003 break;
1004
1005 case ARGT_TAB:
1006 if (arg->data.str.len) {
1007 pname = arg->data.str.str;
1008 px = find_stktable(pname);
1009 }
1010
1011 if (!px) {
1012 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1013 cur->file, cur->line, pname,
1014 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1015 cfgerr++;
1016 break;
1017 }
1018
1019 if (!px->table.size) {
1020 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1021 cur->file, cur->line, pname,
1022 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1023 cfgerr++;
1024 break;
1025 }
1026
1027 free(arg->data.str.str);
1028 arg->data.str.str = NULL;
1029 arg->unresolved = 0;
1030 arg->data.prx = px;
1031 break;
1032
1033 case ARGT_USR:
1034 if (!arg->data.str.len) {
1035 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1036 cur->file, cur->line,
1037 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1038 cfgerr++;
1039 break;
1040 }
1041
1042 if (p->uri_auth && p->uri_auth->userlist &&
1043 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1044 ul = p->uri_auth->userlist;
1045 else
1046 ul = auth_find_userlist(arg->data.str.str);
1047
1048 if (!ul) {
1049 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1050 cur->file, cur->line, arg->data.str.str,
1051 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1052 cfgerr++;
1053 break;
1054 }
1055
1056 free(arg->data.str.str);
1057 arg->data.str.str = NULL;
1058 arg->unresolved = 0;
1059 arg->data.usr = ul;
1060 break;
1061 }
1062
1063 LIST_DEL(&cur->list);
1064 free(cur);
1065 } /* end of args processing */
1066
1067 return cfgerr;
1068}
1069
1070/*
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001071 * Process a fetch + format conversion as defined by the sample expression <expr>
1072 * on request or response considering the <opt> parameter. The output is always of
1073 * type string. Returns either NULL if no sample could be extracted, or a pointer
1074 * to the converted result stored in static temp_smp in format string.
1075 */
1076struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1077 unsigned int opt, struct sample_expr *expr)
1078{
1079 struct sample *smp;
1080
1081 smp = sample_process(px, l4, l7, opt, expr, NULL);
1082 if (!smp)
1083 return NULL;
1084
1085 if (!sample_casts[smp->type][SMP_T_CSTR])
1086 return NULL;
1087
1088 if (!sample_casts[smp->type][SMP_T_CSTR](smp))
1089 return NULL;
1090
1091 smp->type = SMP_T_CSTR;
1092 return smp;
1093}
1094
Emeric Brun107ca302010-01-04 16:16:05 +01001095/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001096/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001097/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001098/*****************************************************************/
1099
Willy Tarreau12785782012-04-27 21:37:17 +02001100static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001101{
1102 int i;
1103
Willy Tarreau342acb42012-04-23 22:03:39 +02001104 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001105 return 0;
1106
Willy Tarreau342acb42012-04-23 22:03:39 +02001107 for (i = 0; i < smp->data.str.len; i++) {
1108 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1109 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001110 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001111 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +01001112 return 1;
1113}
1114
Willy Tarreau12785782012-04-27 21:37:17 +02001115static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001116{
1117 int i;
1118
Willy Tarreau342acb42012-04-23 22:03:39 +02001119 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001120 return 0;
1121
Willy Tarreau342acb42012-04-23 22:03:39 +02001122 for (i = 0; i < smp->data.str.len; i++) {
1123 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1124 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001125 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001126 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +01001127 return 1;
1128}
1129
Willy Tarreauf9954102012-04-20 14:03:29 +02001130/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +02001131static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001132{
Willy Tarreau342acb42012-04-23 22:03:39 +02001133 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001134 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001135 return 1;
1136}
1137
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001138/************************************************************************/
1139/* All supported sample fetch functions must be declared here */
1140/************************************************************************/
1141
1142/* force TRUE to be returned at the fetch level */
1143static int
1144smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001145 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001146{
1147 smp->type = SMP_T_BOOL;
1148 smp->data.uint = 1;
1149 return 1;
1150}
1151
1152/* force FALSE to be returned at the fetch level */
1153static int
1154smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001155 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001156{
1157 smp->type = SMP_T_BOOL;
1158 smp->data.uint = 0;
1159 return 1;
1160}
1161
1162/* retrieve environment variable $1 as a string */
1163static int
1164smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001165 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001166{
1167 char *env;
1168
1169 if (!args || args[0].type != ARGT_STR)
1170 return 0;
1171
1172 env = getenv(args[0].data.str.str);
1173 if (!env)
1174 return 0;
1175
1176 smp->type = SMP_T_CSTR;
1177 smp->data.str.str = env;
1178 smp->data.str.len = strlen(env);
1179 return 1;
1180}
1181
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001182/* retrieve the current local date in epoch time, and applies an optional offset
1183 * of args[0] seconds.
1184 */
1185static int
1186smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02001187 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001188{
1189 smp->data.uint = date.tv_sec;
1190
1191 /* add offset */
1192 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
1193 smp->data.uint += args[0].data.sint;
1194
1195 smp->type = SMP_T_UINT;
1196 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
1197 return 1;
1198}
1199
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001200/* Note: must not be declared <const> as its list will be overwritten.
1201 * Note: fetches that may return multiple types must be declared as the lowest
1202 * common denominator, the type that can be casted into all other ones. For
1203 * instance IPv4/IPv6 must be declared IPv4.
1204 */
1205static struct sample_fetch_kw_list smp_kws = {ILH, {
1206 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
1207 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
1208 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02001209 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001210 { /* END */ },
1211}};
1212
Emeric Brun107ca302010-01-04 16:16:05 +01001213/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001214static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau12785782012-04-27 21:37:17 +02001215 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
1216 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
1217 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001218 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01001219}};
1220
1221__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02001222static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01001223{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02001224 /* register sample fetch and format conversion keywords */
1225 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02001226 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01001227}