blob: 896f6897abd1c7d793963f23b83e05c4a6a1b6e1 [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
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020024#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020025#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026
Willy Tarreau9fcb9842012-04-20 14:45:49 +020027#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020028#include <proto/auth.h>
29#include <proto/log.h>
Thierry FOURNIERd4373142013-12-17 01:10:10 +010030#include <proto/proto_http.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020032#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020033#include <proto/stick_table.h>
Emeric Brun107ca302010-01-04 16:16:05 +010034
Willy Tarreau1cf8f082014-02-07 12:14:54 +010035/* sample type names */
36const char *smp_to_type[SMP_TYPES] = {
37 [SMP_T_BOOL] = "bool",
38 [SMP_T_UINT] = "uint",
39 [SMP_T_SINT] = "sint",
40 [SMP_T_ADDR] = "addr",
41 [SMP_T_IPV4] = "ipv4",
42 [SMP_T_IPV6] = "ipv6",
43 [SMP_T_STR] = "str",
44 [SMP_T_BIN] = "bin",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045};
46
Willy Tarreau12785782012-04-27 21:37:17 +020047/* static sample used in sample_process() when <p> is NULL */
Willy Tarreaub4a88f02012-04-23 21:35:11 +020048static struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010049
Willy Tarreau12785782012-04-27 21:37:17 +020050/* list head of all known sample fetch keywords */
51static struct sample_fetch_kw_list sample_fetches = {
52 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* list head of all known sample format conversion keywords */
56static struct sample_conv_kw_list sample_convs = {
57 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010058};
59
Willy Tarreau80aca902013-01-07 15:42:20 +010060const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
61 [SMP_SRC_INTRN] = (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_LISTN] = (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_FTEND] = (SMP_VAL_FE_CON_ACC | 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_L4CLI] = (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_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | 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_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | 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_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_L6REQ] = (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___________ |
113 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
114 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
115 SMP_VAL___________),
116
117 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | 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_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
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_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
134 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
137 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
138 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
139 SMP_VAL___________),
140
141 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | 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_SERVR] = (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_BE_SRV_CON | 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_L4SRV] = (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_FE_LOG_END),
164
165 [SMP_SRC_L5SRV] = (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_FE_LOG_END),
172
173 [SMP_SRC_L6RES] = (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___________),
180
181 [SMP_SRC_HRSHV] = (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_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | 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_HRSHP] = (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_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL_FE_LOG_END),
196
197 [SMP_SRC_HRSBO] = (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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL___________),
204
205 [SMP_SRC_RQFIN] = (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_RSFIN] = (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 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236};
237
238static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
239 [SMP_SRC_INTRN] = "internal state",
240 [SMP_SRC_LISTN] = "listener",
241 [SMP_SRC_FTEND] = "frontend",
242 [SMP_SRC_L4CLI] = "client address",
243 [SMP_SRC_L5CLI] = "client-side connection",
244 [SMP_SRC_TRACK] = "track counters",
245 [SMP_SRC_L6REQ] = "request buffer",
246 [SMP_SRC_HRQHV] = "HTTP request headers",
247 [SMP_SRC_HRQHP] = "HTTP request",
248 [SMP_SRC_HRQBO] = "HTTP request body",
249 [SMP_SRC_BKEND] = "backend",
250 [SMP_SRC_SERVR] = "server",
251 [SMP_SRC_L4SRV] = "server address",
252 [SMP_SRC_L5SRV] = "server-side connection",
253 [SMP_SRC_L6RES] = "response buffer",
254 [SMP_SRC_HRSHV] = "HTTP response headers",
255 [SMP_SRC_HRSHP] = "HTTP response",
256 [SMP_SRC_HRSBO] = "HTTP response body",
257 [SMP_SRC_RQFIN] = "request buffer statistics",
258 [SMP_SRC_RSFIN] = "response buffer statistics",
259 [SMP_SRC_TXFIN] = "transaction statistics",
260 [SMP_SRC_SSFIN] = "session statistics",
261};
262
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100263static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
264 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
265 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
266 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
267 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
268 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
269 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
270 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
271 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
272 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
273 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
274 [SMP_CKP_BE_SRV_CON] = "server source selection",
275 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
276 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
277 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
278 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
279 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
280 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
281 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
282 [SMP_CKP_FE_LOG_END] = "logs",
283};
284
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100285/* This function returns the type of the data returned by the sample_expr.
286 * It assumes that the <expr> and all of its converters are properly
287 * initialized.
288 */
289inline
290int smp_expr_output_type(struct sample_expr *expr)
291{
292 struct sample_conv_expr *smp_expr;
293
294 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
295 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
296 return smp_expr->conv->out_type;
297 }
298 return expr->fetch->out_type;
299}
300
301
Willy Tarreau80aca902013-01-07 15:42:20 +0100302/* fill the trash with a comma-delimited list of source names for the <use> bit
303 * field which must be composed of a non-null set of SMP_USE_* flags. The return
304 * value is the pointer to the string in the trash buffer.
305 */
306const char *sample_src_names(unsigned int use)
307{
308 int bit;
309
310 trash.len = 0;
311 trash.str[0] = '\0';
312 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
313 if (!(use & ~((1 << bit) - 1)))
314 break; /* no more bits */
315
316 if (!(use & (1 << bit)))
317 continue; /* bit not set */
318
319 trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s",
320 (use & ((1 << bit) - 1)) ? "," : "",
321 fetch_src_names[bit]);
322 }
323 return trash.str;
324}
325
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100326/* return a pointer to the correct sample checkpoint name, or "unknown" when
327 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
328 * if set.
329 */
330const char *sample_ckp_names(unsigned int use)
331{
332 int bit;
333
334 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
335 if (use & (1 << bit))
336 return fetch_ckp_names[bit];
337 return "unknown sample check place, please report this bug";
338}
339
Emeric Brun107ca302010-01-04 16:16:05 +0100340/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100341 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
342 * for next parsing sessions. The fetch keywords capabilities are also computed
343 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100344 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100345void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100346{
Willy Tarreau80aca902013-01-07 15:42:20 +0100347 struct sample_fetch *sf;
348 int bit;
349
350 for (sf = kwl->kw; sf->kw != NULL; sf++) {
351 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
352 if (sf->use & (1 << bit))
353 sf->val |= fetch_cap[bit];
354 }
355 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100356}
357
358/*
Willy Tarreau12785782012-04-27 21:37:17 +0200359 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100360 * parsing sessions.
361 */
Willy Tarreau12785782012-04-27 21:37:17 +0200362void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100363{
Willy Tarreau12785782012-04-27 21:37:17 +0200364 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * string of <len> in buffer <kw>.
370 *
371 */
Willy Tarreau12785782012-04-27 21:37:17 +0200372struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100373{
374 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200375 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100376
Willy Tarreau12785782012-04-27 21:37:17 +0200377 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100378 for (index = 0; kwl->kw[index].kw != NULL; index++) {
379 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
380 kwl->kw[index].kw[len] == '\0')
381 return &kwl->kw[index];
382 }
383 }
384 return NULL;
385}
386
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100387/* This fucntion browse the list of available saple fetch. <current> is
388 * the last used sample fetch. If it is the first call, it must set to NULL.
389 * <idx> is the index of the next sampleèfetch entry. It is used as private
390 * value. It is useles to initiate it.
391 *
392 * It returns always the newt fetch_sample entry, and NULL when the end of
393 * the list is reached.
394 */
395struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
396{
397 struct sample_fetch_kw_list *kwl;
398 struct sample_fetch *base;
399
400 if (!current) {
401 /* Get first kwl entry. */
402 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
403 (*idx) = 0;
404 } else {
405 /* Get kwl corresponding to the curret entry. */
406 base = current + 1 - (*idx);
407 kwl = container_of(base, struct sample_fetch_kw_list, kw);
408 }
409
410 while (1) {
411
412 /* Check if kwl is the last entry. */
413 if (&kwl->list == &sample_fetches.list)
414 return NULL;
415
416 /* idx contain the next keyword. If it is available, return it. */
417 if (kwl->kw[*idx].kw) {
418 (*idx)++;
419 return &kwl->kw[(*idx)-1];
420 }
421
422 /* get next entry in the main list, and return NULL if the end is reached. */
423 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
424
425 /* Set index to 0, ans do one other loop. */
426 (*idx) = 0;
427 }
428}
429
Emeric Brun107ca302010-01-04 16:16:05 +0100430/*
Willy Tarreau12785782012-04-27 21:37:17 +0200431 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100432 * string of <len> in buffer <kw>.
433 *
434 */
Willy Tarreau12785782012-04-27 21:37:17 +0200435struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100436{
437 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200438 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100439
Willy Tarreau12785782012-04-27 21:37:17 +0200440 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100441 for (index = 0; kwl->kw[index].kw != NULL; index++) {
442 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
443 kwl->kw[index].kw[len] == '\0')
444 return &kwl->kw[index];
445 }
446 }
447 return NULL;
448}
449
Emeric Brun107ca302010-01-04 16:16:05 +0100450/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200451/* Sample casts functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200452/* Note: these functions do *NOT* set the output type on the */
453/* sample, the caller is responsible for doing this on return. */
Emeric Brun107ca302010-01-04 16:16:05 +0100454/******************************************************************/
455
Willy Tarreau342acb42012-04-23 22:03:39 +0200456static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100457{
Willy Tarreau342acb42012-04-23 22:03:39 +0200458 smp->data.uint = ntohl(smp->data.ipv4.s_addr);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100459 smp->type = SMP_T_UINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100460 return 1;
461}
462
Willy Tarreau342acb42012-04-23 22:03:39 +0200463static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100464{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100465 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100466
Willy Tarreau342acb42012-04-23 22:03:39 +0200467 if (!inet_ntop(AF_INET, (void *)&smp->data.ipv4, trash->str, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100468 return 0;
469
470 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200471 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100472 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100473 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100474
475 return 1;
476}
477
Willy Tarreau342acb42012-04-23 22:03:39 +0200478static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100479{
Willy Tarreau342acb42012-04-23 22:03:39 +0200480 v4tov6(&smp->data.ipv6, &smp->data.ipv4);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100481 smp->type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100482 return 1;
483}
484
Willy Tarreau342acb42012-04-23 22:03:39 +0200485static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100486{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100487 struct chunk *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100488
Willy Tarreau342acb42012-04-23 22:03:39 +0200489 if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100490 return 0;
491
492 trash->len = strlen(trash->str);
Willy Tarreau342acb42012-04-23 22:03:39 +0200493 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100494 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100495 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100496 return 1;
497}
498
499/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200500static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100501{
Willy Tarreau342acb42012-04-23 22:03:39 +0200502 return v6tov4(&smp->data.ipv4, &smp->data.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100503}
504*/
505
Willy Tarreau342acb42012-04-23 22:03:39 +0200506static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100507{
Willy Tarreau342acb42012-04-23 22:03:39 +0200508 smp->data.ipv4.s_addr = htonl(smp->data.uint);
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100509 smp->type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100510 return 1;
511}
512
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100513static int c_str2addr(struct sample *smp)
514{
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100515 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) {
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100516 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
517 return 0;
518 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100519 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100520 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100521 }
522 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100523 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100524 return 1;
525}
526
Willy Tarreau342acb42012-04-23 22:03:39 +0200527static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100528{
Willy Tarreau342acb42012-04-23 22:03:39 +0200529 if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100530 return 0;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100531 smp->type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100532 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100533 return 1;
534}
535
Willy Tarreau342acb42012-04-23 22:03:39 +0200536static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100537{
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100538 if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6))
539 return 0;
540 smp->type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100541 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100542 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100543}
544
Emeric Brun4b9e8022014-11-03 18:17:10 +0100545/*
546 * The NULL char always enforces the end of string if it is met.
547 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100548 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200549static int c_bin2str(struct sample *smp)
550{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100551 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200552
Emeric Brun4b9e8022014-11-03 18:17:10 +0100553 for (i = 0; i < smp->data.str.len; i++) {
554 if (!smp->data.str.str[i]) {
555 smp->data.str.len = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100556 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100557 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200558 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200559 return 1;
560}
561
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100563{
Willy Tarreau47ca5452012-12-23 20:22:19 +0100564 struct chunk *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100565 char *pos;
566
Willy Tarreau342acb42012-04-23 22:03:39 +0200567 pos = ultoa_r(smp->data.uint, trash->str, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100568
569 if (!pos)
570 return 0;
571
Emeric Brun485479d2010-09-23 18:02:19 +0200572 trash->size = trash->size - (pos - trash->str);
Emeric Brun107ca302010-01-04 16:16:05 +0100573 trash->str = pos;
574 trash->len = strlen(pos);
Willy Tarreau342acb42012-04-23 22:03:39 +0200575 smp->data.str = *trash;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100576 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100577 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100578 return 1;
579}
580
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100581/* This function duplicates data and removes the flag "const". */
582int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200583{
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100584 struct chunk *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200585
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100586 /* If the const flag is not set, we don't need to duplicate the
587 * pattern as it can be modified in place.
588 */
589 if (!(smp->flags & SMP_F_CONST))
590 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100591
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100592 switch (smp->type) {
593 case SMP_T_BOOL:
594 case SMP_T_UINT:
595 case SMP_T_SINT:
596 case SMP_T_ADDR:
597 case SMP_T_IPV4:
598 case SMP_T_IPV6:
599 /* These type are not const. */
600 break;
601 case SMP_T_STR:
602 case SMP_T_BIN:
603 /* Duplicate data. */
604 trash = get_trash_chunk();
605 trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size;
606 memcpy(trash->str, smp->data.str.str, trash->len);
607 smp->data.str = *trash;
608 break;
609 default:
610 /* Other cases are unexpected. */
611 return 0;
612 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100613
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100614 /* remove const flag */
615 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200616 return 1;
617}
618
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100619int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100620{
621 return 1;
622}
623
Willy Tarreau342acb42012-04-23 22:03:39 +0200624static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100625{
626 int i;
627 uint32_t ret = 0;
628
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100629 if (smp->data.str.len == 0)
630 return 0;
631
Willy Tarreau342acb42012-04-23 22:03:39 +0200632 for (i = 0; i < smp->data.str.len; i++) {
633 uint32_t val = smp->data.str.str[i] - '0';
Emeric Brun107ca302010-01-04 16:16:05 +0100634
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100635 if (val > 9) {
636 if (i == 0)
637 return 0;
Emeric Brun107ca302010-01-04 16:16:05 +0100638 break;
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100639 }
Emeric Brun107ca302010-01-04 16:16:05 +0100640
641 ret = ret * 10 + val;
642 }
643
Willy Tarreau342acb42012-04-23 22:03:39 +0200644 smp->data.uint = ret;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100645 smp->type = SMP_T_UINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100646 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100647 return 1;
648}
649
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100650static int c_str2meth(struct sample *smp)
651{
652 enum http_meth_t meth;
653 int len;
654
655 meth = find_http_meth(smp->data.str.str, smp->data.str.len);
656 if (meth == HTTP_METH_OTHER) {
657 len = smp->data.str.len;
658 smp->data.meth.str.str = smp->data.str.str;
659 smp->data.meth.str.len = len;
660 }
661 else
662 smp->flags &= ~SMP_F_CONST;
663 smp->data.meth.meth = meth;
664 smp->type = SMP_T_METH;
665 return 1;
666}
667
668static int c_meth2str(struct sample *smp)
669{
670 int len;
671 enum http_meth_t meth;
672
673 if (smp->data.meth.meth == HTTP_METH_OTHER) {
674 /* The method is unknown. Copy the original pointer. */
675 len = smp->data.meth.str.len;
676 smp->data.str.str = smp->data.meth.str.str;
677 smp->data.str.len = len;
678 smp->type = SMP_T_STR;
679 }
680 else if (smp->data.meth.meth < HTTP_METH_OTHER) {
681 /* The method is known, copy the pointer containing the string. */
682 meth = smp->data.meth.meth;
683 smp->data.str.str = http_known_methods[meth].name;
684 smp->data.str.len = http_known_methods[meth].len;
685 smp->flags |= SMP_F_CONST;
686 smp->type = SMP_T_STR;
687 }
688 else {
689 /* Unknown method */
690 return 0;
691 }
692 return 1;
693}
694
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200695static int c_addr2bin(struct sample *smp)
696{
697 struct chunk *chk = get_trash_chunk();
698
699 if (smp->type == SMP_T_IPV4) {
700 chk->len = 4;
701 memcpy(chk->str, &smp->data.ipv4, chk->len);
702 }
703 else if (smp->type == SMP_T_IPV6) {
704 chk->len = 16;
705 memcpy(chk->str, &smp->data.ipv6, chk->len);
706 }
707 else
708 return 0;
709
710 smp->data.str = *chk;
711 smp->type = SMP_T_BIN;
712 return 1;
713}
714
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200715static int c_int2bin(struct sample *smp)
716{
717 struct chunk *chk = get_trash_chunk();
718
719 *(unsigned int *)chk->str = htonl(smp->data.uint);
720 chk->len = 4;
721
722 smp->data.str = *chk;
723 smp->type = SMP_T_BIN;
724 return 1;
725}
726
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200727
Emeric Brun107ca302010-01-04 16:16:05 +0100728/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200729/* Sample casts matrix: */
730/* sample_casts[from type][to type] */
731/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100732/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100733
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100734sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735/* to: BOOL UINT SINT ADDR IPV4 IPV6 STR BIN METH */
736/* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200737/* UINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
738/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739/* ADDR */ { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200740/* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
741/* IPV6 */ { NULL, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100742/* STR */ { c_str2int, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
743/* BIN */ { NULL, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
744/* METH */ { NULL, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, },
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200745};
Emeric Brun107ca302010-01-04 16:16:05 +0100746
Emeric Brun107ca302010-01-04 16:16:05 +0100747/*
Willy Tarreau12785782012-04-27 21:37:17 +0200748 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100749 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200750 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200751 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100752 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100753struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100754{
Willy Tarreau833cc792013-07-24 15:34:19 +0200755 const char *begw; /* beginning of word */
756 const char *endw; /* end of word */
757 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200758 struct sample_expr *expr;
759 struct sample_fetch *fetch;
760 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100761 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200762 char *fkw = NULL;
763 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100764 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100765
Willy Tarreau833cc792013-07-24 15:34:19 +0200766 begw = str[*idx];
767 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
768
769 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100770 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100771 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200772 }
Emeric Brun107ca302010-01-04 16:16:05 +0100773
Willy Tarreau833cc792013-07-24 15:34:19 +0200774 /* keep a copy of the current fetch keyword for error reporting */
775 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100776
Willy Tarreau833cc792013-07-24 15:34:19 +0200777 fetch = find_sample_fetch(begw, endw - begw);
778 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100779 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100780 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200781 }
Emeric Brun107ca302010-01-04 16:16:05 +0100782
Willy Tarreau833cc792013-07-24 15:34:19 +0200783 endt = endw;
784 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100785 /* look for the end of this term and skip the opening parenthesis */
786 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200787 while (*endt && *endt != ')')
788 endt++;
789 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100790 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200791 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200792 }
Emeric Brun485479d2010-09-23 18:02:19 +0200793 }
Emeric Brun107ca302010-01-04 16:16:05 +0100794
Willy Tarreau833cc792013-07-24 15:34:19 +0200795 /* At this point, we have :
796 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100797 * - endw : end of the keyword, first character not part of keyword
798 * nor the opening parenthesis (so first character of args
799 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200800 * - endt : end of the term (=endw or last parenthesis if args are present)
801 */
802
803 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100804 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100805 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200806 }
Emeric Brun107ca302010-01-04 16:16:05 +0100807 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200808
Willy Tarreau12785782012-04-27 21:37:17 +0200809 expr = calloc(1, sizeof(struct sample_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200810 if (!expr)
811 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100812
813 LIST_INIT(&(expr->conv_exprs));
814 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200815 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100816
Willy Tarreau689a1df2013-12-13 00:40:11 +0100817 /* Note that we call the argument parser even with an empty string,
818 * this allows it to automatically create entries for mandatory
819 * implicit arguments (eg: local proxy name).
820 */
821 al->kw = expr->fetch->kw;
822 al->conv = NULL;
823 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
824 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
825 goto out_error;
826 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200827
Willy Tarreau689a1df2013-12-13 00:40:11 +0100828 if (!expr->arg_p) {
829 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200830 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100831 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
832 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200833 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100834 }
835
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 /* Now process the converters if any. We have two supported syntaxes
837 * for the converters, which can be combined :
838 * - comma-delimited list of converters just after the keyword and args ;
839 * - one converter per keyword
840 * The combination allows to have each keyword being a comma-delimited
841 * series of converters.
842 *
843 * We want to process the former first, then the latter. For this we start
844 * from the beginning of the supposed place in the exiting conv chain, which
845 * starts at the last comma (endt).
846 */
847
848 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200849 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Willy Tarreau833cc792013-07-24 15:34:19 +0200851 if (*endt == ')') /* skip last closing parenthesis */
852 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100853
Willy Tarreau833cc792013-07-24 15:34:19 +0200854 if (*endt && *endt != ',') {
855 if (ckw)
Willy Tarreau975c1782013-12-12 23:16:54 +0100856 memprintf(err_msg, "missing comma after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100858 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100859 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200860 }
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 while (*endt == ',') /* then trailing commas */
863 endt++;
864
865 begw = endt; /* start of conv keyword */
866
867 if (!*begw) {
868 /* none ? skip to next string */
869 (*idx)++;
870 begw = str[*idx];
871 if (!begw || !*begw)
872 break;
873 }
874
875 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
876
877 free(ckw);
878 ckw = my_strndup(begw, endw - begw);
879
880 conv = find_sample_conv(begw, endw - begw);
881 if (!conv) {
882 /* we found an isolated keyword that we don't know, it's not ours */
883 if (begw == str[*idx])
884 break;
Willy Tarreau975c1782013-12-12 23:16:54 +0100885 memprintf(err_msg, "unknown conv method '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200886 goto out_error;
887 }
Emeric Brun107ca302010-01-04 16:16:05 +0100888
Willy Tarreau833cc792013-07-24 15:34:19 +0200889 endt = endw;
890 if (*endt == '(') {
891 /* look for the end of this term */
892 while (*endt && *endt != ')')
893 endt++;
894 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100895 memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200896 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200897 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200898 }
899
900 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100901 memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100902 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200903 }
Emeric Brun107ca302010-01-04 16:16:05 +0100904
905 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200906 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100907 memprintf(err_msg, "conv method '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100908 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200909 }
Emeric Brun107ca302010-01-04 16:16:05 +0100910
911 prev_type = conv->out_type;
Willy Tarreau12785782012-04-27 21:37:17 +0200912 conv_expr = calloc(1, sizeof(struct sample_conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200913 if (!conv_expr)
914 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100915
916 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
917 conv_expr->conv = conv;
918
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200920 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200921
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200922 if (!conv->arg_mask) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100923 memprintf(err_msg, "conv method '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200924 goto out_error;
925 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100926
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200927 al->kw = expr->fetch->kw;
928 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +0100929 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
930 memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +0200931 goto out_error;
932 }
933
Willy Tarreau2e845be2012-10-19 19:49:09 +0200934 if (!conv_expr->arg_p)
935 conv_expr->arg_p = empty_arg_list;
936
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100937 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100938 memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200939 goto out_error;
940 }
941 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200942 else if (ARGM(conv->arg_mask)) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100943 memprintf(err_msg, "missing args for conv method '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200944 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100945 }
946 }
Emeric Brun485479d2010-09-23 18:02:19 +0200947
Willy Tarreau833cc792013-07-24 15:34:19 +0200948 out:
949 free(fkw);
950 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100951 return expr;
952
953out_error:
Willy Tarreau12785782012-04-27 21:37:17 +0200954 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +0200955 expr = NULL;
956 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +0100957}
958
959/*
Willy Tarreau12785782012-04-27 21:37:17 +0200960 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200961 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +0200962 * Returns a pointer on a typed sample structure containing the result or NULL if
963 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +0100964 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +0200965 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +0200966 *
967 * Note: the fetch functions are required to properly set the return type. The
968 * conversion functions must do so too. However the cast functions do not need
969 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200970 *
971 * The caller may indicate in <opt> if it considers the result final or not.
972 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
973 * if the result is stable or not, according to the following table :
974 *
975 * return MAY_CHANGE FINAL Meaning for the sample
976 * NULL 0 * Not present and will never be (eg: header)
977 * NULL 1 0 Not present yet, could change (eg: POST param)
978 * NULL 1 1 Not present yet, will not change anymore
979 * smp 0 * Present and will not change (eg: header)
980 * smp 1 0 Present, may change (eg: request length)
981 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +0100982 */
Willy Tarreau12785782012-04-27 21:37:17 +0200983struct sample *sample_process(struct proxy *px, struct session *l4, void *l7,
984 unsigned int opt,
985 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +0100986{
Willy Tarreau12785782012-04-27 21:37:17 +0200987 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100988
Willy Tarreau18387e22013-07-25 12:02:38 +0200989 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +0200990 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +0200991 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +0200992 }
Emeric Brun107ca302010-01-04 16:16:05 +0100993
Thierry FOURNIERf41a8092014-12-07 18:37:57 +0100994 if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +0100995 return NULL;
996
Emeric Brun107ca302010-01-04 16:16:05 +0100997 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +0200998 /* we want to ensure that p->type can be casted into
999 * conv_expr->conv->in_type. We have 3 possibilities :
1000 * - NULL => not castable.
1001 * - c_none => nothing to do (let's optimize it)
1002 * - other => apply cast and prepare to fail
1003 */
Willy Tarreau12785782012-04-27 21:37:17 +02001004 if (!sample_casts[p->type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001005 return NULL;
1006
Willy Tarreau12785782012-04-27 21:37:17 +02001007 if (sample_casts[p->type][conv_expr->conv->in_type] != c_none &&
1008 !sample_casts[p->type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001009 return NULL;
1010
Willy Tarreau12e50112012-04-25 17:21:49 +02001011 /* OK cast succeeded */
1012
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001013 if (!conv_expr->conv->process(l4, conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001014 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001015 }
1016 return p;
1017}
1018
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001019/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001020 * Resolve all remaining arguments in proxy <p>. Returns the number of
1021 * errors or 0 if everything is fine.
1022 */
1023int smp_resolve_args(struct proxy *p)
1024{
1025 struct arg_list *cur, *bak;
1026 const char *ctx, *where;
1027 const char *conv_ctx, *conv_pre, *conv_pos;
1028 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001029 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001030 struct arg *arg;
1031 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001032 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001033
1034 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1035 struct proxy *px;
1036 struct server *srv;
1037 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001038 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001039
1040 arg = cur->arg;
1041
1042 /* prepare output messages */
1043 conv_pre = conv_pos = conv_ctx = "";
1044 if (cur->conv) {
1045 conv_ctx = cur->conv;
1046 conv_pre = "conversion keyword '";
1047 conv_pos = "' for ";
1048 }
1049
1050 where = "in";
1051 ctx = "sample fetch keyword";
1052 switch (cur->ctx) {
Willy Tarreau46947782015-01-19 19:00:58 +01001053 case ARGC_STK: where = "in stick rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001054 case ARGC_TRK: where = "in tracking rule in"; break;
1055 case ARGC_LOG: where = "in log-format string in"; break;
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01001056 case ARGC_HRQ: where = "in http-request header format string in"; break;
1057 case ARGC_HRS: where = "in http-response header format string in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001058 case ARGC_UIF: where = "in unique-id-format string in"; break;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01001059 case ARGC_RDR: where = "in redirect format string in"; break;
Willy Tarreau3a4ac422014-06-13 16:17:14 +02001060 case ARGC_CAP: where = "in capture rule in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001061 case ARGC_ACL: ctx = "ACL keyword"; break;
1062 }
1063
1064 /* set a few default settings */
1065 px = p;
1066 pname = p->id;
1067
1068 switch (arg->type) {
1069 case ARGT_SRV:
1070 if (!arg->data.str.len) {
1071 Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1072 cur->file, cur->line,
1073 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1074 cfgerr++;
1075 continue;
1076 }
1077
1078 /* we support two formats : "bck/srv" and "srv" */
1079 sname = strrchr(arg->data.str.str, '/');
1080
1081 if (sname) {
1082 *sname++ = '\0';
1083 pname = arg->data.str.str;
1084
1085 px = findproxy(pname, PR_CAP_BE);
1086 if (!px) {
1087 Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1088 cur->file, cur->line, pname,
1089 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1090 cfgerr++;
1091 break;
1092 }
1093 }
1094 else
1095 sname = arg->data.str.str;
1096
1097 srv = findserver(px, sname);
1098 if (!srv) {
1099 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",
1100 cur->file, cur->line, sname, pname,
1101 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1102 cfgerr++;
1103 break;
1104 }
1105
1106 free(arg->data.str.str);
1107 arg->data.str.str = NULL;
1108 arg->unresolved = 0;
1109 arg->data.srv = srv;
1110 break;
1111
1112 case ARGT_FE:
1113 if (arg->data.str.len) {
1114 pname = arg->data.str.str;
1115 px = findproxy(pname, PR_CAP_FE);
1116 }
1117
1118 if (!px) {
1119 Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1120 cur->file, cur->line, pname,
1121 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1122 cfgerr++;
1123 break;
1124 }
1125
1126 if (!(px->cap & PR_CAP_FE)) {
1127 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1128 cur->file, cur->line, pname,
1129 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1130 cfgerr++;
1131 break;
1132 }
1133
1134 free(arg->data.str.str);
1135 arg->data.str.str = NULL;
1136 arg->unresolved = 0;
1137 arg->data.prx = px;
1138 break;
1139
1140 case ARGT_BE:
1141 if (arg->data.str.len) {
1142 pname = arg->data.str.str;
1143 px = findproxy(pname, PR_CAP_BE);
1144 }
1145
1146 if (!px) {
1147 Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1148 cur->file, cur->line, pname,
1149 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1150 cfgerr++;
1151 break;
1152 }
1153
1154 if (!(px->cap & PR_CAP_BE)) {
1155 Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1156 cur->file, cur->line, pname,
1157 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1158 cfgerr++;
1159 break;
1160 }
1161
1162 free(arg->data.str.str);
1163 arg->data.str.str = NULL;
1164 arg->unresolved = 0;
1165 arg->data.prx = px;
1166 break;
1167
1168 case ARGT_TAB:
1169 if (arg->data.str.len) {
1170 pname = arg->data.str.str;
1171 px = find_stktable(pname);
1172 }
1173
1174 if (!px) {
1175 Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1176 cur->file, cur->line, pname,
1177 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1178 cfgerr++;
1179 break;
1180 }
1181
1182 if (!px->table.size) {
1183 Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1184 cur->file, cur->line, pname,
1185 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1186 cfgerr++;
1187 break;
1188 }
1189
1190 free(arg->data.str.str);
1191 arg->data.str.str = NULL;
1192 arg->unresolved = 0;
1193 arg->data.prx = px;
1194 break;
1195
1196 case ARGT_USR:
1197 if (!arg->data.str.len) {
1198 Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1199 cur->file, cur->line,
1200 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1201 cfgerr++;
1202 break;
1203 }
1204
1205 if (p->uri_auth && p->uri_auth->userlist &&
1206 !strcmp(p->uri_auth->userlist->name, arg->data.str.str))
1207 ul = p->uri_auth->userlist;
1208 else
1209 ul = auth_find_userlist(arg->data.str.str);
1210
1211 if (!ul) {
1212 Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1213 cur->file, cur->line, arg->data.str.str,
1214 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1215 cfgerr++;
1216 break;
1217 }
1218
1219 free(arg->data.str.str);
1220 arg->data.str.str = NULL;
1221 arg->unresolved = 0;
1222 arg->data.usr = ul;
1223 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001224
1225 case ARGT_REG:
1226 if (!arg->data.str.len) {
1227 Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1228 cur->file, cur->line,
1229 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1230 cfgerr++;
1231 continue;
1232 }
1233
1234 reg = calloc(1, sizeof(*reg));
1235 if (!reg) {
1236 Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1237 cur->file, cur->line,
1238 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
1239 cfgerr++;
1240 continue;
1241 }
1242
1243 rflags = 0;
1244 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1245 err = NULL;
1246
Willy Tarreaud817e462015-01-23 20:23:17 +01001247 if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Willy Tarreau46947782015-01-19 19:00:58 +01001248 Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1249 cur->file, cur->line,
1250 arg->data.str.str,
1251 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
1252 cfgerr++;
1253 continue;
1254 }
1255
1256 free(arg->data.str.str);
1257 arg->data.str.str = NULL;
1258 arg->unresolved = 0;
1259 arg->data.reg = reg;
1260 break;
1261
1262
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 }
1264
1265 LIST_DEL(&cur->list);
1266 free(cur);
1267 } /* end of args processing */
1268
1269 return cfgerr;
1270}
1271
1272/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001273 * Process a fetch + format conversion as defined by the sample expression
1274 * <expr> on request or response considering the <opt> parameter. The output is
1275 * always of type string. If a stable sample can be fetched, or an unstable one
1276 * when <opt> contains SMP_OPT_FINAL, the sample is converted to a string and
1277 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1278 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1279 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1280 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001281 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1282 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1283 * still hope to get it after waiting longer, and is not converted to string.
1284 * The possible output combinations are the following :
1285 *
1286 * return MAY_CHANGE FINAL Meaning for the sample
1287 * NULL * * Not present and will never be (eg: header)
1288 * smp 0 * Final value converted (eg: header)
1289 * smp 1 0 Not present yet, may appear later (eg: header)
1290 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001291 */
1292struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7,
1293 unsigned int opt, struct sample_expr *expr)
1294{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001295 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001296
Willy Tarreau6c616e02014-06-25 16:56:41 +02001297 memset(smp, 0, sizeof(*smp));
1298
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001299 if (!sample_process(px, l4, l7, opt, expr, smp)) {
1300 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1301 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001302 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001303 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001304
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001305 if (!sample_casts[smp->type][SMP_T_STR])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001306 return NULL;
1307
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001308 if (!sample_casts[smp->type][SMP_T_STR](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001309 return NULL;
1310
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001311 smp->type = SMP_T_STR;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001312 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001313 return smp;
1314}
1315
Emeric Brun107ca302010-01-04 16:16:05 +01001316/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001317/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001318/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001319/*****************************************************************/
1320
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001321static int sample_conv_bin2base64(struct session *session, const struct arg *arg_p,
1322 struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001323{
1324 struct chunk *trash = get_trash_chunk();
1325 int b64_len;
1326
1327 trash->len = 0;
1328 b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size);
1329 if (b64_len < 0)
1330 return 0;
1331
1332 trash->len = b64_len;
1333 smp->data.str = *trash;
1334 smp->type = SMP_T_STR;
1335 smp->flags &= ~SMP_F_CONST;
1336 return 1;
1337}
1338
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001339static int sample_conv_bin2hex(struct session *session, const struct arg *arg_p,
1340 struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001341{
1342 struct chunk *trash = get_trash_chunk();
1343 unsigned char c;
1344 int ptr = 0;
1345
1346 trash->len = 0;
1347 while (ptr < smp->data.str.len && trash->len <= trash->size - 2) {
1348 c = smp->data.str.str[ptr++];
1349 trash->str[trash->len++] = hextab[(c >> 4) & 0xF];
1350 trash->str[trash->len++] = hextab[c & 0xF];
1351 }
1352 smp->data.str = *trash;
1353 smp->type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001354 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001355 return 1;
1356}
1357
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001358/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001359static int sample_conv_djb2(struct session *session, const struct arg *arg_p,
1360 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001361{
1362 smp->data.uint = hash_djb2(smp->data.str.str, smp->data.str.len);
1363 if (arg_p && arg_p->data.uint)
1364 smp->data.uint = full_hash(smp->data.uint);
1365 smp->type = SMP_T_UINT;
1366 return 1;
1367}
1368
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001369static int sample_conv_str2lower(struct session *session, const struct arg *arg_p,
1370 struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001371{
1372 int i;
1373
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001374 if (!smp_dup(smp))
1375 return 0;
1376
Willy Tarreau342acb42012-04-23 22:03:39 +02001377 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001378 return 0;
1379
Willy Tarreau342acb42012-04-23 22:03:39 +02001380 for (i = 0; i < smp->data.str.len; i++) {
1381 if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z'))
1382 smp->data.str.str[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001383 }
1384 return 1;
1385}
1386
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001387static int sample_conv_str2upper(struct session *session, const struct arg *arg_p,
1388 struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001389{
1390 int i;
1391
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001392 if (!smp_dup(smp))
1393 return 0;
1394
Willy Tarreau342acb42012-04-23 22:03:39 +02001395 if (!smp->data.str.size)
Emeric Brun485479d2010-09-23 18:02:19 +02001396 return 0;
1397
Willy Tarreau342acb42012-04-23 22:03:39 +02001398 for (i = 0; i < smp->data.str.len; i++) {
1399 if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z'))
1400 smp->data.str.str[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001401 }
1402 return 1;
1403}
1404
Willy Tarreauf9954102012-04-20 14:03:29 +02001405/* takes the netmask in arg_p */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001406static int sample_conv_ipmask(struct session *session, const struct arg *arg_p,
1407 struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001408{
Willy Tarreau342acb42012-04-23 22:03:39 +02001409 smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr;
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001410 smp->type = SMP_T_IPV4;
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001411 return 1;
1412}
1413
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001414/* takes an UINT value on input supposed to represent the time since EPOCH,
1415 * adds an optional offset found in args[1] and emits a string representing
1416 * the local time in the format specified in args[1] using strftime().
1417 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001418static int sample_conv_ltime(struct session *session, const struct arg *args,
1419 struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001420{
1421 struct chunk *temp;
1422 time_t curr_date = smp->data.uint;
1423
1424 /* add offset */
1425 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1426 curr_date += args[1].data.sint;
1427
1428 temp = get_trash_chunk();
1429 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, localtime(&curr_date));
1430 smp->data.str = *temp;
1431 smp->type = SMP_T_STR;
1432 return 1;
1433}
1434
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001435/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001436static int sample_conv_sdbm(struct session *session, const struct arg *arg_p,
1437 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001438{
1439 smp->data.uint = hash_sdbm(smp->data.str.str, smp->data.str.len);
1440 if (arg_p && arg_p->data.uint)
1441 smp->data.uint = full_hash(smp->data.uint);
1442 smp->type = SMP_T_UINT;
1443 return 1;
1444}
1445
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001446/* takes an UINT value on input supposed to represent the time since EPOCH,
1447 * adds an optional offset found in args[1] and emits a string representing
1448 * the UTC date in the format specified in args[1] using strftime().
1449 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001450static int sample_conv_utime(struct session *session, const struct arg *args,
1451 struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001452{
1453 struct chunk *temp;
1454 time_t curr_date = smp->data.uint;
1455
1456 /* add offset */
1457 if (args[1].type == ARGT_SINT || args[1].type == ARGT_UINT)
1458 curr_date += args[1].data.sint;
1459
1460 temp = get_trash_chunk();
1461 temp->len = strftime(temp->str, temp->size, args[0].data.str.str, gmtime(&curr_date));
1462 smp->data.str = *temp;
1463 smp->type = SMP_T_STR;
1464 return 1;
1465}
1466
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001467/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001468static int sample_conv_wt6(struct session *session, const struct arg *arg_p,
1469 struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001470{
1471 smp->data.uint = hash_wt6(smp->data.str.str, smp->data.str.len);
1472 if (arg_p && arg_p->data.uint)
1473 smp->data.uint = full_hash(smp->data.uint);
1474 smp->type = SMP_T_UINT;
1475 return 1;
1476}
1477
Willy Tarreau80599772015-01-20 19:35:24 +01001478/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001479static int sample_conv_crc32(struct session *session, const struct arg *arg_p,
1480 struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001481{
1482 smp->data.uint = hash_crc32(smp->data.str.str, smp->data.str.len);
1483 if (arg_p && arg_p->data.uint)
1484 smp->data.uint = full_hash(smp->data.uint);
1485 smp->type = SMP_T_UINT;
1486 return 1;
1487}
1488
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001489/* This function escape special json characters. The returned string can be
1490 * safely set between two '"' and used as json string. The json string is
1491 * defined like this:
1492 *
1493 * any Unicode character except '"' or '\' or control character
1494 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1495 *
1496 * The enum input_type contain all the allowed mode for decoding the input
1497 * string.
1498 */
1499enum input_type {
1500 IT_ASCII = 0,
1501 IT_UTF8,
1502 IT_UTF8S,
1503 IT_UTF8P,
1504 IT_UTF8PS,
1505};
1506static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1507 const char *file, int line, char **err)
1508{
1509 if (!arg) {
1510 memprintf(err, "Unexpected empty arg list");
1511 return 0;
1512 }
1513
1514 if (arg->type != ARGT_STR) {
1515 memprintf(err, "Unexpected arg type");
1516 return 0;
1517 }
1518
1519 if (strcmp(arg->data.str.str, "") == 0) {
1520 arg->type = ARGT_UINT;
1521 arg->data.uint = IT_ASCII;
1522 return 1;
1523 }
1524
1525 else if (strcmp(arg->data.str.str, "ascii") == 0) {
1526 arg->type = ARGT_UINT;
1527 arg->data.uint = IT_ASCII;
1528 return 1;
1529 }
1530
1531 else if (strcmp(arg->data.str.str, "utf8") == 0) {
1532 arg->type = ARGT_UINT;
1533 arg->data.uint = IT_UTF8;
1534 return 1;
1535 }
1536
1537 else if (strcmp(arg->data.str.str, "utf8s") == 0) {
1538 arg->type = ARGT_UINT;
1539 arg->data.uint = IT_UTF8S;
1540 return 1;
1541 }
1542
1543 else if (strcmp(arg->data.str.str, "utf8p") == 0) {
1544 arg->type = ARGT_UINT;
1545 arg->data.uint = IT_UTF8P;
1546 return 1;
1547 }
1548
1549 else if (strcmp(arg->data.str.str, "utf8ps") == 0) {
1550 arg->type = ARGT_UINT;
1551 arg->data.uint = IT_UTF8PS;
1552 return 1;
1553 }
1554
1555 memprintf(err, "Unexpected input code type at file '%s', line %d. "
1556 "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line);
1557 return 0;
1558}
1559
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001560static int sample_conv_json(struct session *session, const struct arg *arg_p,
1561 struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001562{
1563 struct chunk *temp;
1564 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1565 const char *str;
1566 int len;
1567 enum input_type input_type = IT_ASCII;
1568 unsigned int c;
1569 unsigned int ret;
1570 char *p;
1571
1572 if (arg_p)
1573 input_type = arg_p->data.uint;
1574
1575 temp = get_trash_chunk();
1576 temp->len = 0;
1577
1578 p = smp->data.str.str;
1579 while (p < smp->data.str.str + smp->data.str.len) {
1580
1581 if (input_type == IT_ASCII) {
1582 /* Read input as ASCII. */
1583 c = *(unsigned char *)p;
1584 p++;
1585 }
1586 else {
1587 /* Read input as UTF8. */
1588 ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c);
1589 p += utf8_return_length(ret);
1590
1591 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1592 return 0;
1593 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1594 continue;
1595 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1596 return 0;
1597 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1598 continue;
1599
1600 /* Check too big values. */
1601 if ((unsigned int)c > 0xffff) {
1602 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1603 return 0;
1604 continue;
1605 }
1606 }
1607
1608 /* Convert character. */
1609 if (c == '"') {
1610 len = 2;
1611 str = "\\\"";
1612 }
1613 else if (c == '\\') {
1614 len = 2;
1615 str = "\\\\";
1616 }
1617 else if (c == '/') {
1618 len = 2;
1619 str = "\\/";
1620 }
1621 else if (c == '\b') {
1622 len = 2;
1623 str = "\\b";
1624 }
1625 else if (c == '\f') {
1626 len = 2;
1627 str = "\\f";
1628 }
1629 else if (c == '\r') {
1630 len = 2;
1631 str = "\\r";
1632 }
1633 else if (c == '\n') {
1634 len = 2;
1635 str = "\\n";
1636 }
1637 else if (c == '\t') {
1638 len = 2;
1639 str = "\\t";
1640 }
1641 else if (c > 0xff || !isprint(c)) {
1642 /* isprint generate a segfault if c is too big. The man says that
1643 * c must have the value of an unsigned char or EOF.
1644 */
1645 len = 6;
1646 _str[0] = '\\';
1647 _str[1] = 'u';
1648 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1649 str = _str;
1650 }
1651 else {
1652 len = 1;
1653 str = (char *)&c;
1654 }
1655
1656 /* Check length */
1657 if (temp->len + len > temp->size)
1658 return 0;
1659
1660 /* Copy string. */
1661 memcpy(temp->str + temp->len, str, len);
1662 temp->len += len;
1663 }
1664
1665 smp->flags &= ~SMP_F_CONST;
1666 smp->data.str = *temp;
1667 smp->type = SMP_T_STR;
1668
1669 return 1;
1670}
1671
Emeric Brun54c4ac82014-11-03 15:32:43 +01001672/* This sample function is designed to extract some bytes from an input buffer.
1673 * First arg is the offset.
1674 * Optional second arg is the length to truncate */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001675static int sample_conv_bytes(struct session *session, const struct arg *arg_p,
1676 struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001677{
1678 if (smp->data.str.len <= arg_p[0].data.uint) {
1679 smp->data.str.len = 0;
1680 return 1;
1681 }
1682
1683 if (smp->data.str.size)
1684 smp->data.str.size -= arg_p[0].data.uint;
1685 smp->data.str.len -= arg_p[0].data.uint;
1686 smp->data.str.str += arg_p[0].data.uint;
1687
1688 if ((arg_p[1].type == ARGT_UINT) && (arg_p[1].data.uint < smp->data.str.len))
1689 smp->data.str.len = arg_p[1].data.uint;
1690
1691 return 1;
1692}
1693
Emeric Brunf399b0d2014-11-03 17:07:03 +01001694static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1695 const char *file, int line, char **err)
1696{
1697 struct arg *arg = args;
1698
1699 if (!arg) {
1700 memprintf(err, "Unexpected empty arg list");
1701 return 0;
1702 }
1703
1704 if (arg->type != ARGT_UINT) {
1705 memprintf(err, "Unexpected arg type");
1706 return 0;
1707 }
1708
1709 if (!arg->data.uint) {
1710 memprintf(err, "Unexpected value 0 for index");
1711 return 0;
1712 }
1713
1714 arg++;
1715
1716 if (arg->type != ARGT_STR) {
1717 memprintf(err, "Unexpected arg type");
1718 return 0;
1719 }
1720
1721 if (!arg->data.str.len) {
1722 memprintf(err, "Empty separators list");
1723 return 0;
1724 }
1725
1726 return 1;
1727}
1728
1729/* This sample function is designed to a return selected part of a string (field).
1730 * First arg is the index of the field (start at 1)
1731 * Second arg is a char list of separators (type string)
1732 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001733static int sample_conv_field(struct session *session, const struct arg *arg_p,
1734 struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01001735{
1736 unsigned int field;
1737 char *start, *end;
1738 int i;
1739
1740 if (!arg_p[0].data.uint)
1741 return 0;
1742
1743 field = 1;
1744 end = start = smp->data.str.str;
1745 while (end - smp->data.str.str < smp->data.str.len) {
1746
1747 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1748 if (*end == arg_p[1].data.str.str[i]) {
1749 if (field == arg_p[0].data.uint)
1750 goto found;
1751 start = end+1;
1752 field++;
1753 break;
1754 }
1755 }
1756 end++;
1757 }
1758
1759 /* Field not found */
1760 if (field != arg_p[0].data.uint) {
1761 smp->data.str.len = 0;
1762 return 1;
1763 }
1764found:
1765 smp->data.str.len = end - start;
1766 /* If ret string is len 0, no need to
1767 change pointers or to update size */
1768 if (!smp->data.str.len)
1769 return 1;
1770
1771 smp->data.str.str = start;
1772
1773 /* Compute remaining size if needed
1774 Note: smp->data.str.size cannot be set to 0 */
1775 if (smp->data.str.size)
1776 smp->data.str.size -= start - smp->data.str.str;
1777
1778 return 1;
1779}
1780
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001781/* This sample function is designed to return a word from a string.
1782 * First arg is the index of the word (start at 1)
1783 * Second arg is a char list of words separators (type string)
1784 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001785static int sample_conv_word(struct session *session, const struct arg *arg_p,
1786 struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01001787{
1788 unsigned int word;
1789 char *start, *end;
1790 int i, issep, inword;
1791
1792 if (!arg_p[0].data.uint)
1793 return 0;
1794
1795 word = 0;
1796 inword = 0;
1797 end = start = smp->data.str.str;
1798 while (end - smp->data.str.str < smp->data.str.len) {
1799 issep = 0;
1800 for (i = 0 ; i < arg_p[1].data.str.len ; i++) {
1801 if (*end == arg_p[1].data.str.str[i]) {
1802 issep = 1;
1803 break;
1804 }
1805 }
1806 if (!inword) {
1807 if (!issep) {
1808 word++;
1809 start = end;
1810 inword = 1;
1811 }
1812 }
1813 else if (issep) {
1814 if (word == arg_p[0].data.uint)
1815 goto found;
1816 inword = 0;
1817 }
1818 end++;
1819 }
1820
1821 /* Field not found */
1822 if (word != arg_p[0].data.uint) {
1823 smp->data.str.len = 0;
1824 return 1;
1825 }
1826found:
1827 smp->data.str.len = end - start;
1828 /* If ret string is len 0, no need to
1829 change pointers or to update size */
1830 if (!smp->data.str.len)
1831 return 1;
1832
1833 smp->data.str.str = start;
1834
1835 /* Compute remaining size if needed
1836 Note: smp->data.str.size cannot be set to 0 */
1837 if (smp->data.str.size)
1838 smp->data.str.size -= start - smp->data.str.str;
1839
1840 return 1;
1841}
1842
Willy Tarreau7eda8492015-01-20 19:47:06 +01001843static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
1844 const char *file, int line, char **err)
1845{
1846 struct arg *arg = args;
1847 char *p;
1848 int len;
1849
1850 /* arg0 is a regex, it uses type_flag for ICASE and global match */
1851 arg[0].type_flags = 0;
1852
1853 if (arg[2].type != ARGT_STR)
1854 return 1;
1855
1856 p = arg[2].data.str.str;
1857 len = arg[2].data.str.len;
1858 while (len) {
1859 if (*p == 'i') {
1860 arg[0].type_flags |= ARGF_REG_ICASE;
1861 }
1862 else if (*p == 'g') {
1863 arg[0].type_flags |= ARGF_REG_GLOB;
1864 }
1865 else {
1866 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
1867 return 0;
1868 }
1869 p++;
1870 len--;
1871 }
1872 return 1;
1873}
1874
1875/* This sample function is designed to do the equivalent of s/match/replace/ on
1876 * the input string. It applies a regex and restarts from the last matched
1877 * location until nothing matches anymore. First arg is the regex to apply to
1878 * the input string, second arg is the replacement expression.
1879 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001880static int sample_conv_regsub(struct session *session, const struct arg *arg_p,
1881 struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01001882{
1883 char *start, *end;
1884 struct my_regex *reg = arg_p[0].data.reg;
1885 regmatch_t pmatch[MAX_MATCH];
1886 struct chunk *trash = get_trash_chunk();
1887 int flag, max;
1888 int found;
1889
1890 start = smp->data.str.str;
1891 end = start + smp->data.str.len;
1892
1893 flag = 0;
1894 while (1) {
1895 /* check for last round which is used to copy remaining parts
1896 * when not running in global replacement mode.
1897 */
1898 found = 0;
1899 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
1900 /* Note: we can have start == end on empty strings or at the end */
1901 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
1902 }
1903
1904 if (!found)
1905 pmatch[0].rm_so = end - start;
1906
1907 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
1908 max = trash->size - trash->len;
1909 if (max && pmatch[0].rm_so > 0) {
1910 if (max > pmatch[0].rm_so)
1911 max = pmatch[0].rm_so;
1912 memcpy(trash->str + trash->len, start, max);
1913 trash->len += max;
1914 }
1915
1916 if (!found)
1917 break;
1918
1919 /* replace the matching part */
1920 max = trash->size - trash->len;
1921 if (max) {
1922 if (max > arg_p[1].data.str.len)
1923 max = arg_p[1].data.str.len;
1924 memcpy(trash->str + trash->len, arg_p[1].data.str.str, max);
1925 trash->len += max;
1926 }
1927
1928 /* stop here if we're done with this string */
1929 if (start >= end)
1930 break;
1931
1932 /* We have a special case for matches of length 0 (eg: "x*y*").
1933 * These ones are considered to match in front of a character,
1934 * so we have to copy that character and skip to the next one.
1935 */
1936 if (!pmatch[0].rm_eo) {
1937 if (trash->len < trash->size)
1938 trash->str[trash->len++] = start[pmatch[0].rm_eo];
1939 pmatch[0].rm_eo++;
1940 }
1941
1942 start += pmatch[0].rm_eo;
1943 flag |= REG_NOTBOL;
1944 }
1945
1946 smp->data.str = *trash;
1947 return 1;
1948}
1949
Willy Tarreau97707872015-01-27 15:12:13 +01001950/* Takes a UINT on input, applies a binary twos complement and returns the UINT
1951 * result.
1952 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001953static int sample_conv_binary_cpl(struct session *session, const struct arg *arg_p,
1954 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001955{
1956 smp->data.uint = ~smp->data.uint;
1957 return 1;
1958}
1959
1960/* Takes a UINT on input, applies a binary "and" with the UINT in arg_p, and
1961 * returns the UINT result.
1962 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001963static int sample_conv_binary_and(struct session *session, const struct arg *arg_p,
1964 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001965{
1966 smp->data.uint &= arg_p->data.uint;
1967 return 1;
1968}
1969
1970/* Takes a UINT on input, applies a binary "or" with the UINT in arg_p, and
1971 * returns the UINT result.
1972 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001973static int sample_conv_binary_or(struct session *session, const struct arg *arg_p,
1974 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001975{
1976 smp->data.uint |= arg_p->data.uint;
1977 return 1;
1978}
1979
1980/* Takes a UINT on input, applies a binary "xor" with the UINT in arg_p, and
1981 * returns the UINT result.
1982 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001983static int sample_conv_binary_xor(struct session *session, const struct arg *arg_p,
1984 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001985{
1986 smp->data.uint ^= arg_p->data.uint;
1987 return 1;
1988}
1989
1990/* Takes a UINT on input, applies an arithmetic "add" with the UINT in arg_p,
1991 * and returns the UINT result.
1992 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01001993static int sample_conv_arith_add(struct session *session, const struct arg *arg_p,
1994 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01001995{
1996 smp->data.uint += arg_p->data.uint;
1997 return 1;
1998}
1999
2000/* Takes a UINT on input, applies an arithmetic "sub" with the UINT in arg_p,
2001 * and returns the UINT result.
2002 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002003static int sample_conv_arith_sub(struct session *session, const struct arg *arg_p,
2004 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002005{
2006 smp->data.uint -= arg_p->data.uint;
2007 return 1;
2008}
2009
2010/* Takes a UINT on input, applies an arithmetic "mul" with the UINT in arg_p,
2011 * and returns the UINT result.
2012 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002013static int sample_conv_arith_mul(struct session *session, const struct arg *arg_p,
2014 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002015{
2016 smp->data.uint *= arg_p->data.uint;
2017 return 1;
2018}
2019
2020/* Takes a UINT on input, applies an arithmetic "div" with the UINT in arg_p,
2021 * and returns the UINT result. If arg_p makes the result overflow, then the
2022 * largest possible quantity is returned.
2023 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002024static int sample_conv_arith_div(struct session *session, const struct arg *arg_p,
2025 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002026{
2027 if (arg_p->data.uint)
2028 smp->data.uint /= arg_p->data.uint;
2029 else
2030 smp->data.uint = ~0;
2031 return 1;
2032}
2033
2034/* Takes a UINT on input, applies an arithmetic "mod" with the UINT in arg_p,
2035 * and returns the UINT result. If arg_p makes the result overflow, then zero
2036 * is returned.
2037 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002038static int sample_conv_arith_mod(struct session *session, const struct arg *arg_p,
2039 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002040{
2041 if (arg_p->data.uint)
2042 smp->data.uint %= arg_p->data.uint;
2043 else
2044 smp->data.uint = 0;
2045 return 1;
2046}
2047
2048/* Takes an UINT on input, applies an arithmetic "neg" and returns the UINT
2049 * result.
2050 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002051static int sample_conv_arith_neg(struct session *session, const struct arg *arg_p,
2052 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002053{
2054 smp->data.uint = -smp->data.uint;
2055 return 1;
2056}
2057
2058/* Takes a UINT on input, returns true is the value is non-null, otherwise
2059 * false. The output is a BOOL.
2060 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002061static int sample_conv_arith_bool(struct session *session, const struct arg *arg_p,
2062 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002063{
2064 smp->data.uint = !!smp->data.uint;
2065 smp->type = SMP_T_BOOL;
2066 return 1;
2067}
2068
2069/* Takes a UINT on input, returns false is the value is non-null, otherwise
2070 * truee. The output is a BOOL.
2071 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002072static int sample_conv_arith_not(struct session *session, const struct arg *arg_p,
2073 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002074{
2075 smp->data.uint = !smp->data.uint;
2076 smp->type = SMP_T_BOOL;
2077 return 1;
2078}
2079
2080/* Takes a UINT on input, returns true is the value is odd, otherwise false.
2081 * The output is a BOOL.
2082 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002083static int sample_conv_arith_odd(struct session *session, const struct arg *arg_p,
2084 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002085{
2086 smp->data.uint = smp->data.uint & 1;
2087 smp->type = SMP_T_BOOL;
2088 return 1;
2089}
2090
2091/* Takes a UINT on input, returns true is the value is even, otherwise false.
2092 * The output is a BOOL.
2093 */
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002094static int sample_conv_arith_even(struct session *session, const struct arg *arg_p,
2095 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002096{
2097 smp->data.uint = !(smp->data.uint & 1);
2098 smp->type = SMP_T_BOOL;
2099 return 1;
2100}
2101
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002102/************************************************************************/
2103/* All supported sample fetch functions must be declared here */
2104/************************************************************************/
2105
2106/* force TRUE to be returned at the fetch level */
2107static int
2108smp_fetch_true(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002109 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002110{
2111 smp->type = SMP_T_BOOL;
2112 smp->data.uint = 1;
2113 return 1;
2114}
2115
2116/* force FALSE to be returned at the fetch level */
2117static int
2118smp_fetch_false(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002119 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002120{
2121 smp->type = SMP_T_BOOL;
2122 smp->data.uint = 0;
2123 return 1;
2124}
2125
2126/* retrieve environment variable $1 as a string */
2127static int
2128smp_fetch_env(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002129 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002130{
2131 char *env;
2132
2133 if (!args || args[0].type != ARGT_STR)
2134 return 0;
2135
2136 env = getenv(args[0].data.str.str);
2137 if (!env)
2138 return 0;
2139
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002140 smp->type = SMP_T_STR;
2141 smp->flags = SMP_F_CONST;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002142 smp->data.str.str = env;
2143 smp->data.str.len = strlen(env);
2144 return 1;
2145}
2146
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002147/* retrieve the current local date in epoch time, and applies an optional offset
2148 * of args[0] seconds.
2149 */
2150static int
2151smp_fetch_date(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002152 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002153{
2154 smp->data.uint = date.tv_sec;
2155
2156 /* add offset */
2157 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
2158 smp->data.uint += args[0].data.sint;
2159
2160 smp->type = SMP_T_UINT;
2161 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2162 return 1;
2163}
2164
Willy Tarreau0f30d262014-11-24 16:02:05 +01002165/* returns the number of processes */
2166static int
2167smp_fetch_nbproc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002168 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002169{
2170 smp->type = SMP_T_UINT;
2171 smp->data.uint = global.nbproc;
2172 return 1;
2173}
2174
2175/* returns the number of the current process (between 1 and nbproc */
2176static int
2177smp_fetch_proc(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002178 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002179{
2180 smp->type = SMP_T_UINT;
2181 smp->data.uint = relative_pid;
2182 return 1;
2183}
2184
Willy Tarreau84310e22014-02-14 11:59:04 +01002185/* generate a random 32-bit integer for whatever purpose, with an optional
2186 * range specified in argument.
2187 */
2188static int
2189smp_fetch_rand(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002190 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002191{
2192 smp->data.uint = random();
2193
2194 /* reduce if needed. Don't do a modulo, use all bits! */
2195 if (args && args[0].type == ARGT_UINT)
Vincent Bernat1228dc02014-12-10 10:31:37 +01002196 smp->data.uint = ((uint64_t)smp->data.uint * args[0].data.uint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002197
2198 smp->type = SMP_T_UINT;
2199 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2200 return 1;
2201}
2202
Willy Tarreau0f30d262014-11-24 16:02:05 +01002203/* returns true if the current process is stopping */
2204static int
2205smp_fetch_stopping(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002206 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002207{
2208 smp->type = SMP_T_BOOL;
2209 smp->data.uint = stopping;
2210 return 1;
2211}
2212
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002213/* Note: must not be declared <const> as its list will be overwritten.
2214 * Note: fetches that may return multiple types must be declared as the lowest
2215 * common denominator, the type that can be casted into all other ones. For
2216 * instance IPv4/IPv6 must be declared IPv4.
2217 */
2218static struct sample_fetch_kw_list smp_kws = {ILH, {
2219 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
2220 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002221 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002222 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002223 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_UINT, SMP_USE_INTRN },
2224 { "proc", smp_fetch_proc, 0, NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau84310e22014-02-14 11:59:04 +01002225 { "rand", smp_fetch_rand, ARG1(0,UINT), NULL, SMP_T_UINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01002226 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002227 { /* END */ },
2228}};
2229
Emeric Brun107ca302010-01-04 16:16:05 +01002230/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02002231static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Emeric Brun53d1a982014-04-30 18:21:37 +02002232 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002233 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
2234 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01002235 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02002236 { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 },
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02002237 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
2238 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_UINT, SMP_T_STR },
Willy Tarreau80599772015-01-20 19:35:24 +01002239 { "crc32", sample_conv_crc32, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02002240 { "djb2", sample_conv_djb2, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2241 { "sdbm", sample_conv_sdbm, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
2242 { "wt6", sample_conv_wt6, ARG1(0,UINT), NULL, SMP_T_BIN, SMP_T_UINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002243 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Emeric Brun54c4ac82014-11-03 15:32:43 +01002244 { "bytes", sample_conv_bytes, ARG2(1,UINT,UINT), NULL, SMP_T_BIN, SMP_T_BIN },
Emeric Brunf399b0d2014-11-03 17:07:03 +01002245 { "field", sample_conv_field, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002246 { "word", sample_conv_word, ARG2(2,UINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01002247 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau97707872015-01-27 15:12:13 +01002248
2249 { "and", sample_conv_binary_and, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2250 { "or", sample_conv_binary_or, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2251 { "xor", sample_conv_binary_xor, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2252 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2253 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2254 { "not", sample_conv_arith_not, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2255 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2256 { "even", sample_conv_arith_even, 0, NULL, SMP_T_UINT, SMP_T_BOOL },
2257 { "add", sample_conv_arith_add, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2258 { "sub", sample_conv_arith_sub, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2259 { "mul", sample_conv_arith_mul, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2260 { "div", sample_conv_arith_div, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2261 { "mod", sample_conv_arith_mod, ARG1(1,UINT), NULL, SMP_T_UINT, SMP_T_UINT },
2262 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_UINT, SMP_T_UINT },
2263
Willy Tarreau9fcb9842012-04-20 14:45:49 +02002264 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01002265}};
2266
2267__attribute__((constructor))
Willy Tarreau12785782012-04-27 21:37:17 +02002268static void __sample_init(void)
Emeric Brun107ca302010-01-04 16:16:05 +01002269{
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002270 /* register sample fetch and format conversion keywords */
2271 sample_register_fetches(&smp_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02002272 sample_register_convs(&sample_conv_kws);
Emeric Brun107ca302010-01-04 16:16:05 +01002273}