blob: fd18934e9050b7b5cbe928398bc14a485dbe2adf [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
Willy Tarreau80aca902013-01-07 15:42:20 +0100269/* fill the trash with a comma-delimited list of source names for the <use> bit
270 * field which must be composed of a non-null set of SMP_USE_* flags. The return
271 * value is the pointer to the string in the trash buffer.
272 */
273const char *sample_src_names(unsigned int use)
274{
275 int bit;
276
277 trash.len = 0;
278 trash.str[0] = '\0';
279 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
280 if (!(use & ~((1 << bit) - 1)))
281 break; /* no more bits */
282
283 if (!(use & (1 << bit)))
284 continue; /* bit not set */
285
286 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
287 (use & ((1 << bit) - 1)) ? "," : "",
288 fetch_src_names[bit]);
289 }
290 return trash.str;
291}
292
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100293/* return a pointer to the correct sample checkpoint name, or "unknown" when
294 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
295 * if set.
296 */
297const char *sample_ckp_names(unsigned int use)
298{
299 int bit;
300
301 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
302 if (use & (1 << bit))
303 return fetch_ckp_names[bit];
304 return "unknown sample check place, please report this bug";
305}
306
Emeric Brun107ca302010-01-04 16:16:05 +0100307/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100308 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
309 * for next parsing sessions. The fetch keywords capabilities are also computed
310 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100311 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100312void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100313{
Willy Tarreau80aca902013-01-07 15:42:20 +0100314 struct sample_fetch *sf;
315 int bit;
316
317 for (sf = kwl->kw; sf->kw != NULL; sf++) {
318 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
319 if (sf->use & (1 << bit))
320 sf->val |= fetch_cap[bit];
321 }
322 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100323}
324
325/*
Willy Tarreau12785782012-04-27 21:37:17 +0200326 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100327 * parsing sessions.
328 */
Willy Tarreau12785782012-04-27 21:37:17 +0200329void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100330{
Willy Tarreau12785782012-04-27 21:37:17 +0200331 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100332}
333
334/*
Willy Tarreau12785782012-04-27 21:37:17 +0200335 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100336 * string of <len> in buffer <kw>.
337 *
338 */
Willy Tarreau12785782012-04-27 21:37:17 +0200339struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100340{
341 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200342 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100343
Willy Tarreau12785782012-04-27 21:37:17 +0200344 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100345 for (index = 0; kwl->kw[index].kw != NULL; index++) {
346 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
347 kwl->kw[index].kw[len] == '\0')
348 return &kwl->kw[index];
349 }
350 }
351 return NULL;
352}
353
354/*
Willy Tarreau12785782012-04-27 21:37:17 +0200355 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100356 * string of <len> in buffer <kw>.
357 *
358 */
Willy Tarreau12785782012-04-27 21:37:17 +0200359struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100360{
361 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200362 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100363
Willy Tarreau12785782012-04-27 21:37:17 +0200364 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100365 for (index = 0; kwl->kw[index].kw != NULL; index++) {
366 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
367 kwl->kw[index].kw[len] == '\0')
368 return &kwl->kw[index];
369 }
370 }
371 return NULL;
372}
373
Emeric Brun107ca302010-01-04 16:16:05 +0100374/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200375/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200376/* Note: these functions do *NOT* set the output type on the */
377/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100378/******************************************************************/
379
Willy Tarreau342acb42012-04-23 22:03:39 +0200380static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100381{
Willy Tarreau342acb42012-04-23 22:03:39 +0200382 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Emeric Brun107ca302010-01-04 16:16:05 +0100383 return 1;
384}
385
Willy Tarreau342acb42012-04-23 22:03:39 +0200386static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100387{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100388 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100389
Willy Tarreau342acb42012-04-23 22:03:39 +0200390 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100391 return 0;
392
393 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200394 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100395
396 return 1;
397}
398
Willy Tarreau342acb42012-04-23 22:03:39 +0200399static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100400{
Willy Tarreau342acb42012-04-23 22:03:39 +0200401 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
David du Colombier4f92d322011-03-24 11:09:31 +0100402 return 1;
403}
404
Willy Tarreau342acb42012-04-23 22:03:39 +0200405static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100406{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100407 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100408
Willy Tarreau342acb42012-04-23 22:03:39 +0200409 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100410 return 0;
411
412 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200413 smp->data.str = *trash;
David du Colombier4f92d322011-03-24 11:09:31 +0100414 return 1;
415}
416
417/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200418static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100419{
Willy Tarreau342acb42012-04-23 22:03:39 +0200420 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100421}
422*/
423
Willy Tarreau342acb42012-04-23 22:03:39 +0200424static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100425{
Willy Tarreau342acb42012-04-23 22:03:39 +0200426 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Emeric Brun107ca302010-01-04 16:16:05 +0100427 return 1;
428}
429
Willy Tarreau342acb42012-04-23 22:03:39 +0200430static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100431{
Willy Tarreau342acb42012-04-23 22:03:39 +0200432 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100433 return 0;
434 return 1;
435}
436
Willy Tarreau342acb42012-04-23 22:03:39 +0200437static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100438{
Willy Tarreau342acb42012-04-23 22:03:39 +0200439 return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100440}
441
Emeric Brun8ac33d92012-10-17 13:36:06 +0200442static int c_bin2str(struct sample *smp)
443{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100444 struct chunk *trash = get_trash_chunk();
Emeric Brun8ac33d92012-10-17 13:36:06 +0200445 unsigned char c;
446 int ptr = 0;
447
448 trash->len = 0;
449 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
450 c = smp->data.str.str[ptr++];
451 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
452 trash->str[trash->len++] = hextab[c & 0xF];
453 }
454 smp->data.str = *trash;
455 return 1;
456}
457
Willy Tarreau342acb42012-04-23 22:03:39 +0200458static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100459{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100460 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100461 char *pos;
462
Willy Tarreau342acb42012-04-23 22:03:39 +0200463 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100464
465 if (!pos)
466 return 0;
467
Emeric Brun485479d2010-09-23 18:02:19 +0200468 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100469 trash->str = pos;
470 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200471 smp->data.str = *trash;
Emeric Brun107ca302010-01-04 16:16:05 +0100472 return 1;
473}
474
Willy Tarreau342acb42012-04-23 22:03:39 +0200475static int c_datadup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200476{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100477 struct chunk *trash = get_trash_chunk();
Emeric Brun485479d2010-09-23 18:02:19 +0200478
Willy Tarreau342acb42012-04-23 22:03:39 +0200479 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
480 memcpy(trash->str, smp->data.str.str, trash->len);
481 smp->data.str = *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200482 return 1;
483}
484
485
Willy Tarreau342acb42012-04-23 22:03:39 +0200486static int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100487{
488 return 1;
489}
490
Willy Tarreau342acb42012-04-23 22:03:39 +0200491static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100492{
493 int i;
494 uint32_t ret = 0;
495
Willy Tarreau342acb42012-04-23 22:03:39 +0200496 for (i = 0; i < smp->data.str.len; i++) {
497 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100498
499 if (val > 9)
500 break;
501
502 ret = ret * 10 + val;
503 }
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505 smp->data.uint = ret;
Emeric Brun107ca302010-01-04 16:16:05 +0100506 return 1;
507}
508
509/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200510/* Sample casts matrix: */
511/* sample_casts[from type][to type] */
512/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100513/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau12785782012-04-27 21:37:17 +0200515typedef int (*sample_cast_fct)(struct sample *smp);
516static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Willy Tarreau422aa072012-04-20 20:49:27 +0200517/* to: BOOL UINT SINT IPV4 IPV6 STR BIN CSTR CBIN */
Willy Tarreaud167e6d2012-12-21 17:38:05 +0100518/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, c_int2str, NULL, c_int2str, NULL },
Willy Tarreau422aa072012-04-20 20:49:27 +0200519/* UINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
520/* SINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL },
521/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL },
522/* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL },
523/* 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 +0200524/* BIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none },
Willy Tarreau422aa072012-04-20 20:49:27 +0200525/* 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 +0200526/* CBIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200527};
Emeric Brun107ca302010-01-04 16:16:05 +0100528
Emeric Brun107ca302010-01-04 16:16:05 +0100529/*
Willy Tarreau12785782012-04-27 21:37:17 +0200530 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100531 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200532 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200533 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100534 */
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200535struct 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 +0100536{
537 const char *endw;
538 const char *end;
Willy Tarreau12785782012-04-27 21:37:17 +0200539 struct sample_expr *expr;
540 struct sample_fetch *fetch;
541 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100542 unsigned long prev_type;
Emeric Brun485479d2010-09-23 18:02:19 +0200543 char *p;
Emeric Brun107ca302010-01-04 16:16:05 +0100544
Emeric Brun485479d2010-09-23 18:02:19 +0200545 snprintf(err, err_size, "memory error.");
546 if (!str[*idx]) {
547
548 snprintf(err, err_size, "missing fetch method.");
Emeric Brun107ca302010-01-04 16:16:05 +0100549 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200550 }
Emeric Brun107ca302010-01-04 16:16:05 +0100551
552 end = str[*idx] + strlen(str[*idx]);
553 endw = strchr(str[*idx], '(');
554
555 if (!endw)
556 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200557 else if ((end-1)[0] != ')') {
558 p = my_strndup(str[*idx], endw - str[*idx]);
559 if (p) {
560 snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p);
561 free(p);
562 }
Emeric Brun107ca302010-01-04 16:16:05 +0100563 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200564 }
Emeric Brun107ca302010-01-04 16:16:05 +0100565
Willy Tarreau12785782012-04-27 21:37:17 +0200566 fetch = find_sample_fetch(str[*idx], endw - str[*idx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200567 if (!fetch) {
568 p = my_strndup(str[*idx], endw - str[*idx]);
569 if (p) {
570 snprintf(err, err_size, "unknown fetch method '%s'.", p);
571 free(p);
572 }
Emeric Brun107ca302010-01-04 16:16:05 +0100573 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200574 }
Willy Tarreau422aa072012-04-20 20:49:27 +0200575 if (fetch->out_type >= SMP_TYPES) {
Emeric Brun107ca302010-01-04 16:16:05 +0100576
Emeric Brun485479d2010-09-23 18:02:19 +0200577 p = my_strndup(str[*idx], endw - str[*idx]);
578 if (p) {
579 snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p);
580 free(p);
581 }
Emeric Brun107ca302010-01-04 16:16:05 +0100582 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200583 }
Emeric Brun107ca302010-01-04 16:16:05 +0100584
585 prev_type = fetch->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200586 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200587 if (!expr)
588 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100589
590 LIST_INIT(&(expr->conv_exprs));
591 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200592 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100593
594 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200595 char *err_msg = NULL;
596 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200597
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200598 if (!fetch->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200599 p = my_strndup(str[*idx], endw - str[*idx]);
600 if (p) {
601 snprintf(err, err_size, "fetch method '%s' does not support any args.", p);
602 free(p);
603 }
604 goto out_error;
605 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200606
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200607 al->kw = expr->fetch->kw;
608 al->conv = NULL;
609 if (make_arg_list(endw + 1, end - endw - 2, fetch->arg_mask, &expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
Emeric Brun485479d2010-09-23 18:02:19 +0200610 p = my_strndup(str[*idx], endw - str[*idx]);
611 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200612 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 +0200613 free(p);
614 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200615 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200616 goto out_error;
617 }
618
Willy Tarreau2e845be2012-10-19 19:49:09 +0200619 if (!expr->arg_p)
620 expr->arg_p = empty_arg_list;
621
Willy Tarreau21d68a62012-04-20 15:52:36 +0200622 if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) {
623 p = my_strndup(str[*idx], endw - str[*idx]);
624 if (p) {
625 snprintf(err, err_size, "invalid args in fetch method '%s' : %s.", p, err_msg);
626 free(p);
627 }
628 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200629 goto out_error;
630 }
631 }
Willy Tarreauf22a5082012-10-19 16:47:23 +0200632 else if (ARGM(fetch->arg_mask)) {
Emeric Brun485479d2010-09-23 18:02:19 +0200633 p = my_strndup(str[*idx], endw - str[*idx]);
634 if (p) {
635 snprintf(err, err_size, "missing args for fetch method '%s'.", p);
636 free(p);
637 }
638 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100639 }
640
641 for (*idx += 1; *(str[*idx]); (*idx)++) {
Willy Tarreau12785782012-04-27 21:37:17 +0200642 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100643
644 end = str[*idx] + strlen(str[*idx]);
645 endw = strchr(str[*idx], '(');
646
647 if (!endw)
648 endw = end;
Emeric Brun485479d2010-09-23 18:02:19 +0200649 else if ((end-1)[0] != ')') {
650 p = my_strndup(str[*idx], endw - str[*idx]);
651 if (p) {
652 snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p);
653 free(p);
654 }
Emeric Brun107ca302010-01-04 16:16:05 +0100655 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200656 }
Emeric Brun107ca302010-01-04 16:16:05 +0100657
Willy Tarreau12785782012-04-27 21:37:17 +0200658 conv = find_sample_conv(str[*idx], endw - str[*idx]);
Emeric Brun107ca302010-01-04 16:16:05 +0100659 if (!conv)
660 break;
661
Willy Tarreau422aa072012-04-20 20:49:27 +0200662 if (conv->in_type >= SMP_TYPES ||
663 conv->out_type >= SMP_TYPES) {
Emeric Brun485479d2010-09-23 18:02:19 +0200664 p = my_strndup(str[*idx], endw - str[*idx]);
665 if (p) {
666 snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p);
667 free(p);
668 }
Emeric Brun107ca302010-01-04 16:16:05 +0100669 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200670 }
Emeric Brun107ca302010-01-04 16:16:05 +0100671
672 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200673 if (!sample_casts[prev_type][conv->in_type]) {
Emeric Brun485479d2010-09-23 18:02:19 +0200674 p = my_strndup(str[*idx], endw - str[*idx]);
675 if (p) {
676 snprintf(err, err_size, "conv method '%s' cannot be applied.", p);
677 free(p);
678 }
Emeric Brun107ca302010-01-04 16:16:05 +0100679 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200680 }
Emeric Brun107ca302010-01-04 16:16:05 +0100681
682 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200683 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200684 if (!conv_expr)
685 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100686
687 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
688 conv_expr->conv = conv;
689
690 if (end != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200691 char *err_msg = NULL;
692 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200693
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200694 if (!conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200695 p = my_strndup(str[*idx], endw - str[*idx]);
696
697 if (p) {
698 snprintf(err, err_size, "conv method '%s' does not support any args.", p);
699 free(p);
700 }
701 goto out_error;
702 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100703
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200704 al->kw = expr->fetch->kw;
705 al->conv = conv_expr->conv->kw;
706 if (make_arg_list(endw + 1, end - endw - 2, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg, al) < 0) {
Emeric Brun485479d2010-09-23 18:02:19 +0200707 p = my_strndup(str[*idx], endw - str[*idx]);
708 if (p) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200709 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 +0200710 free(p);
711 }
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200712 free(err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200713 goto out_error;
714 }
715
Willy Tarreau2e845be2012-10-19 19:49:09 +0200716 if (!conv_expr->arg_p)
717 conv_expr->arg_p = empty_arg_list;
718
Willy Tarreau21d68a62012-04-20 15:52:36 +0200719 if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) {
720 p = my_strndup(str[*idx], endw - str[*idx]);
721 if (p) {
722 snprintf(err, err_size, "invalid args in conv method '%s' : %s.", p, err_msg);
723 free(p);
724 }
725 free(err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200726 goto out_error;
727 }
728 }
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200729 else if (conv->arg_mask) {
Emeric Brun485479d2010-09-23 18:02:19 +0200730 p = my_strndup(str[*idx], endw - str[*idx]);
731 if (p) {
732 snprintf(err, err_size, "missing args for conv method '%s'.", p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100733 free(p);
Willy Tarreau9e92d322010-01-26 17:58:06 +0100734 }
Emeric Brun485479d2010-09-23 18:02:19 +0200735 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100736 }
Emeric Brun485479d2010-09-23 18:02:19 +0200737
Emeric Brun107ca302010-01-04 16:16:05 +0100738 }
Emeric Brun485479d2010-09-23 18:02:19 +0200739
Emeric Brun107ca302010-01-04 16:16:05 +0100740 return expr;
741
742out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200743 /* TODO: prune_sample_expr(expr); */
Emeric Brun107ca302010-01-04 16:16:05 +0100744 return NULL;
745}
746
747/*
Willy Tarreau12785782012-04-27 21:37:17 +0200748 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200749 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200750 * Returns a pointer on a typed sample structure containing the result or NULL if
751 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100752 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200753 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200754 *
755 * Note: the fetch functions are required to properly set the return type. The
756 * conversion functions must do so too. However the cast functions do not need
757 * to since they're made to cast mutiple types according to what is required.
Emeric Brun107ca302010-01-04 16:16:05 +0100758 */
Willy Tarreau12785782012-04-27 21:37:17 +0200759struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
760 unsigned int opt,
761 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100762{
Willy Tarreau12785782012-04-27 21:37:17 +0200763 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100764
765 if (p == NULL)
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200766 p = &temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +0100767
Willy Tarreau342acb42012-04-23 22:03:39 +0200768 p->flags = 0;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200769 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100770 return NULL;
771
Willy Tarreau40aebd92012-04-26 11:05:50 +0200772 if (p->flags & SMP_F_MAY_CHANGE)
Willy Tarreau12785782012-04-27 21:37:17 +0200773 return NULL; /* we can only use stable samples */
Willy Tarreau40aebd92012-04-26 11:05:50 +0200774
Emeric Brun107ca302010-01-04 16:16:05 +0100775 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200776 /* we want to ensure that p->type can be casted into
777 * conv_expr->conv->in_type. We have 3 possibilities :
778 * - NULL => not castable.
779 * - c_none => nothing to do (let's optimize it)
780 * - other => apply cast and prepare to fail
781 */
Willy Tarreau12785782012-04-27 21:37:17 +0200782 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +0200783 return NULL;
784
Willy Tarreau12785782012-04-27 21:37:17 +0200785 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
786 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +0100787 return NULL;
788
Willy Tarreau12e50112012-04-25 17:21:49 +0200789 /* OK cast succeeded */
790
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200791 /* force the output type after a cast */
Emeric Brun107ca302010-01-04 16:16:05 +0100792 p->type = conv_expr->conv->in_type;
Willy Tarreau342acb42012-04-23 22:03:39 +0200793 if (!conv_expr->conv->process(conv_expr->arg_p, p))
Emeric Brun107ca302010-01-04 16:16:05 +0100794 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +0100795 }
796 return p;
797}
798
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +0100799/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200800 * Resolve all remaining arguments in proxy <p>. Returns the number of
801 * errors or 0 if everything is fine.
802 */
803int smp_resolve_args(struct proxy *p)
804{
805 struct arg_list *cur, *bak;
806 const char *ctx, *where;
807 const char *conv_ctx, *conv_pre, *conv_pos;
808 struct userlist *ul;
809 struct arg *arg;
810 int cfgerr = 0;
811
812 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
813 struct proxy *px;
814 struct server *srv;
815 char *pname, *sname;
816
817 arg = cur->arg;
818
819 /* prepare output messages */
820 conv_pre = conv_pos = conv_ctx = "";
821 if (cur->conv) {
822 conv_ctx = cur->conv;
823 conv_pre = "conversion keyword '";
824 conv_pos = "' for ";
825 }
826
827 where = "in";
828 ctx = "sample fetch keyword";
829 switch (cur->ctx) {
830 case ARGC_STK:where = "in stick rule in"; break;
831 case ARGC_TRK: where = "in tracking rule in"; break;
832 case ARGC_LOG: where = "in log-format string in"; break;
833 case ARGC_HDR: where = "in HTTP header format string in"; break;
834 case ARGC_UIF: where = "in unique-id-format string in"; break;
835 case ARGC_ACL: ctx = "ACL keyword"; break;
836 }
837
838 /* set a few default settings */
839 px = p;
840 pname = p->id;
841
842 switch (arg->type) {
843 case ARGT_SRV:
844 if (!arg->data.str.len) {
845 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
846 cur->file, cur->line,
847 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
848 cfgerr++;
849 continue;
850 }
851
852 /* we support two formats : "bck/srv" and "srv" */
853 sname = strrchr(arg->data.str.str, '/');
854
855 if (sname) {
856 *sname++ = '\0';
857 pname = arg->data.str.str;
858
859 px = findproxy(pname, PR_CAP_BE);
860 if (!px) {
861 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
862 cur->file, cur->line, pname,
863 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
864 cfgerr++;
865 break;
866 }
867 }
868 else
869 sname = arg->data.str.str;
870
871 srv = findserver(px, sname);
872 if (!srv) {
873 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",
874 cur->file, cur->line, sname, pname,
875 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
876 cfgerr++;
877 break;
878 }
879
880 free(arg->data.str.str);
881 arg->data.str.str = NULL;
882 arg->unresolved = 0;
883 arg->data.srv = srv;
884 break;
885
886 case ARGT_FE:
887 if (arg->data.str.len) {
888 pname = arg->data.str.str;
889 px = findproxy(pname, PR_CAP_FE);
890 }
891
892 if (!px) {
893 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
894 cur->file, cur->line, pname,
895 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
896 cfgerr++;
897 break;
898 }
899
900 if (!(px->cap & PR_CAP_FE)) {
901 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
902 cur->file, cur->line, pname,
903 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
904 cfgerr++;
905 break;
906 }
907
908 free(arg->data.str.str);
909 arg->data.str.str = NULL;
910 arg->unresolved = 0;
911 arg->data.prx = px;
912 break;
913
914 case ARGT_BE:
915 if (arg->data.str.len) {
916 pname = arg->data.str.str;
917 px = findproxy(pname, PR_CAP_BE);
918 }
919
920 if (!px) {
921 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
922 cur->file, cur->line, pname,
923 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
924 cfgerr++;
925 break;
926 }
927
928 if (!(px->cap & PR_CAP_BE)) {
929 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
930 cur->file, cur->line, pname,
931 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
932 cfgerr++;
933 break;
934 }
935
936 free(arg->data.str.str);
937 arg->data.str.str = NULL;
938 arg->unresolved = 0;
939 arg->data.prx = px;
940 break;
941
942 case ARGT_TAB:
943 if (arg->data.str.len) {
944 pname = arg->data.str.str;
945 px = find_stktable(pname);
946 }
947
948 if (!px) {
949 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
950 cur->file, cur->line, pname,
951 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
952 cfgerr++;
953 break;
954 }
955
956 if (!px->table.size) {
957 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
958 cur->file, cur->line, pname,
959 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
960 cfgerr++;
961 break;
962 }
963
964 free(arg->data.str.str);
965 arg->data.str.str = NULL;
966 arg->unresolved = 0;
967 arg->data.prx = px;
968 break;
969
970 case ARGT_USR:
971 if (!arg->data.str.len) {
972 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
973 cur->file, cur->line,
974 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
975 cfgerr++;
976 break;
977 }
978
979 if (p->uri_auth && p->uri_auth->userlist &&
980 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
981 ul = p->uri_auth->userlist;
982 else
983 ul = auth_find_userlist(arg->data.str.str);
984
985 if (!ul) {
986 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
987 cur->file, cur->line, arg->data.str.str,
988 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
989 cfgerr++;
990 break;
991 }
992
993 free(arg->data.str.str);
994 arg->data.str.str = NULL;
995 arg->unresolved = 0;
996 arg->data.usr = ul;
997 break;
998 }
999
1000 LIST_DEL(&cur->list);
1001 free(cur);
1002 } /* end of args processing */
1003
1004 return cfgerr;
1005}
1006
1007/*
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001008 * Process a fetch + format conversion as defined by the sample expression <expr>
1009 * on request or response considering the <opt> parameter. The output is always of
1010 * type string. Returns either NULL if no sample could be extracted, or a pointer
1011 * to the converted result stored in static temp_smp in format string.
1012 */
1013struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1014 unsigned int opt, struct sample_expr *expr)
1015{
1016 struct sample *smp;
1017
1018 smp = sample_process(px, l4, l7, opt, expr, NULL);
1019 if (!smp)
1020 return NULL;
1021
1022 if (!sample_casts[smp->type][SMP_T_CSTR])
1023 return NULL;
1024
1025 if (!sample_casts[smp->type][SMP_T_CSTR](smp))
1026 return NULL;
1027
1028 smp->type = SMP_T_CSTR;
1029 return smp;
1030}
1031
Emeric Brun107ca302010-01-04 16:16:05 +01001032/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001033/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001034/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001035/*****************************************************************/
1036
Willy Tarreau12785782012-04-27 21:37:17 +02001037static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001038{
1039 int i;
1040
Willy Tarreau342acb42012-04-23 22:03:39 +02001041 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001042 return 0;
1043
Willy Tarreau342acb42012-04-23 22:03:39 +02001044 for (i = 0; i < smp->data.str.len; i++) {
1045 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1046 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001047 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001048 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +01001049 return 1;
1050}
1051
Willy Tarreau12785782012-04-27 21:37:17 +02001052static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +01001053{
1054 int i;
1055
Willy Tarreau342acb42012-04-23 22:03:39 +02001056 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001057 return 0;
1058
Willy Tarreau342acb42012-04-23 22:03:39 +02001059 for (i = 0; i < smp->data.str.len; i++) {
1060 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1061 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001062 }
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001063 smp->type = SMP_T_STR;
Emeric Brun107ca302010-01-04 16:16:05 +01001064 return 1;
1065}
1066
Willy Tarreauf9954102012-04-20 14:03:29 +02001067/* takes the netmask in arg_p */
Willy Tarreau12785782012-04-27 21:37:17 +02001068static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001069{
Willy Tarreau342acb42012-04-23 22:03:39 +02001070 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001071 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001072 return 1;
1073}
1074
Emeric Brun107ca302010-01-04 16:16:05 +01001075/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02001076static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau12785782012-04-27 21:37:17 +02001077 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
1078 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
1079 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau9fcb9842012-04-20 14:45:49 +02001080 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01001081}};
1082
1083__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02001084static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01001085{
Willy Tarreau12785782012-04-27 21:37:17 +02001086 /* register sample format convert keywords */
1087 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01001088}