blob: d9439fb50bd5f157d29d655214ea7d8de9edbca6 [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 Tarreau0851fd52019-12-17 10:07:25 +010035#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020036#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020037#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010038
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020039#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010040#include <import/xxhash.h>
41
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042/* sample type names */
43const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020044 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_SINT] = "sint",
47 [SMP_T_ADDR] = "addr",
48 [SMP_T_IPV4] = "ipv4",
49 [SMP_T_IPV6] = "ipv6",
50 [SMP_T_STR] = "str",
51 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020052 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020056static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010057
Willy Tarreau12785782012-04-27 21:37:17 +020058/* list head of all known sample fetch keywords */
59static struct sample_fetch_kw_list sample_fetches = {
60 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010061};
62
Willy Tarreau12785782012-04-27 21:37:17 +020063/* list head of all known sample format conversion keywords */
64static struct sample_conv_kw_list sample_convs = {
65 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010066};
67
Willy Tarreau80aca902013-01-07 15:42:20 +010068const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
69 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
115 SMP_VAL_FE_LOG_END),
116
117 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________),
132
133 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
134 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
137 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
138 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
139 SMP_VAL_FE_LOG_END),
140
141 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________),
148
149 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
152 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL_FE_LOG_END),
164
165 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL_FE_LOG_END),
172
173 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL_FE_LOG_END),
180
181 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL___________),
196
197 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
201 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL_FE_LOG_END),
204
205 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
210 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
211 SMP_VAL___________),
212
213 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220
221 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236
237 [SMP_SRC_SSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
243 SMP_VAL_FE_LOG_END),
244};
245
246static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
247 [SMP_SRC_INTRN] = "internal state",
248 [SMP_SRC_LISTN] = "listener",
249 [SMP_SRC_FTEND] = "frontend",
250 [SMP_SRC_L4CLI] = "client address",
251 [SMP_SRC_L5CLI] = "client-side connection",
252 [SMP_SRC_TRACK] = "track counters",
253 [SMP_SRC_L6REQ] = "request buffer",
254 [SMP_SRC_HRQHV] = "HTTP request headers",
255 [SMP_SRC_HRQHP] = "HTTP request",
256 [SMP_SRC_HRQBO] = "HTTP request body",
257 [SMP_SRC_BKEND] = "backend",
258 [SMP_SRC_SERVR] = "server",
259 [SMP_SRC_L4SRV] = "server address",
260 [SMP_SRC_L5SRV] = "server-side connection",
261 [SMP_SRC_L6RES] = "response buffer",
262 [SMP_SRC_HRSHV] = "HTTP response headers",
263 [SMP_SRC_HRSHP] = "HTTP response",
264 [SMP_SRC_HRSBO] = "HTTP response body",
265 [SMP_SRC_RQFIN] = "request buffer statistics",
266 [SMP_SRC_RSFIN] = "response buffer statistics",
267 [SMP_SRC_TXFIN] = "transaction statistics",
268 [SMP_SRC_SSFIN] = "session statistics",
269};
270
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100271static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
272 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
273 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
274 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
275 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
276 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
277 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
278 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
279 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
280 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
281 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
282 [SMP_CKP_BE_SRV_CON] = "server source selection",
283 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
284 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
285 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
286 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
287 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
288 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
289 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
290 [SMP_CKP_FE_LOG_END] = "logs",
291};
292
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100293/* This function returns the type of the data returned by the sample_expr.
294 * It assumes that the <expr> and all of its converters are properly
295 * initialized.
296 */
297inline
298int smp_expr_output_type(struct sample_expr *expr)
299{
300 struct sample_conv_expr *smp_expr;
301
302 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
303 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
304 return smp_expr->conv->out_type;
305 }
306 return expr->fetch->out_type;
307}
308
309
Willy Tarreau80aca902013-01-07 15:42:20 +0100310/* fill the trash with a comma-delimited list of source names for the <use> bit
311 * field which must be composed of a non-null set of SMP_USE_* flags. The return
312 * value is the pointer to the string in the trash buffer.
313 */
314const char *sample_src_names(unsigned int use)
315{
316 int bit;
317
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200318 trash.data = 0;
319 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100320 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
321 if (!(use & ~((1 << bit) - 1)))
322 break; /* no more bits */
323
324 if (!(use & (1 << bit)))
325 continue; /* bit not set */
326
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200327 trash.data += snprintf(trash.area + trash.data,
328 trash.size - trash.data, "%s%s",
329 (use & ((1 << bit) - 1)) ? "," : "",
330 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100331 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200332 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100333}
334
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100335/* return a pointer to the correct sample checkpoint name, or "unknown" when
336 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
337 * if set.
338 */
339const char *sample_ckp_names(unsigned int use)
340{
341 int bit;
342
343 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
344 if (use & (1 << bit))
345 return fetch_ckp_names[bit];
346 return "unknown sample check place, please report this bug";
347}
348
Emeric Brun107ca302010-01-04 16:16:05 +0100349/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100350 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
351 * for next parsing sessions. The fetch keywords capabilities are also computed
352 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100353 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100354void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100355{
Willy Tarreau80aca902013-01-07 15:42:20 +0100356 struct sample_fetch *sf;
357 int bit;
358
359 for (sf = kwl->kw; sf->kw != NULL; sf++) {
360 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
361 if (sf->use & (1 << bit))
362 sf->val |= fetch_cap[bit];
363 }
364 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * parsing sessions.
370 */
Willy Tarreau12785782012-04-27 21:37:17 +0200371void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100372{
Willy Tarreau12785782012-04-27 21:37:17 +0200373 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100374}
375
376/*
Willy Tarreau12785782012-04-27 21:37:17 +0200377 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100378 * string of <len> in buffer <kw>.
379 *
380 */
Willy Tarreau12785782012-04-27 21:37:17 +0200381struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100382{
383 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200384 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100385
Willy Tarreau12785782012-04-27 21:37:17 +0200386 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100387 for (index = 0; kwl->kw[index].kw != NULL; index++) {
388 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
389 kwl->kw[index].kw[len] == '\0')
390 return &kwl->kw[index];
391 }
392 }
393 return NULL;
394}
395
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100396/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100397 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398 * <idx> is the index of the next sample fetch entry. It is used as private
399 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100400 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100401 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 * the list is reached.
403 */
404struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
405{
406 struct sample_fetch_kw_list *kwl;
407 struct sample_fetch *base;
408
409 if (!current) {
410 /* Get first kwl entry. */
411 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
412 (*idx) = 0;
413 } else {
414 /* Get kwl corresponding to the curret entry. */
415 base = current + 1 - (*idx);
416 kwl = container_of(base, struct sample_fetch_kw_list, kw);
417 }
418
419 while (1) {
420
421 /* Check if kwl is the last entry. */
422 if (&kwl->list == &sample_fetches.list)
423 return NULL;
424
425 /* idx contain the next keyword. If it is available, return it. */
426 if (kwl->kw[*idx].kw) {
427 (*idx)++;
428 return &kwl->kw[(*idx)-1];
429 }
430
431 /* get next entry in the main list, and return NULL if the end is reached. */
432 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
433
434 /* Set index to 0, ans do one other loop. */
435 (*idx) = 0;
436 }
437}
438
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100439/* This function browses the list of available converters. <current> is
440 * the last used converter. If it is the first call, it must set to NULL.
441 * <idx> is the index of the next converter entry. It is used as private
442 * value. It is useless to initiate it.
443 *
444 * It returns always the next sample_conv entry, and NULL when the end of
445 * the list is reached.
446 */
447struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
448{
449 struct sample_conv_kw_list *kwl;
450 struct sample_conv *base;
451
452 if (!current) {
453 /* Get first kwl entry. */
454 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
455 (*idx) = 0;
456 } else {
457 /* Get kwl corresponding to the curret entry. */
458 base = current + 1 - (*idx);
459 kwl = container_of(base, struct sample_conv_kw_list, kw);
460 }
461
462 while (1) {
463 /* Check if kwl is the last entry. */
464 if (&kwl->list == &sample_convs.list)
465 return NULL;
466
467 /* idx contain the next keyword. If it is available, return it. */
468 if (kwl->kw[*idx].kw) {
469 (*idx)++;
470 return &kwl->kw[(*idx)-1];
471 }
472
473 /* get next entry in the main list, and return NULL if the end is reached. */
474 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
475
476 /* Set index to 0, ans do one other loop. */
477 (*idx) = 0;
478 }
479}
480
Emeric Brun107ca302010-01-04 16:16:05 +0100481/*
Willy Tarreau12785782012-04-27 21:37:17 +0200482 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100483 * string of <len> in buffer <kw>.
484 *
485 */
Willy Tarreau12785782012-04-27 21:37:17 +0200486struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100487{
488 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200489 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100490
Willy Tarreau12785782012-04-27 21:37:17 +0200491 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100492 for (index = 0; kwl->kw[index].kw != NULL; index++) {
493 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
494 kwl->kw[index].kw[len] == '\0')
495 return &kwl->kw[index];
496 }
497 }
498 return NULL;
499}
500
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200502/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100503/******************************************************************/
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100506{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200508 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100509 return 1;
510}
511
Willy Tarreau342acb42012-04-23 22:03:39 +0200512static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100513{
Willy Tarreau83061a82018-07-13 11:56:34 +0200514 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100517 return 0;
518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200521 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100522 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100523
524 return 1;
525}
526
Willy Tarreau342acb42012-04-23 22:03:39 +0200527static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100528{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200529 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100531 return 1;
532}
533
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534static int c_ipv62ip(struct sample *smp)
535{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200536 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100538 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539 return 1;
540}
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100543{
Willy Tarreau83061a82018-07-13 11:56:34 +0200544 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100545
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100547 return 0;
548
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200551 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100552 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100553 return 1;
554}
555
556/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200557static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100558{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100560}
561*/
562
Willy Tarreau342acb42012-04-23 22:03:39 +0200563static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100567 return 1;
568}
569
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570static int c_int2ipv6(struct sample *smp)
571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
573 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 1;
576}
577
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100578static int c_str2addr(struct sample *smp)
579{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
581 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200583 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100584 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100585 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100586 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100589 return 1;
590}
591
Willy Tarreau342acb42012-04-23 22:03:39 +0200592static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100593{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200594 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100595 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200596 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100597 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100598 return 1;
599}
600
Willy Tarreau342acb42012-04-23 22:03:39 +0200601static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100602{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100604 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100606 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100607 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100608}
609
Emeric Brun4b9e8022014-11-03 18:17:10 +0100610/*
611 * The NULL char always enforces the end of string if it is met.
612 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100613 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614static int c_bin2str(struct sample *smp)
615{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100616 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200617
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 for (i = 0; i < smp->data.u.str.data; i++) {
619 if (!smp->data.u.str.area[i]) {
620 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100621 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200624 return 1;
625}
626
Willy Tarreau342acb42012-04-23 22:03:39 +0200627static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100628{
Willy Tarreau83061a82018-07-13 11:56:34 +0200629 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100630 char *pos;
631
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100633 if (!pos)
634 return 0;
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 trash->size = trash->size - (pos - trash->area);
637 trash->area = pos;
638 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200639 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100641 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100642 return 1;
643}
644
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800645/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200646 * For strings and binary blocks, it also provides a known allocated size with
647 * a length that is capped to the size, and ensures a trailing zero is always
648 * appended for strings. This is necessary for some operations which may
649 * require to extend the length. It returns 0 if it fails, 1 on success.
650 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200652{
Willy Tarreau83061a82018-07-13 11:56:34 +0200653 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200654
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200655 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_SINT:
658 case SMP_T_ADDR:
659 case SMP_T_IPV4:
660 case SMP_T_IPV6:
661 /* These type are not const. */
662 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200663
Christopher Fauletec100512017-07-24 15:38:41 +0200664 case SMP_T_METH:
665 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
666 break;
667 /* Fall through */
668
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100669 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200670 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100671 trash->data = smp->data.type == SMP_T_STR ?
672 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 if (trash->data > trash->size - 1)
674 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200675
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100676 memcpy(trash->area, smp->data.type == SMP_T_STR ?
677 smp->data.u.str.area : smp->data.u.meth.str.area,
678 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200680 smp->data.u.str = *trash;
681 break;
682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 trash->data = smp->data.u.str.data;
686 if (trash->data > trash->size)
687 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200688
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200690 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 default:
694 /* Other cases are unexpected. */
695 return 0;
696 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100697
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 /* remove const flag */
699 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200700 return 1;
701}
702
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100703int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
705 return 1;
706}
707
Willy Tarreau342acb42012-04-23 22:03:39 +0200708static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100709{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200710 const char *str;
711 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100712
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100714 return 0;
715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 str = smp->data.u.str.area;
717 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200719 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200720 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100722 return 1;
723}
724
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725static int c_str2meth(struct sample *smp)
726{
727 enum http_meth_t meth;
728 int len;
729
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100731 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 len = smp->data.u.str.data;
733 smp->data.u.meth.str.area = smp->data.u.str.area;
734 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 }
736 else
737 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200739 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100740 return 1;
741}
742
743static int c_meth2str(struct sample *smp)
744{
745 int len;
746 enum http_meth_t meth;
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 len = smp->data.u.meth.str.data;
751 smp->data.u.str.area = smp->data.u.meth.str.area;
752 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200755 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200758 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200761 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 }
763 else {
764 /* Unknown method */
765 return 0;
766 }
767 return 1;
768}
769
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770static int c_addr2bin(struct sample *smp)
771{
Willy Tarreau83061a82018-07-13 11:56:34 +0200772 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 chk->data = 4;
776 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 chk->data = 16;
780 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 }
782 else
783 return 0;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200786 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787 return 1;
788}
789
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200790static int c_int2bin(struct sample *smp)
791{
Willy Tarreau83061a82018-07-13 11:56:34 +0200792 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
795 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799 return 1;
800}
801
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200802
Emeric Brun107ca302010-01-04 16:16:05 +0100803/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200804/* Sample casts matrix: */
805/* sample_casts[from type][to type] */
806/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100807/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100808
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100809sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200810/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
811/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
812/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200813/* 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 +0200814/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
815/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200816/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200817/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
818/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
819/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200820};
Emeric Brun107ca302010-01-04 16:16:05 +0100821
Emeric Brun107ca302010-01-04 16:16:05 +0100822/*
Willy Tarreau12785782012-04-27 21:37:17 +0200823 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100824 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200825 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200826 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100827 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100828struct 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 +0100829{
Willy Tarreau833cc792013-07-24 15:34:19 +0200830 const char *begw; /* beginning of word */
831 const char *endw; /* end of word */
832 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200833 struct sample_expr *expr;
834 struct sample_fetch *fetch;
835 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100836 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 char *fkw = NULL;
838 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100839 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100840
Willy Tarreau833cc792013-07-24 15:34:19 +0200841 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100842 for (endw = begw; is_idchar(*endw); endw++)
843 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200844
845 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100846 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100847 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200848 }
Emeric Brun107ca302010-01-04 16:16:05 +0100849
Willy Tarreau833cc792013-07-24 15:34:19 +0200850 /* keep a copy of the current fetch keyword for error reporting */
851 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100852
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 fetch = find_sample_fetch(begw, endw - begw);
854 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100855 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100856 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200857 }
Emeric Brun107ca302010-01-04 16:16:05 +0100858
Willy Tarreau833cc792013-07-24 15:34:19 +0200859 endt = endw;
860 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100861 /* look for the end of this term and skip the opening parenthesis */
862 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200863 while (*endt && *endt != ')')
864 endt++;
865 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100866 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200867 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200868 }
Emeric Brun485479d2010-09-23 18:02:19 +0200869 }
Emeric Brun107ca302010-01-04 16:16:05 +0100870
Willy Tarreau833cc792013-07-24 15:34:19 +0200871 /* At this point, we have :
872 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100873 * - endw : end of the keyword, first character not part of keyword
874 * nor the opening parenthesis (so first character of args
875 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200876 * - endt : end of the term (=endw or last parenthesis if args are present)
877 */
878
879 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100880 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100881 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200882 }
Emeric Brun107ca302010-01-04 16:16:05 +0100883 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200884
Vincent Bernat02779b62016-04-03 13:48:43 +0200885 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200886 if (!expr)
887 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100888
889 LIST_INIT(&(expr->conv_exprs));
890 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200891 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100892
Willy Tarreau689a1df2013-12-13 00:40:11 +0100893 /* Note that we call the argument parser even with an empty string,
894 * this allows it to automatically create entries for mandatory
895 * implicit arguments (eg: local proxy name).
896 */
897 al->kw = expr->fetch->kw;
898 al->conv = NULL;
899 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
900 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
901 goto out_error;
902 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200903
Willy Tarreau689a1df2013-12-13 00:40:11 +0100904 if (!expr->arg_p) {
905 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200906 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100907 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
908 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200909 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100910 }
911
Willy Tarreau833cc792013-07-24 15:34:19 +0200912 /* Now process the converters if any. We have two supported syntaxes
913 * for the converters, which can be combined :
914 * - comma-delimited list of converters just after the keyword and args ;
915 * - one converter per keyword
916 * The combination allows to have each keyword being a comma-delimited
917 * series of converters.
918 *
919 * We want to process the former first, then the latter. For this we start
920 * from the beginning of the supposed place in the exiting conv chain, which
921 * starts at the last comma (endt).
922 */
923
924 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200925 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100926 int err_arg;
927 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100928
Willy Tarreau833cc792013-07-24 15:34:19 +0200929 if (*endt == ')') /* skip last closing parenthesis */
930 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100931
Willy Tarreau833cc792013-07-24 15:34:19 +0200932 if (*endt && *endt != ',') {
933 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100934 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200935 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100936 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100937 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200938 }
Emeric Brun107ca302010-01-04 16:16:05 +0100939
Willy Tarreau833cc792013-07-24 15:34:19 +0200940 while (*endt == ',') /* then trailing commas */
941 endt++;
942
Willy Tarreau97108e02016-11-25 07:33:24 +0100943 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200944
945 if (!*begw) {
946 /* none ? skip to next string */
947 (*idx)++;
948 begw = str[*idx];
949 if (!begw || !*begw)
950 break;
951 }
952
Willy Tarreaued2c6622020-02-14 18:27:10 +0100953 for (endw = begw; is_idchar(*endw); endw++)
954 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200955
956 free(ckw);
957 ckw = my_strndup(begw, endw - begw);
958
959 conv = find_sample_conv(begw, endw - begw);
960 if (!conv) {
961 /* we found an isolated keyword that we don't know, it's not ours */
962 if (begw == str[*idx])
963 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100964 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 goto out_error;
966 }
Emeric Brun107ca302010-01-04 16:16:05 +0100967
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 endt = endw;
969 if (*endt == '(') {
970 /* look for the end of this term */
Willy Tarreau46dfd782019-12-17 10:25:29 +0100971 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200972 while (*endt && *endt != ')')
973 endt++;
974 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100975 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200976 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200977 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200978 }
979
980 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100981 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100982 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200983 }
Emeric Brun107ca302010-01-04 16:16:05 +0100984
985 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200986 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100987 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100988 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200989 }
Emeric Brun107ca302010-01-04 16:16:05 +0100990
991 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200992 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200993 if (!conv_expr)
994 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100995
996 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
997 conv_expr->conv = conv;
998
Willy Tarreau46dfd782019-12-17 10:25:29 +0100999 al->kw = expr->fetch->kw;
1000 al->conv = conv_expr->conv->kw;
1001 argcnt = make_arg_list(endw, endt - endw, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al);
1002 if (argcnt < 0) {
1003 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1004 goto out_error;
1005 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001006
Willy Tarreau46dfd782019-12-17 10:25:29 +01001007 if (argcnt && !conv->arg_mask) {
1008 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1009 goto out_error;
1010 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001011
Willy Tarreau46dfd782019-12-17 10:25:29 +01001012 if (!conv_expr->arg_p)
1013 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001014
Willy Tarreau46dfd782019-12-17 10:25:29 +01001015 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1016 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001017 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001018 }
1019 }
Emeric Brun485479d2010-09-23 18:02:19 +02001020
Willy Tarreau833cc792013-07-24 15:34:19 +02001021 out:
1022 free(fkw);
1023 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001024 return expr;
1025
1026out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001027 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001028 expr = NULL;
1029 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001030}
1031
1032/*
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001034 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001035 * Returns a pointer on a typed sample structure containing the result or NULL if
1036 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001037 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001038 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001039 *
1040 * Note: the fetch functions are required to properly set the return type. The
1041 * conversion functions must do so too. However the cast functions do not need
1042 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001043 *
1044 * The caller may indicate in <opt> if it considers the result final or not.
1045 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1046 * if the result is stable or not, according to the following table :
1047 *
1048 * return MAY_CHANGE FINAL Meaning for the sample
1049 * NULL 0 * Not present and will never be (eg: header)
1050 * NULL 1 0 Not present yet, could change (eg: POST param)
1051 * NULL 1 1 Not present yet, will not change anymore
1052 * smp 0 * Present and will not change (eg: header)
1053 * smp 1 0 Present, may change (eg: request length)
1054 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001055 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001056struct sample *sample_process(struct proxy *px, struct session *sess,
1057 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001058 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001059{
Willy Tarreau12785782012-04-27 21:37:17 +02001060 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001061
Willy Tarreau18387e22013-07-25 12:02:38 +02001062 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001063 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001064 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001065 }
Emeric Brun107ca302010-01-04 16:16:05 +01001066
Willy Tarreau1777ea62016-03-10 16:15:46 +01001067 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001068 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001069 return NULL;
1070
Emeric Brun107ca302010-01-04 16:16:05 +01001071 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001072 /* we want to ensure that p->type can be casted into
1073 * conv_expr->conv->in_type. We have 3 possibilities :
1074 * - NULL => not castable.
1075 * - c_none => nothing to do (let's optimize it)
1076 * - other => apply cast and prepare to fail
1077 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001078 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001079 return NULL;
1080
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001081 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1082 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001083 return NULL;
1084
Willy Tarreau12e50112012-04-25 17:21:49 +02001085 /* OK cast succeeded */
1086
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001087 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001088 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001089 }
1090 return p;
1091}
1092
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001093/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001094 * Resolve all remaining arguments in proxy <p>. Returns the number of
1095 * errors or 0 if everything is fine.
1096 */
1097int smp_resolve_args(struct proxy *p)
1098{
1099 struct arg_list *cur, *bak;
1100 const char *ctx, *where;
1101 const char *conv_ctx, *conv_pre, *conv_pos;
1102 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001103 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001104 struct arg *arg;
1105 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001106 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001107
1108 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1109 struct proxy *px;
1110 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001111 struct stktable *t;
1112 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001113 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001114
1115 arg = cur->arg;
1116
1117 /* prepare output messages */
1118 conv_pre = conv_pos = conv_ctx = "";
1119 if (cur->conv) {
1120 conv_ctx = cur->conv;
1121 conv_pre = "conversion keyword '";
1122 conv_pos = "' for ";
1123 }
1124
1125 where = "in";
1126 ctx = "sample fetch keyword";
1127 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001128 case ARGC_STK: where = "in stick rule in"; break;
1129 case ARGC_TRK: where = "in tracking rule in"; break;
1130 case ARGC_LOG: where = "in log-format string in"; break;
1131 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1132 case ARGC_HRQ: where = "in http-request header format string in"; break;
1133 case ARGC_HRS: where = "in http-response header format string in"; break;
1134 case ARGC_UIF: where = "in unique-id-format string in"; break;
1135 case ARGC_RDR: where = "in redirect format string in"; break;
1136 case ARGC_CAP: where = "in capture rule in"; break;
1137 case ARGC_ACL: ctx = "ACL keyword"; break;
1138 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001139 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001140 }
1141
1142 /* set a few default settings */
1143 px = p;
1144 pname = p->id;
1145
1146 switch (arg->type) {
1147 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001148 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1150 cur->file, cur->line,
1151 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152 cfgerr++;
1153 continue;
1154 }
1155
1156 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158
1159 if (sname) {
1160 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001161 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001163 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1166 cur->file, cur->line, pname,
1167 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001168 cfgerr++;
1169 break;
1170 }
1171 }
1172 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001173 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174
1175 srv = findserver(px, sname);
1176 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 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",
1178 cur->file, cur->line, sname, pname,
1179 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001180 cfgerr++;
1181 break;
1182 }
1183
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001184 free(arg->data.str.area);
1185 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 arg->unresolved = 0;
1187 arg->data.srv = srv;
1188 break;
1189
1190 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001191 if (arg->data.str.data) {
1192 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001193 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 }
1195
1196 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 cur->file, cur->line, pname,
1199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203
1204 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001205 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",
1206 cur->file, cur->line, pname,
1207 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 cfgerr++;
1209 break;
1210 }
1211
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001212 free(arg->data.str.area);
1213 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 arg->unresolved = 0;
1215 arg->data.prx = px;
1216 break;
1217
1218 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001219 if (arg->data.str.data) {
1220 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001221 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222 }
1223
1224 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1226 cur->file, cur->line, pname,
1227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228 cfgerr++;
1229 break;
1230 }
1231
1232 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001233 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",
1234 cur->file, cur->line, pname,
1235 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001236 cfgerr++;
1237 break;
1238 }
1239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001240 free(arg->data.str.area);
1241 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001242 arg->unresolved = 0;
1243 arg->data.prx = px;
1244 break;
1245
1246 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001247 if (arg->data.str.data)
1248 stktname = arg->data.str.area;
1249 else
1250 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001251
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001252 t = stktable_find_by_name(stktname);
1253 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 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 +01001255 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001256 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001257 cfgerr++;
1258 break;
1259 }
1260
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001261 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 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 +01001263 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001264 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001265 cfgerr++;
1266 break;
1267 }
1268
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001269 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001270 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 +01001271 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001272 cfgerr++;
1273 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001274 }
1275
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001276 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001277 p->next_stkt_ref = t->proxies_list;
1278 t->proxies_list = p;
1279 }
1280
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001281 free(arg->data.str.area);
1282 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001284 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001285 break;
1286
1287 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001288 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001289 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1290 cur->file, cur->line,
1291 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001292 cfgerr++;
1293 break;
1294 }
1295
1296 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001297 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001298 ul = p->uri_auth->userlist;
1299 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001300 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001301
1302 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001303 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 +02001304 cur->file, cur->line,
1305 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001306 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001307 cfgerr++;
1308 break;
1309 }
1310
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001311 free(arg->data.str.area);
1312 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001313 arg->unresolved = 0;
1314 arg->data.usr = ul;
1315 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001316
1317 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001318 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001319 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1320 cur->file, cur->line,
1321 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001322 cfgerr++;
1323 continue;
1324 }
1325
Willy Tarreau46947782015-01-19 19:00:58 +01001326 rflags = 0;
1327 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1328 err = NULL;
1329
Dragan Dosen26743032019-04-30 15:54:36 +02001330 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001331 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1332 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001334 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001335 cfgerr++;
1336 continue;
1337 }
1338
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001339 free(arg->data.str.area);
1340 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001341 arg->unresolved = 0;
1342 arg->data.reg = reg;
1343 break;
1344
1345
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001346 }
1347
1348 LIST_DEL(&cur->list);
1349 free(cur);
1350 } /* end of args processing */
1351
1352 return cfgerr;
1353}
1354
1355/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001356 * Process a fetch + format conversion as defined by the sample expression
1357 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001358 * not explicitly set to <smp_type>, but shall be compatible with it as
1359 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1360 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001361 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1362 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1363 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1364 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001365 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1366 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1367 * still hope to get it after waiting longer, and is not converted to string.
1368 * The possible output combinations are the following :
1369 *
1370 * return MAY_CHANGE FINAL Meaning for the sample
1371 * NULL * * Not present and will never be (eg: header)
1372 * smp 0 * Final value converted (eg: header)
1373 * smp 1 0 Not present yet, may appear later (eg: header)
1374 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001376struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001377 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001378 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381
Willy Tarreau6c616e02014-06-25 16:56:41 +02001382 memset(smp, 0, sizeof(*smp));
1383
Willy Tarreau192252e2015-04-04 01:47:55 +02001384 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001385 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1386 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001387 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001388 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001390 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001391 return NULL;
1392
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001393 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001394 return NULL;
1395
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001396 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001397 return smp;
1398}
1399
Christopher Faulet476e5d02016-10-26 11:34:47 +02001400static void release_sample_arg(struct arg *p)
1401{
1402 struct arg *p_back = p;
1403
1404 if (!p)
1405 return;
1406
1407 while (p->type != ARGT_STOP) {
1408 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001409 free(p->data.str.area);
1410 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001411 p->unresolved = 0;
1412 }
1413 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001414 regex_free(p->data.reg);
1415 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001416 }
1417 p++;
1418 }
1419
1420 if (p_back != empty_arg_list)
1421 free(p_back);
1422}
1423
1424void release_sample_expr(struct sample_expr *expr)
1425{
1426 struct sample_conv_expr *conv_expr, *conv_exprb;
1427
1428 if (!expr)
1429 return;
1430
1431 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1432 release_sample_arg(conv_expr->arg_p);
1433 release_sample_arg(expr->arg_p);
1434 free(expr);
1435}
1436
Emeric Brun107ca302010-01-04 16:16:05 +01001437/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001438/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001439/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001440/*****************************************************************/
1441
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001442static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1443{
1444 int i;
1445 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001446 struct buffer *buf;
1447 struct sink *sink;
1448 struct ist line;
1449 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001450
Willy Tarreau0851fd52019-12-17 10:07:25 +01001451 buf = alloc_trash_chunk();
1452 if (!buf)
1453 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001454
Willy Tarreau0851fd52019-12-17 10:07:25 +01001455 sink = (struct sink *)arg_p[1].data.str.area;
1456 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001457
Willy Tarreau0851fd52019-12-17 10:07:25 +01001458 pfx = arg_p[0].data.str.area;
1459 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001460
Willy Tarreau0851fd52019-12-17 10:07:25 +01001461 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1462 if (!sample_casts[smp->data.type][SMP_T_STR])
1463 goto nocast;
1464
1465 /* Copy sample fetch. This puts the sample as const, the
1466 * cast will copy data if a transformation is required.
1467 */
1468 memcpy(&tmp, smp, sizeof(struct sample));
1469 tmp.flags = SMP_F_CONST;
1470
1471 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1472 goto nocast;
1473
1474 /* Display the displayable chars*. */
1475 b_putchr(buf, '<');
1476 for (i = 0; i < tmp.data.u.str.data; i++) {
1477 if (isprint(tmp.data.u.str.area[i]))
1478 b_putchr(buf, tmp.data.u.str.area[i]);
1479 else
1480 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001481 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001482 b_putchr(buf, '>');
1483
1484 done:
1485 line = ist2(buf->area, buf->data);
1486 sink_write(sink, &line, 1);
1487 end:
1488 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001489 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001490 nocast:
1491 chunk_appendf(buf, "(undisplayable)");
1492 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001493}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001494
1495// This function checks the "debug" converter's arguments.
1496static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1497 const char *file, int line, char **err)
1498{
1499 const char *name = "buf0";
1500 struct sink *sink = NULL;
1501
1502 if (args[0].type != ARGT_STR) {
1503 /* optional prefix */
1504 args[0].data.str.area = "";
1505 args[0].data.str.data = 0;
1506 }
1507
1508 if (args[1].type == ARGT_STR)
1509 name = args[1].data.str.area;
1510
1511 sink = sink_find(name);
1512 if (!sink) {
1513 memprintf(err, "No such sink '%s'", name);
1514 return 0;
1515 }
1516
1517 args[1].data.str.area = (char *)sink;
1518 args[1].data.str.data = 0; // that's not a string anymore
1519 return 1;
1520}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001521
Holger Just1bfc24b2017-05-06 00:56:53 +02001522static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1523{
Willy Tarreau83061a82018-07-13 11:56:34 +02001524 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001525 int bin_len;
1526
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 trash->data = 0;
1528 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1529 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001530 if (bin_len < 0)
1531 return 0;
1532
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001533 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001534 smp->data.u.str = *trash;
1535 smp->data.type = SMP_T_BIN;
1536 smp->flags &= ~SMP_F_CONST;
1537 return 1;
1538}
1539
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001540static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001541{
Willy Tarreau83061a82018-07-13 11:56:34 +02001542 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001543 int b64_len;
1544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001545 trash->data = 0;
1546 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1547 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001548 if (b64_len < 0)
1549 return 0;
1550
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001551 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001552 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001553 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001554 smp->flags &= ~SMP_F_CONST;
1555 return 1;
1556}
1557
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001558static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1559{
1560 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001561 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001562
1563 memset(&ctx, 0, sizeof(ctx));
1564
1565 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001566 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1567 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001568
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001569 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001570 smp->data.u.str = *trash;
1571 smp->data.type = SMP_T_BIN;
1572 smp->flags &= ~SMP_F_CONST;
1573 return 1;
1574}
1575
Tim Duesterhusd4376302019-06-17 12:41:44 +02001576#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001577static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1578 const char *file, int line, char **err)
1579{
1580 if (args[0].type == ARGT_STOP)
1581 return 1;
1582 if (args[0].type != ARGT_SINT) {
1583 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1584 return 0;
1585 }
1586
1587 switch (args[0].data.sint) {
1588 case 224:
1589 case 256:
1590 case 384:
1591 case 512:
1592 /* this is okay */
1593 return 1;
1594 default:
1595 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1596 return 0;
1597 }
1598}
1599
Tim Duesterhusd4376302019-06-17 12:41:44 +02001600static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1601{
1602 struct buffer *trash = get_trash_chunk();
1603 int bits = 256;
1604 if (arg_p && arg_p->data.sint)
1605 bits = arg_p->data.sint;
1606
1607 switch (bits) {
1608 case 224: {
1609 SHA256_CTX ctx;
1610
1611 memset(&ctx, 0, sizeof(ctx));
1612
1613 SHA224_Init(&ctx);
1614 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1615 SHA224_Final((unsigned char *) trash->area, &ctx);
1616 trash->data = SHA224_DIGEST_LENGTH;
1617 break;
1618 }
1619 case 256: {
1620 SHA256_CTX ctx;
1621
1622 memset(&ctx, 0, sizeof(ctx));
1623
1624 SHA256_Init(&ctx);
1625 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1626 SHA256_Final((unsigned char *) trash->area, &ctx);
1627 trash->data = SHA256_DIGEST_LENGTH;
1628 break;
1629 }
1630 case 384: {
1631 SHA512_CTX ctx;
1632
1633 memset(&ctx, 0, sizeof(ctx));
1634
1635 SHA384_Init(&ctx);
1636 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1637 SHA384_Final((unsigned char *) trash->area, &ctx);
1638 trash->data = SHA384_DIGEST_LENGTH;
1639 break;
1640 }
1641 case 512: {
1642 SHA512_CTX ctx;
1643
1644 memset(&ctx, 0, sizeof(ctx));
1645
1646 SHA512_Init(&ctx);
1647 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1648 SHA512_Final((unsigned char *) trash->area, &ctx);
1649 trash->data = SHA512_DIGEST_LENGTH;
1650 break;
1651 }
1652 default:
1653 return 0;
1654 }
1655
1656 smp->data.u.str = *trash;
1657 smp->data.type = SMP_T_BIN;
1658 smp->flags &= ~SMP_F_CONST;
1659 return 1;
1660}
1661#endif
1662
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001663static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001664{
Willy Tarreau83061a82018-07-13 11:56:34 +02001665 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001666 unsigned char c;
1667 int ptr = 0;
1668
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001669 trash->data = 0;
1670 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1671 c = smp->data.u.str.area[ptr++];
1672 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1673 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001674 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001675 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001676 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001677 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001678 return 1;
1679}
1680
Dragan Dosen3f957b22017-10-24 09:27:34 +02001681static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1682{
1683 long long int n = 0;
1684 int i, c;
1685
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001686 for (i = 0; i < smp->data.u.str.data; i++) {
1687 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001688 return 0;
1689 n = (n << 4) + c;
1690 }
1691
1692 smp->data.u.sint = n;
1693 smp->data.type = SMP_T_SINT;
1694 smp->flags &= ~SMP_F_CONST;
1695 return 1;
1696}
1697
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001698/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001699static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001700{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001701 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1702 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001703 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001704 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001705 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001706 return 1;
1707}
1708
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001709static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001710{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001711 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001712 smp->data.u.sint = i;
1713 smp->data.type = SMP_T_SINT;
1714 return 1;
1715}
1716
1717
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001718static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001719{
1720 int i;
1721
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001722 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001723 return 0;
1724
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001725 for (i = 0; i < smp->data.u.str.data; i++) {
1726 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1727 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001728 }
1729 return 1;
1730}
1731
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001732static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001733{
1734 int i;
1735
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001736 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001737 return 0;
1738
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001739 for (i = 0; i < smp->data.u.str.data; i++) {
1740 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1741 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001742 }
1743 return 1;
1744}
1745
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001746/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1747static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001748{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001749 /* Attempt to convert to IPv4 to apply the correct mask. */
1750 c_ipv62ip(smp);
1751
1752 if (smp->data.type == SMP_T_IPV4) {
1753 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1754 smp->data.type = SMP_T_IPV4;
1755 }
1756 else if (smp->data.type == SMP_T_IPV6) {
1757 /* IPv6 cannot be converted without an IPv6 mask. */
1758 if (args[1].type != ARGT_IPV6)
1759 return 0;
1760
1761 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1762 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1763 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1764 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1765 smp->data.type = SMP_T_IPV6;
1766 }
1767
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001768 return 1;
1769}
1770
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001771/* takes an UINT value on input supposed to represent the time since EPOCH,
1772 * adds an optional offset found in args[1] and emits a string representing
1773 * the local time in the format specified in args[1] using strftime().
1774 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001775static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001776{
Willy Tarreau83061a82018-07-13 11:56:34 +02001777 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001778 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001779 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001780 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001781
1782 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001783 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001784 curr_date += args[1].data.sint;
1785
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001786 tm = localtime(&curr_date);
1787 if (!tm)
1788 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001789 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001790 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1791 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001792 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001793 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001794 return 1;
1795}
1796
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001797/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001798static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001799{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001800 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1801 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001802 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001803 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001804 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001805 return 1;
1806}
1807
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001808/* takes an UINT value on input supposed to represent the time since EPOCH,
1809 * adds an optional offset found in args[1] and emits a string representing
1810 * the UTC date in the format specified in args[1] using strftime().
1811 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001812static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001813{
Willy Tarreau83061a82018-07-13 11:56:34 +02001814 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001815 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001816 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001817 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001818
1819 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001820 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001821 curr_date += args[1].data.sint;
1822
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001823 tm = gmtime(&curr_date);
1824 if (!tm)
1825 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001826 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001827 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1828 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001829 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001830 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001831 return 1;
1832}
1833
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001834/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001835static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001836{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001837 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1838 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001839 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001840 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001841 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001842 return 1;
1843}
1844
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001845/* hashes the binary input into a 32-bit unsigned int using xxh.
1846 * The seed of the hash defaults to 0 but can be changd in argument 1.
1847 */
1848static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1849{
1850 unsigned int seed;
1851
1852 if (arg_p && arg_p->data.sint)
1853 seed = arg_p->data.sint;
1854 else
1855 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001856 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1857 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001858 smp->data.type = SMP_T_SINT;
1859 return 1;
1860}
1861
1862/* hashes the binary input into a 64-bit unsigned int using xxh.
1863 * In fact, the function returns a 64 bit unsigned, but the sample
1864 * storage of haproxy only proposes 64-bits signed, so the value is
1865 * cast as signed. This cast doesn't impact the hash repartition.
1866 * The seed of the hash defaults to 0 but can be changd in argument 1.
1867 */
1868static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1869{
1870 unsigned long long int seed;
1871
1872 if (arg_p && arg_p->data.sint)
1873 seed = (unsigned long long int)arg_p->data.sint;
1874 else
1875 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001876 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1877 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001878 smp->data.type = SMP_T_SINT;
1879 return 1;
1880}
1881
Willy Tarreau80599772015-01-20 19:35:24 +01001882/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001883static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001884{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001885 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1886 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001887 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001888 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001889 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001890 return 1;
1891}
1892
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001893/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1894static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1895{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001896 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1897 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001898 if (arg_p && arg_p->data.sint)
1899 smp->data.u.sint = full_hash(smp->data.u.sint);
1900 smp->data.type = SMP_T_SINT;
1901 return 1;
1902}
1903
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001904/* This function escape special json characters. The returned string can be
1905 * safely set between two '"' and used as json string. The json string is
1906 * defined like this:
1907 *
1908 * any Unicode character except '"' or '\' or control character
1909 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1910 *
1911 * The enum input_type contain all the allowed mode for decoding the input
1912 * string.
1913 */
1914enum input_type {
1915 IT_ASCII = 0,
1916 IT_UTF8,
1917 IT_UTF8S,
1918 IT_UTF8P,
1919 IT_UTF8PS,
1920};
1921static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1922 const char *file, int line, char **err)
1923{
1924 if (!arg) {
1925 memprintf(err, "Unexpected empty arg list");
1926 return 0;
1927 }
1928
1929 if (arg->type != ARGT_STR) {
1930 memprintf(err, "Unexpected arg type");
1931 return 0;
1932 }
1933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001934 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001935 arg->type = ARGT_SINT;
1936 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001937 return 1;
1938 }
1939
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001940 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001941 arg->type = ARGT_SINT;
1942 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001943 return 1;
1944 }
1945
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001946 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001947 arg->type = ARGT_SINT;
1948 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001949 return 1;
1950 }
1951
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001952 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001953 arg->type = ARGT_SINT;
1954 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001955 return 1;
1956 }
1957
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001959 arg->type = ARGT_SINT;
1960 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001961 return 1;
1962 }
1963
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001964 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001965 arg->type = ARGT_SINT;
1966 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001967 return 1;
1968 }
1969
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001970 memprintf(err, "Unexpected input code type. "
1971 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001972 return 0;
1973}
1974
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001975static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001976{
Willy Tarreau83061a82018-07-13 11:56:34 +02001977 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001978 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1979 const char *str;
1980 int len;
1981 enum input_type input_type = IT_ASCII;
1982 unsigned int c;
1983 unsigned int ret;
1984 char *p;
1985
1986 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001987 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001988
1989 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001991
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001992 p = smp->data.u.str.area;
1993 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001994
1995 if (input_type == IT_ASCII) {
1996 /* Read input as ASCII. */
1997 c = *(unsigned char *)p;
1998 p++;
1999 }
2000 else {
2001 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002002 ret = utf8_next(p,
2003 smp->data.u.str.data - ( p - smp->data.u.str.area),
2004 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002005 p += utf8_return_length(ret);
2006
2007 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2008 return 0;
2009 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2010 continue;
2011 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2012 return 0;
2013 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2014 continue;
2015
2016 /* Check too big values. */
2017 if ((unsigned int)c > 0xffff) {
2018 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2019 return 0;
2020 continue;
2021 }
2022 }
2023
2024 /* Convert character. */
2025 if (c == '"') {
2026 len = 2;
2027 str = "\\\"";
2028 }
2029 else if (c == '\\') {
2030 len = 2;
2031 str = "\\\\";
2032 }
2033 else if (c == '/') {
2034 len = 2;
2035 str = "\\/";
2036 }
2037 else if (c == '\b') {
2038 len = 2;
2039 str = "\\b";
2040 }
2041 else if (c == '\f') {
2042 len = 2;
2043 str = "\\f";
2044 }
2045 else if (c == '\r') {
2046 len = 2;
2047 str = "\\r";
2048 }
2049 else if (c == '\n') {
2050 len = 2;
2051 str = "\\n";
2052 }
2053 else if (c == '\t') {
2054 len = 2;
2055 str = "\\t";
2056 }
2057 else if (c > 0xff || !isprint(c)) {
2058 /* isprint generate a segfault if c is too big. The man says that
2059 * c must have the value of an unsigned char or EOF.
2060 */
2061 len = 6;
2062 _str[0] = '\\';
2063 _str[1] = 'u';
2064 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2065 str = _str;
2066 }
2067 else {
2068 len = 1;
2069 str = (char *)&c;
2070 }
2071
2072 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002073 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002074 return 0;
2075
2076 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002077 memcpy(temp->area + temp->data, str, len);
2078 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002079 }
2080
2081 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002082 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002083 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002084
2085 return 1;
2086}
2087
Emeric Brun54c4ac82014-11-03 15:32:43 +01002088/* This sample function is designed to extract some bytes from an input buffer.
2089 * First arg is the offset.
2090 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002091static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002092{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002093 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2094 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002095 return 1;
2096 }
2097
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002098 if (smp->data.u.str.size)
2099 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002100 smp->data.u.str.data -= arg_p[0].data.sint;
2101 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002102
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002103 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2104 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002105
2106 return 1;
2107}
2108
Emeric Brunf399b0d2014-11-03 17:07:03 +01002109static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2110 const char *file, int line, char **err)
2111{
2112 struct arg *arg = args;
2113
2114 if (!arg) {
2115 memprintf(err, "Unexpected empty arg list");
2116 return 0;
2117 }
2118
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002119 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002120 memprintf(err, "Unexpected arg type");
2121 return 0;
2122 }
2123
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002124 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002125 memprintf(err, "Unexpected value 0 for index");
2126 return 0;
2127 }
2128
2129 arg++;
2130
2131 if (arg->type != ARGT_STR) {
2132 memprintf(err, "Unexpected arg type");
2133 return 0;
2134 }
2135
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002136 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002137 memprintf(err, "Empty separators list");
2138 return 0;
2139 }
2140
2141 return 1;
2142}
2143
2144/* This sample function is designed to a return selected part of a string (field).
2145 * First arg is the index of the field (start at 1)
2146 * Second arg is a char list of separators (type string)
2147 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002148static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002149{
Marcin Deranek9631a282018-04-16 14:30:46 +02002150 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002151 char *start, *end;
2152 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002153 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002154
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002155 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002156 return 0;
2157
Marcin Deranek9631a282018-04-16 14:30:46 +02002158 if (arg_p[0].data.sint < 0) {
2159 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002160 end = start = smp->data.u.str.area + smp->data.u.str.data;
2161 while (start > smp->data.u.str.area) {
2162 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2163 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002164 if (field == arg_p[0].data.sint) {
2165 if (count == 1)
2166 goto found;
2167 else if (count > 1)
2168 count--;
2169 } else {
2170 end = start-1;
2171 field--;
2172 }
2173 break;
2174 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002175 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002176 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002177 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002178 } else {
2179 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002180 end = start = smp->data.u.str.area;
2181 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2182 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2183 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002184 if (field == arg_p[0].data.sint) {
2185 if (count == 1)
2186 goto found;
2187 else if (count > 1)
2188 count--;
2189 } else {
2190 start = end+1;
2191 field++;
2192 }
2193 break;
2194 }
2195 }
2196 end++;
2197 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002198 }
2199
2200 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002201 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002202 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002203 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002204 }
2205found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002206 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002207 /* If ret string is len 0, no need to
2208 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002209 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002210 return 1;
2211
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002212 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002213
2214 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002215 Note: smp->data.u.str.size cannot be set to 0 */
2216 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002217 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002218
2219 return 1;
2220}
2221
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002222/* This sample function is designed to return a word from a string.
2223 * First arg is the index of the word (start at 1)
2224 * Second arg is a char list of words separators (type string)
2225 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002226static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002227{
Marcin Deranek9631a282018-04-16 14:30:46 +02002228 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002229 char *start, *end;
2230 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002231 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002232
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002233 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002234 return 0;
2235
2236 word = 0;
2237 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002238 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002239 end = start = smp->data.u.str.area + smp->data.u.str.data;
2240 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002241 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002242 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2243 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002244 issep = 1;
2245 break;
2246 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002247 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002248 if (!inword) {
2249 if (!issep) {
2250 if (word != arg_p[0].data.sint) {
2251 word--;
2252 end = start;
2253 }
2254 inword = 1;
2255 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002256 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002257 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002258 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002259 if (count == 1)
2260 goto found;
2261 else if (count > 1)
2262 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002263 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002264 inword = 0;
2265 }
2266 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002267 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002268 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002269 end = start = smp->data.u.str.area;
2270 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002271 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002272 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2273 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002274 issep = 1;
2275 break;
2276 }
2277 }
2278 if (!inword) {
2279 if (!issep) {
2280 if (word != arg_p[0].data.sint) {
2281 word++;
2282 start = end;
2283 }
2284 inword = 1;
2285 }
2286 }
2287 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002288 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002289 if (count == 1)
2290 goto found;
2291 else if (count > 1)
2292 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002293 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002294 inword = 0;
2295 }
2296 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002297 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002298 }
2299
2300 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002301 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002302 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002303 return 1;
2304 }
2305found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002307 /* If ret string is len 0, no need to
2308 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002310 return 1;
2311
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002312 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002313
2314 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002315 Note: smp->data.u.str.size cannot be set to 0 */
2316 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002317 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002318
2319 return 1;
2320}
2321
Willy Tarreau7eda8492015-01-20 19:47:06 +01002322static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2323 const char *file, int line, char **err)
2324{
2325 struct arg *arg = args;
2326 char *p;
2327 int len;
2328
2329 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2330 arg[0].type_flags = 0;
2331
2332 if (arg[2].type != ARGT_STR)
2333 return 1;
2334
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002335 p = arg[2].data.str.area;
2336 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002337 while (len) {
2338 if (*p == 'i') {
2339 arg[0].type_flags |= ARGF_REG_ICASE;
2340 }
2341 else if (*p == 'g') {
2342 arg[0].type_flags |= ARGF_REG_GLOB;
2343 }
2344 else {
2345 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2346 return 0;
2347 }
2348 p++;
2349 len--;
2350 }
2351 return 1;
2352}
2353
2354/* This sample function is designed to do the equivalent of s/match/replace/ on
2355 * the input string. It applies a regex and restarts from the last matched
2356 * location until nothing matches anymore. First arg is the regex to apply to
2357 * the input string, second arg is the replacement expression.
2358 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002359static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002360{
2361 char *start, *end;
2362 struct my_regex *reg = arg_p[0].data.reg;
2363 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002364 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002365 int flag, max;
2366 int found;
2367
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002368 start = smp->data.u.str.area;
2369 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002370
2371 flag = 0;
2372 while (1) {
2373 /* check for last round which is used to copy remaining parts
2374 * when not running in global replacement mode.
2375 */
2376 found = 0;
2377 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2378 /* Note: we can have start == end on empty strings or at the end */
2379 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2380 }
2381
2382 if (!found)
2383 pmatch[0].rm_so = end - start;
2384
2385 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002387 if (max && pmatch[0].rm_so > 0) {
2388 if (max > pmatch[0].rm_so)
2389 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 memcpy(trash->area + trash->data, start, max);
2391 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002392 }
2393
2394 if (!found)
2395 break;
2396
2397 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002399 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 if (max > arg_p[1].data.str.data)
2401 max = arg_p[1].data.str.data;
2402 memcpy(trash->area + trash->data,
2403 arg_p[1].data.str.area, max);
2404 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002405 }
2406
2407 /* stop here if we're done with this string */
2408 if (start >= end)
2409 break;
2410
2411 /* We have a special case for matches of length 0 (eg: "x*y*").
2412 * These ones are considered to match in front of a character,
2413 * so we have to copy that character and skip to the next one.
2414 */
2415 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002416 if (trash->data < trash->size)
2417 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002418 pmatch[0].rm_eo++;
2419 }
2420
2421 start += pmatch[0].rm_eo;
2422 flag |= REG_NOTBOL;
2423 }
2424
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002425 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002426 return 1;
2427}
2428
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002429/* This function check an operator entry. It expects a string.
2430 * The string can be an integer or a variable name.
2431 */
2432static int check_operator(struct arg *args, struct sample_conv *conv,
2433 const char *file, int line, char **err)
2434{
2435 const char *str;
2436 const char *end;
2437
2438 /* Try to decode a variable. */
2439 if (vars_check_arg(&args[0], NULL))
2440 return 1;
2441
2442 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002443 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002444 end = str + strlen(str);
2445 args[0].data.sint = read_int64(&str, end);
2446 if (*str != '\0') {
2447 memprintf(err, "expects an integer or a variable name");
2448 return 0;
2449 }
2450 args[0].type = ARGT_SINT;
2451 return 1;
2452}
2453
Willy Tarreau6204cd92016-03-10 16:33:04 +01002454/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002455 * If the arg contain an integer, the integer is returned in the
2456 * sample. If the arg contains a variable descriptor, it returns the
2457 * variable value.
2458 *
2459 * This function returns 0 if an error occurs, otherwise it returns 1.
2460 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002461static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002462{
2463 switch (arg->type) {
2464 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002465 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002466 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002467 return 1;
2468 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002469 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002470 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002471 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002472 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002473 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002474 return 0;
2475 return 1;
2476 default:
2477 return 0;
2478 }
2479}
2480
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002481/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002482 * result.
2483 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002484static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002485{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002486 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002487 return 1;
2488}
2489
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002490/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002491 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002492 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002493static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002494{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002495 struct sample tmp;
2496
Willy Tarreau7560dd42016-03-10 16:28:58 +01002497 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002498 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002499 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002500 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002501 return 1;
2502}
2503
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002504/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002505 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002506 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002507static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002508{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002509 struct sample tmp;
2510
Willy Tarreau7560dd42016-03-10 16:28:58 +01002511 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002512 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002513 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002514 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002515 return 1;
2516}
2517
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002518/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002519 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002520 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002521static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002522{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002523 struct sample tmp;
2524
Willy Tarreau7560dd42016-03-10 16:28:58 +01002525 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002526 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002527 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002528 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002529 return 1;
2530}
2531
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002532static inline long long int arith_add(long long int a, long long int b)
2533{
2534 /* Prevent overflow and makes capped calculus.
2535 * We must ensure that the check calculus doesn't
2536 * exceed the signed 64 bits limits.
2537 *
2538 * +----------+----------+
2539 * | a<0 | a>=0 |
2540 * +------+----------+----------+
2541 * | b<0 | MIN-a>b | no check |
2542 * +------+----------+----------+
2543 * | b>=0 | no check | MAX-a<b |
2544 * +------+----------+----------+
2545 */
2546 if ((a ^ b) >= 0) {
2547 /* signs are differents. */
2548 if (a < 0) {
2549 if (LLONG_MIN - a > b)
2550 return LLONG_MIN;
2551 }
2552 if (LLONG_MAX - a < b)
2553 return LLONG_MAX;
2554 }
2555 return a + b;
2556}
2557
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002558/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002559 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002560 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002561static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002562{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002563 struct sample tmp;
2564
Willy Tarreau7560dd42016-03-10 16:28:58 +01002565 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002566 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002567 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002568 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002569 return 1;
2570}
2571
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002572/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002573 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002574 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002575static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002576 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002577{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002578 struct sample tmp;
2579
Willy Tarreau7560dd42016-03-10 16:28:58 +01002580 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002581 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002582 return 0;
2583
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002584 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2585 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2586 * of -LLONG_MIN and correct the result.
2587 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002588 if (tmp.data.u.sint == LLONG_MIN) {
2589 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2590 if (smp->data.u.sint < LLONG_MAX)
2591 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002592 return 1;
2593 }
2594
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002595 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002596 * the second operand.
2597 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002598 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002599 return 1;
2600}
2601
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002602/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002603 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002604 * If the result makes an overflow, then the largest possible quantity is
2605 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002606 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002607static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002608 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002609{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002610 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002611 long long int c;
2612
Willy Tarreau7560dd42016-03-10 16:28:58 +01002613 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002614 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002615 return 0;
2616
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002617 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002618 if (!smp->data.u.sint || !tmp.data.u.sint) {
2619 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002620 return 1;
2621 }
2622
2623 /* The multiply between LLONG_MIN and -1 returns a
2624 * "floting point exception".
2625 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002626 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2627 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002628 return 1;
2629 }
2630
2631 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002632 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002633
2634 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002635 if (smp->data.u.sint != c / tmp.data.u.sint) {
2636 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2637 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002638 return 1;
2639 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002640 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002641 return 1;
2642 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002643 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002644 return 1;
2645}
2646
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002647/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002648 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002649 * If arg_p makes the result overflow, then the largest possible quantity is
2650 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002651 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002652static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002653 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002654{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002655 struct sample tmp;
2656
Willy Tarreau7560dd42016-03-10 16:28:58 +01002657 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002658 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002659 return 0;
2660
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002661 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002662 /* The divide between LLONG_MIN and -1 returns a
2663 * "floting point exception".
2664 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002665 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2666 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002667 return 1;
2668 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002669 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002670 return 1;
2671 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002672 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002673 return 1;
2674}
2675
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002676/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002677 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002678 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002679 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002680static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002681 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002682{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002683 struct sample tmp;
2684
Willy Tarreau7560dd42016-03-10 16:28:58 +01002685 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002686 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002687 return 0;
2688
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002689 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002690 /* The divide between LLONG_MIN and -1 returns a
2691 * "floting point exception".
2692 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002693 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2694 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002695 return 1;
2696 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002697 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002698 return 1;
2699 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002700 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002701 return 1;
2702}
2703
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002704/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002705 * result.
2706 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002707static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002708 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002709{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002710 if (smp->data.u.sint == LLONG_MIN)
2711 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002712 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002713 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002714 return 1;
2715}
2716
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002717/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002718 * false. The output is a BOOL.
2719 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002720static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002721 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002722{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002723 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002724 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002725 return 1;
2726}
2727
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002728/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002729 * truee. The output is a BOOL.
2730 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002731static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002732 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002733{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002734 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002735 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002736 return 1;
2737}
2738
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002739/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002740 * The output is a BOOL.
2741 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002742static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002743 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002744{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002745 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002746 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002747 return 1;
2748}
2749
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002750/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002751 * The output is a BOOL.
2752 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002753static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002754 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002755{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002756 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002757 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002758 return 1;
2759}
2760
Willy Tarreau280f42b2018-02-19 15:34:12 +01002761/* appends an optional const string, an optional variable contents and another
2762 * optional const string to an existing string.
2763 */
2764static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2765{
Willy Tarreau83061a82018-07-13 11:56:34 +02002766 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002767 struct sample tmp;
2768 int max;
2769
2770 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002771 trash->data = smp->data.u.str.data;
2772 if (trash->data > trash->size - 1)
2773 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002774
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002775 memcpy(trash->area, smp->data.u.str.area, trash->data);
2776 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002777
2778 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002779 max = arg_p[0].data.str.data;
2780 if (max > trash->size - 1 - trash->data)
2781 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002782
2783 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002784 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2785 trash->data += max;
2786 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002787 }
2788
2789 /* append second string (variable) if it's found and we can turn it
2790 * into a string.
2791 */
2792 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2793 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2794 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2795 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2796
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002797 max = tmp.data.u.str.data;
2798 if (max > trash->size - 1 - trash->data)
2799 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002800
2801 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002802 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2803 max);
2804 trash->data += max;
2805 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002806 }
2807 }
2808
2809 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002810 max = arg_p[2].data.str.data;
2811 if (max > trash->size - 1 - trash->data)
2812 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002813
2814 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002815 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2816 trash->data += max;
2817 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002818 }
2819
2820 smp->data.u.str = *trash;
2821 smp->data.type = SMP_T_STR;
2822 return 1;
2823}
2824
2825/* This function checks the "concat" converter's arguments and extracts the
2826 * variable name and its scope.
2827 */
2828static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2829 const char *file, int line, char **err)
2830{
2831 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002832 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2833 memprintf(err, "failed to register variable name '%s'",
2834 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002835 return 0;
2836 }
2837 return 1;
2838}
2839
Tim Duesterhusca097c12018-04-27 21:18:45 +02002840/* compares string with a variable containing a string. Return value
2841 * is compatible with strcmp(3)'s return value.
2842 */
2843static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2844{
2845 struct sample tmp;
2846 int max, result;
2847
2848 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2849 if (arg_p[0].type != ARGT_VAR)
2850 return 0;
2851 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2852 return 0;
2853 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2854 return 0;
2855
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002856 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2857 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002858 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002859 if (smp->data.u.str.data != tmp.data.u.str.data) {
2860 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002861 result = -1;
2862 }
2863 else {
2864 result = 1;
2865 }
2866 }
2867 }
2868
2869 smp->data.u.sint = result;
2870 smp->data.type = SMP_T_SINT;
2871 return 1;
2872}
2873
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002874#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2875#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2876#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2877
2878/*
2879 * Extract the field value of an input binary sample. Takes a mandatory argument:
2880 * the protocol buffers field identifier (dotted notation) internally represented
2881 * as an array of unsigned integers and its size.
2882 * Return 1 if the field was found, 0 if not.
2883 */
2884static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2885{
2886 unsigned char *pos;
2887 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002888
2889 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002890 grpc_left = smp->data.u.str.data;
2891
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002892 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002893 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002894
2895 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2896
2897 pos += GRPC_MSG_HEADER_SZ;
2898 grpc_left -= GRPC_MSG_HEADER_SZ;
2899
2900 if (grpc_left < left)
2901 return 0;
2902
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002903 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2904 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002905
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002906 grpc_left -= grpc_msg_len;
2907 }
2908
2909 return 0;
2910}
2911
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002912static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2913{
2914 unsigned char *pos;
2915 size_t left;
2916
2917 pos = (unsigned char *)smp->data.u.str.area;
2918 left = smp->data.u.str.data;
2919
2920 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2921}
2922
2923static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2924 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002925{
2926 if (!args[1].type) {
2927 args[1].type = ARGT_SINT;
2928 args[1].data.sint = PBUF_T_BINARY;
2929 }
2930 else {
2931 int pbuf_type;
2932
2933 pbuf_type = protobuf_type(args[1].data.str.area);
2934 if (pbuf_type == -1) {
2935 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2936 return 0;
2937 }
2938
2939 args[1].type = ARGT_SINT;
2940 args[1].data.sint = pbuf_type;
2941 }
2942
2943 return 1;
2944}
2945
Tim Duesterhusca097c12018-04-27 21:18:45 +02002946/* This function checks the "strcmp" converter's arguments and extracts the
2947 * variable name and its scope.
2948 */
2949static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2950 const char *file, int line, char **err)
2951{
2952 /* Try to decode a variable. */
2953 if (vars_check_arg(&args[0], NULL))
2954 return 1;
2955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002956 memprintf(err, "failed to register variable name '%s'",
2957 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002958 return 0;
2959}
2960
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002961/************************************************************************/
2962/* All supported sample fetch functions must be declared here */
2963/************************************************************************/
2964
2965/* force TRUE to be returned at the fetch level */
2966static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002967smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002968{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002969 if (!smp_make_rw(smp))
2970 return 0;
2971
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002972 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002973 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002974 return 1;
2975}
2976
2977/* force FALSE to be returned at the fetch level */
2978static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002979smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002980{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002981 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002982 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002983 return 1;
2984}
2985
2986/* retrieve environment variable $1 as a string */
2987static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002988smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002989{
2990 char *env;
2991
2992 if (!args || args[0].type != ARGT_STR)
2993 return 0;
2994
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002995 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002996 if (!env)
2997 return 0;
2998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002999 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003000 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003001 smp->data.u.str.area = env;
3002 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003003 return 1;
3004}
3005
Damien Claisseae6f1252019-10-30 15:57:28 +00003006/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3007 * optional string representing the unit of the result: "s" for seconds, "ms" for
3008 * milliseconds and "us" for microseconds.
3009 * Returns 0 on error and non-zero if OK.
3010 */
3011int smp_check_date_unit(struct arg *args, char **err)
3012{
3013 if (args[1].type == ARGT_STR) {
3014 if (strcmp(args[1].data.str.area, "s") == 0) {
3015 args[1].data.sint = TIME_UNIT_S;
3016 }
3017 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3018 args[1].data.sint = TIME_UNIT_MS;
3019 }
3020 else if (strcmp(args[1].data.str.area, "us") == 0) {
3021 args[1].data.sint = TIME_UNIT_US;
3022 }
3023 else {
3024 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3025 args[1].data.str.area);
3026 return 0;
3027 }
3028 free(args[1].data.str.area);
3029 args[1].data.str.area = NULL;
3030 args[1].type = ARGT_SINT;
3031 }
3032 else if (args[1].type != ARGT_STOP) {
3033 memprintf(err, "Unexpected arg type");
3034 return 0;
3035 }
3036
3037 return 1;
3038}
3039
3040/* retrieve the current local date in epoch time, converts it to milliseconds
3041 * or microseconds if asked to in optional args[1] unit param, and applies an
3042 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003043 */
3044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003045smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003046{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003047 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003048
Damien Claisseae6f1252019-10-30 15:57:28 +00003049 /* report in milliseconds */
3050 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3051 smp->data.u.sint *= 1000;
3052 smp->data.u.sint += date.tv_usec / 1000;
3053 }
3054 /* report in microseconds */
3055 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3056 smp->data.u.sint *= 1000000;
3057 smp->data.u.sint += date.tv_usec;
3058 }
3059
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003060 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003061 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003062 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003063
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003064 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003065 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3066 return 1;
3067}
3068
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003069/* retrieve the current microsecond part of the date */
3070static int
3071smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3072{
3073 smp->data.u.sint = date.tv_usec;
3074 smp->data.type = SMP_T_SINT;
3075 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3076 return 1;
3077}
3078
3079
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003080/* returns the hostname */
3081static int
3082smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3083{
3084 smp->data.type = SMP_T_STR;
3085 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003086 smp->data.u.str.area = hostname;
3087 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003088 return 1;
3089}
3090
Willy Tarreau0f30d262014-11-24 16:02:05 +01003091/* returns the number of processes */
3092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003093smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003094{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003095 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003096 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003097 return 1;
3098}
3099
3100/* returns the number of the current process (between 1 and nbproc */
3101static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003102smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003103{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003105 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003106 return 1;
3107}
3108
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003109/* returns the number of the current thread (between 1 and nbthread */
3110static int
3111smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3112{
3113 smp->data.type = SMP_T_SINT;
3114 smp->data.u.sint = tid;
3115 return 1;
3116}
3117
Willy Tarreau84310e22014-02-14 11:59:04 +01003118/* generate a random 32-bit integer for whatever purpose, with an optional
3119 * range specified in argument.
3120 */
3121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003122smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003123{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003124 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003125
3126 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003127 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003128 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003129
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003130 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003131 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3132 return 1;
3133}
3134
Willy Tarreau0f30d262014-11-24 16:02:05 +01003135/* returns true if the current process is stopping */
3136static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003137smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003138{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003139 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003140 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003141 return 1;
3142}
3143
Willy Tarreau70fe9442018-11-22 16:07:39 +01003144/* returns the number of calls of the current stream's process_stream() */
3145static int
3146smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3147{
3148 smp->data.type = SMP_T_SINT;
3149 smp->data.u.sint = smp->strm->task->calls;
3150 return 1;
3151}
3152
3153/* returns the average number of nanoseconds spent processing the stream per call */
3154static int
3155smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3156{
3157 smp->data.type = SMP_T_SINT;
3158 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3159 return 1;
3160}
3161
3162/* returns the total number of nanoseconds spent processing the stream */
3163static int
3164smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3165{
3166 smp->data.type = SMP_T_SINT;
3167 smp->data.u.sint = smp->strm->task->cpu_time;
3168 return 1;
3169}
3170
3171/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3172static int
3173smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3174{
3175 smp->data.type = SMP_T_SINT;
3176 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3177 return 1;
3178}
3179
3180/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3181static int
3182smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3183{
3184 smp->data.type = SMP_T_SINT;
3185 smp->data.u.sint = smp->strm->task->lat_time;
3186 return 1;
3187}
3188
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003189static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3190{
3191 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003192 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 smp->data.u.str.area = args[0].data.str.area;
3194 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003195 return 1;
3196}
3197
3198static int smp_check_const_bool(struct arg *args, char **err)
3199{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003200 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3201 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003202 args[0].type = ARGT_SINT;
3203 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003204 return 1;
3205 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003206 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3207 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003208 args[0].type = ARGT_SINT;
3209 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003210 return 1;
3211 }
3212 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3213 return 0;
3214}
3215
3216static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3217{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003218 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003219 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003220 return 1;
3221}
3222
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003223static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003224{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003225 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003226 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003227 return 1;
3228}
3229
3230static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3231{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003232 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003233 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003234 return 1;
3235}
3236
3237static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3238{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003239 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003240 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003241 return 1;
3242}
3243
3244static int smp_check_const_bin(struct arg *args, char **err)
3245{
David Carlier64a16ab2016-04-08 10:37:02 +01003246 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003247 int binstrlen;
3248
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003249 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003250 return 0;
3251 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003252 args[0].data.str.area = binstr;
3253 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003254 return 1;
3255}
3256
3257static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3258{
3259 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003260 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003261 smp->data.u.str.area = args[0].data.str.area;
3262 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003263 return 1;
3264}
3265
3266static int smp_check_const_meth(struct arg *args, char **err)
3267{
3268 enum http_meth_t meth;
3269 int i;
3270
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003271 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003272 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003273 args[0].type = ARGT_SINT;
3274 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003275 } else {
3276 /* Check method avalaibility. A methos is a token defined as :
3277 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3278 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3279 * token = 1*tchar
3280 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003281 for (i = 0; i < args[0].data.str.data; i++) {
3282 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003283 memprintf(err, "expects valid method.");
3284 return 0;
3285 }
3286 }
3287 }
3288 return 1;
3289}
3290
3291static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3292{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003293 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003294 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003295 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003296 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003297 smp->data.u.meth.str.area = "";
3298 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003299 } else {
3300 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003301 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003302 smp->data.u.meth.str.area = args[0].data.str.area;
3303 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003304 }
3305 return 1;
3306}
3307
Luca Schimweg8a694b82019-09-10 15:42:52 +02003308// This function checks the "uuid" sample's arguments.
3309// Function won't get called when no parameter is specified (maybe a bug?)
3310static int smp_check_uuid(struct arg *args, char **err)
3311{
3312 if (!args[0].type) {
3313 args[0].type = ARGT_SINT;
3314 args[0].data.sint = 4;
3315 }
3316 else if (args[0].data.sint != 4) {
3317 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3318 return 0;
3319 }
3320
3321 return 1;
3322}
3323
3324// Generate a RFC4122 UUID (default is v4 = fully random)
3325static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3326{
3327 if (args[0].data.sint == 4 || !args[0].type) {
3328 uint32_t rnd[4] = { 0, 0, 0, 0 };
3329 uint64_t last = 0;
3330 int byte = 0;
3331 uint8_t bits = 0;
3332 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3333
3334 while (byte < 4) {
3335 while (bits < 32) {
3336 last |= (uint64_t)random() << bits;
3337 bits += rand_max_bits;
3338 }
3339 rnd[byte++] = last;
3340 last >>= 32u;
3341 bits -= 32;
3342 }
3343
3344 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3345 rnd[0],
3346 rnd[1] & 0xFFFF,
3347 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3348 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3349 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3350 );
3351
3352 smp->data.type = SMP_T_STR;
3353 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3354 smp->data.u.str = trash;
3355 return 1;
3356 }
3357
3358 // more implementations of other uuid formats possible here
3359 return 0;
3360}
3361
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003362/* Note: must not be declared <const> as its list will be overwritten.
3363 * Note: fetches that may return multiple types must be declared as the lowest
3364 * common denominator, the type that can be casted into all other ones. For
3365 * instance IPv4/IPv6 must be declared IPv4.
3366 */
3367static struct sample_fetch_kw_list smp_kws = {ILH, {
3368 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3369 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003370 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003371 { "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 +01003372 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003373 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003374 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3375 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003376 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003377 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003378 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003379 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003380 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003381
3382 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3383 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3384 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3385 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3386 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003387
3388 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3389 { "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 +02003390 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003391 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3392 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3393 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3394 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3395
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003396 { /* END */ },
3397}};
3398
Willy Tarreau0108d902018-11-25 19:14:37 +01003399INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3400
Emeric Brun107ca302010-01-04 16:16:05 +01003401/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003402static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003403 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003404 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003405 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003406 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3407 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003408 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003409 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003410 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003411 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003412 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3413 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003414 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003415 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003416 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3417 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3418 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003419 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3420 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003421 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003422 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003423 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3424 { "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 +01003425 { "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 +02003426 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003427#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003428 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003429#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003430 { "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 +02003431 { "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 +01003432
3433 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003434 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3435 { "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 +01003436
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003437 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3438 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3439 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3440 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3441 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3442 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3443 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3444 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3445 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3446 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3447 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3448 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3449 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3450 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003451
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003452 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003453}};
3454
Willy Tarreau0108d902018-11-25 19:14:37 +01003455INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);