blob: d82451fc3fa521f5efd230144a4a809a4f7daf5f [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 /* At this point, we have :
860 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100861 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 */
863
864 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100865 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100866 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun107ca302010-01-04 16:16:05 +0100868 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200869
Vincent Bernat02779b62016-04-03 13:48:43 +0200870 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200871 if (!expr)
872 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100873
874 LIST_INIT(&(expr->conv_exprs));
875 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200876 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100877
Willy Tarreau689a1df2013-12-13 00:40:11 +0100878 /* Note that we call the argument parser even with an empty string,
879 * this allows it to automatically create entries for mandatory
880 * implicit arguments (eg: local proxy name).
881 */
882 al->kw = expr->fetch->kw;
883 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100884 if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100885 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
886 goto out_error;
887 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200888
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100889 /* now endt is our first char not part of the arg list, typically the
890 * comma after the sample fetch name or after the closing parenthesis,
891 * or the NUL char.
892 */
893
Willy Tarreau689a1df2013-12-13 00:40:11 +0100894 if (!expr->arg_p) {
895 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200896 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100897 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
898 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200899 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100900 }
901
Willy Tarreau833cc792013-07-24 15:34:19 +0200902 /* Now process the converters if any. We have two supported syntaxes
903 * for the converters, which can be combined :
904 * - comma-delimited list of converters just after the keyword and args ;
905 * - one converter per keyword
906 * The combination allows to have each keyword being a comma-delimited
907 * series of converters.
908 *
909 * We want to process the former first, then the latter. For this we start
910 * from the beginning of the supposed place in the exiting conv chain, which
911 * starts at the last comma (endt).
912 */
913
914 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200915 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100916 int err_arg;
917 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100918
Willy Tarreau833cc792013-07-24 15:34:19 +0200919 if (*endt && *endt != ',') {
920 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100921 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200922 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100923 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100924 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200925 }
Emeric Brun107ca302010-01-04 16:16:05 +0100926
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100927 /* FIXME: how long should we support such idiocies ? Maybe we
928 * should already warn ?
929 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 while (*endt == ',') /* then trailing commas */
931 endt++;
932
Willy Tarreau97108e02016-11-25 07:33:24 +0100933 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200934
935 if (!*begw) {
936 /* none ? skip to next string */
937 (*idx)++;
938 begw = str[*idx];
939 if (!begw || !*begw)
940 break;
941 }
942
Willy Tarreaued2c6622020-02-14 18:27:10 +0100943 for (endw = begw; is_idchar(*endw); endw++)
944 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200945
946 free(ckw);
947 ckw = my_strndup(begw, endw - begw);
948
949 conv = find_sample_conv(begw, endw - begw);
950 if (!conv) {
951 /* we found an isolated keyword that we don't know, it's not ours */
952 if (begw == str[*idx])
953 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100954 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200955 goto out_error;
956 }
Emeric Brun107ca302010-01-04 16:16:05 +0100957
Willy Tarreau833cc792013-07-24 15:34:19 +0200958 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100959 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100960 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200961 }
Emeric Brun107ca302010-01-04 16:16:05 +0100962
963 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200964 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100966 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200967 }
Emeric Brun107ca302010-01-04 16:16:05 +0100968
969 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200970 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200971 if (!conv_expr)
972 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100973
974 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
975 conv_expr->conv = conv;
976
Willy Tarreau46dfd782019-12-17 10:25:29 +0100977 al->kw = expr->fetch->kw;
978 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100979 argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
Willy Tarreau46dfd782019-12-17 10:25:29 +0100980 if (argcnt < 0) {
981 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
982 goto out_error;
983 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100984
Willy Tarreau46dfd782019-12-17 10:25:29 +0100985 if (argcnt && !conv->arg_mask) {
986 memprintf(err_msg, "converter '%s' does not support any args", ckw);
987 goto out_error;
988 }
Willy Tarreau21d68a62012-04-20 15:52:36 +0200989
Willy Tarreau46dfd782019-12-17 10:25:29 +0100990 if (!conv_expr->arg_p)
991 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200992
Willy Tarreau46dfd782019-12-17 10:25:29 +0100993 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
994 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200995 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100996 }
997 }
Emeric Brun485479d2010-09-23 18:02:19 +0200998
Willy Tarreau833cc792013-07-24 15:34:19 +0200999 out:
1000 free(fkw);
1001 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001002 return expr;
1003
1004out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001005 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001006 expr = NULL;
1007 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001008}
1009
1010/*
Willy Tarreau12785782012-04-27 21:37:17 +02001011 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001012 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001013 * Returns a pointer on a typed sample structure containing the result or NULL if
1014 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001015 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001016 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001017 *
1018 * Note: the fetch functions are required to properly set the return type. The
1019 * conversion functions must do so too. However the cast functions do not need
1020 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001021 *
1022 * The caller may indicate in <opt> if it considers the result final or not.
1023 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1024 * if the result is stable or not, according to the following table :
1025 *
1026 * return MAY_CHANGE FINAL Meaning for the sample
1027 * NULL 0 * Not present and will never be (eg: header)
1028 * NULL 1 0 Not present yet, could change (eg: POST param)
1029 * NULL 1 1 Not present yet, will not change anymore
1030 * smp 0 * Present and will not change (eg: header)
1031 * smp 1 0 Present, may change (eg: request length)
1032 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001033 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001034struct sample *sample_process(struct proxy *px, struct session *sess,
1035 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001036 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001037{
Willy Tarreau12785782012-04-27 21:37:17 +02001038 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001039
Willy Tarreau18387e22013-07-25 12:02:38 +02001040 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001041 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001042 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001043 }
Emeric Brun107ca302010-01-04 16:16:05 +01001044
Willy Tarreau1777ea62016-03-10 16:15:46 +01001045 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001046 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001047 return NULL;
1048
Emeric Brun107ca302010-01-04 16:16:05 +01001049 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001050 /* we want to ensure that p->type can be casted into
1051 * conv_expr->conv->in_type. We have 3 possibilities :
1052 * - NULL => not castable.
1053 * - c_none => nothing to do (let's optimize it)
1054 * - other => apply cast and prepare to fail
1055 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001056 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001057 return NULL;
1058
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001059 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1060 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001061 return NULL;
1062
Willy Tarreau12e50112012-04-25 17:21:49 +02001063 /* OK cast succeeded */
1064
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001065 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001066 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001067 }
1068 return p;
1069}
1070
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001071/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001072 * Resolve all remaining arguments in proxy <p>. Returns the number of
1073 * errors or 0 if everything is fine.
1074 */
1075int smp_resolve_args(struct proxy *p)
1076{
1077 struct arg_list *cur, *bak;
1078 const char *ctx, *where;
1079 const char *conv_ctx, *conv_pre, *conv_pos;
1080 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001081 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001082 struct arg *arg;
1083 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001084 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001085
1086 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1087 struct proxy *px;
1088 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001089 struct stktable *t;
1090 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001091 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001092
1093 arg = cur->arg;
1094
1095 /* prepare output messages */
1096 conv_pre = conv_pos = conv_ctx = "";
1097 if (cur->conv) {
1098 conv_ctx = cur->conv;
1099 conv_pre = "conversion keyword '";
1100 conv_pos = "' for ";
1101 }
1102
1103 where = "in";
1104 ctx = "sample fetch keyword";
1105 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001106 case ARGC_STK: where = "in stick rule in"; break;
1107 case ARGC_TRK: where = "in tracking rule in"; break;
1108 case ARGC_LOG: where = "in log-format string in"; break;
1109 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1110 case ARGC_HRQ: where = "in http-request header format string in"; break;
1111 case ARGC_HRS: where = "in http-response header format string in"; break;
1112 case ARGC_UIF: where = "in unique-id-format string in"; break;
1113 case ARGC_RDR: where = "in redirect format string in"; break;
1114 case ARGC_CAP: where = "in capture rule in"; break;
1115 case ARGC_ACL: ctx = "ACL keyword"; break;
1116 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001117 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001118 }
1119
1120 /* set a few default settings */
1121 px = p;
1122 pname = p->id;
1123
1124 switch (arg->type) {
1125 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001126 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001127 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1128 cur->file, cur->line,
1129 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001130 cfgerr++;
1131 continue;
1132 }
1133
1134 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001135 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001136
1137 if (sname) {
1138 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001139 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001140
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001141 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001142 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001143 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1144 cur->file, cur->line, pname,
1145 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001146 cfgerr++;
1147 break;
1148 }
1149 }
1150 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001151 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152
1153 srv = findserver(px, sname);
1154 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001155 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",
1156 cur->file, cur->line, sname, pname,
1157 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158 cfgerr++;
1159 break;
1160 }
1161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001162 free(arg->data.str.area);
1163 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 arg->unresolved = 0;
1165 arg->data.srv = srv;
1166 break;
1167
1168 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001169 if (arg->data.str.data) {
1170 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001171 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172 }
1173
1174 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1176 cur->file, cur->line, pname,
1177 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001178 cfgerr++;
1179 break;
1180 }
1181
1182 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 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",
1184 cur->file, cur->line, pname,
1185 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 cfgerr++;
1187 break;
1188 }
1189
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001190 free(arg->data.str.area);
1191 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001192 arg->unresolved = 0;
1193 arg->data.prx = px;
1194 break;
1195
1196 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001197 if (arg->data.str.data) {
1198 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001199 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 }
1201
1202 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1204 cur->file, cur->line, pname,
1205 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 cfgerr++;
1207 break;
1208 }
1209
1210 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001211 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",
1212 cur->file, cur->line, pname,
1213 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 cfgerr++;
1215 break;
1216 }
1217
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001218 free(arg->data.str.area);
1219 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 arg->unresolved = 0;
1221 arg->data.prx = px;
1222 break;
1223
1224 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001225 if (arg->data.str.data)
1226 stktname = arg->data.str.area;
1227 else
1228 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001229
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001230 t = stktable_find_by_name(stktname);
1231 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 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 +01001233 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001234 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 cfgerr++;
1236 break;
1237 }
1238
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001239 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001240 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 +01001241 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001242 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243 cfgerr++;
1244 break;
1245 }
1246
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001247 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001248 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 +01001249 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001250 cfgerr++;
1251 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001252 }
1253
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001254 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001255 p->next_stkt_ref = t->proxies_list;
1256 t->proxies_list = p;
1257 }
1258
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001259 free(arg->data.str.area);
1260 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001261 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001262 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 break;
1264
1265 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001266 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001267 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1268 cur->file, cur->line,
1269 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001270 cfgerr++;
1271 break;
1272 }
1273
1274 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001275 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276 ul = p->uri_auth->userlist;
1277 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001278 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001279
1280 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001281 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 +02001282 cur->file, cur->line,
1283 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001284 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001285 cfgerr++;
1286 break;
1287 }
1288
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001289 free(arg->data.str.area);
1290 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001291 arg->unresolved = 0;
1292 arg->data.usr = ul;
1293 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001294
1295 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001296 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001297 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1298 cur->file, cur->line,
1299 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001300 cfgerr++;
1301 continue;
1302 }
1303
Willy Tarreau46947782015-01-19 19:00:58 +01001304 rflags = 0;
1305 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1306 err = NULL;
1307
Dragan Dosen26743032019-04-30 15:54:36 +02001308 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001309 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1310 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001311 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001312 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001313 cfgerr++;
1314 continue;
1315 }
1316
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001317 free(arg->data.str.area);
1318 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001319 arg->unresolved = 0;
1320 arg->data.reg = reg;
1321 break;
1322
1323
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001324 }
1325
1326 LIST_DEL(&cur->list);
1327 free(cur);
1328 } /* end of args processing */
1329
1330 return cfgerr;
1331}
1332
1333/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001334 * Process a fetch + format conversion as defined by the sample expression
1335 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001336 * not explicitly set to <smp_type>, but shall be compatible with it as
1337 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1338 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001339 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1340 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1341 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1342 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001343 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1344 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1345 * still hope to get it after waiting longer, and is not converted to string.
1346 * The possible output combinations are the following :
1347 *
1348 * return MAY_CHANGE FINAL Meaning for the sample
1349 * NULL * * Not present and will never be (eg: header)
1350 * smp 0 * Final value converted (eg: header)
1351 * smp 1 0 Not present yet, may appear later (eg: header)
1352 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001353 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001354struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001355 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001356 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001357{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001358 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001359
Willy Tarreau6c616e02014-06-25 16:56:41 +02001360 memset(smp, 0, sizeof(*smp));
1361
Willy Tarreau192252e2015-04-04 01:47:55 +02001362 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001363 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1364 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001365 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001366 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001367
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001368 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001369 return NULL;
1370
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001371 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372 return NULL;
1373
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001374 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375 return smp;
1376}
1377
Christopher Faulet476e5d02016-10-26 11:34:47 +02001378static void release_sample_arg(struct arg *p)
1379{
1380 struct arg *p_back = p;
1381
1382 if (!p)
1383 return;
1384
1385 while (p->type != ARGT_STOP) {
1386 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001387 free(p->data.str.area);
1388 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001389 p->unresolved = 0;
1390 }
1391 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001392 regex_free(p->data.reg);
1393 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001394 }
1395 p++;
1396 }
1397
1398 if (p_back != empty_arg_list)
1399 free(p_back);
1400}
1401
1402void release_sample_expr(struct sample_expr *expr)
1403{
1404 struct sample_conv_expr *conv_expr, *conv_exprb;
1405
1406 if (!expr)
1407 return;
1408
1409 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1410 release_sample_arg(conv_expr->arg_p);
1411 release_sample_arg(expr->arg_p);
1412 free(expr);
1413}
1414
Emeric Brun107ca302010-01-04 16:16:05 +01001415/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001416/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001417/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001418/*****************************************************************/
1419
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001420static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1421{
1422 int i;
1423 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001424 struct buffer *buf;
1425 struct sink *sink;
1426 struct ist line;
1427 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001428
Willy Tarreau0851fd52019-12-17 10:07:25 +01001429 buf = alloc_trash_chunk();
1430 if (!buf)
1431 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001432
Willy Tarreau0851fd52019-12-17 10:07:25 +01001433 sink = (struct sink *)arg_p[1].data.str.area;
1434 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001435
Willy Tarreau0851fd52019-12-17 10:07:25 +01001436 pfx = arg_p[0].data.str.area;
1437 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001438
Willy Tarreau0851fd52019-12-17 10:07:25 +01001439 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1440 if (!sample_casts[smp->data.type][SMP_T_STR])
1441 goto nocast;
1442
1443 /* Copy sample fetch. This puts the sample as const, the
1444 * cast will copy data if a transformation is required.
1445 */
1446 memcpy(&tmp, smp, sizeof(struct sample));
1447 tmp.flags = SMP_F_CONST;
1448
1449 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1450 goto nocast;
1451
1452 /* Display the displayable chars*. */
1453 b_putchr(buf, '<');
1454 for (i = 0; i < tmp.data.u.str.data; i++) {
1455 if (isprint(tmp.data.u.str.area[i]))
1456 b_putchr(buf, tmp.data.u.str.area[i]);
1457 else
1458 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001459 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001460 b_putchr(buf, '>');
1461
1462 done:
1463 line = ist2(buf->area, buf->data);
1464 sink_write(sink, &line, 1);
1465 end:
1466 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001467 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001468 nocast:
1469 chunk_appendf(buf, "(undisplayable)");
1470 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001471}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001472
1473// This function checks the "debug" converter's arguments.
1474static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1475 const char *file, int line, char **err)
1476{
1477 const char *name = "buf0";
1478 struct sink *sink = NULL;
1479
1480 if (args[0].type != ARGT_STR) {
1481 /* optional prefix */
1482 args[0].data.str.area = "";
1483 args[0].data.str.data = 0;
1484 }
1485
1486 if (args[1].type == ARGT_STR)
1487 name = args[1].data.str.area;
1488
1489 sink = sink_find(name);
1490 if (!sink) {
1491 memprintf(err, "No such sink '%s'", name);
1492 return 0;
1493 }
1494
1495 args[1].data.str.area = (char *)sink;
1496 args[1].data.str.data = 0; // that's not a string anymore
1497 return 1;
1498}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001499
Holger Just1bfc24b2017-05-06 00:56:53 +02001500static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1501{
Willy Tarreau83061a82018-07-13 11:56:34 +02001502 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001503 int bin_len;
1504
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001505 trash->data = 0;
1506 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1507 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001508 if (bin_len < 0)
1509 return 0;
1510
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001511 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001512 smp->data.u.str = *trash;
1513 smp->data.type = SMP_T_BIN;
1514 smp->flags &= ~SMP_F_CONST;
1515 return 1;
1516}
1517
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001518static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001519{
Willy Tarreau83061a82018-07-13 11:56:34 +02001520 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001521 int b64_len;
1522
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001523 trash->data = 0;
1524 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1525 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001526 if (b64_len < 0)
1527 return 0;
1528
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001529 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001530 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001531 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001532 smp->flags &= ~SMP_F_CONST;
1533 return 1;
1534}
1535
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001536static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1537{
1538 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001539 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001540
1541 memset(&ctx, 0, sizeof(ctx));
1542
1543 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001544 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1545 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001546
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001547 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001548 smp->data.u.str = *trash;
1549 smp->data.type = SMP_T_BIN;
1550 smp->flags &= ~SMP_F_CONST;
1551 return 1;
1552}
1553
Tim Duesterhusd4376302019-06-17 12:41:44 +02001554#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001555static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1556 const char *file, int line, char **err)
1557{
1558 if (args[0].type == ARGT_STOP)
1559 return 1;
1560 if (args[0].type != ARGT_SINT) {
1561 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1562 return 0;
1563 }
1564
1565 switch (args[0].data.sint) {
1566 case 224:
1567 case 256:
1568 case 384:
1569 case 512:
1570 /* this is okay */
1571 return 1;
1572 default:
1573 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1574 return 0;
1575 }
1576}
1577
Tim Duesterhusd4376302019-06-17 12:41:44 +02001578static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1579{
1580 struct buffer *trash = get_trash_chunk();
1581 int bits = 256;
1582 if (arg_p && arg_p->data.sint)
1583 bits = arg_p->data.sint;
1584
1585 switch (bits) {
1586 case 224: {
1587 SHA256_CTX ctx;
1588
1589 memset(&ctx, 0, sizeof(ctx));
1590
1591 SHA224_Init(&ctx);
1592 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1593 SHA224_Final((unsigned char *) trash->area, &ctx);
1594 trash->data = SHA224_DIGEST_LENGTH;
1595 break;
1596 }
1597 case 256: {
1598 SHA256_CTX ctx;
1599
1600 memset(&ctx, 0, sizeof(ctx));
1601
1602 SHA256_Init(&ctx);
1603 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1604 SHA256_Final((unsigned char *) trash->area, &ctx);
1605 trash->data = SHA256_DIGEST_LENGTH;
1606 break;
1607 }
1608 case 384: {
1609 SHA512_CTX ctx;
1610
1611 memset(&ctx, 0, sizeof(ctx));
1612
1613 SHA384_Init(&ctx);
1614 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1615 SHA384_Final((unsigned char *) trash->area, &ctx);
1616 trash->data = SHA384_DIGEST_LENGTH;
1617 break;
1618 }
1619 case 512: {
1620 SHA512_CTX ctx;
1621
1622 memset(&ctx, 0, sizeof(ctx));
1623
1624 SHA512_Init(&ctx);
1625 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1626 SHA512_Final((unsigned char *) trash->area, &ctx);
1627 trash->data = SHA512_DIGEST_LENGTH;
1628 break;
1629 }
1630 default:
1631 return 0;
1632 }
1633
1634 smp->data.u.str = *trash;
1635 smp->data.type = SMP_T_BIN;
1636 smp->flags &= ~SMP_F_CONST;
1637 return 1;
1638}
1639#endif
1640
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001641static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001642{
Willy Tarreau83061a82018-07-13 11:56:34 +02001643 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001644 unsigned char c;
1645 int ptr = 0;
1646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001647 trash->data = 0;
1648 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1649 c = smp->data.u.str.area[ptr++];
1650 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1651 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001652 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001653 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001654 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001655 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001656 return 1;
1657}
1658
Dragan Dosen3f957b22017-10-24 09:27:34 +02001659static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1660{
1661 long long int n = 0;
1662 int i, c;
1663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001664 for (i = 0; i < smp->data.u.str.data; i++) {
1665 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001666 return 0;
1667 n = (n << 4) + c;
1668 }
1669
1670 smp->data.u.sint = n;
1671 smp->data.type = SMP_T_SINT;
1672 smp->flags &= ~SMP_F_CONST;
1673 return 1;
1674}
1675
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001676/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001677static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001678{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001679 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1680 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001681 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001682 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001683 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001684 return 1;
1685}
1686
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001687static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001688{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001689 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001690 smp->data.u.sint = i;
1691 smp->data.type = SMP_T_SINT;
1692 return 1;
1693}
1694
1695
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001696static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001697{
1698 int i;
1699
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001700 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001701 return 0;
1702
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001703 for (i = 0; i < smp->data.u.str.data; i++) {
1704 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1705 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001706 }
1707 return 1;
1708}
1709
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001710static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001711{
1712 int i;
1713
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001714 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001715 return 0;
1716
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001717 for (i = 0; i < smp->data.u.str.data; i++) {
1718 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1719 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001720 }
1721 return 1;
1722}
1723
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001724/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1725static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001726{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001727 /* Attempt to convert to IPv4 to apply the correct mask. */
1728 c_ipv62ip(smp);
1729
1730 if (smp->data.type == SMP_T_IPV4) {
1731 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1732 smp->data.type = SMP_T_IPV4;
1733 }
1734 else if (smp->data.type == SMP_T_IPV6) {
1735 /* IPv6 cannot be converted without an IPv6 mask. */
1736 if (args[1].type != ARGT_IPV6)
1737 return 0;
1738
1739 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1740 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1741 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1742 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1743 smp->data.type = SMP_T_IPV6;
1744 }
1745
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001746 return 1;
1747}
1748
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001749/* takes an UINT value on input supposed to represent the time since EPOCH,
1750 * adds an optional offset found in args[1] and emits a string representing
1751 * the local time in the format specified in args[1] using strftime().
1752 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001753static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001754{
Willy Tarreau83061a82018-07-13 11:56:34 +02001755 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001756 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001757 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001758 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001759
1760 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001761 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001762 curr_date += args[1].data.sint;
1763
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001764 tm = localtime(&curr_date);
1765 if (!tm)
1766 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001767 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001768 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1769 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001770 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001771 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001772 return 1;
1773}
1774
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001775/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001776static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001777{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001778 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1779 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001780 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001781 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001782 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001783 return 1;
1784}
1785
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001786/* takes an UINT value on input supposed to represent the time since EPOCH,
1787 * adds an optional offset found in args[1] and emits a string representing
1788 * the UTC date in the format specified in args[1] using strftime().
1789 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001790static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001791{
Willy Tarreau83061a82018-07-13 11:56:34 +02001792 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001793 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001794 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001795 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001796
1797 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001798 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001799 curr_date += args[1].data.sint;
1800
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001801 tm = gmtime(&curr_date);
1802 if (!tm)
1803 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001804 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001805 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1806 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001807 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001808 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001809 return 1;
1810}
1811
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001812/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001813static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001814{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001815 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1816 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001817 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001818 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001819 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001820 return 1;
1821}
1822
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001823/* hashes the binary input into a 32-bit unsigned int using xxh.
1824 * The seed of the hash defaults to 0 but can be changd in argument 1.
1825 */
1826static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1827{
1828 unsigned int seed;
1829
1830 if (arg_p && arg_p->data.sint)
1831 seed = arg_p->data.sint;
1832 else
1833 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001834 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1835 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001836 smp->data.type = SMP_T_SINT;
1837 return 1;
1838}
1839
1840/* hashes the binary input into a 64-bit unsigned int using xxh.
1841 * In fact, the function returns a 64 bit unsigned, but the sample
1842 * storage of haproxy only proposes 64-bits signed, so the value is
1843 * cast as signed. This cast doesn't impact the hash repartition.
1844 * The seed of the hash defaults to 0 but can be changd in argument 1.
1845 */
1846static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1847{
1848 unsigned long long int seed;
1849
1850 if (arg_p && arg_p->data.sint)
1851 seed = (unsigned long long int)arg_p->data.sint;
1852 else
1853 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1855 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001856 smp->data.type = SMP_T_SINT;
1857 return 1;
1858}
1859
Willy Tarreau80599772015-01-20 19:35:24 +01001860/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001861static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001862{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001863 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1864 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001865 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001866 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001867 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001868 return 1;
1869}
1870
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001871/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1872static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1873{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001874 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1875 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001876 if (arg_p && arg_p->data.sint)
1877 smp->data.u.sint = full_hash(smp->data.u.sint);
1878 smp->data.type = SMP_T_SINT;
1879 return 1;
1880}
1881
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001882/* This function escape special json characters. The returned string can be
1883 * safely set between two '"' and used as json string. The json string is
1884 * defined like this:
1885 *
1886 * any Unicode character except '"' or '\' or control character
1887 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1888 *
1889 * The enum input_type contain all the allowed mode for decoding the input
1890 * string.
1891 */
1892enum input_type {
1893 IT_ASCII = 0,
1894 IT_UTF8,
1895 IT_UTF8S,
1896 IT_UTF8P,
1897 IT_UTF8PS,
1898};
1899static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1900 const char *file, int line, char **err)
1901{
1902 if (!arg) {
1903 memprintf(err, "Unexpected empty arg list");
1904 return 0;
1905 }
1906
1907 if (arg->type != ARGT_STR) {
1908 memprintf(err, "Unexpected arg type");
1909 return 0;
1910 }
1911
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001912 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001913 arg->type = ARGT_SINT;
1914 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001915 return 1;
1916 }
1917
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001918 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001919 arg->type = ARGT_SINT;
1920 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001921 return 1;
1922 }
1923
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001924 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001925 arg->type = ARGT_SINT;
1926 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001927 return 1;
1928 }
1929
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001930 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001931 arg->type = ARGT_SINT;
1932 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001933 return 1;
1934 }
1935
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001936 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001937 arg->type = ARGT_SINT;
1938 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001939 return 1;
1940 }
1941
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001942 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001943 arg->type = ARGT_SINT;
1944 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001945 return 1;
1946 }
1947
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001948 memprintf(err, "Unexpected input code type. "
1949 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001950 return 0;
1951}
1952
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001953static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001954{
Willy Tarreau83061a82018-07-13 11:56:34 +02001955 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001956 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1957 const char *str;
1958 int len;
1959 enum input_type input_type = IT_ASCII;
1960 unsigned int c;
1961 unsigned int ret;
1962 char *p;
1963
1964 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001965 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001966
1967 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001968 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001969
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001970 p = smp->data.u.str.area;
1971 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001972
1973 if (input_type == IT_ASCII) {
1974 /* Read input as ASCII. */
1975 c = *(unsigned char *)p;
1976 p++;
1977 }
1978 else {
1979 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001980 ret = utf8_next(p,
1981 smp->data.u.str.data - ( p - smp->data.u.str.area),
1982 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001983 p += utf8_return_length(ret);
1984
1985 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1986 return 0;
1987 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1988 continue;
1989 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1990 return 0;
1991 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1992 continue;
1993
1994 /* Check too big values. */
1995 if ((unsigned int)c > 0xffff) {
1996 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1997 return 0;
1998 continue;
1999 }
2000 }
2001
2002 /* Convert character. */
2003 if (c == '"') {
2004 len = 2;
2005 str = "\\\"";
2006 }
2007 else if (c == '\\') {
2008 len = 2;
2009 str = "\\\\";
2010 }
2011 else if (c == '/') {
2012 len = 2;
2013 str = "\\/";
2014 }
2015 else if (c == '\b') {
2016 len = 2;
2017 str = "\\b";
2018 }
2019 else if (c == '\f') {
2020 len = 2;
2021 str = "\\f";
2022 }
2023 else if (c == '\r') {
2024 len = 2;
2025 str = "\\r";
2026 }
2027 else if (c == '\n') {
2028 len = 2;
2029 str = "\\n";
2030 }
2031 else if (c == '\t') {
2032 len = 2;
2033 str = "\\t";
2034 }
2035 else if (c > 0xff || !isprint(c)) {
2036 /* isprint generate a segfault if c is too big. The man says that
2037 * c must have the value of an unsigned char or EOF.
2038 */
2039 len = 6;
2040 _str[0] = '\\';
2041 _str[1] = 'u';
2042 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2043 str = _str;
2044 }
2045 else {
2046 len = 1;
2047 str = (char *)&c;
2048 }
2049
2050 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002051 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002052 return 0;
2053
2054 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002055 memcpy(temp->area + temp->data, str, len);
2056 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002057 }
2058
2059 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002060 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002061 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002062
2063 return 1;
2064}
2065
Emeric Brun54c4ac82014-11-03 15:32:43 +01002066/* This sample function is designed to extract some bytes from an input buffer.
2067 * First arg is the offset.
2068 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002069static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002070{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002071 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2072 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002073 return 1;
2074 }
2075
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002076 if (smp->data.u.str.size)
2077 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 smp->data.u.str.data -= arg_p[0].data.sint;
2079 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002080
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002081 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2082 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002083
2084 return 1;
2085}
2086
Emeric Brunf399b0d2014-11-03 17:07:03 +01002087static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2088 const char *file, int line, char **err)
2089{
2090 struct arg *arg = args;
2091
2092 if (!arg) {
2093 memprintf(err, "Unexpected empty arg list");
2094 return 0;
2095 }
2096
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002097 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002098 memprintf(err, "Unexpected arg type");
2099 return 0;
2100 }
2101
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002102 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002103 memprintf(err, "Unexpected value 0 for index");
2104 return 0;
2105 }
2106
2107 arg++;
2108
2109 if (arg->type != ARGT_STR) {
2110 memprintf(err, "Unexpected arg type");
2111 return 0;
2112 }
2113
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002114 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002115 memprintf(err, "Empty separators list");
2116 return 0;
2117 }
2118
2119 return 1;
2120}
2121
2122/* This sample function is designed to a return selected part of a string (field).
2123 * First arg is the index of the field (start at 1)
2124 * Second arg is a char list of separators (type string)
2125 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002126static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002127{
Marcin Deranek9631a282018-04-16 14:30:46 +02002128 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002129 char *start, *end;
2130 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002131 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002132
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002133 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002134 return 0;
2135
Marcin Deranek9631a282018-04-16 14:30:46 +02002136 if (arg_p[0].data.sint < 0) {
2137 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002138 end = start = smp->data.u.str.area + smp->data.u.str.data;
2139 while (start > smp->data.u.str.area) {
2140 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2141 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002142 if (field == arg_p[0].data.sint) {
2143 if (count == 1)
2144 goto found;
2145 else if (count > 1)
2146 count--;
2147 } else {
2148 end = start-1;
2149 field--;
2150 }
2151 break;
2152 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002153 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002154 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002155 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002156 } else {
2157 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002158 end = start = smp->data.u.str.area;
2159 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2160 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2161 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002162 if (field == arg_p[0].data.sint) {
2163 if (count == 1)
2164 goto found;
2165 else if (count > 1)
2166 count--;
2167 } else {
2168 start = end+1;
2169 field++;
2170 }
2171 break;
2172 }
2173 }
2174 end++;
2175 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002176 }
2177
2178 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002179 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002180 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002181 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002182 }
2183found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002184 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002185 /* If ret string is len 0, no need to
2186 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002187 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002188 return 1;
2189
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002190 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002191
2192 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002193 Note: smp->data.u.str.size cannot be set to 0 */
2194 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002195 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002196
2197 return 1;
2198}
2199
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002200/* This sample function is designed to return a word from a string.
2201 * First arg is the index of the word (start at 1)
2202 * Second arg is a char list of words separators (type string)
2203 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002204static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002205{
Marcin Deranek9631a282018-04-16 14:30:46 +02002206 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002207 char *start, *end;
2208 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002209 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002210
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002211 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002212 return 0;
2213
2214 word = 0;
2215 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002216 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002217 end = start = smp->data.u.str.area + smp->data.u.str.data;
2218 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002219 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002220 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2221 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002222 issep = 1;
2223 break;
2224 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002225 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002226 if (!inword) {
2227 if (!issep) {
2228 if (word != arg_p[0].data.sint) {
2229 word--;
2230 end = start;
2231 }
2232 inword = 1;
2233 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002234 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002235 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002236 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002237 if (count == 1)
2238 goto found;
2239 else if (count > 1)
2240 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002241 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002242 inword = 0;
2243 }
2244 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002245 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002246 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002247 end = start = smp->data.u.str.area;
2248 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002249 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002250 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2251 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002252 issep = 1;
2253 break;
2254 }
2255 }
2256 if (!inword) {
2257 if (!issep) {
2258 if (word != arg_p[0].data.sint) {
2259 word++;
2260 start = end;
2261 }
2262 inword = 1;
2263 }
2264 }
2265 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002266 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002267 if (count == 1)
2268 goto found;
2269 else if (count > 1)
2270 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002271 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002272 inword = 0;
2273 }
2274 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002275 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002276 }
2277
2278 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002279 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002280 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002281 return 1;
2282 }
2283found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002284 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002285 /* If ret string is len 0, no need to
2286 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002287 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002288 return 1;
2289
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002290 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002291
2292 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002293 Note: smp->data.u.str.size cannot be set to 0 */
2294 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002296
2297 return 1;
2298}
2299
Willy Tarreau7eda8492015-01-20 19:47:06 +01002300static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2301 const char *file, int line, char **err)
2302{
2303 struct arg *arg = args;
2304 char *p;
2305 int len;
2306
2307 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2308 arg[0].type_flags = 0;
2309
2310 if (arg[2].type != ARGT_STR)
2311 return 1;
2312
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002313 p = arg[2].data.str.area;
2314 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002315 while (len) {
2316 if (*p == 'i') {
2317 arg[0].type_flags |= ARGF_REG_ICASE;
2318 }
2319 else if (*p == 'g') {
2320 arg[0].type_flags |= ARGF_REG_GLOB;
2321 }
2322 else {
2323 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2324 return 0;
2325 }
2326 p++;
2327 len--;
2328 }
2329 return 1;
2330}
2331
2332/* This sample function is designed to do the equivalent of s/match/replace/ on
2333 * the input string. It applies a regex and restarts from the last matched
2334 * location until nothing matches anymore. First arg is the regex to apply to
2335 * the input string, second arg is the replacement expression.
2336 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002337static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002338{
2339 char *start, *end;
2340 struct my_regex *reg = arg_p[0].data.reg;
2341 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002342 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002343 int flag, max;
2344 int found;
2345
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002346 start = smp->data.u.str.area;
2347 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002348
2349 flag = 0;
2350 while (1) {
2351 /* check for last round which is used to copy remaining parts
2352 * when not running in global replacement mode.
2353 */
2354 found = 0;
2355 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2356 /* Note: we can have start == end on empty strings or at the end */
2357 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2358 }
2359
2360 if (!found)
2361 pmatch[0].rm_so = end - start;
2362
2363 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002364 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002365 if (max && pmatch[0].rm_so > 0) {
2366 if (max > pmatch[0].rm_so)
2367 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002368 memcpy(trash->area + trash->data, start, max);
2369 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002370 }
2371
2372 if (!found)
2373 break;
2374
2375 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002376 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002377 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002378 if (max > arg_p[1].data.str.data)
2379 max = arg_p[1].data.str.data;
2380 memcpy(trash->area + trash->data,
2381 arg_p[1].data.str.area, max);
2382 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002383 }
2384
2385 /* stop here if we're done with this string */
2386 if (start >= end)
2387 break;
2388
2389 /* We have a special case for matches of length 0 (eg: "x*y*").
2390 * These ones are considered to match in front of a character,
2391 * so we have to copy that character and skip to the next one.
2392 */
2393 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002394 if (trash->data < trash->size)
2395 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002396 pmatch[0].rm_eo++;
2397 }
2398
2399 start += pmatch[0].rm_eo;
2400 flag |= REG_NOTBOL;
2401 }
2402
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002403 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002404 return 1;
2405}
2406
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002407/* This function check an operator entry. It expects a string.
2408 * The string can be an integer or a variable name.
2409 */
2410static int check_operator(struct arg *args, struct sample_conv *conv,
2411 const char *file, int line, char **err)
2412{
2413 const char *str;
2414 const char *end;
2415
2416 /* Try to decode a variable. */
2417 if (vars_check_arg(&args[0], NULL))
2418 return 1;
2419
2420 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002421 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002422 end = str + strlen(str);
2423 args[0].data.sint = read_int64(&str, end);
2424 if (*str != '\0') {
2425 memprintf(err, "expects an integer or a variable name");
2426 return 0;
2427 }
2428 args[0].type = ARGT_SINT;
2429 return 1;
2430}
2431
Willy Tarreau6204cd92016-03-10 16:33:04 +01002432/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002433 * If the arg contain an integer, the integer is returned in the
2434 * sample. If the arg contains a variable descriptor, it returns the
2435 * variable value.
2436 *
2437 * This function returns 0 if an error occurs, otherwise it returns 1.
2438 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002439static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002440{
2441 switch (arg->type) {
2442 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002443 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002444 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002445 return 1;
2446 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002447 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002448 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002449 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002450 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002451 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002452 return 0;
2453 return 1;
2454 default:
2455 return 0;
2456 }
2457}
2458
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002459/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002460 * result.
2461 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002462static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002463{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002465 return 1;
2466}
2467
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002468/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002469 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002470 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002471static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002472{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002473 struct sample tmp;
2474
Willy Tarreau7560dd42016-03-10 16:28:58 +01002475 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002476 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002477 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002478 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002479 return 1;
2480}
2481
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002482/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002483 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002484 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002485static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002486{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002487 struct sample tmp;
2488
Willy Tarreau7560dd42016-03-10 16:28:58 +01002489 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002490 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002491 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002492 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002493 return 1;
2494}
2495
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002496/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002497 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002498 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002499static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002500{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002501 struct sample tmp;
2502
Willy Tarreau7560dd42016-03-10 16:28:58 +01002503 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002504 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002505 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002506 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002507 return 1;
2508}
2509
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002510static inline long long int arith_add(long long int a, long long int b)
2511{
2512 /* Prevent overflow and makes capped calculus.
2513 * We must ensure that the check calculus doesn't
2514 * exceed the signed 64 bits limits.
2515 *
2516 * +----------+----------+
2517 * | a<0 | a>=0 |
2518 * +------+----------+----------+
2519 * | b<0 | MIN-a>b | no check |
2520 * +------+----------+----------+
2521 * | b>=0 | no check | MAX-a<b |
2522 * +------+----------+----------+
2523 */
2524 if ((a ^ b) >= 0) {
2525 /* signs are differents. */
2526 if (a < 0) {
2527 if (LLONG_MIN - a > b)
2528 return LLONG_MIN;
2529 }
2530 if (LLONG_MAX - a < b)
2531 return LLONG_MAX;
2532 }
2533 return a + b;
2534}
2535
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002536/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002537 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002538 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002539static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002540{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002541 struct sample tmp;
2542
Willy Tarreau7560dd42016-03-10 16:28:58 +01002543 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002544 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002545 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002546 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002547 return 1;
2548}
2549
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002550/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002551 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002552 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002553static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002554 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002555{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002556 struct sample tmp;
2557
Willy Tarreau7560dd42016-03-10 16:28:58 +01002558 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002559 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002560 return 0;
2561
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002562 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2563 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2564 * of -LLONG_MIN and correct the result.
2565 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002566 if (tmp.data.u.sint == LLONG_MIN) {
2567 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2568 if (smp->data.u.sint < LLONG_MAX)
2569 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002570 return 1;
2571 }
2572
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002573 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002574 * the second operand.
2575 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002576 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002577 return 1;
2578}
2579
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002580/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002581 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002582 * If the result makes an overflow, then the largest possible quantity is
2583 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002584 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002585static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002586 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002587{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002588 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002589 long long int c;
2590
Willy Tarreau7560dd42016-03-10 16:28:58 +01002591 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002592 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002593 return 0;
2594
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002595 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002596 if (!smp->data.u.sint || !tmp.data.u.sint) {
2597 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002598 return 1;
2599 }
2600
2601 /* The multiply between LLONG_MIN and -1 returns a
2602 * "floting point exception".
2603 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002604 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2605 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002606 return 1;
2607 }
2608
2609 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002610 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002611
2612 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002613 if (smp->data.u.sint != c / tmp.data.u.sint) {
2614 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2615 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002616 return 1;
2617 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002618 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002619 return 1;
2620 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002621 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002622 return 1;
2623}
2624
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002625/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002626 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002627 * If arg_p makes the result overflow, then the largest possible quantity is
2628 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002629 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002630static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002631 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002632{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002633 struct sample tmp;
2634
Willy Tarreau7560dd42016-03-10 16:28:58 +01002635 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002636 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002637 return 0;
2638
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002639 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002640 /* The divide between LLONG_MIN and -1 returns a
2641 * "floting point exception".
2642 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002643 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2644 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002645 return 1;
2646 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002647 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002648 return 1;
2649 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002650 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002651 return 1;
2652}
2653
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002654/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002655 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002656 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002657 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002658static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002659 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002660{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002661 struct sample tmp;
2662
Willy Tarreau7560dd42016-03-10 16:28:58 +01002663 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002664 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002665 return 0;
2666
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002667 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002668 /* The divide between LLONG_MIN and -1 returns a
2669 * "floting point exception".
2670 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002671 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2672 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002673 return 1;
2674 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002675 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002676 return 1;
2677 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002678 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002679 return 1;
2680}
2681
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002682/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002683 * result.
2684 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002685static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002686 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002687{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002688 if (smp->data.u.sint == LLONG_MIN)
2689 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002690 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002691 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002692 return 1;
2693}
2694
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002695/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002696 * false. The output is a BOOL.
2697 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002698static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002699 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002700{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002701 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002702 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002703 return 1;
2704}
2705
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002706/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002707 * truee. The output is a BOOL.
2708 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002709static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002710 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002711{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002712 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002713 smp->data.type = SMP_T_BOOL;
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 odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002718 * The output is a BOOL.
2719 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002720static int sample_conv_arith_odd(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 & 1;
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 true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002729 * The output is a BOOL.
2730 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002731static int sample_conv_arith_even(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 & 1);
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
Willy Tarreau280f42b2018-02-19 15:34:12 +01002739/* appends an optional const string, an optional variable contents and another
2740 * optional const string to an existing string.
2741 */
2742static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2743{
Willy Tarreau83061a82018-07-13 11:56:34 +02002744 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002745 struct sample tmp;
2746 int max;
2747
2748 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002749 trash->data = smp->data.u.str.data;
2750 if (trash->data > trash->size - 1)
2751 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002752
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002753 memcpy(trash->area, smp->data.u.str.area, trash->data);
2754 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002755
2756 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002757 max = arg_p[0].data.str.data;
2758 if (max > trash->size - 1 - trash->data)
2759 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002760
2761 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002762 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2763 trash->data += max;
2764 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002765 }
2766
2767 /* append second string (variable) if it's found and we can turn it
2768 * into a string.
2769 */
2770 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2771 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2772 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2773 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2774
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002775 max = tmp.data.u.str.data;
2776 if (max > trash->size - 1 - trash->data)
2777 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002778
2779 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002780 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2781 max);
2782 trash->data += max;
2783 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002784 }
2785 }
2786
2787 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002788 max = arg_p[2].data.str.data;
2789 if (max > trash->size - 1 - trash->data)
2790 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002791
2792 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002793 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2794 trash->data += max;
2795 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002796 }
2797
2798 smp->data.u.str = *trash;
2799 smp->data.type = SMP_T_STR;
2800 return 1;
2801}
2802
2803/* This function checks the "concat" converter's arguments and extracts the
2804 * variable name and its scope.
2805 */
2806static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2807 const char *file, int line, char **err)
2808{
2809 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002810 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2811 memprintf(err, "failed to register variable name '%s'",
2812 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002813 return 0;
2814 }
2815 return 1;
2816}
2817
Tim Duesterhusca097c12018-04-27 21:18:45 +02002818/* compares string with a variable containing a string. Return value
2819 * is compatible with strcmp(3)'s return value.
2820 */
2821static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2822{
2823 struct sample tmp;
2824 int max, result;
2825
2826 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2827 if (arg_p[0].type != ARGT_VAR)
2828 return 0;
2829 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2830 return 0;
2831 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2832 return 0;
2833
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002834 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2835 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002836 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002837 if (smp->data.u.str.data != tmp.data.u.str.data) {
2838 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002839 result = -1;
2840 }
2841 else {
2842 result = 1;
2843 }
2844 }
2845 }
2846
2847 smp->data.u.sint = result;
2848 smp->data.type = SMP_T_SINT;
2849 return 1;
2850}
2851
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002852#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2853#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2854#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2855
2856/*
2857 * Extract the field value of an input binary sample. Takes a mandatory argument:
2858 * the protocol buffers field identifier (dotted notation) internally represented
2859 * as an array of unsigned integers and its size.
2860 * Return 1 if the field was found, 0 if not.
2861 */
2862static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2863{
2864 unsigned char *pos;
2865 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002866
2867 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002868 grpc_left = smp->data.u.str.data;
2869
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002870 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002871 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002872
2873 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2874
2875 pos += GRPC_MSG_HEADER_SZ;
2876 grpc_left -= GRPC_MSG_HEADER_SZ;
2877
2878 if (grpc_left < left)
2879 return 0;
2880
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002881 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2882 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002883
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002884 grpc_left -= grpc_msg_len;
2885 }
2886
2887 return 0;
2888}
2889
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002890static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2891{
2892 unsigned char *pos;
2893 size_t left;
2894
2895 pos = (unsigned char *)smp->data.u.str.area;
2896 left = smp->data.u.str.data;
2897
2898 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2899}
2900
2901static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2902 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002903{
2904 if (!args[1].type) {
2905 args[1].type = ARGT_SINT;
2906 args[1].data.sint = PBUF_T_BINARY;
2907 }
2908 else {
2909 int pbuf_type;
2910
2911 pbuf_type = protobuf_type(args[1].data.str.area);
2912 if (pbuf_type == -1) {
2913 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2914 return 0;
2915 }
2916
2917 args[1].type = ARGT_SINT;
2918 args[1].data.sint = pbuf_type;
2919 }
2920
2921 return 1;
2922}
2923
Tim Duesterhusca097c12018-04-27 21:18:45 +02002924/* This function checks the "strcmp" converter's arguments and extracts the
2925 * variable name and its scope.
2926 */
2927static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2928 const char *file, int line, char **err)
2929{
2930 /* Try to decode a variable. */
2931 if (vars_check_arg(&args[0], NULL))
2932 return 1;
2933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002934 memprintf(err, "failed to register variable name '%s'",
2935 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002936 return 0;
2937}
2938
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002939/************************************************************************/
2940/* All supported sample fetch functions must be declared here */
2941/************************************************************************/
2942
2943/* force TRUE to be returned at the fetch level */
2944static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002945smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002946{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002947 if (!smp_make_rw(smp))
2948 return 0;
2949
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002950 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002951 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002952 return 1;
2953}
2954
2955/* force FALSE to be returned at the fetch level */
2956static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002957smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002958{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002959 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002960 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002961 return 1;
2962}
2963
2964/* retrieve environment variable $1 as a string */
2965static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002966smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002967{
2968 char *env;
2969
2970 if (!args || args[0].type != ARGT_STR)
2971 return 0;
2972
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002973 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002974 if (!env)
2975 return 0;
2976
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002977 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002978 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002979 smp->data.u.str.area = env;
2980 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002981 return 1;
2982}
2983
Damien Claisseae6f1252019-10-30 15:57:28 +00002984/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
2985 * optional string representing the unit of the result: "s" for seconds, "ms" for
2986 * milliseconds and "us" for microseconds.
2987 * Returns 0 on error and non-zero if OK.
2988 */
2989int smp_check_date_unit(struct arg *args, char **err)
2990{
2991 if (args[1].type == ARGT_STR) {
2992 if (strcmp(args[1].data.str.area, "s") == 0) {
2993 args[1].data.sint = TIME_UNIT_S;
2994 }
2995 else if (strcmp(args[1].data.str.area, "ms") == 0) {
2996 args[1].data.sint = TIME_UNIT_MS;
2997 }
2998 else if (strcmp(args[1].data.str.area, "us") == 0) {
2999 args[1].data.sint = TIME_UNIT_US;
3000 }
3001 else {
3002 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3003 args[1].data.str.area);
3004 return 0;
3005 }
3006 free(args[1].data.str.area);
3007 args[1].data.str.area = NULL;
3008 args[1].type = ARGT_SINT;
3009 }
3010 else if (args[1].type != ARGT_STOP) {
3011 memprintf(err, "Unexpected arg type");
3012 return 0;
3013 }
3014
3015 return 1;
3016}
3017
3018/* retrieve the current local date in epoch time, converts it to milliseconds
3019 * or microseconds if asked to in optional args[1] unit param, and applies an
3020 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003021 */
3022static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003023smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003024{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003025 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003026
Damien Claisseae6f1252019-10-30 15:57:28 +00003027 /* report in milliseconds */
3028 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3029 smp->data.u.sint *= 1000;
3030 smp->data.u.sint += date.tv_usec / 1000;
3031 }
3032 /* report in microseconds */
3033 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3034 smp->data.u.sint *= 1000000;
3035 smp->data.u.sint += date.tv_usec;
3036 }
3037
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003038 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003039 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003040 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003041
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003042 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003043 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3044 return 1;
3045}
3046
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003047/* retrieve the current microsecond part of the date */
3048static int
3049smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3050{
3051 smp->data.u.sint = date.tv_usec;
3052 smp->data.type = SMP_T_SINT;
3053 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3054 return 1;
3055}
3056
3057
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003058/* returns the hostname */
3059static int
3060smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3061{
3062 smp->data.type = SMP_T_STR;
3063 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003064 smp->data.u.str.area = hostname;
3065 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003066 return 1;
3067}
3068
Willy Tarreau0f30d262014-11-24 16:02:05 +01003069/* returns the number of processes */
3070static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003071smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003072{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003073 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003074 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003075 return 1;
3076}
3077
3078/* returns the number of the current process (between 1 and nbproc */
3079static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003080smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003081{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003082 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003083 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003084 return 1;
3085}
3086
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003087/* returns the number of the current thread (between 1 and nbthread */
3088static int
3089smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3090{
3091 smp->data.type = SMP_T_SINT;
3092 smp->data.u.sint = tid;
3093 return 1;
3094}
3095
Willy Tarreau84310e22014-02-14 11:59:04 +01003096/* generate a random 32-bit integer for whatever purpose, with an optional
3097 * range specified in argument.
3098 */
3099static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003100smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003101{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003102 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003103
3104 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003105 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003106 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003107
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003108 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003109 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3110 return 1;
3111}
3112
Willy Tarreau0f30d262014-11-24 16:02:05 +01003113/* returns true if the current process is stopping */
3114static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003115smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003116{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003117 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003118 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003119 return 1;
3120}
3121
Willy Tarreau70fe9442018-11-22 16:07:39 +01003122/* returns the number of calls of the current stream's process_stream() */
3123static int
3124smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3125{
3126 smp->data.type = SMP_T_SINT;
3127 smp->data.u.sint = smp->strm->task->calls;
3128 return 1;
3129}
3130
3131/* returns the average number of nanoseconds spent processing the stream per call */
3132static int
3133smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3134{
3135 smp->data.type = SMP_T_SINT;
3136 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3137 return 1;
3138}
3139
3140/* returns the total number of nanoseconds spent processing the stream */
3141static int
3142smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3143{
3144 smp->data.type = SMP_T_SINT;
3145 smp->data.u.sint = smp->strm->task->cpu_time;
3146 return 1;
3147}
3148
3149/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3150static int
3151smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3152{
3153 smp->data.type = SMP_T_SINT;
3154 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3155 return 1;
3156}
3157
3158/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3159static int
3160smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3161{
3162 smp->data.type = SMP_T_SINT;
3163 smp->data.u.sint = smp->strm->task->lat_time;
3164 return 1;
3165}
3166
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003167static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3168{
3169 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003170 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003171 smp->data.u.str.area = args[0].data.str.area;
3172 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003173 return 1;
3174}
3175
3176static int smp_check_const_bool(struct arg *args, char **err)
3177{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003178 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3179 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003180 args[0].type = ARGT_SINT;
3181 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003182 return 1;
3183 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003184 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3185 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003186 args[0].type = ARGT_SINT;
3187 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003188 return 1;
3189 }
3190 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3191 return 0;
3192}
3193
3194static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3195{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003196 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003197 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003198 return 1;
3199}
3200
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003201static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003202{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003203 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003204 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003205 return 1;
3206}
3207
3208static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3209{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003210 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003211 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003212 return 1;
3213}
3214
3215static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3216{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003217 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003218 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003219 return 1;
3220}
3221
3222static int smp_check_const_bin(struct arg *args, char **err)
3223{
David Carlier64a16ab2016-04-08 10:37:02 +01003224 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003225 int binstrlen;
3226
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003227 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003228 return 0;
3229 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003230 args[0].data.str.area = binstr;
3231 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003232 return 1;
3233}
3234
3235static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3236{
3237 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003238 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003239 smp->data.u.str.area = args[0].data.str.area;
3240 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003241 return 1;
3242}
3243
3244static int smp_check_const_meth(struct arg *args, char **err)
3245{
3246 enum http_meth_t meth;
3247 int i;
3248
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003249 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003250 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003251 args[0].type = ARGT_SINT;
3252 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003253 } else {
3254 /* Check method avalaibility. A methos is a token defined as :
3255 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3256 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3257 * token = 1*tchar
3258 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003259 for (i = 0; i < args[0].data.str.data; i++) {
3260 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003261 memprintf(err, "expects valid method.");
3262 return 0;
3263 }
3264 }
3265 }
3266 return 1;
3267}
3268
3269static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3270{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003271 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003272 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003273 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003274 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003275 smp->data.u.meth.str.area = "";
3276 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003277 } else {
3278 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003279 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003280 smp->data.u.meth.str.area = args[0].data.str.area;
3281 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003282 }
3283 return 1;
3284}
3285
Luca Schimweg8a694b82019-09-10 15:42:52 +02003286// This function checks the "uuid" sample's arguments.
3287// Function won't get called when no parameter is specified (maybe a bug?)
3288static int smp_check_uuid(struct arg *args, char **err)
3289{
3290 if (!args[0].type) {
3291 args[0].type = ARGT_SINT;
3292 args[0].data.sint = 4;
3293 }
3294 else if (args[0].data.sint != 4) {
3295 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3296 return 0;
3297 }
3298
3299 return 1;
3300}
3301
3302// Generate a RFC4122 UUID (default is v4 = fully random)
3303static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3304{
3305 if (args[0].data.sint == 4 || !args[0].type) {
3306 uint32_t rnd[4] = { 0, 0, 0, 0 };
3307 uint64_t last = 0;
3308 int byte = 0;
3309 uint8_t bits = 0;
3310 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3311
3312 while (byte < 4) {
3313 while (bits < 32) {
3314 last |= (uint64_t)random() << bits;
3315 bits += rand_max_bits;
3316 }
3317 rnd[byte++] = last;
3318 last >>= 32u;
3319 bits -= 32;
3320 }
3321
3322 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3323 rnd[0],
3324 rnd[1] & 0xFFFF,
3325 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3326 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3327 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3328 );
3329
3330 smp->data.type = SMP_T_STR;
3331 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3332 smp->data.u.str = trash;
3333 return 1;
3334 }
3335
3336 // more implementations of other uuid formats possible here
3337 return 0;
3338}
3339
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003340/* Note: must not be declared <const> as its list will be overwritten.
3341 * Note: fetches that may return multiple types must be declared as the lowest
3342 * common denominator, the type that can be casted into all other ones. For
3343 * instance IPv4/IPv6 must be declared IPv4.
3344 */
3345static struct sample_fetch_kw_list smp_kws = {ILH, {
3346 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3347 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003348 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003349 { "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 +01003350 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003351 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003352 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3353 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003354 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003355 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003356 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003357 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003358 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003359
3360 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3361 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3362 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3363 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3364 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003365
3366 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3367 { "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 +02003368 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003369 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3370 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3371 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3372 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3373
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003374 { /* END */ },
3375}};
3376
Willy Tarreau0108d902018-11-25 19:14:37 +01003377INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3378
Emeric Brun107ca302010-01-04 16:16:05 +01003379/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003380static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003381 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003382 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003383 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003384 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3385 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003386 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003387 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003388 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003389 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003390 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3391 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003392 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003393 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003394 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3395 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3396 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003397 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3398 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003399 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003400 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003401 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3402 { "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 +01003403 { "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 +02003404 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003405#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003406 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003407#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003408 { "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 +02003409 { "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 +01003410
3411 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003412 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3413 { "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 +01003414
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003415 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3416 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3417 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3418 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3419 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3420 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3421 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3422 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3423 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3424 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3425 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3426 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3427 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3428 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003429
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003430 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003431}};
3432
Willy Tarreau0108d902018-11-25 19:14:37 +01003433INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);