blob: 82078fce1d929412f65276fb3c1b9b9c61df0a7a [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 Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020035#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020036#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010037
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020038#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010039#include <import/xxhash.h>
40
Willy Tarreau1cf8f082014-02-07 12:14:54 +010041/* sample type names */
42const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020043 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_SINT] = "sint",
46 [SMP_T_ADDR] = "addr",
47 [SMP_T_IPV4] = "ipv4",
48 [SMP_T_IPV6] = "ipv6",
49 [SMP_T_STR] = "str",
50 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020051 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052};
53
Willy Tarreau12785782012-04-27 21:37:17 +020054/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020055static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010056
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample fetch keywords */
58static struct sample_fetch_kw_list sample_fetches = {
59 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample format conversion keywords */
63static struct sample_conv_kw_list sample_convs = {
64 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau80aca902013-01-07 15:42:20 +010067const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
68 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
69 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
70 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
71 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
72 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
73 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
74 SMP_VAL_FE_LOG_END),
75
76 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
77 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
78 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
79 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
80 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
81 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
82 SMP_VAL_FE_LOG_END),
83
84 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
85 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
86 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
87 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
88 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
89 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
90 SMP_VAL_FE_LOG_END),
91
92 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
98 SMP_VAL_FE_LOG_END),
99
100 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
106 SMP_VAL_FE_LOG_END),
107
108 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
109 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
110 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
111 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
112 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
113 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
114 SMP_VAL_FE_LOG_END),
115
116 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
117 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
118 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
119 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________),
123
124 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
125 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
126 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
127 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________),
131
132 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
133 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
134 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
135 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
136 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
137 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
138 SMP_VAL_FE_LOG_END),
139
140 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
142 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
143 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________),
147
148 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
151 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
152 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
153 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
154 SMP_VAL_FE_LOG_END),
155
156 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
160 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
161 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
162 SMP_VAL_FE_LOG_END),
163
164 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
168 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
169 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
170 SMP_VAL_FE_LOG_END),
171
172 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
178 SMP_VAL_FE_LOG_END),
179
180 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
184 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
185 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
186 SMP_VAL___________),
187
188 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
192 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
193 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
194 SMP_VAL___________),
195
196 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
200 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
201 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
202 SMP_VAL_FE_LOG_END),
203
204 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 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_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
209 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
210 SMP_VAL___________),
211
212 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 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_FE_LOG_END),
219
220 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 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_FE_LOG_END),
227
228 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 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_FE_LOG_END),
235
236 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL_FE_LOG_END),
243};
244
245static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
246 [SMP_SRC_INTRN] = "internal state",
247 [SMP_SRC_LISTN] = "listener",
248 [SMP_SRC_FTEND] = "frontend",
249 [SMP_SRC_L4CLI] = "client address",
250 [SMP_SRC_L5CLI] = "client-side connection",
251 [SMP_SRC_TRACK] = "track counters",
252 [SMP_SRC_L6REQ] = "request buffer",
253 [SMP_SRC_HRQHV] = "HTTP request headers",
254 [SMP_SRC_HRQHP] = "HTTP request",
255 [SMP_SRC_HRQBO] = "HTTP request body",
256 [SMP_SRC_BKEND] = "backend",
257 [SMP_SRC_SERVR] = "server",
258 [SMP_SRC_L4SRV] = "server address",
259 [SMP_SRC_L5SRV] = "server-side connection",
260 [SMP_SRC_L6RES] = "response buffer",
261 [SMP_SRC_HRSHV] = "HTTP response headers",
262 [SMP_SRC_HRSHP] = "HTTP response",
263 [SMP_SRC_HRSBO] = "HTTP response body",
264 [SMP_SRC_RQFIN] = "request buffer statistics",
265 [SMP_SRC_RSFIN] = "response buffer statistics",
266 [SMP_SRC_TXFIN] = "transaction statistics",
267 [SMP_SRC_SSFIN] = "session statistics",
268};
269
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100270static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
271 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
272 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
273 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
274 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
275 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
276 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
277 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
278 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
279 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
280 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
281 [SMP_CKP_BE_SRV_CON] = "server source selection",
282 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
283 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
284 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
285 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
286 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
287 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
288 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
289 [SMP_CKP_FE_LOG_END] = "logs",
290};
291
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292/* This function returns the type of the data returned by the sample_expr.
293 * It assumes that the <expr> and all of its converters are properly
294 * initialized.
295 */
296inline
297int smp_expr_output_type(struct sample_expr *expr)
298{
299 struct sample_conv_expr *smp_expr;
300
301 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
302 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
303 return smp_expr->conv->out_type;
304 }
305 return expr->fetch->out_type;
306}
307
308
Willy Tarreau80aca902013-01-07 15:42:20 +0100309/* fill the trash with a comma-delimited list of source names for the <use> bit
310 * field which must be composed of a non-null set of SMP_USE_* flags. The return
311 * value is the pointer to the string in the trash buffer.
312 */
313const char *sample_src_names(unsigned int use)
314{
315 int bit;
316
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200317 trash.data = 0;
318 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100319 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
320 if (!(use & ~((1 << bit) - 1)))
321 break; /* no more bits */
322
323 if (!(use & (1 << bit)))
324 continue; /* bit not set */
325
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200326 trash.data += snprintf(trash.area + trash.data,
327 trash.size - trash.data, "%s%s",
328 (use & ((1 << bit) - 1)) ? "," : "",
329 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100332}
333
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100334/* return a pointer to the correct sample checkpoint name, or "unknown" when
335 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
336 * if set.
337 */
338const char *sample_ckp_names(unsigned int use)
339{
340 int bit;
341
342 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
343 if (use & (1 << bit))
344 return fetch_ckp_names[bit];
345 return "unknown sample check place, please report this bug";
346}
347
Emeric Brun107ca302010-01-04 16:16:05 +0100348/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
350 * for next parsing sessions. The fetch keywords capabilities are also computed
351 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100352 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100353void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100354{
Willy Tarreau80aca902013-01-07 15:42:20 +0100355 struct sample_fetch *sf;
356 int bit;
357
358 for (sf = kwl->kw; sf->kw != NULL; sf++) {
359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
360 if (sf->use & (1 << bit))
361 sf->val |= fetch_cap[bit];
362 }
363 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100364}
365
366/*
Willy Tarreau12785782012-04-27 21:37:17 +0200367 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100368 * parsing sessions.
369 */
Willy Tarreau12785782012-04-27 21:37:17 +0200370void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100371{
Willy Tarreau12785782012-04-27 21:37:17 +0200372 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100373}
374
375/*
Willy Tarreau12785782012-04-27 21:37:17 +0200376 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100377 * string of <len> in buffer <kw>.
378 *
379 */
Willy Tarreau12785782012-04-27 21:37:17 +0200380struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100381{
382 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200383 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100384
Willy Tarreau12785782012-04-27 21:37:17 +0200385 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100386 for (index = 0; kwl->kw[index].kw != NULL; index++) {
387 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
388 kwl->kw[index].kw[len] == '\0')
389 return &kwl->kw[index];
390 }
391 }
392 return NULL;
393}
394
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100395/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100396 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397 * <idx> is the index of the next sample fetch entry. It is used as private
398 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100401 * the list is reached.
402 */
403struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
404{
405 struct sample_fetch_kw_list *kwl;
406 struct sample_fetch *base;
407
408 if (!current) {
409 /* Get first kwl entry. */
410 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
411 (*idx) = 0;
412 } else {
413 /* Get kwl corresponding to the curret entry. */
414 base = current + 1 - (*idx);
415 kwl = container_of(base, struct sample_fetch_kw_list, kw);
416 }
417
418 while (1) {
419
420 /* Check if kwl is the last entry. */
421 if (&kwl->list == &sample_fetches.list)
422 return NULL;
423
424 /* idx contain the next keyword. If it is available, return it. */
425 if (kwl->kw[*idx].kw) {
426 (*idx)++;
427 return &kwl->kw[(*idx)-1];
428 }
429
430 /* get next entry in the main list, and return NULL if the end is reached. */
431 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
432
433 /* Set index to 0, ans do one other loop. */
434 (*idx) = 0;
435 }
436}
437
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100438/* This function browses the list of available converters. <current> is
439 * the last used converter. If it is the first call, it must set to NULL.
440 * <idx> is the index of the next converter entry. It is used as private
441 * value. It is useless to initiate it.
442 *
443 * It returns always the next sample_conv entry, and NULL when the end of
444 * the list is reached.
445 */
446struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
447{
448 struct sample_conv_kw_list *kwl;
449 struct sample_conv *base;
450
451 if (!current) {
452 /* Get first kwl entry. */
453 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
454 (*idx) = 0;
455 } else {
456 /* Get kwl corresponding to the curret entry. */
457 base = current + 1 - (*idx);
458 kwl = container_of(base, struct sample_conv_kw_list, kw);
459 }
460
461 while (1) {
462 /* Check if kwl is the last entry. */
463 if (&kwl->list == &sample_convs.list)
464 return NULL;
465
466 /* idx contain the next keyword. If it is available, return it. */
467 if (kwl->kw[*idx].kw) {
468 (*idx)++;
469 return &kwl->kw[(*idx)-1];
470 }
471
472 /* get next entry in the main list, and return NULL if the end is reached. */
473 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
474
475 /* Set index to 0, ans do one other loop. */
476 (*idx) = 0;
477 }
478}
479
Emeric Brun107ca302010-01-04 16:16:05 +0100480/*
Willy Tarreau12785782012-04-27 21:37:17 +0200481 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100482 * string of <len> in buffer <kw>.
483 *
484 */
Willy Tarreau12785782012-04-27 21:37:17 +0200485struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100486{
487 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200488 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100489
Willy Tarreau12785782012-04-27 21:37:17 +0200490 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100491 for (index = 0; kwl->kw[index].kw != NULL; index++) {
492 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
493 kwl->kw[index].kw[len] == '\0')
494 return &kwl->kw[index];
495 }
496 }
497 return NULL;
498}
499
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200501/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
503
Willy Tarreau342acb42012-04-23 22:03:39 +0200504static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100505{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100508 return 1;
509}
510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100512{
Willy Tarreau83061a82018-07-13 11:56:34 +0200513 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100516 return 0;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200519 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100521 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100522
523 return 1;
524}
525
Willy Tarreau342acb42012-04-23 22:03:39 +0200526static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100527{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200528 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100530 return 1;
531}
532
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200533static int c_ipv62ip(struct sample *smp)
534{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200535 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100537 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200538 return 1;
539}
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100542{
Willy Tarreau83061a82018-07-13 11:56:34 +0200543 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100546 return 0;
547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100551 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100552 return 1;
553}
554
555/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200556static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100557{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100559}
560*/
561
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200565 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100566 return 1;
567}
568
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200569static int c_int2ipv6(struct sample *smp)
570{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200571 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
572 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 1;
575}
576
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100577static int c_str2addr(struct sample *smp)
578{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
580 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100581 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100585 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100587 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 return 1;
589}
590
Willy Tarreau342acb42012-04-23 22:03:39 +0200591static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100592{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100594 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200595 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100596 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 1;
598}
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100601{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100603 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200604 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100605 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100607}
608
Emeric Brun4b9e8022014-11-03 18:17:10 +0100609/*
610 * The NULL char always enforces the end of string if it is met.
611 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100612 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200613static int c_bin2str(struct sample *smp)
614{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100615 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 for (i = 0; i < smp->data.u.str.data; i++) {
618 if (!smp->data.u.str.area[i]) {
619 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100620 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 return 1;
624}
625
Willy Tarreau342acb42012-04-23 22:03:39 +0200626static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100627{
Willy Tarreau83061a82018-07-13 11:56:34 +0200628 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100629 char *pos;
630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100632 if (!pos)
633 return 0;
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 trash->size = trash->size - (pos - trash->area);
636 trash->area = pos;
637 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200638 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200639 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100640 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100641 return 1;
642}
643
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800644/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200645 * For strings and binary blocks, it also provides a known allocated size with
646 * a length that is capped to the size, and ensures a trailing zero is always
647 * appended for strings. This is necessary for some operations which may
648 * require to extend the length. It returns 0 if it fails, 1 on success.
649 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100650int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200651{
Willy Tarreau83061a82018-07-13 11:56:34 +0200652 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200653
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_SINT:
657 case SMP_T_ADDR:
658 case SMP_T_IPV4:
659 case SMP_T_IPV6:
660 /* These type are not const. */
661 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200662
Christopher Fauletec100512017-07-24 15:38:41 +0200663 case SMP_T_METH:
664 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
665 break;
666 /* Fall through */
667
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100668 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200669 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100670 trash->data = smp->data.type == SMP_T_STR ?
671 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 if (trash->data > trash->size - 1)
673 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200674
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100675 memcpy(trash->area, smp->data.type == SMP_T_STR ?
676 smp->data.u.str.area : smp->data.u.meth.str.area,
677 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200679 smp->data.u.str = *trash;
680 break;
681
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100682 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 trash->data = smp->data.u.str.data;
685 if (trash->data > trash->size)
686 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200687
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200689 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200691
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100692 default:
693 /* Other cases are unexpected. */
694 return 0;
695 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100696
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 /* remove const flag */
698 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200699 return 1;
700}
701
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100702int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100703{
704 return 1;
705}
706
Willy Tarreau342acb42012-04-23 22:03:39 +0200707static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100708{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200709 const char *str;
710 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100711
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100713 return 0;
714
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 str = smp->data.u.str.area;
716 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100717
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200718 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200719 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100720 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100721 return 1;
722}
723
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100724static int c_str2meth(struct sample *smp)
725{
726 enum http_meth_t meth;
727 int len;
728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 len = smp->data.u.str.data;
732 smp->data.u.meth.str.area = smp->data.u.str.area;
733 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 }
735 else
736 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739 return 1;
740}
741
742static int c_meth2str(struct sample *smp)
743{
744 int len;
745 enum http_meth_t meth;
746
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200747 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200749 len = smp->data.u.meth.str.data;
750 smp->data.u.str.area = smp->data.u.meth.str.area;
751 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200752 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100753 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200754 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200757 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100759 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100761 }
762 else {
763 /* Unknown method */
764 return 0;
765 }
766 return 1;
767}
768
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769static int c_addr2bin(struct sample *smp)
770{
Willy Tarreau83061a82018-07-13 11:56:34 +0200771 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200773 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 chk->data = 4;
775 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 chk->data = 16;
779 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200780 }
781 else
782 return 0;
783
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200784 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200785 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200786 return 1;
787}
788
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200789static int c_int2bin(struct sample *smp)
790{
Willy Tarreau83061a82018-07-13 11:56:34 +0200791 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200792
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
794 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200795
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200796 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200797 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200798 return 1;
799}
800
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200801
Emeric Brun107ca302010-01-04 16:16:05 +0100802/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200803/* Sample casts matrix: */
804/* sample_casts[from type][to type] */
805/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100806/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100807
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100808sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200809/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
810/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
811/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200812/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200813/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
814/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200815/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200816/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
817/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
818/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200819};
Emeric Brun107ca302010-01-04 16:16:05 +0100820
Emeric Brun107ca302010-01-04 16:16:05 +0100821/*
Willy Tarreau12785782012-04-27 21:37:17 +0200822 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100823 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200824 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200825 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100826 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100827struct 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 +0100828{
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 const char *begw; /* beginning of word */
830 const char *endw; /* end of word */
831 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200832 struct sample_expr *expr;
833 struct sample_fetch *fetch;
834 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100835 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 char *fkw = NULL;
837 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100838 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100839
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 begw = str[*idx];
841 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
842
843 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100844 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100845 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200846 }
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 /* keep a copy of the current fetch keyword for error reporting */
849 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Willy Tarreau833cc792013-07-24 15:34:19 +0200851 fetch = find_sample_fetch(begw, endw - begw);
852 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 endt = endw;
858 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100859 /* look for the end of this term and skip the opening parenthesis */
860 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 while (*endt && *endt != ')')
862 endt++;
863 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100864 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun107ca302010-01-04 16:16:05 +0100868
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 /* At this point, we have :
870 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100871 * - endw : end of the keyword, first character not part of keyword
872 * nor the opening parenthesis (so first character of args
873 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200874 * - endt : end of the term (=endw or last parenthesis if args are present)
875 */
876
877 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100878 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100879 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200880 }
Emeric Brun107ca302010-01-04 16:16:05 +0100881 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200882
Vincent Bernat02779b62016-04-03 13:48:43 +0200883 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200884 if (!expr)
885 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
887 LIST_INIT(&(expr->conv_exprs));
888 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200889 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100890
Willy Tarreau689a1df2013-12-13 00:40:11 +0100891 /* Note that we call the argument parser even with an empty string,
892 * this allows it to automatically create entries for mandatory
893 * implicit arguments (eg: local proxy name).
894 */
895 al->kw = expr->fetch->kw;
896 al->conv = NULL;
897 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
898 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
899 goto out_error;
900 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200901
Willy Tarreau689a1df2013-12-13 00:40:11 +0100902 if (!expr->arg_p) {
903 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200904 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100905 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
906 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200907 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100908 }
909
Willy Tarreau833cc792013-07-24 15:34:19 +0200910 /* Now process the converters if any. We have two supported syntaxes
911 * for the converters, which can be combined :
912 * - comma-delimited list of converters just after the keyword and args ;
913 * - one converter per keyword
914 * The combination allows to have each keyword being a comma-delimited
915 * series of converters.
916 *
917 * We want to process the former first, then the latter. For this we start
918 * from the beginning of the supposed place in the exiting conv chain, which
919 * starts at the last comma (endt).
920 */
921
922 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200923 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100924 int err_arg;
925 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100926
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 if (*endt == ')') /* skip last closing parenthesis */
928 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100929
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 if (*endt && *endt != ',') {
931 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100932 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200933 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100934 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100935 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200936 }
Emeric Brun107ca302010-01-04 16:16:05 +0100937
Willy Tarreau833cc792013-07-24 15:34:19 +0200938 while (*endt == ',') /* then trailing commas */
939 endt++;
940
Willy Tarreau97108e02016-11-25 07:33:24 +0100941 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200942
943 if (!*begw) {
944 /* none ? skip to next string */
945 (*idx)++;
946 begw = str[*idx];
947 if (!begw || !*begw)
948 break;
949 }
950
951 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
952
953 free(ckw);
954 ckw = my_strndup(begw, endw - begw);
955
956 conv = find_sample_conv(begw, endw - begw);
957 if (!conv) {
958 /* we found an isolated keyword that we don't know, it's not ours */
959 if (begw == str[*idx])
960 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100961 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200962 goto out_error;
963 }
Emeric Brun107ca302010-01-04 16:16:05 +0100964
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 endt = endw;
966 if (*endt == '(') {
967 /* look for the end of this term */
Willy Tarreau46dfd782019-12-17 10:25:29 +0100968 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200969 while (*endt && *endt != ')')
970 endt++;
971 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100972 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200973 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200974 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200975 }
976
977 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100978 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100979 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200980 }
Emeric Brun107ca302010-01-04 16:16:05 +0100981
982 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200983 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100984 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100985 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200986 }
Emeric Brun107ca302010-01-04 16:16:05 +0100987
988 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200989 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200990 if (!conv_expr)
991 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100992
993 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
994 conv_expr->conv = conv;
995
Willy Tarreau46dfd782019-12-17 10:25:29 +0100996 al->kw = expr->fetch->kw;
997 al->conv = conv_expr->conv->kw;
998 argcnt = make_arg_list(endw, endt - endw, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al);
999 if (argcnt < 0) {
1000 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1001 goto out_error;
1002 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001003
Willy Tarreau46dfd782019-12-17 10:25:29 +01001004 if (argcnt && !conv->arg_mask) {
1005 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1006 goto out_error;
1007 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001008
Willy Tarreau46dfd782019-12-17 10:25:29 +01001009 if (!conv_expr->arg_p)
1010 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001011
Willy Tarreau46dfd782019-12-17 10:25:29 +01001012 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1013 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001014 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001015 }
1016 }
Emeric Brun485479d2010-09-23 18:02:19 +02001017
Willy Tarreau833cc792013-07-24 15:34:19 +02001018 out:
1019 free(fkw);
1020 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001021 return expr;
1022
1023out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001024 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001025 expr = NULL;
1026 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001027}
1028
1029/*
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001031 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001032 * Returns a pointer on a typed sample structure containing the result or NULL if
1033 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001034 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001035 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001036 *
1037 * Note: the fetch functions are required to properly set the return type. The
1038 * conversion functions must do so too. However the cast functions do not need
1039 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001040 *
1041 * The caller may indicate in <opt> if it considers the result final or not.
1042 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1043 * if the result is stable or not, according to the following table :
1044 *
1045 * return MAY_CHANGE FINAL Meaning for the sample
1046 * NULL 0 * Not present and will never be (eg: header)
1047 * NULL 1 0 Not present yet, could change (eg: POST param)
1048 * NULL 1 1 Not present yet, will not change anymore
1049 * smp 0 * Present and will not change (eg: header)
1050 * smp 1 0 Present, may change (eg: request length)
1051 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001052 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001053struct sample *sample_process(struct proxy *px, struct session *sess,
1054 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001055 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001056{
Willy Tarreau12785782012-04-27 21:37:17 +02001057 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001058
Willy Tarreau18387e22013-07-25 12:02:38 +02001059 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001060 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001061 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001062 }
Emeric Brun107ca302010-01-04 16:16:05 +01001063
Willy Tarreau1777ea62016-03-10 16:15:46 +01001064 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001065 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001066 return NULL;
1067
Emeric Brun107ca302010-01-04 16:16:05 +01001068 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001069 /* we want to ensure that p->type can be casted into
1070 * conv_expr->conv->in_type. We have 3 possibilities :
1071 * - NULL => not castable.
1072 * - c_none => nothing to do (let's optimize it)
1073 * - other => apply cast and prepare to fail
1074 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001075 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001076 return NULL;
1077
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001078 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1079 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
1081
Willy Tarreau12e50112012-04-25 17:21:49 +02001082 /* OK cast succeeded */
1083
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001084 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001085 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001086 }
1087 return p;
1088}
1089
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001090/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001091 * Resolve all remaining arguments in proxy <p>. Returns the number of
1092 * errors or 0 if everything is fine.
1093 */
1094int smp_resolve_args(struct proxy *p)
1095{
1096 struct arg_list *cur, *bak;
1097 const char *ctx, *where;
1098 const char *conv_ctx, *conv_pre, *conv_pos;
1099 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001100 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001101 struct arg *arg;
1102 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001103 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001104
1105 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1106 struct proxy *px;
1107 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001108 struct stktable *t;
1109 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001110 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001111
1112 arg = cur->arg;
1113
1114 /* prepare output messages */
1115 conv_pre = conv_pos = conv_ctx = "";
1116 if (cur->conv) {
1117 conv_ctx = cur->conv;
1118 conv_pre = "conversion keyword '";
1119 conv_pos = "' for ";
1120 }
1121
1122 where = "in";
1123 ctx = "sample fetch keyword";
1124 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001125 case ARGC_STK: where = "in stick rule in"; break;
1126 case ARGC_TRK: where = "in tracking rule in"; break;
1127 case ARGC_LOG: where = "in log-format string in"; break;
1128 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1129 case ARGC_HRQ: where = "in http-request header format string in"; break;
1130 case ARGC_HRS: where = "in http-response header format string in"; break;
1131 case ARGC_UIF: where = "in unique-id-format string in"; break;
1132 case ARGC_RDR: where = "in redirect format string in"; break;
1133 case ARGC_CAP: where = "in capture rule in"; break;
1134 case ARGC_ACL: ctx = "ACL keyword"; break;
1135 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001136 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001137 }
1138
1139 /* set a few default settings */
1140 px = p;
1141 pname = p->id;
1142
1143 switch (arg->type) {
1144 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001145 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001146 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1147 cur->file, cur->line,
1148 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001149 cfgerr++;
1150 continue;
1151 }
1152
1153 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001154 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001155
1156 if (sname) {
1157 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001158 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001159
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001160 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001161 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001162 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1163 cur->file, cur->line, pname,
1164 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001165 cfgerr++;
1166 break;
1167 }
1168 }
1169 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001170 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001171
1172 srv = findserver(px, sname);
1173 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001174 ha_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",
1175 cur->file, cur->line, sname, pname,
1176 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001177 cfgerr++;
1178 break;
1179 }
1180
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001181 free(arg->data.str.area);
1182 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001183 arg->unresolved = 0;
1184 arg->data.srv = srv;
1185 break;
1186
1187 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001188 if (arg->data.str.data) {
1189 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001190 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001191 }
1192
1193 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001194 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1195 cur->file, cur->line, pname,
1196 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001197 cfgerr++;
1198 break;
1199 }
1200
1201 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001202 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1203 cur->file, cur->line, pname,
1204 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001205 cfgerr++;
1206 break;
1207 }
1208
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001209 free(arg->data.str.area);
1210 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001211 arg->unresolved = 0;
1212 arg->data.prx = px;
1213 break;
1214
1215 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001216 if (arg->data.str.data) {
1217 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001218 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001219 }
1220
1221 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001222 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1223 cur->file, cur->line, pname,
1224 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001225 cfgerr++;
1226 break;
1227 }
1228
1229 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001230 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1231 cur->file, cur->line, pname,
1232 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001233 cfgerr++;
1234 break;
1235 }
1236
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001237 free(arg->data.str.area);
1238 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001239 arg->unresolved = 0;
1240 arg->data.prx = px;
1241 break;
1242
1243 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001244 if (arg->data.str.data)
1245 stktname = arg->data.str.area;
1246 else
1247 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001248
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001249 t = stktable_find_by_name(stktname);
1250 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001251 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001252 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001254 cfgerr++;
1255 break;
1256 }
1257
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001258 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001259 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001260 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001261 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001262 cfgerr++;
1263 break;
1264 }
1265
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001266 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001267 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001268 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001269 cfgerr++;
1270 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001271 }
1272
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001273 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001274 p->next_stkt_ref = t->proxies_list;
1275 t->proxies_list = p;
1276 }
1277
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001278 free(arg->data.str.area);
1279 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001280 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001281 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001282 break;
1283
1284 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001285 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001286 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1287 cur->file, cur->line,
1288 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001289 cfgerr++;
1290 break;
1291 }
1292
1293 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001294 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001295 ul = p->uri_auth->userlist;
1296 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001297 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001298
1299 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001301 cur->file, cur->line,
1302 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001303 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001304 cfgerr++;
1305 break;
1306 }
1307
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001308 free(arg->data.str.area);
1309 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001310 arg->unresolved = 0;
1311 arg->data.usr = ul;
1312 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001313
1314 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001315 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001316 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1317 cur->file, cur->line,
1318 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001319 cfgerr++;
1320 continue;
1321 }
1322
Willy Tarreau46947782015-01-19 19:00:58 +01001323 rflags = 0;
1324 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1325 err = NULL;
1326
Dragan Dosen26743032019-04-30 15:54:36 +02001327 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1329 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001331 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001332 cfgerr++;
1333 continue;
1334 }
1335
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001336 free(arg->data.str.area);
1337 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001338 arg->unresolved = 0;
1339 arg->data.reg = reg;
1340 break;
1341
1342
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001343 }
1344
1345 LIST_DEL(&cur->list);
1346 free(cur);
1347 } /* end of args processing */
1348
1349 return cfgerr;
1350}
1351
1352/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001353 * Process a fetch + format conversion as defined by the sample expression
1354 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001355 * not explicitly set to <smp_type>, but shall be compatible with it as
1356 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1357 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001358 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1359 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1360 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1361 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001362 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1363 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1364 * still hope to get it after waiting longer, and is not converted to string.
1365 * The possible output combinations are the following :
1366 *
1367 * return MAY_CHANGE FINAL Meaning for the sample
1368 * NULL * * Not present and will never be (eg: header)
1369 * smp 0 * Final value converted (eg: header)
1370 * smp 1 0 Not present yet, may appear later (eg: header)
1371 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001373struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001374 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001375 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001376{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001377 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378
Willy Tarreau6c616e02014-06-25 16:56:41 +02001379 memset(smp, 0, sizeof(*smp));
1380
Willy Tarreau192252e2015-04-04 01:47:55 +02001381 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1383 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001385 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001387 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
1389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001390 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001391 return NULL;
1392
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001393 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001394 return smp;
1395}
1396
Christopher Faulet476e5d02016-10-26 11:34:47 +02001397static void release_sample_arg(struct arg *p)
1398{
1399 struct arg *p_back = p;
1400
1401 if (!p)
1402 return;
1403
1404 while (p->type != ARGT_STOP) {
1405 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001406 free(p->data.str.area);
1407 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001408 p->unresolved = 0;
1409 }
1410 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001411 regex_free(p->data.reg);
1412 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001413 }
1414 p++;
1415 }
1416
1417 if (p_back != empty_arg_list)
1418 free(p_back);
1419}
1420
1421void release_sample_expr(struct sample_expr *expr)
1422{
1423 struct sample_conv_expr *conv_expr, *conv_exprb;
1424
1425 if (!expr)
1426 return;
1427
1428 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1429 release_sample_arg(conv_expr->arg_p);
1430 release_sample_arg(expr->arg_p);
1431 free(expr);
1432}
1433
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001435/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001436/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001437/*****************************************************************/
1438
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001439#ifdef DEBUG_EXPR
1440static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1441{
1442 int i;
1443 struct sample tmp;
1444
1445 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001446 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1447 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448 fprintf(stderr, "(undisplayable)");
1449 } else {
1450
1451 /* Copy sample fetch. This put the sample as const, the
1452 * cast will copy data if a transformation is required.
1453 */
1454 memcpy(&tmp, smp, sizeof(struct sample));
1455 tmp.flags = SMP_F_CONST;
1456
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001457 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001458 fprintf(stderr, "(undisplayable)");
1459
1460 else {
1461 /* Display the displayable chars*. */
Willy Tarreau50603262019-12-17 09:00:15 +01001462 fputc('<', stderr);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001463 for (i = 0; i < tmp.data.u.str.data; i++) {
1464 if (isprint(tmp.data.u.str.area[i]))
1465 fputc(tmp.data.u.str.area[i],
1466 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001467 else
1468 fputc('.', stderr);
1469 }
Willy Tarreau50603262019-12-17 09:00:15 +01001470 fputc('>', stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001471 }
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001472 }
Willy Tarreau50603262019-12-17 09:00:15 +01001473 fputc('\n', stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001474 }
1475 return 1;
1476}
1477#endif
1478
Holger Just1bfc24b2017-05-06 00:56:53 +02001479static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1480{
Willy Tarreau83061a82018-07-13 11:56:34 +02001481 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001482 int bin_len;
1483
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001484 trash->data = 0;
1485 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1486 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001487 if (bin_len < 0)
1488 return 0;
1489
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001491 smp->data.u.str = *trash;
1492 smp->data.type = SMP_T_BIN;
1493 smp->flags &= ~SMP_F_CONST;
1494 return 1;
1495}
1496
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001497static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001498{
Willy Tarreau83061a82018-07-13 11:56:34 +02001499 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001500 int b64_len;
1501
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001502 trash->data = 0;
1503 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1504 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001505 if (b64_len < 0)
1506 return 0;
1507
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001508 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001509 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001510 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001511 smp->flags &= ~SMP_F_CONST;
1512 return 1;
1513}
1514
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001515static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1516{
1517 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001518 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001519
1520 memset(&ctx, 0, sizeof(ctx));
1521
1522 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001523 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1524 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001525
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001526 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001527 smp->data.u.str = *trash;
1528 smp->data.type = SMP_T_BIN;
1529 smp->flags &= ~SMP_F_CONST;
1530 return 1;
1531}
1532
Tim Duesterhusd4376302019-06-17 12:41:44 +02001533#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001534static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1535 const char *file, int line, char **err)
1536{
1537 if (args[0].type == ARGT_STOP)
1538 return 1;
1539 if (args[0].type != ARGT_SINT) {
1540 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1541 return 0;
1542 }
1543
1544 switch (args[0].data.sint) {
1545 case 224:
1546 case 256:
1547 case 384:
1548 case 512:
1549 /* this is okay */
1550 return 1;
1551 default:
1552 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1553 return 0;
1554 }
1555}
1556
Tim Duesterhusd4376302019-06-17 12:41:44 +02001557static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1558{
1559 struct buffer *trash = get_trash_chunk();
1560 int bits = 256;
1561 if (arg_p && arg_p->data.sint)
1562 bits = arg_p->data.sint;
1563
1564 switch (bits) {
1565 case 224: {
1566 SHA256_CTX ctx;
1567
1568 memset(&ctx, 0, sizeof(ctx));
1569
1570 SHA224_Init(&ctx);
1571 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1572 SHA224_Final((unsigned char *) trash->area, &ctx);
1573 trash->data = SHA224_DIGEST_LENGTH;
1574 break;
1575 }
1576 case 256: {
1577 SHA256_CTX ctx;
1578
1579 memset(&ctx, 0, sizeof(ctx));
1580
1581 SHA256_Init(&ctx);
1582 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1583 SHA256_Final((unsigned char *) trash->area, &ctx);
1584 trash->data = SHA256_DIGEST_LENGTH;
1585 break;
1586 }
1587 case 384: {
1588 SHA512_CTX ctx;
1589
1590 memset(&ctx, 0, sizeof(ctx));
1591
1592 SHA384_Init(&ctx);
1593 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1594 SHA384_Final((unsigned char *) trash->area, &ctx);
1595 trash->data = SHA384_DIGEST_LENGTH;
1596 break;
1597 }
1598 case 512: {
1599 SHA512_CTX ctx;
1600
1601 memset(&ctx, 0, sizeof(ctx));
1602
1603 SHA512_Init(&ctx);
1604 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1605 SHA512_Final((unsigned char *) trash->area, &ctx);
1606 trash->data = SHA512_DIGEST_LENGTH;
1607 break;
1608 }
1609 default:
1610 return 0;
1611 }
1612
1613 smp->data.u.str = *trash;
1614 smp->data.type = SMP_T_BIN;
1615 smp->flags &= ~SMP_F_CONST;
1616 return 1;
1617}
1618#endif
1619
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001620static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001621{
Willy Tarreau83061a82018-07-13 11:56:34 +02001622 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001623 unsigned char c;
1624 int ptr = 0;
1625
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001626 trash->data = 0;
1627 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1628 c = smp->data.u.str.area[ptr++];
1629 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1630 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001631 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001632 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001633 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001634 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001635 return 1;
1636}
1637
Dragan Dosen3f957b22017-10-24 09:27:34 +02001638static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1639{
1640 long long int n = 0;
1641 int i, c;
1642
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001643 for (i = 0; i < smp->data.u.str.data; i++) {
1644 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001645 return 0;
1646 n = (n << 4) + c;
1647 }
1648
1649 smp->data.u.sint = n;
1650 smp->data.type = SMP_T_SINT;
1651 smp->flags &= ~SMP_F_CONST;
1652 return 1;
1653}
1654
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001655/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001656static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001657{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001658 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1659 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001660 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001661 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001662 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001663 return 1;
1664}
1665
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001666static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001667{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001668 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001669 smp->data.u.sint = i;
1670 smp->data.type = SMP_T_SINT;
1671 return 1;
1672}
1673
1674
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001675static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001676{
1677 int i;
1678
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001679 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001680 return 0;
1681
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001682 for (i = 0; i < smp->data.u.str.data; i++) {
1683 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1684 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001685 }
1686 return 1;
1687}
1688
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001689static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001690{
1691 int i;
1692
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001693 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001694 return 0;
1695
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001696 for (i = 0; i < smp->data.u.str.data; i++) {
1697 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1698 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001699 }
1700 return 1;
1701}
1702
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001703/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1704static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001705{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001706 /* Attempt to convert to IPv4 to apply the correct mask. */
1707 c_ipv62ip(smp);
1708
1709 if (smp->data.type == SMP_T_IPV4) {
1710 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1711 smp->data.type = SMP_T_IPV4;
1712 }
1713 else if (smp->data.type == SMP_T_IPV6) {
1714 /* IPv6 cannot be converted without an IPv6 mask. */
1715 if (args[1].type != ARGT_IPV6)
1716 return 0;
1717
1718 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1719 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1720 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1721 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1722 smp->data.type = SMP_T_IPV6;
1723 }
1724
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001725 return 1;
1726}
1727
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001728/* takes an UINT value on input supposed to represent the time since EPOCH,
1729 * adds an optional offset found in args[1] and emits a string representing
1730 * the local time in the format specified in args[1] using strftime().
1731 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001732static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001733{
Willy Tarreau83061a82018-07-13 11:56:34 +02001734 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001735 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001736 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001737 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001738
1739 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001740 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001741 curr_date += args[1].data.sint;
1742
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001743 tm = localtime(&curr_date);
1744 if (!tm)
1745 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001746 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1748 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001749 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001750 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001751 return 1;
1752}
1753
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001754/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001755static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001756{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001757 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1758 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001759 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001760 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001761 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001762 return 1;
1763}
1764
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001765/* takes an UINT value on input supposed to represent the time since EPOCH,
1766 * adds an optional offset found in args[1] and emits a string representing
1767 * the UTC date in the format specified in args[1] using strftime().
1768 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001769static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001770{
Willy Tarreau83061a82018-07-13 11:56:34 +02001771 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001772 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001773 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001774 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001775
1776 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001777 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001778 curr_date += args[1].data.sint;
1779
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001780 tm = gmtime(&curr_date);
1781 if (!tm)
1782 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001783 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001784 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1785 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001786 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001787 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001788 return 1;
1789}
1790
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001791/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001792static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001793{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001794 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1795 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001796 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001797 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001798 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001799 return 1;
1800}
1801
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001802/* hashes the binary input into a 32-bit unsigned int using xxh.
1803 * The seed of the hash defaults to 0 but can be changd in argument 1.
1804 */
1805static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1806{
1807 unsigned int seed;
1808
1809 if (arg_p && arg_p->data.sint)
1810 seed = arg_p->data.sint;
1811 else
1812 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001813 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1814 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001815 smp->data.type = SMP_T_SINT;
1816 return 1;
1817}
1818
1819/* hashes the binary input into a 64-bit unsigned int using xxh.
1820 * In fact, the function returns a 64 bit unsigned, but the sample
1821 * storage of haproxy only proposes 64-bits signed, so the value is
1822 * cast as signed. This cast doesn't impact the hash repartition.
1823 * The seed of the hash defaults to 0 but can be changd in argument 1.
1824 */
1825static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1826{
1827 unsigned long long int seed;
1828
1829 if (arg_p && arg_p->data.sint)
1830 seed = (unsigned long long int)arg_p->data.sint;
1831 else
1832 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001833 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1834 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001835 smp->data.type = SMP_T_SINT;
1836 return 1;
1837}
1838
Willy Tarreau80599772015-01-20 19:35:24 +01001839/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001840static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001841{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001842 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1843 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001844 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001845 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001846 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001847 return 1;
1848}
1849
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001850/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1851static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1852{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1854 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001855 if (arg_p && arg_p->data.sint)
1856 smp->data.u.sint = full_hash(smp->data.u.sint);
1857 smp->data.type = SMP_T_SINT;
1858 return 1;
1859}
1860
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001861/* This function escape special json characters. The returned string can be
1862 * safely set between two '"' and used as json string. The json string is
1863 * defined like this:
1864 *
1865 * any Unicode character except '"' or '\' or control character
1866 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1867 *
1868 * The enum input_type contain all the allowed mode for decoding the input
1869 * string.
1870 */
1871enum input_type {
1872 IT_ASCII = 0,
1873 IT_UTF8,
1874 IT_UTF8S,
1875 IT_UTF8P,
1876 IT_UTF8PS,
1877};
1878static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1879 const char *file, int line, char **err)
1880{
1881 if (!arg) {
1882 memprintf(err, "Unexpected empty arg list");
1883 return 0;
1884 }
1885
1886 if (arg->type != ARGT_STR) {
1887 memprintf(err, "Unexpected arg type");
1888 return 0;
1889 }
1890
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001891 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001892 arg->type = ARGT_SINT;
1893 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001894 return 1;
1895 }
1896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001897 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001898 arg->type = ARGT_SINT;
1899 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001900 return 1;
1901 }
1902
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001903 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001904 arg->type = ARGT_SINT;
1905 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001906 return 1;
1907 }
1908
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001909 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001910 arg->type = ARGT_SINT;
1911 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001912 return 1;
1913 }
1914
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001915 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001916 arg->type = ARGT_SINT;
1917 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001918 return 1;
1919 }
1920
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001921 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001922 arg->type = ARGT_SINT;
1923 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001924 return 1;
1925 }
1926
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001927 memprintf(err, "Unexpected input code type. "
1928 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001929 return 0;
1930}
1931
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001932static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001933{
Willy Tarreau83061a82018-07-13 11:56:34 +02001934 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001935 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1936 const char *str;
1937 int len;
1938 enum input_type input_type = IT_ASCII;
1939 unsigned int c;
1940 unsigned int ret;
1941 char *p;
1942
1943 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001944 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001945
1946 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001947 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001948
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001949 p = smp->data.u.str.area;
1950 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001951
1952 if (input_type == IT_ASCII) {
1953 /* Read input as ASCII. */
1954 c = *(unsigned char *)p;
1955 p++;
1956 }
1957 else {
1958 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001959 ret = utf8_next(p,
1960 smp->data.u.str.data - ( p - smp->data.u.str.area),
1961 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001962 p += utf8_return_length(ret);
1963
1964 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1965 return 0;
1966 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1967 continue;
1968 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1969 return 0;
1970 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1971 continue;
1972
1973 /* Check too big values. */
1974 if ((unsigned int)c > 0xffff) {
1975 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1976 return 0;
1977 continue;
1978 }
1979 }
1980
1981 /* Convert character. */
1982 if (c == '"') {
1983 len = 2;
1984 str = "\\\"";
1985 }
1986 else if (c == '\\') {
1987 len = 2;
1988 str = "\\\\";
1989 }
1990 else if (c == '/') {
1991 len = 2;
1992 str = "\\/";
1993 }
1994 else if (c == '\b') {
1995 len = 2;
1996 str = "\\b";
1997 }
1998 else if (c == '\f') {
1999 len = 2;
2000 str = "\\f";
2001 }
2002 else if (c == '\r') {
2003 len = 2;
2004 str = "\\r";
2005 }
2006 else if (c == '\n') {
2007 len = 2;
2008 str = "\\n";
2009 }
2010 else if (c == '\t') {
2011 len = 2;
2012 str = "\\t";
2013 }
2014 else if (c > 0xff || !isprint(c)) {
2015 /* isprint generate a segfault if c is too big. The man says that
2016 * c must have the value of an unsigned char or EOF.
2017 */
2018 len = 6;
2019 _str[0] = '\\';
2020 _str[1] = 'u';
2021 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2022 str = _str;
2023 }
2024 else {
2025 len = 1;
2026 str = (char *)&c;
2027 }
2028
2029 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002030 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002031 return 0;
2032
2033 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002034 memcpy(temp->area + temp->data, str, len);
2035 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002036 }
2037
2038 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002039 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002040 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002041
2042 return 1;
2043}
2044
Emeric Brun54c4ac82014-11-03 15:32:43 +01002045/* This sample function is designed to extract some bytes from an input buffer.
2046 * First arg is the offset.
2047 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002048static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002049{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002050 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2051 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002052 return 1;
2053 }
2054
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002055 if (smp->data.u.str.size)
2056 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002057 smp->data.u.str.data -= arg_p[0].data.sint;
2058 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002059
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002060 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2061 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002062
2063 return 1;
2064}
2065
Emeric Brunf399b0d2014-11-03 17:07:03 +01002066static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2067 const char *file, int line, char **err)
2068{
2069 struct arg *arg = args;
2070
2071 if (!arg) {
2072 memprintf(err, "Unexpected empty arg list");
2073 return 0;
2074 }
2075
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002076 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002077 memprintf(err, "Unexpected arg type");
2078 return 0;
2079 }
2080
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002081 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002082 memprintf(err, "Unexpected value 0 for index");
2083 return 0;
2084 }
2085
2086 arg++;
2087
2088 if (arg->type != ARGT_STR) {
2089 memprintf(err, "Unexpected arg type");
2090 return 0;
2091 }
2092
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002094 memprintf(err, "Empty separators list");
2095 return 0;
2096 }
2097
2098 return 1;
2099}
2100
2101/* This sample function is designed to a return selected part of a string (field).
2102 * First arg is the index of the field (start at 1)
2103 * Second arg is a char list of separators (type string)
2104 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002105static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002106{
Marcin Deranek9631a282018-04-16 14:30:46 +02002107 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002108 char *start, *end;
2109 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002110 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002111
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002112 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002113 return 0;
2114
Marcin Deranek9631a282018-04-16 14:30:46 +02002115 if (arg_p[0].data.sint < 0) {
2116 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002117 end = start = smp->data.u.str.area + smp->data.u.str.data;
2118 while (start > smp->data.u.str.area) {
2119 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2120 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002121 if (field == arg_p[0].data.sint) {
2122 if (count == 1)
2123 goto found;
2124 else if (count > 1)
2125 count--;
2126 } else {
2127 end = start-1;
2128 field--;
2129 }
2130 break;
2131 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002132 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002133 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002134 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002135 } else {
2136 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002137 end = start = smp->data.u.str.area;
2138 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2139 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2140 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002141 if (field == arg_p[0].data.sint) {
2142 if (count == 1)
2143 goto found;
2144 else if (count > 1)
2145 count--;
2146 } else {
2147 start = end+1;
2148 field++;
2149 }
2150 break;
2151 }
2152 }
2153 end++;
2154 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002155 }
2156
2157 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002158 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002159 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002160 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002161 }
2162found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002163 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002164 /* If ret string is len 0, no need to
2165 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002166 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002167 return 1;
2168
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002169 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002170
2171 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002172 Note: smp->data.u.str.size cannot be set to 0 */
2173 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002175
2176 return 1;
2177}
2178
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002179/* This sample function is designed to return a word from a string.
2180 * First arg is the index of the word (start at 1)
2181 * Second arg is a char list of words separators (type string)
2182 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002183static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002184{
Marcin Deranek9631a282018-04-16 14:30:46 +02002185 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002186 char *start, *end;
2187 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002188 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002189
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002190 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002191 return 0;
2192
2193 word = 0;
2194 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002195 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002196 end = start = smp->data.u.str.area + smp->data.u.str.data;
2197 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002198 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002199 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2200 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002201 issep = 1;
2202 break;
2203 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002204 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002205 if (!inword) {
2206 if (!issep) {
2207 if (word != arg_p[0].data.sint) {
2208 word--;
2209 end = start;
2210 }
2211 inword = 1;
2212 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002213 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002214 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002215 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002216 if (count == 1)
2217 goto found;
2218 else if (count > 1)
2219 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002220 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002221 inword = 0;
2222 }
2223 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002224 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002225 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002226 end = start = smp->data.u.str.area;
2227 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002228 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002229 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2230 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002231 issep = 1;
2232 break;
2233 }
2234 }
2235 if (!inword) {
2236 if (!issep) {
2237 if (word != arg_p[0].data.sint) {
2238 word++;
2239 start = end;
2240 }
2241 inword = 1;
2242 }
2243 }
2244 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002245 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002246 if (count == 1)
2247 goto found;
2248 else if (count > 1)
2249 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002250 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002251 inword = 0;
2252 }
2253 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002254 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002255 }
2256
2257 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002258 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002259 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002260 return 1;
2261 }
2262found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002263 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002264 /* If ret string is len 0, no need to
2265 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002267 return 1;
2268
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002269 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002270
2271 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002272 Note: smp->data.u.str.size cannot be set to 0 */
2273 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002274 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002275
2276 return 1;
2277}
2278
Willy Tarreau7eda8492015-01-20 19:47:06 +01002279static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2280 const char *file, int line, char **err)
2281{
2282 struct arg *arg = args;
2283 char *p;
2284 int len;
2285
2286 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2287 arg[0].type_flags = 0;
2288
2289 if (arg[2].type != ARGT_STR)
2290 return 1;
2291
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002292 p = arg[2].data.str.area;
2293 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002294 while (len) {
2295 if (*p == 'i') {
2296 arg[0].type_flags |= ARGF_REG_ICASE;
2297 }
2298 else if (*p == 'g') {
2299 arg[0].type_flags |= ARGF_REG_GLOB;
2300 }
2301 else {
2302 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2303 return 0;
2304 }
2305 p++;
2306 len--;
2307 }
2308 return 1;
2309}
2310
2311/* This sample function is designed to do the equivalent of s/match/replace/ on
2312 * the input string. It applies a regex and restarts from the last matched
2313 * location until nothing matches anymore. First arg is the regex to apply to
2314 * the input string, second arg is the replacement expression.
2315 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002316static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002317{
2318 char *start, *end;
2319 struct my_regex *reg = arg_p[0].data.reg;
2320 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002321 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002322 int flag, max;
2323 int found;
2324
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 start = smp->data.u.str.area;
2326 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002327
2328 flag = 0;
2329 while (1) {
2330 /* check for last round which is used to copy remaining parts
2331 * when not running in global replacement mode.
2332 */
2333 found = 0;
2334 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2335 /* Note: we can have start == end on empty strings or at the end */
2336 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2337 }
2338
2339 if (!found)
2340 pmatch[0].rm_so = end - start;
2341
2342 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002343 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002344 if (max && pmatch[0].rm_so > 0) {
2345 if (max > pmatch[0].rm_so)
2346 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002347 memcpy(trash->area + trash->data, start, max);
2348 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002349 }
2350
2351 if (!found)
2352 break;
2353
2354 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002355 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002356 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002357 if (max > arg_p[1].data.str.data)
2358 max = arg_p[1].data.str.data;
2359 memcpy(trash->area + trash->data,
2360 arg_p[1].data.str.area, max);
2361 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002362 }
2363
2364 /* stop here if we're done with this string */
2365 if (start >= end)
2366 break;
2367
2368 /* We have a special case for matches of length 0 (eg: "x*y*").
2369 * These ones are considered to match in front of a character,
2370 * so we have to copy that character and skip to the next one.
2371 */
2372 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002373 if (trash->data < trash->size)
2374 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002375 pmatch[0].rm_eo++;
2376 }
2377
2378 start += pmatch[0].rm_eo;
2379 flag |= REG_NOTBOL;
2380 }
2381
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002382 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002383 return 1;
2384}
2385
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002386/* This function check an operator entry. It expects a string.
2387 * The string can be an integer or a variable name.
2388 */
2389static int check_operator(struct arg *args, struct sample_conv *conv,
2390 const char *file, int line, char **err)
2391{
2392 const char *str;
2393 const char *end;
2394
2395 /* Try to decode a variable. */
2396 if (vars_check_arg(&args[0], NULL))
2397 return 1;
2398
2399 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002401 end = str + strlen(str);
2402 args[0].data.sint = read_int64(&str, end);
2403 if (*str != '\0') {
2404 memprintf(err, "expects an integer or a variable name");
2405 return 0;
2406 }
2407 args[0].type = ARGT_SINT;
2408 return 1;
2409}
2410
Willy Tarreau6204cd92016-03-10 16:33:04 +01002411/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002412 * If the arg contain an integer, the integer is returned in the
2413 * sample. If the arg contains a variable descriptor, it returns the
2414 * variable value.
2415 *
2416 * This function returns 0 if an error occurs, otherwise it returns 1.
2417 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002418static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002419{
2420 switch (arg->type) {
2421 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002422 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002423 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002424 return 1;
2425 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002426 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002427 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002428 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002429 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002430 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002431 return 0;
2432 return 1;
2433 default:
2434 return 0;
2435 }
2436}
2437
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002438/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002439 * result.
2440 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002441static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002442{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002443 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002444 return 1;
2445}
2446
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002447/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002448 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002449 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002450static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002451{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002452 struct sample tmp;
2453
Willy Tarreau7560dd42016-03-10 16:28:58 +01002454 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002455 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002456 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002457 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002458 return 1;
2459}
2460
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002461/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002462 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002463 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002464static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002465{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002466 struct sample tmp;
2467
Willy Tarreau7560dd42016-03-10 16:28:58 +01002468 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002469 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002470 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002471 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002472 return 1;
2473}
2474
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002475/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002476 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002477 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002478static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002479{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002480 struct sample tmp;
2481
Willy Tarreau7560dd42016-03-10 16:28:58 +01002482 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002483 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002484 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002485 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002486 return 1;
2487}
2488
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002489static inline long long int arith_add(long long int a, long long int b)
2490{
2491 /* Prevent overflow and makes capped calculus.
2492 * We must ensure that the check calculus doesn't
2493 * exceed the signed 64 bits limits.
2494 *
2495 * +----------+----------+
2496 * | a<0 | a>=0 |
2497 * +------+----------+----------+
2498 * | b<0 | MIN-a>b | no check |
2499 * +------+----------+----------+
2500 * | b>=0 | no check | MAX-a<b |
2501 * +------+----------+----------+
2502 */
2503 if ((a ^ b) >= 0) {
2504 /* signs are differents. */
2505 if (a < 0) {
2506 if (LLONG_MIN - a > b)
2507 return LLONG_MIN;
2508 }
2509 if (LLONG_MAX - a < b)
2510 return LLONG_MAX;
2511 }
2512 return a + b;
2513}
2514
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002515/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002516 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002517 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002518static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002519{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002520 struct sample tmp;
2521
Willy Tarreau7560dd42016-03-10 16:28:58 +01002522 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002523 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002524 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002525 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002526 return 1;
2527}
2528
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002529/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002530 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002531 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002532static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002533 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002534{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002535 struct sample tmp;
2536
Willy Tarreau7560dd42016-03-10 16:28:58 +01002537 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002538 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002539 return 0;
2540
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002541 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2542 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2543 * of -LLONG_MIN and correct the result.
2544 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002545 if (tmp.data.u.sint == LLONG_MIN) {
2546 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2547 if (smp->data.u.sint < LLONG_MAX)
2548 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002549 return 1;
2550 }
2551
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002552 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002553 * the second operand.
2554 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002555 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002556 return 1;
2557}
2558
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002559/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002560 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002561 * If the result makes an overflow, then the largest possible quantity is
2562 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002563 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002564static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002565 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002566{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002567 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002568 long long int c;
2569
Willy Tarreau7560dd42016-03-10 16:28:58 +01002570 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002571 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002572 return 0;
2573
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002574 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002575 if (!smp->data.u.sint || !tmp.data.u.sint) {
2576 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002577 return 1;
2578 }
2579
2580 /* The multiply between LLONG_MIN and -1 returns a
2581 * "floting point exception".
2582 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002583 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2584 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002585 return 1;
2586 }
2587
2588 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002589 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002590
2591 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002592 if (smp->data.u.sint != c / tmp.data.u.sint) {
2593 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2594 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002595 return 1;
2596 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002597 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002598 return 1;
2599 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002600 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002601 return 1;
2602}
2603
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002604/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002605 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002606 * If arg_p makes the result overflow, then the largest possible quantity is
2607 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002608 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002609static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002610 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002611{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002612 struct sample tmp;
2613
Willy Tarreau7560dd42016-03-10 16:28:58 +01002614 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002615 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002616 return 0;
2617
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002618 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002619 /* The divide between LLONG_MIN and -1 returns a
2620 * "floting point exception".
2621 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002622 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2623 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002624 return 1;
2625 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002626 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002627 return 1;
2628 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002629 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002630 return 1;
2631}
2632
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002633/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002634 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002635 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002636 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002637static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002638 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002639{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002640 struct sample tmp;
2641
Willy Tarreau7560dd42016-03-10 16:28:58 +01002642 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002643 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002644 return 0;
2645
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002646 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002647 /* The divide between LLONG_MIN and -1 returns a
2648 * "floting point exception".
2649 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002650 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2651 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002652 return 1;
2653 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002654 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002655 return 1;
2656 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002657 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002658 return 1;
2659}
2660
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002661/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002662 * result.
2663 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002664static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002665 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002666{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002667 if (smp->data.u.sint == LLONG_MIN)
2668 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002669 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002670 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002671 return 1;
2672}
2673
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002674/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002675 * false. The output is a BOOL.
2676 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002677static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002678 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002679{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002680 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002681 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002682 return 1;
2683}
2684
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002685/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002686 * truee. The output is a BOOL.
2687 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002688static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002689 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002690{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002691 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002692 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002693 return 1;
2694}
2695
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002696/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002697 * The output is a BOOL.
2698 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002699static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002700 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002701{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002702 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002703 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002704 return 1;
2705}
2706
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002707/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002708 * The output is a BOOL.
2709 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002710static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002711 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002712{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002713 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002714 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002715 return 1;
2716}
2717
Willy Tarreau280f42b2018-02-19 15:34:12 +01002718/* appends an optional const string, an optional variable contents and another
2719 * optional const string to an existing string.
2720 */
2721static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2722{
Willy Tarreau83061a82018-07-13 11:56:34 +02002723 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002724 struct sample tmp;
2725 int max;
2726
2727 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 trash->data = smp->data.u.str.data;
2729 if (trash->data > trash->size - 1)
2730 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002731
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002732 memcpy(trash->area, smp->data.u.str.area, trash->data);
2733 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002734
2735 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002736 max = arg_p[0].data.str.data;
2737 if (max > trash->size - 1 - trash->data)
2738 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002739
2740 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002741 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2742 trash->data += max;
2743 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002744 }
2745
2746 /* append second string (variable) if it's found and we can turn it
2747 * into a string.
2748 */
2749 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2750 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2751 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2752 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2753
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002754 max = tmp.data.u.str.data;
2755 if (max > trash->size - 1 - trash->data)
2756 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002757
2758 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002759 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2760 max);
2761 trash->data += max;
2762 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002763 }
2764 }
2765
2766 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002767 max = arg_p[2].data.str.data;
2768 if (max > trash->size - 1 - trash->data)
2769 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002770
2771 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002772 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2773 trash->data += max;
2774 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002775 }
2776
2777 smp->data.u.str = *trash;
2778 smp->data.type = SMP_T_STR;
2779 return 1;
2780}
2781
2782/* This function checks the "concat" converter's arguments and extracts the
2783 * variable name and its scope.
2784 */
2785static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2786 const char *file, int line, char **err)
2787{
2788 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002789 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2790 memprintf(err, "failed to register variable name '%s'",
2791 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002792 return 0;
2793 }
2794 return 1;
2795}
2796
Tim Duesterhusca097c12018-04-27 21:18:45 +02002797/* compares string with a variable containing a string. Return value
2798 * is compatible with strcmp(3)'s return value.
2799 */
2800static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2801{
2802 struct sample tmp;
2803 int max, result;
2804
2805 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2806 if (arg_p[0].type != ARGT_VAR)
2807 return 0;
2808 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2809 return 0;
2810 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2811 return 0;
2812
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2814 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002815 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002816 if (smp->data.u.str.data != tmp.data.u.str.data) {
2817 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002818 result = -1;
2819 }
2820 else {
2821 result = 1;
2822 }
2823 }
2824 }
2825
2826 smp->data.u.sint = result;
2827 smp->data.type = SMP_T_SINT;
2828 return 1;
2829}
2830
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002831#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2832#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2833#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2834
2835/*
2836 * Extract the field value of an input binary sample. Takes a mandatory argument:
2837 * the protocol buffers field identifier (dotted notation) internally represented
2838 * as an array of unsigned integers and its size.
2839 * Return 1 if the field was found, 0 if not.
2840 */
2841static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2842{
2843 unsigned char *pos;
2844 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002845
2846 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002847 grpc_left = smp->data.u.str.data;
2848
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002849 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002850 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002851
2852 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2853
2854 pos += GRPC_MSG_HEADER_SZ;
2855 grpc_left -= GRPC_MSG_HEADER_SZ;
2856
2857 if (grpc_left < left)
2858 return 0;
2859
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002860 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2861 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002862
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002863 grpc_left -= grpc_msg_len;
2864 }
2865
2866 return 0;
2867}
2868
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002869static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2870{
2871 unsigned char *pos;
2872 size_t left;
2873
2874 pos = (unsigned char *)smp->data.u.str.area;
2875 left = smp->data.u.str.data;
2876
2877 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2878}
2879
2880static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2881 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002882{
2883 if (!args[1].type) {
2884 args[1].type = ARGT_SINT;
2885 args[1].data.sint = PBUF_T_BINARY;
2886 }
2887 else {
2888 int pbuf_type;
2889
2890 pbuf_type = protobuf_type(args[1].data.str.area);
2891 if (pbuf_type == -1) {
2892 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2893 return 0;
2894 }
2895
2896 args[1].type = ARGT_SINT;
2897 args[1].data.sint = pbuf_type;
2898 }
2899
2900 return 1;
2901}
2902
Tim Duesterhusca097c12018-04-27 21:18:45 +02002903/* This function checks the "strcmp" converter's arguments and extracts the
2904 * variable name and its scope.
2905 */
2906static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2907 const char *file, int line, char **err)
2908{
2909 /* Try to decode a variable. */
2910 if (vars_check_arg(&args[0], NULL))
2911 return 1;
2912
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002913 memprintf(err, "failed to register variable name '%s'",
2914 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002915 return 0;
2916}
2917
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002918/************************************************************************/
2919/* All supported sample fetch functions must be declared here */
2920/************************************************************************/
2921
2922/* force TRUE to be returned at the fetch level */
2923static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002924smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002925{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002926 if (!smp_make_rw(smp))
2927 return 0;
2928
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002929 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002930 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002931 return 1;
2932}
2933
2934/* force FALSE to be returned at the fetch level */
2935static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002936smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002937{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002938 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002939 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002940 return 1;
2941}
2942
2943/* retrieve environment variable $1 as a string */
2944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002945smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002946{
2947 char *env;
2948
2949 if (!args || args[0].type != ARGT_STR)
2950 return 0;
2951
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002952 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002953 if (!env)
2954 return 0;
2955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002956 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002957 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 smp->data.u.str.area = env;
2959 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002960 return 1;
2961}
2962
Damien Claisseae6f1252019-10-30 15:57:28 +00002963/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
2964 * optional string representing the unit of the result: "s" for seconds, "ms" for
2965 * milliseconds and "us" for microseconds.
2966 * Returns 0 on error and non-zero if OK.
2967 */
2968int smp_check_date_unit(struct arg *args, char **err)
2969{
2970 if (args[1].type == ARGT_STR) {
2971 if (strcmp(args[1].data.str.area, "s") == 0) {
2972 args[1].data.sint = TIME_UNIT_S;
2973 }
2974 else if (strcmp(args[1].data.str.area, "ms") == 0) {
2975 args[1].data.sint = TIME_UNIT_MS;
2976 }
2977 else if (strcmp(args[1].data.str.area, "us") == 0) {
2978 args[1].data.sint = TIME_UNIT_US;
2979 }
2980 else {
2981 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
2982 args[1].data.str.area);
2983 return 0;
2984 }
2985 free(args[1].data.str.area);
2986 args[1].data.str.area = NULL;
2987 args[1].type = ARGT_SINT;
2988 }
2989 else if (args[1].type != ARGT_STOP) {
2990 memprintf(err, "Unexpected arg type");
2991 return 0;
2992 }
2993
2994 return 1;
2995}
2996
2997/* retrieve the current local date in epoch time, converts it to milliseconds
2998 * or microseconds if asked to in optional args[1] unit param, and applies an
2999 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003000 */
3001static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003002smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003003{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003004 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003005
Damien Claisseae6f1252019-10-30 15:57:28 +00003006 /* report in milliseconds */
3007 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3008 smp->data.u.sint *= 1000;
3009 smp->data.u.sint += date.tv_usec / 1000;
3010 }
3011 /* report in microseconds */
3012 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3013 smp->data.u.sint *= 1000000;
3014 smp->data.u.sint += date.tv_usec;
3015 }
3016
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003017 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003018 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003019 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003020
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003021 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003022 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3023 return 1;
3024}
3025
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003026/* retrieve the current microsecond part of the date */
3027static int
3028smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3029{
3030 smp->data.u.sint = date.tv_usec;
3031 smp->data.type = SMP_T_SINT;
3032 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3033 return 1;
3034}
3035
3036
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003037/* returns the hostname */
3038static int
3039smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3040{
3041 smp->data.type = SMP_T_STR;
3042 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003043 smp->data.u.str.area = hostname;
3044 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003045 return 1;
3046}
3047
Willy Tarreau0f30d262014-11-24 16:02:05 +01003048/* returns the number of processes */
3049static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003050smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003051{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003052 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003053 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003054 return 1;
3055}
3056
3057/* returns the number of the current process (between 1 and nbproc */
3058static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003059smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003060{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003061 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003063 return 1;
3064}
3065
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003066/* returns the number of the current thread (between 1 and nbthread */
3067static int
3068smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3069{
3070 smp->data.type = SMP_T_SINT;
3071 smp->data.u.sint = tid;
3072 return 1;
3073}
3074
Willy Tarreau84310e22014-02-14 11:59:04 +01003075/* generate a random 32-bit integer for whatever purpose, with an optional
3076 * range specified in argument.
3077 */
3078static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003079smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003080{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003081 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003082
3083 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003084 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003085 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003086
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003087 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003088 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3089 return 1;
3090}
3091
Willy Tarreau0f30d262014-11-24 16:02:05 +01003092/* returns true if the current process is stopping */
3093static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003094smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003095{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003096 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003097 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003098 return 1;
3099}
3100
Willy Tarreau70fe9442018-11-22 16:07:39 +01003101/* returns the number of calls of the current stream's process_stream() */
3102static int
3103smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3104{
3105 smp->data.type = SMP_T_SINT;
3106 smp->data.u.sint = smp->strm->task->calls;
3107 return 1;
3108}
3109
3110/* returns the average number of nanoseconds spent processing the stream per call */
3111static int
3112smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3113{
3114 smp->data.type = SMP_T_SINT;
3115 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3116 return 1;
3117}
3118
3119/* returns the total number of nanoseconds spent processing the stream */
3120static int
3121smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3122{
3123 smp->data.type = SMP_T_SINT;
3124 smp->data.u.sint = smp->strm->task->cpu_time;
3125 return 1;
3126}
3127
3128/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3129static int
3130smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3131{
3132 smp->data.type = SMP_T_SINT;
3133 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3134 return 1;
3135}
3136
3137/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3138static int
3139smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3140{
3141 smp->data.type = SMP_T_SINT;
3142 smp->data.u.sint = smp->strm->task->lat_time;
3143 return 1;
3144}
3145
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003146static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3147{
3148 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003149 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003150 smp->data.u.str.area = args[0].data.str.area;
3151 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003152 return 1;
3153}
3154
3155static int smp_check_const_bool(struct arg *args, char **err)
3156{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003157 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3158 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003159 args[0].type = ARGT_SINT;
3160 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003161 return 1;
3162 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003163 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3164 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003165 args[0].type = ARGT_SINT;
3166 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003167 return 1;
3168 }
3169 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3170 return 0;
3171}
3172
3173static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3174{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003175 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003176 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003177 return 1;
3178}
3179
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003180static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003181{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003182 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003183 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003184 return 1;
3185}
3186
3187static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3188{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003189 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003190 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003191 return 1;
3192}
3193
3194static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3195{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003196 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003197 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003198 return 1;
3199}
3200
3201static int smp_check_const_bin(struct arg *args, char **err)
3202{
David Carlier64a16ab2016-04-08 10:37:02 +01003203 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003204 int binstrlen;
3205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003207 return 0;
3208 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003209 args[0].data.str.area = binstr;
3210 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003211 return 1;
3212}
3213
3214static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3215{
3216 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003217 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003218 smp->data.u.str.area = args[0].data.str.area;
3219 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003220 return 1;
3221}
3222
3223static int smp_check_const_meth(struct arg *args, char **err)
3224{
3225 enum http_meth_t meth;
3226 int i;
3227
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003228 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003229 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003230 args[0].type = ARGT_SINT;
3231 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003232 } else {
3233 /* Check method avalaibility. A methos is a token defined as :
3234 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3235 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3236 * token = 1*tchar
3237 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003238 for (i = 0; i < args[0].data.str.data; i++) {
3239 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003240 memprintf(err, "expects valid method.");
3241 return 0;
3242 }
3243 }
3244 }
3245 return 1;
3246}
3247
3248static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3249{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003250 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003251 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003252 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003253 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003254 smp->data.u.meth.str.area = "";
3255 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003256 } else {
3257 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003258 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003259 smp->data.u.meth.str.area = args[0].data.str.area;
3260 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003261 }
3262 return 1;
3263}
3264
Luca Schimweg8a694b82019-09-10 15:42:52 +02003265// This function checks the "uuid" sample's arguments.
3266// Function won't get called when no parameter is specified (maybe a bug?)
3267static int smp_check_uuid(struct arg *args, char **err)
3268{
3269 if (!args[0].type) {
3270 args[0].type = ARGT_SINT;
3271 args[0].data.sint = 4;
3272 }
3273 else if (args[0].data.sint != 4) {
3274 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3275 return 0;
3276 }
3277
3278 return 1;
3279}
3280
3281// Generate a RFC4122 UUID (default is v4 = fully random)
3282static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3283{
3284 if (args[0].data.sint == 4 || !args[0].type) {
3285 uint32_t rnd[4] = { 0, 0, 0, 0 };
3286 uint64_t last = 0;
3287 int byte = 0;
3288 uint8_t bits = 0;
3289 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3290
3291 while (byte < 4) {
3292 while (bits < 32) {
3293 last |= (uint64_t)random() << bits;
3294 bits += rand_max_bits;
3295 }
3296 rnd[byte++] = last;
3297 last >>= 32u;
3298 bits -= 32;
3299 }
3300
3301 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3302 rnd[0],
3303 rnd[1] & 0xFFFF,
3304 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3305 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3306 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3307 );
3308
3309 smp->data.type = SMP_T_STR;
3310 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3311 smp->data.u.str = trash;
3312 return 1;
3313 }
3314
3315 // more implementations of other uuid formats possible here
3316 return 0;
3317}
3318
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003319/* Note: must not be declared <const> as its list will be overwritten.
3320 * Note: fetches that may return multiple types must be declared as the lowest
3321 * common denominator, the type that can be casted into all other ones. For
3322 * instance IPv4/IPv6 must be declared IPv4.
3323 */
3324static struct sample_fetch_kw_list smp_kws = {ILH, {
3325 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3326 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003327 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003328 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003329 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003330 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003331 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3332 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003333 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003334 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003335 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003336 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003337 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003338
3339 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3340 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3341 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3342 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3343 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003344
3345 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3346 { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003347 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003348 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3349 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3350 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3351 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3352
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003353 { /* END */ },
3354}};
3355
Willy Tarreau0108d902018-11-25 19:14:37 +01003356INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3357
Emeric Brun107ca302010-01-04 16:16:05 +01003358/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003359static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003360#ifdef DEBUG_EXPR
3361 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3362#endif
3363
Holger Just1bfc24b2017-05-06 00:56:53 +02003364 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003365 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003366 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3367 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003368 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003369 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003370 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003371 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003372 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3373 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003374 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003375 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003376 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3377 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3378 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003379 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3380 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003381 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003382 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003383 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3384 { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
Willy Tarreau7eda8492015-01-20 19:47:06 +01003385 { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR },
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02003386 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003387#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003388 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003389#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003390 { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR },
Tim Duesterhusca097c12018-04-27 21:18:45 +02003391 { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT },
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01003392
3393 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003394 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3395 { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau97707872015-01-27 15:12:13 +01003396
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003397 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3398 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3399 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3400 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3401 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3402 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3403 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3404 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3405 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3406 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3407 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3408 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3409 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3410 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003411
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003412 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003413}};
3414
Willy Tarreau0108d902018-11-25 19:14:37 +01003415INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);