blob: 3c611122404d83ca9f2086f0d939d996108d6bed [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 Tarreaua8b7ecd2020-02-25 09:43:22 +010025#include <common/net_helper.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020027#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020028#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029
Willy Tarreau9fcb9842012-04-20 14:45:49 +020030#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/auth.h>
32#include <proto/log.h>
33#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010034#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020035#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010036#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020037#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020038#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010039
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020040#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010041#include <import/xxhash.h>
42
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043/* sample type names */
44const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020045 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047 [SMP_T_SINT] = "sint",
48 [SMP_T_ADDR] = "addr",
49 [SMP_T_IPV4] = "ipv4",
50 [SMP_T_IPV6] = "ipv6",
51 [SMP_T_STR] = "str",
52 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020053 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010054};
55
Willy Tarreau12785782012-04-27 21:37:17 +020056/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020057static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010058
Willy Tarreau12785782012-04-27 21:37:17 +020059/* list head of all known sample fetch keywords */
60static struct sample_fetch_kw_list sample_fetches = {
61 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010062};
63
Willy Tarreau12785782012-04-27 21:37:17 +020064/* list head of all known sample format conversion keywords */
65static struct sample_conv_kw_list sample_convs = {
66 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010067};
68
Willy Tarreau80aca902013-01-07 15:42:20 +010069const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
70 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
71 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
72 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
73 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
74 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
75 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
76 SMP_VAL_FE_LOG_END),
77
78 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
79 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
80 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
81 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
82 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
83 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
84 SMP_VAL_FE_LOG_END),
85
86 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
87 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
88 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
89 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
90 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
91 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
92 SMP_VAL_FE_LOG_END),
93
94 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
95 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
96 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
97 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
98 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
99 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
100 SMP_VAL_FE_LOG_END),
101
102 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
103 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
104 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
105 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
106 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
107 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
108 SMP_VAL_FE_LOG_END),
109
110 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
111 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
112 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
113 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
114 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
115 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
116 SMP_VAL_FE_LOG_END),
117
118 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
119 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
120 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
121 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
124 SMP_VAL___________),
125
126 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
127 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
128 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
129 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
132 SMP_VAL___________),
133
134 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
135 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
136 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
137 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
138 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
139 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
140 SMP_VAL_FE_LOG_END),
141
142 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
144 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
145 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
148 SMP_VAL___________),
149
150 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
153 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
154 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
155 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
156 SMP_VAL_FE_LOG_END),
157
158 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
162 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
163 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
164 SMP_VAL_FE_LOG_END),
165
166 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
170 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
171 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
172 SMP_VAL_FE_LOG_END),
173
174 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
178 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
179 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
180 SMP_VAL_FE_LOG_END),
181
182 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
186 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
187 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
188 SMP_VAL___________),
189
190 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
194 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
195 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
196 SMP_VAL___________),
197
198 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
202 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
203 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
204 SMP_VAL_FE_LOG_END),
205
206 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
211 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
212 SMP_VAL___________),
213
214 [SMP_SRC_RQFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
220 SMP_VAL_FE_LOG_END),
221
222 [SMP_SRC_RSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
228 SMP_VAL_FE_LOG_END),
229
230 [SMP_SRC_TXFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
236 SMP_VAL_FE_LOG_END),
237
238 [SMP_SRC_SSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ |
244 SMP_VAL_FE_LOG_END),
245};
246
247static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
248 [SMP_SRC_INTRN] = "internal state",
249 [SMP_SRC_LISTN] = "listener",
250 [SMP_SRC_FTEND] = "frontend",
251 [SMP_SRC_L4CLI] = "client address",
252 [SMP_SRC_L5CLI] = "client-side connection",
253 [SMP_SRC_TRACK] = "track counters",
254 [SMP_SRC_L6REQ] = "request buffer",
255 [SMP_SRC_HRQHV] = "HTTP request headers",
256 [SMP_SRC_HRQHP] = "HTTP request",
257 [SMP_SRC_HRQBO] = "HTTP request body",
258 [SMP_SRC_BKEND] = "backend",
259 [SMP_SRC_SERVR] = "server",
260 [SMP_SRC_L4SRV] = "server address",
261 [SMP_SRC_L5SRV] = "server-side connection",
262 [SMP_SRC_L6RES] = "response buffer",
263 [SMP_SRC_HRSHV] = "HTTP response headers",
264 [SMP_SRC_HRSHP] = "HTTP response",
265 [SMP_SRC_HRSBO] = "HTTP response body",
266 [SMP_SRC_RQFIN] = "request buffer statistics",
267 [SMP_SRC_RSFIN] = "response buffer statistics",
268 [SMP_SRC_TXFIN] = "transaction statistics",
269 [SMP_SRC_SSFIN] = "session statistics",
270};
271
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100272static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
273 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
274 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
275 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
276 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
277 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
278 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
279 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
280 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
281 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
282 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
283 [SMP_CKP_BE_SRV_CON] = "server source selection",
284 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
285 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
286 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
287 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
288 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
289 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
290 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
291 [SMP_CKP_FE_LOG_END] = "logs",
292};
293
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100294/* This function returns the type of the data returned by the sample_expr.
295 * It assumes that the <expr> and all of its converters are properly
296 * initialized.
297 */
298inline
299int smp_expr_output_type(struct sample_expr *expr)
300{
301 struct sample_conv_expr *smp_expr;
302
303 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
304 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
305 return smp_expr->conv->out_type;
306 }
307 return expr->fetch->out_type;
308}
309
310
Willy Tarreau80aca902013-01-07 15:42:20 +0100311/* fill the trash with a comma-delimited list of source names for the <use> bit
312 * field which must be composed of a non-null set of SMP_USE_* flags. The return
313 * value is the pointer to the string in the trash buffer.
314 */
315const char *sample_src_names(unsigned int use)
316{
317 int bit;
318
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200319 trash.data = 0;
320 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100321 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
322 if (!(use & ~((1 << bit) - 1)))
323 break; /* no more bits */
324
325 if (!(use & (1 << bit)))
326 continue; /* bit not set */
327
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200328 trash.data += snprintf(trash.area + trash.data,
329 trash.size - trash.data, "%s%s",
330 (use & ((1 << bit) - 1)) ? "," : "",
331 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100332 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200333 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100334}
335
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100336/* return a pointer to the correct sample checkpoint name, or "unknown" when
337 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
338 * if set.
339 */
340const char *sample_ckp_names(unsigned int use)
341{
342 int bit;
343
344 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
345 if (use & (1 << bit))
346 return fetch_ckp_names[bit];
347 return "unknown sample check place, please report this bug";
348}
349
Emeric Brun107ca302010-01-04 16:16:05 +0100350/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100351 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
352 * for next parsing sessions. The fetch keywords capabilities are also computed
353 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100354 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100355void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100356{
Willy Tarreau80aca902013-01-07 15:42:20 +0100357 struct sample_fetch *sf;
358 int bit;
359
360 for (sf = kwl->kw; sf->kw != NULL; sf++) {
361 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
362 if (sf->use & (1 << bit))
363 sf->val |= fetch_cap[bit];
364 }
365 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100366}
367
368/*
Willy Tarreau12785782012-04-27 21:37:17 +0200369 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100370 * parsing sessions.
371 */
Willy Tarreau12785782012-04-27 21:37:17 +0200372void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100373{
Willy Tarreau12785782012-04-27 21:37:17 +0200374 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100375}
376
377/*
Willy Tarreau12785782012-04-27 21:37:17 +0200378 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100379 * string of <len> in buffer <kw>.
380 *
381 */
Willy Tarreau12785782012-04-27 21:37:17 +0200382struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100383{
384 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200385 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100386
Willy Tarreau12785782012-04-27 21:37:17 +0200387 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100388 for (index = 0; kwl->kw[index].kw != NULL; index++) {
389 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
390 kwl->kw[index].kw[len] == '\0')
391 return &kwl->kw[index];
392 }
393 }
394 return NULL;
395}
396
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100398 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100399 * <idx> is the index of the next sample fetch entry. It is used as private
400 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100401 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100402 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100403 * the list is reached.
404 */
405struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
406{
407 struct sample_fetch_kw_list *kwl;
408 struct sample_fetch *base;
409
410 if (!current) {
411 /* Get first kwl entry. */
412 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
413 (*idx) = 0;
414 } else {
415 /* Get kwl corresponding to the curret entry. */
416 base = current + 1 - (*idx);
417 kwl = container_of(base, struct sample_fetch_kw_list, kw);
418 }
419
420 while (1) {
421
422 /* Check if kwl is the last entry. */
423 if (&kwl->list == &sample_fetches.list)
424 return NULL;
425
426 /* idx contain the next keyword. If it is available, return it. */
427 if (kwl->kw[*idx].kw) {
428 (*idx)++;
429 return &kwl->kw[(*idx)-1];
430 }
431
432 /* get next entry in the main list, and return NULL if the end is reached. */
433 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
434
435 /* Set index to 0, ans do one other loop. */
436 (*idx) = 0;
437 }
438}
439
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100440/* This function browses the list of available converters. <current> is
441 * the last used converter. If it is the first call, it must set to NULL.
442 * <idx> is the index of the next converter entry. It is used as private
443 * value. It is useless to initiate it.
444 *
445 * It returns always the next sample_conv entry, and NULL when the end of
446 * the list is reached.
447 */
448struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
449{
450 struct sample_conv_kw_list *kwl;
451 struct sample_conv *base;
452
453 if (!current) {
454 /* Get first kwl entry. */
455 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
456 (*idx) = 0;
457 } else {
458 /* Get kwl corresponding to the curret entry. */
459 base = current + 1 - (*idx);
460 kwl = container_of(base, struct sample_conv_kw_list, kw);
461 }
462
463 while (1) {
464 /* Check if kwl is the last entry. */
465 if (&kwl->list == &sample_convs.list)
466 return NULL;
467
468 /* idx contain the next keyword. If it is available, return it. */
469 if (kwl->kw[*idx].kw) {
470 (*idx)++;
471 return &kwl->kw[(*idx)-1];
472 }
473
474 /* get next entry in the main list, and return NULL if the end is reached. */
475 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
476
477 /* Set index to 0, ans do one other loop. */
478 (*idx) = 0;
479 }
480}
481
Emeric Brun107ca302010-01-04 16:16:05 +0100482/*
Willy Tarreau12785782012-04-27 21:37:17 +0200483 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100484 * string of <len> in buffer <kw>.
485 *
486 */
Willy Tarreau12785782012-04-27 21:37:17 +0200487struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100488{
489 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200490 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100491
Willy Tarreau12785782012-04-27 21:37:17 +0200492 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100493 for (index = 0; kwl->kw[index].kw != NULL; index++) {
494 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
495 kwl->kw[index].kw[len] == '\0')
496 return &kwl->kw[index];
497 }
498 }
499 return NULL;
500}
501
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200503/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100504/******************************************************************/
505
Willy Tarreau342acb42012-04-23 22:03:39 +0200506static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100507{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200508 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200509 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100510 return 1;
511}
512
Willy Tarreau342acb42012-04-23 22:03:39 +0200513static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100514{
Willy Tarreau83061a82018-07-13 11:56:34 +0200515 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100516
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200517 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100518 return 0;
519
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200520 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200521 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200522 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100523 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100524
525 return 1;
526}
527
Willy Tarreau342acb42012-04-23 22:03:39 +0200528static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100529{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200530 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200531 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100532 return 1;
533}
534
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200535static int c_ipv62ip(struct sample *smp)
536{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200537 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200538 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100539 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200540 return 1;
541}
542
Willy Tarreau342acb42012-04-23 22:03:39 +0200543static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100544{
Willy Tarreau83061a82018-07-13 11:56:34 +0200545 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100546
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200547 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100548 return 0;
549
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200550 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200551 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200552 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100553 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100554 return 1;
555}
556
557/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200558static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100559{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200560 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100561}
562*/
563
Willy Tarreau342acb42012-04-23 22:03:39 +0200564static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100565{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200566 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200567 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100568 return 1;
569}
570
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200571static int c_int2ipv6(struct sample *smp)
572{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200573 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
574 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200575 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200576 return 1;
577}
578
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100579static int c_str2addr(struct sample *smp)
580{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200581 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
582 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100583 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200584 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100585 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100586 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100587 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200588 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100589 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100590 return 1;
591}
592
Willy Tarreau342acb42012-04-23 22:03:39 +0200593static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100594{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200595 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100596 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200597 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100598 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100599 return 1;
600}
601
Willy Tarreau342acb42012-04-23 22:03:39 +0200602static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100603{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200604 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100605 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200606 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100607 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100608 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100609}
610
Emeric Brun4b9e8022014-11-03 18:17:10 +0100611/*
612 * The NULL char always enforces the end of string if it is met.
613 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100614 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200615static int c_bin2str(struct sample *smp)
616{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100617 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200618
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200619 for (i = 0; i < smp->data.u.str.data; i++) {
620 if (!smp->data.u.str.area[i]) {
621 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100622 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100623 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200624 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200625 return 1;
626}
627
Willy Tarreau342acb42012-04-23 22:03:39 +0200628static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100629{
Willy Tarreau83061a82018-07-13 11:56:34 +0200630 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100631 char *pos;
632
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200633 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100634 if (!pos)
635 return 0;
636
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200637 trash->size = trash->size - (pos - trash->area);
638 trash->area = pos;
639 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200640 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200641 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100642 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100643 return 1;
644}
645
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800646/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200647 * For strings and binary blocks, it also provides a known allocated size with
648 * a length that is capped to the size, and ensures a trailing zero is always
649 * appended for strings. This is necessary for some operations which may
650 * require to extend the length. It returns 0 if it fails, 1 on success.
651 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100652int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200653{
Willy Tarreau83061a82018-07-13 11:56:34 +0200654 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200655
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200656 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100658 case SMP_T_SINT:
659 case SMP_T_ADDR:
660 case SMP_T_IPV4:
661 case SMP_T_IPV6:
662 /* These type are not const. */
663 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200664
Christopher Fauletec100512017-07-24 15:38:41 +0200665 case SMP_T_METH:
666 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
667 break;
668 /* Fall through */
669
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100670 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200671 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100672 trash->data = smp->data.type == SMP_T_STR ?
673 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200674 if (trash->data > trash->size - 1)
675 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200676
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100677 memcpy(trash->area, smp->data.type == SMP_T_STR ?
678 smp->data.u.str.area : smp->data.u.meth.str.area,
679 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200680 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200681 smp->data.u.str = *trash;
682 break;
683
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100685 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200686 trash->data = smp->data.u.str.data;
687 if (trash->data > trash->size)
688 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200689
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200690 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200691 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100692 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200693
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100694 default:
695 /* Other cases are unexpected. */
696 return 0;
697 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100698
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100699 /* remove const flag */
700 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200701 return 1;
702}
703
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100704int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100705{
706 return 1;
707}
708
Willy Tarreau342acb42012-04-23 22:03:39 +0200709static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100710{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200711 const char *str;
712 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100713
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200714 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100715 return 0;
716
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200717 str = smp->data.u.str.area;
718 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100719
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200720 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200721 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100722 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100723 return 1;
724}
725
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100726static int c_str2meth(struct sample *smp)
727{
728 enum http_meth_t meth;
729 int len;
730
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100732 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200733 len = smp->data.u.str.data;
734 smp->data.u.meth.str.area = smp->data.u.str.area;
735 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100736 }
737 else
738 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200739 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200740 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100741 return 1;
742}
743
744static int c_meth2str(struct sample *smp)
745{
746 int len;
747 enum http_meth_t meth;
748
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200749 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100750 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200751 len = smp->data.u.meth.str.data;
752 smp->data.u.str.area = smp->data.u.meth.str.area;
753 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200754 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100757 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200758 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200759 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200760 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100761 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200762 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100763 }
764 else {
765 /* Unknown method */
766 return 0;
767 }
768 return 1;
769}
770
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200771static int c_addr2bin(struct sample *smp)
772{
Willy Tarreau83061a82018-07-13 11:56:34 +0200773 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200774
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200775 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200776 chk->data = 4;
777 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200778 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200779 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200780 chk->data = 16;
781 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200782 }
783 else
784 return 0;
785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200786 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200787 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200788 return 1;
789}
790
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200791static int c_int2bin(struct sample *smp)
792{
Willy Tarreau83061a82018-07-13 11:56:34 +0200793 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200794
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200795 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
796 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200797
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200798 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200799 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200800 return 1;
801}
802
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200803
Emeric Brun107ca302010-01-04 16:16:05 +0100804/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200805/* Sample casts matrix: */
806/* sample_casts[from type][to type] */
807/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100808/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100809
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100810sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200811/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
812/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
813/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200814/* 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 +0200815/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
816/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200817/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200818/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
819/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
820/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200821};
Emeric Brun107ca302010-01-04 16:16:05 +0100822
Emeric Brun107ca302010-01-04 16:16:05 +0100823/*
Willy Tarreau12785782012-04-27 21:37:17 +0200824 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100825 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200826 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200827 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100828 * If <endptr> is non-nul, it will be set to the first unparsed character
829 * (which may be the final '\0') on success. If it is nul, the expression
830 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100831 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100832struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
Emeric Brun107ca302010-01-04 16:16:05 +0100833{
Willy Tarreau833cc792013-07-24 15:34:19 +0200834 const char *begw; /* beginning of word */
835 const char *endw; /* end of word */
836 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200837 struct sample_expr *expr;
838 struct sample_fetch *fetch;
839 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100840 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200841 char *fkw = NULL;
842 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100843 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100844
Willy Tarreau833cc792013-07-24 15:34:19 +0200845 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100846 for (endw = begw; is_idchar(*endw); endw++)
847 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200848
849 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100850 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100851 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200852 }
Emeric Brun107ca302010-01-04 16:16:05 +0100853
Willy Tarreau833cc792013-07-24 15:34:19 +0200854 /* keep a copy of the current fetch keyword for error reporting */
855 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 fetch = find_sample_fetch(begw, endw - begw);
858 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100859 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100860 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200861 }
Emeric Brun107ca302010-01-04 16:16:05 +0100862
Willy Tarreau833cc792013-07-24 15:34:19 +0200863 /* At this point, we have :
864 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100865 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200866 */
867
868 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100869 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100870 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200871 }
Emeric Brun107ca302010-01-04 16:16:05 +0100872 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200873
Vincent Bernat02779b62016-04-03 13:48:43 +0200874 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200875 if (!expr)
876 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100877
878 LIST_INIT(&(expr->conv_exprs));
879 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200880 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100881
Willy Tarreau689a1df2013-12-13 00:40:11 +0100882 /* Note that we call the argument parser even with an empty string,
883 * this allows it to automatically create entries for mandatory
884 * implicit arguments (eg: local proxy name).
885 */
886 al->kw = expr->fetch->kw;
887 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100888 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 +0100889 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
890 goto out_error;
891 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200892
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100893 /* now endt is our first char not part of the arg list, typically the
894 * comma after the sample fetch name or after the closing parenthesis,
895 * or the NUL char.
896 */
897
Willy Tarreau689a1df2013-12-13 00:40:11 +0100898 if (!expr->arg_p) {
899 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200900 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100901 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
902 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200903 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100904 }
905
Willy Tarreau833cc792013-07-24 15:34:19 +0200906 /* Now process the converters if any. We have two supported syntaxes
907 * for the converters, which can be combined :
908 * - comma-delimited list of converters just after the keyword and args ;
909 * - one converter per keyword
910 * The combination allows to have each keyword being a comma-delimited
911 * series of converters.
912 *
913 * We want to process the former first, then the latter. For this we start
914 * from the beginning of the supposed place in the exiting conv chain, which
915 * starts at the last comma (endt).
916 */
917
918 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200919 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100920 int err_arg;
921 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100922
Willy Tarreau833cc792013-07-24 15:34:19 +0200923 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100924 if (endptr) {
925 /* end found, let's stop here */
926 break;
927 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100929 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200930 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100931 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100932 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200933 }
Emeric Brun107ca302010-01-04 16:16:05 +0100934
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100935 /* FIXME: how long should we support such idiocies ? Maybe we
936 * should already warn ?
937 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200938 while (*endt == ',') /* then trailing commas */
939 endt++;
940
Willy Tarreau97108e02016-11-25 07:33:24 +0100941 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200942
943 if (!*begw) {
944 /* none ? skip to next string */
945 (*idx)++;
946 begw = str[*idx];
947 if (!begw || !*begw)
948 break;
949 }
950
Willy Tarreaued2c6622020-02-14 18:27:10 +0100951 for (endw = begw; is_idchar(*endw); endw++)
952 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200953
954 free(ckw);
955 ckw = my_strndup(begw, endw - begw);
956
957 conv = find_sample_conv(begw, endw - begw);
958 if (!conv) {
959 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100960 if (begw == str[*idx]) {
961 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200962 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100963 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100964 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200965 goto out_error;
966 }
Emeric Brun107ca302010-01-04 16:16:05 +0100967
Willy Tarreau833cc792013-07-24 15:34:19 +0200968 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100969 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100970 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200971 }
Emeric Brun107ca302010-01-04 16:16:05 +0100972
973 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200974 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100975 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100976 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200977 }
Emeric Brun107ca302010-01-04 16:16:05 +0100978
979 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200980 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200981 if (!conv_expr)
982 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100983
984 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
985 conv_expr->conv = conv;
986
Willy Tarreau46dfd782019-12-17 10:25:29 +0100987 al->kw = expr->fetch->kw;
988 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100989 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 +0100990 if (argcnt < 0) {
991 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
992 goto out_error;
993 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100994
Willy Tarreau46dfd782019-12-17 10:25:29 +0100995 if (argcnt && !conv->arg_mask) {
996 memprintf(err_msg, "converter '%s' does not support any args", ckw);
997 goto out_error;
998 }
Willy Tarreau21d68a62012-04-20 15:52:36 +0200999
Willy Tarreau46dfd782019-12-17 10:25:29 +01001000 if (!conv_expr->arg_p)
1001 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001002
Willy Tarreau46dfd782019-12-17 10:25:29 +01001003 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1004 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001005 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001006 }
1007 }
Emeric Brun485479d2010-09-23 18:02:19 +02001008
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001009 if (endptr) {
1010 /* end found, let's stop here */
1011 *endptr = (char *)endt;
1012 }
1013
Willy Tarreau833cc792013-07-24 15:34:19 +02001014 out:
1015 free(fkw);
1016 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001017 return expr;
1018
1019out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001020 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001021 expr = NULL;
1022 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001023}
1024
1025/*
Willy Tarreau12785782012-04-27 21:37:17 +02001026 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001027 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001028 * Returns a pointer on a typed sample structure containing the result or NULL if
1029 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001030 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001032 *
1033 * Note: the fetch functions are required to properly set the return type. The
1034 * conversion functions must do so too. However the cast functions do not need
1035 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001036 *
1037 * The caller may indicate in <opt> if it considers the result final or not.
1038 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1039 * if the result is stable or not, according to the following table :
1040 *
1041 * return MAY_CHANGE FINAL Meaning for the sample
1042 * NULL 0 * Not present and will never be (eg: header)
1043 * NULL 1 0 Not present yet, could change (eg: POST param)
1044 * NULL 1 1 Not present yet, will not change anymore
1045 * smp 0 * Present and will not change (eg: header)
1046 * smp 1 0 Present, may change (eg: request length)
1047 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001048 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001049struct sample *sample_process(struct proxy *px, struct session *sess,
1050 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001051 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001052{
Willy Tarreau12785782012-04-27 21:37:17 +02001053 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001054
Willy Tarreau18387e22013-07-25 12:02:38 +02001055 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001056 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001057 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001058 }
Emeric Brun107ca302010-01-04 16:16:05 +01001059
Willy Tarreau1777ea62016-03-10 16:15:46 +01001060 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001061 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001062 return NULL;
1063
Emeric Brun107ca302010-01-04 16:16:05 +01001064 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001065 /* we want to ensure that p->type can be casted into
1066 * conv_expr->conv->in_type. We have 3 possibilities :
1067 * - NULL => not castable.
1068 * - c_none => nothing to do (let's optimize it)
1069 * - other => apply cast and prepare to fail
1070 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001071 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001072 return NULL;
1073
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001074 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1075 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001076 return NULL;
1077
Willy Tarreau12e50112012-04-25 17:21:49 +02001078 /* OK cast succeeded */
1079
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001080 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001081 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001082 }
1083 return p;
1084}
1085
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001086/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001087 * Resolve all remaining arguments in proxy <p>. Returns the number of
1088 * errors or 0 if everything is fine.
1089 */
1090int smp_resolve_args(struct proxy *p)
1091{
1092 struct arg_list *cur, *bak;
1093 const char *ctx, *where;
1094 const char *conv_ctx, *conv_pre, *conv_pos;
1095 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001096 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001097 struct arg *arg;
1098 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001099 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001100
1101 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1102 struct proxy *px;
1103 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001104 struct stktable *t;
1105 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001106 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001107
1108 arg = cur->arg;
1109
1110 /* prepare output messages */
1111 conv_pre = conv_pos = conv_ctx = "";
1112 if (cur->conv) {
1113 conv_ctx = cur->conv;
1114 conv_pre = "conversion keyword '";
1115 conv_pos = "' for ";
1116 }
1117
1118 where = "in";
1119 ctx = "sample fetch keyword";
1120 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001121 case ARGC_STK: where = "in stick rule in"; break;
1122 case ARGC_TRK: where = "in tracking rule in"; break;
1123 case ARGC_LOG: where = "in log-format string in"; break;
1124 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1125 case ARGC_HRQ: where = "in http-request header format string in"; break;
1126 case ARGC_HRS: where = "in http-response header format string in"; break;
1127 case ARGC_UIF: where = "in unique-id-format string in"; break;
1128 case ARGC_RDR: where = "in redirect format string in"; break;
1129 case ARGC_CAP: where = "in capture rule in"; break;
1130 case ARGC_ACL: ctx = "ACL keyword"; break;
1131 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001132 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001133 }
1134
1135 /* set a few default settings */
1136 px = p;
1137 pname = p->id;
1138
1139 switch (arg->type) {
1140 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001141 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001142 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1143 cur->file, cur->line,
1144 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001145 cfgerr++;
1146 continue;
1147 }
1148
1149 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001150 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001151
1152 if (sname) {
1153 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001154 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001155
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001156 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001157 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001158 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1159 cur->file, cur->line, pname,
1160 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001161 cfgerr++;
1162 break;
1163 }
1164 }
1165 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001166 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001167
1168 srv = findserver(px, sname);
1169 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001170 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",
1171 cur->file, cur->line, sname, pname,
1172 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001173 cfgerr++;
1174 break;
1175 }
1176
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001177 free(arg->data.str.area);
1178 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001179 arg->unresolved = 0;
1180 arg->data.srv = srv;
1181 break;
1182
1183 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001184 if (arg->data.str.data) {
1185 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001186 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001187 }
1188
1189 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001190 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1191 cur->file, cur->line, pname,
1192 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001193 cfgerr++;
1194 break;
1195 }
1196
1197 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001198 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",
1199 cur->file, cur->line, pname,
1200 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001201 cfgerr++;
1202 break;
1203 }
1204
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001205 free(arg->data.str.area);
1206 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001207 arg->unresolved = 0;
1208 arg->data.prx = px;
1209 break;
1210
1211 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001212 if (arg->data.str.data) {
1213 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001214 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001215 }
1216
1217 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001218 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1219 cur->file, cur->line, pname,
1220 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001221 cfgerr++;
1222 break;
1223 }
1224
1225 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001226 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",
1227 cur->file, cur->line, pname,
1228 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001229 cfgerr++;
1230 break;
1231 }
1232
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001233 free(arg->data.str.area);
1234 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001235 arg->unresolved = 0;
1236 arg->data.prx = px;
1237 break;
1238
1239 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001240 if (arg->data.str.data)
1241 stktname = arg->data.str.area;
1242 else
1243 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001244
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001245 t = stktable_find_by_name(stktname);
1246 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001247 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 +01001248 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001249 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001250 cfgerr++;
1251 break;
1252 }
1253
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001254 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001255 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 +01001256 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001258 cfgerr++;
1259 break;
1260 }
1261
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001262 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001263 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 +01001264 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001265 cfgerr++;
1266 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001267 }
1268
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001269 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001270 p->next_stkt_ref = t->proxies_list;
1271 t->proxies_list = p;
1272 }
1273
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001274 free(arg->data.str.area);
1275 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001276 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001277 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001278 break;
1279
1280 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001281 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001282 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1283 cur->file, cur->line,
1284 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
1289 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001290 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001291 ul = p->uri_auth->userlist;
1292 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001293 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001294
1295 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001296 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 +02001297 cur->file, cur->line,
1298 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001300 cfgerr++;
1301 break;
1302 }
1303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001304 free(arg->data.str.area);
1305 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001306 arg->unresolved = 0;
1307 arg->data.usr = ul;
1308 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001309
1310 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001311 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001312 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1313 cur->file, cur->line,
1314 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001315 cfgerr++;
1316 continue;
1317 }
1318
Willy Tarreau46947782015-01-19 19:00:58 +01001319 rflags = 0;
1320 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1321 err = NULL;
1322
Dragan Dosen26743032019-04-30 15:54:36 +02001323 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001324 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1325 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001326 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001327 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001328 cfgerr++;
1329 continue;
1330 }
1331
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001332 free(arg->data.str.area);
1333 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001334 arg->unresolved = 0;
1335 arg->data.reg = reg;
1336 break;
1337
1338
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001339 }
1340
1341 LIST_DEL(&cur->list);
1342 free(cur);
1343 } /* end of args processing */
1344
1345 return cfgerr;
1346}
1347
1348/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001349 * Process a fetch + format conversion as defined by the sample expression
1350 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001351 * not explicitly set to <smp_type>, but shall be compatible with it as
1352 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1353 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001354 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1355 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1356 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1357 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001358 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1359 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1360 * still hope to get it after waiting longer, and is not converted to string.
1361 * The possible output combinations are the following :
1362 *
1363 * return MAY_CHANGE FINAL Meaning for the sample
1364 * NULL * * Not present and will never be (eg: header)
1365 * smp 0 * Final value converted (eg: header)
1366 * smp 1 0 Not present yet, may appear later (eg: header)
1367 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001368 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001369struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001370 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001371 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001372{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001373 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001374
Willy Tarreau6c616e02014-06-25 16:56:41 +02001375 memset(smp, 0, sizeof(*smp));
1376
Willy Tarreau192252e2015-04-04 01:47:55 +02001377 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001378 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1379 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001380 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001381 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001383 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384 return NULL;
1385
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001386 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001387 return NULL;
1388
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001389 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390 return smp;
1391}
1392
Christopher Faulet476e5d02016-10-26 11:34:47 +02001393static void release_sample_arg(struct arg *p)
1394{
1395 struct arg *p_back = p;
1396
1397 if (!p)
1398 return;
1399
1400 while (p->type != ARGT_STOP) {
1401 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001402 free(p->data.str.area);
1403 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001404 p->unresolved = 0;
1405 }
1406 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001407 regex_free(p->data.reg);
1408 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001409 }
1410 p++;
1411 }
1412
1413 if (p_back != empty_arg_list)
1414 free(p_back);
1415}
1416
1417void release_sample_expr(struct sample_expr *expr)
1418{
1419 struct sample_conv_expr *conv_expr, *conv_exprb;
1420
1421 if (!expr)
1422 return;
1423
1424 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1425 release_sample_arg(conv_expr->arg_p);
1426 release_sample_arg(expr->arg_p);
1427 free(expr);
1428}
1429
Emeric Brun107ca302010-01-04 16:16:05 +01001430/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001431/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001432/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001433/*****************************************************************/
1434
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001435static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1436{
1437 int i;
1438 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001439 struct buffer *buf;
1440 struct sink *sink;
1441 struct ist line;
1442 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001443
Willy Tarreau0851fd52019-12-17 10:07:25 +01001444 buf = alloc_trash_chunk();
1445 if (!buf)
1446 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001447
Willy Tarreau0851fd52019-12-17 10:07:25 +01001448 sink = (struct sink *)arg_p[1].data.str.area;
1449 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001450
Willy Tarreau0851fd52019-12-17 10:07:25 +01001451 pfx = arg_p[0].data.str.area;
1452 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001453
Willy Tarreau0851fd52019-12-17 10:07:25 +01001454 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1455 if (!sample_casts[smp->data.type][SMP_T_STR])
1456 goto nocast;
1457
1458 /* Copy sample fetch. This puts the sample as const, the
1459 * cast will copy data if a transformation is required.
1460 */
1461 memcpy(&tmp, smp, sizeof(struct sample));
1462 tmp.flags = SMP_F_CONST;
1463
1464 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1465 goto nocast;
1466
1467 /* Display the displayable chars*. */
1468 b_putchr(buf, '<');
1469 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001470 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001471 b_putchr(buf, tmp.data.u.str.area[i]);
1472 else
1473 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001474 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001475 b_putchr(buf, '>');
1476
1477 done:
1478 line = ist2(buf->area, buf->data);
1479 sink_write(sink, &line, 1);
1480 end:
1481 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001482 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001483 nocast:
1484 chunk_appendf(buf, "(undisplayable)");
1485 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001486}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001487
1488// This function checks the "debug" converter's arguments.
1489static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1490 const char *file, int line, char **err)
1491{
1492 const char *name = "buf0";
1493 struct sink *sink = NULL;
1494
1495 if (args[0].type != ARGT_STR) {
1496 /* optional prefix */
1497 args[0].data.str.area = "";
1498 args[0].data.str.data = 0;
1499 }
1500
1501 if (args[1].type == ARGT_STR)
1502 name = args[1].data.str.area;
1503
1504 sink = sink_find(name);
1505 if (!sink) {
1506 memprintf(err, "No such sink '%s'", name);
1507 return 0;
1508 }
1509
1510 args[1].data.str.area = (char *)sink;
1511 args[1].data.str.data = 0; // that's not a string anymore
1512 return 1;
1513}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001514
Holger Just1bfc24b2017-05-06 00:56:53 +02001515static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1516{
Willy Tarreau83061a82018-07-13 11:56:34 +02001517 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001518 int bin_len;
1519
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001520 trash->data = 0;
1521 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1522 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001523 if (bin_len < 0)
1524 return 0;
1525
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001526 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001527 smp->data.u.str = *trash;
1528 smp->data.type = SMP_T_BIN;
1529 smp->flags &= ~SMP_F_CONST;
1530 return 1;
1531}
1532
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001533static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001534{
Willy Tarreau83061a82018-07-13 11:56:34 +02001535 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001536 int b64_len;
1537
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001538 trash->data = 0;
1539 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1540 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001541 if (b64_len < 0)
1542 return 0;
1543
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001544 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001545 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001546 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001547 smp->flags &= ~SMP_F_CONST;
1548 return 1;
1549}
1550
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001551static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1552{
1553 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001554 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001555
1556 memset(&ctx, 0, sizeof(ctx));
1557
1558 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001559 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1560 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001561
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001562 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001563 smp->data.u.str = *trash;
1564 smp->data.type = SMP_T_BIN;
1565 smp->flags &= ~SMP_F_CONST;
1566 return 1;
1567}
1568
Tim Duesterhusd4376302019-06-17 12:41:44 +02001569#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001570static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1571 const char *file, int line, char **err)
1572{
1573 if (args[0].type == ARGT_STOP)
1574 return 1;
1575 if (args[0].type != ARGT_SINT) {
1576 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1577 return 0;
1578 }
1579
1580 switch (args[0].data.sint) {
1581 case 224:
1582 case 256:
1583 case 384:
1584 case 512:
1585 /* this is okay */
1586 return 1;
1587 default:
1588 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1589 return 0;
1590 }
1591}
1592
Tim Duesterhusd4376302019-06-17 12:41:44 +02001593static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1594{
1595 struct buffer *trash = get_trash_chunk();
1596 int bits = 256;
1597 if (arg_p && arg_p->data.sint)
1598 bits = arg_p->data.sint;
1599
1600 switch (bits) {
1601 case 224: {
1602 SHA256_CTX ctx;
1603
1604 memset(&ctx, 0, sizeof(ctx));
1605
1606 SHA224_Init(&ctx);
1607 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1608 SHA224_Final((unsigned char *) trash->area, &ctx);
1609 trash->data = SHA224_DIGEST_LENGTH;
1610 break;
1611 }
1612 case 256: {
1613 SHA256_CTX ctx;
1614
1615 memset(&ctx, 0, sizeof(ctx));
1616
1617 SHA256_Init(&ctx);
1618 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1619 SHA256_Final((unsigned char *) trash->area, &ctx);
1620 trash->data = SHA256_DIGEST_LENGTH;
1621 break;
1622 }
1623 case 384: {
1624 SHA512_CTX ctx;
1625
1626 memset(&ctx, 0, sizeof(ctx));
1627
1628 SHA384_Init(&ctx);
1629 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1630 SHA384_Final((unsigned char *) trash->area, &ctx);
1631 trash->data = SHA384_DIGEST_LENGTH;
1632 break;
1633 }
1634 case 512: {
1635 SHA512_CTX ctx;
1636
1637 memset(&ctx, 0, sizeof(ctx));
1638
1639 SHA512_Init(&ctx);
1640 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1641 SHA512_Final((unsigned char *) trash->area, &ctx);
1642 trash->data = SHA512_DIGEST_LENGTH;
1643 break;
1644 }
1645 default:
1646 return 0;
1647 }
1648
1649 smp->data.u.str = *trash;
1650 smp->data.type = SMP_T_BIN;
1651 smp->flags &= ~SMP_F_CONST;
1652 return 1;
1653}
1654#endif
1655
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001656static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001657{
Willy Tarreau83061a82018-07-13 11:56:34 +02001658 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001659 unsigned char c;
1660 int ptr = 0;
1661
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001662 trash->data = 0;
1663 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1664 c = smp->data.u.str.area[ptr++];
1665 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1666 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001667 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001668 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001669 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001670 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001671 return 1;
1672}
1673
Dragan Dosen3f957b22017-10-24 09:27:34 +02001674static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1675{
1676 long long int n = 0;
1677 int i, c;
1678
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001679 for (i = 0; i < smp->data.u.str.data; i++) {
1680 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001681 return 0;
1682 n = (n << 4) + c;
1683 }
1684
1685 smp->data.u.sint = n;
1686 smp->data.type = SMP_T_SINT;
1687 smp->flags &= ~SMP_F_CONST;
1688 return 1;
1689}
1690
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001691/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001692static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001693{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001694 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1695 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001696 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001697 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001698 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001699 return 1;
1700}
1701
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001702static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001703{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001704 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001705 smp->data.u.sint = i;
1706 smp->data.type = SMP_T_SINT;
1707 return 1;
1708}
1709
1710
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001711static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001712{
1713 int i;
1714
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001715 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001716 return 0;
1717
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001718 for (i = 0; i < smp->data.u.str.data; i++) {
1719 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1720 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001721 }
1722 return 1;
1723}
1724
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001725static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001726{
1727 int i;
1728
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001729 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001730 return 0;
1731
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001732 for (i = 0; i < smp->data.u.str.data; i++) {
1733 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1734 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001735 }
1736 return 1;
1737}
1738
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001739/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1740static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001741{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001742 /* Attempt to convert to IPv4 to apply the correct mask. */
1743 c_ipv62ip(smp);
1744
1745 if (smp->data.type == SMP_T_IPV4) {
1746 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1747 smp->data.type = SMP_T_IPV4;
1748 }
1749 else if (smp->data.type == SMP_T_IPV6) {
1750 /* IPv6 cannot be converted without an IPv6 mask. */
1751 if (args[1].type != ARGT_IPV6)
1752 return 0;
1753
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001754 write_u64(&smp->data.u.ipv6.s6_addr[0],
1755 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1756 write_u64(&smp->data.u.ipv6.s6_addr[8],
1757 read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8]));
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001758 smp->data.type = SMP_T_IPV6;
1759 }
1760
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001761 return 1;
1762}
1763
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001764/* takes an UINT value on input supposed to represent the time since EPOCH,
1765 * adds an optional offset found in args[1] and emits a string representing
1766 * the local time in the format specified in args[1] using strftime().
1767 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001768static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001769{
Willy Tarreau83061a82018-07-13 11:56:34 +02001770 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001771 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001772 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001773 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001774
1775 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001776 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001777 curr_date += args[1].data.sint;
1778
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001779 tm = localtime(&curr_date);
1780 if (!tm)
1781 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001782 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001783 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1784 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001785 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001786 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001787 return 1;
1788}
1789
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001790/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001791static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001792{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001793 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1794 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001795 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001796 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001797 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001798 return 1;
1799}
1800
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001801/* takes an UINT value on input supposed to represent the time since EPOCH,
1802 * adds an optional offset found in args[1] and emits a string representing
1803 * the UTC date in the format specified in args[1] using strftime().
1804 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001805static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001806{
Willy Tarreau83061a82018-07-13 11:56:34 +02001807 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001808 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001809 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001810 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001811
1812 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001813 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001814 curr_date += args[1].data.sint;
1815
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001816 tm = gmtime(&curr_date);
1817 if (!tm)
1818 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001819 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001820 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1821 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001822 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001823 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001824 return 1;
1825}
1826
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001827/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001828static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001829{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001830 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1831 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001832 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001833 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001834 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001835 return 1;
1836}
1837
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001838/* hashes the binary input into a 32-bit unsigned int using xxh.
1839 * The seed of the hash defaults to 0 but can be changd in argument 1.
1840 */
1841static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1842{
1843 unsigned int seed;
1844
1845 if (arg_p && arg_p->data.sint)
1846 seed = arg_p->data.sint;
1847 else
1848 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001849 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1850 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001851 smp->data.type = SMP_T_SINT;
1852 return 1;
1853}
1854
1855/* hashes the binary input into a 64-bit unsigned int using xxh.
1856 * In fact, the function returns a 64 bit unsigned, but the sample
1857 * storage of haproxy only proposes 64-bits signed, so the value is
1858 * cast as signed. This cast doesn't impact the hash repartition.
1859 * The seed of the hash defaults to 0 but can be changd in argument 1.
1860 */
1861static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1862{
1863 unsigned long long int seed;
1864
1865 if (arg_p && arg_p->data.sint)
1866 seed = (unsigned long long int)arg_p->data.sint;
1867 else
1868 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001869 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1870 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001871 smp->data.type = SMP_T_SINT;
1872 return 1;
1873}
1874
Willy Tarreau80599772015-01-20 19:35:24 +01001875/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001876static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001877{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001878 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1879 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001880 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001881 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001882 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001883 return 1;
1884}
1885
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001886/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1887static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1888{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001889 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1890 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001891 if (arg_p && arg_p->data.sint)
1892 smp->data.u.sint = full_hash(smp->data.u.sint);
1893 smp->data.type = SMP_T_SINT;
1894 return 1;
1895}
1896
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001897/* This function escape special json characters. The returned string can be
1898 * safely set between two '"' and used as json string. The json string is
1899 * defined like this:
1900 *
1901 * any Unicode character except '"' or '\' or control character
1902 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1903 *
1904 * The enum input_type contain all the allowed mode for decoding the input
1905 * string.
1906 */
1907enum input_type {
1908 IT_ASCII = 0,
1909 IT_UTF8,
1910 IT_UTF8S,
1911 IT_UTF8P,
1912 IT_UTF8PS,
1913};
1914static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1915 const char *file, int line, char **err)
1916{
1917 if (!arg) {
1918 memprintf(err, "Unexpected empty arg list");
1919 return 0;
1920 }
1921
1922 if (arg->type != ARGT_STR) {
1923 memprintf(err, "Unexpected arg type");
1924 return 0;
1925 }
1926
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001927 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001928 arg->type = ARGT_SINT;
1929 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001930 return 1;
1931 }
1932
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001933 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001934 arg->type = ARGT_SINT;
1935 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001936 return 1;
1937 }
1938
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001939 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001940 arg->type = ARGT_SINT;
1941 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001942 return 1;
1943 }
1944
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001945 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001946 arg->type = ARGT_SINT;
1947 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001948 return 1;
1949 }
1950
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001951 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001952 arg->type = ARGT_SINT;
1953 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001954 return 1;
1955 }
1956
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001957 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001958 arg->type = ARGT_SINT;
1959 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001960 return 1;
1961 }
1962
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001963 memprintf(err, "Unexpected input code type. "
1964 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001965 return 0;
1966}
1967
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001968static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001969{
Willy Tarreau83061a82018-07-13 11:56:34 +02001970 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001971 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1972 const char *str;
1973 int len;
1974 enum input_type input_type = IT_ASCII;
1975 unsigned int c;
1976 unsigned int ret;
1977 char *p;
1978
1979 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001980 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001981
1982 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001983 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001984
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001985 p = smp->data.u.str.area;
1986 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001987
1988 if (input_type == IT_ASCII) {
1989 /* Read input as ASCII. */
1990 c = *(unsigned char *)p;
1991 p++;
1992 }
1993 else {
1994 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001995 ret = utf8_next(p,
1996 smp->data.u.str.data - ( p - smp->data.u.str.area),
1997 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001998 p += utf8_return_length(ret);
1999
2000 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2001 return 0;
2002 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2003 continue;
2004 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2005 return 0;
2006 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2007 continue;
2008
2009 /* Check too big values. */
2010 if ((unsigned int)c > 0xffff) {
2011 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2012 return 0;
2013 continue;
2014 }
2015 }
2016
2017 /* Convert character. */
2018 if (c == '"') {
2019 len = 2;
2020 str = "\\\"";
2021 }
2022 else if (c == '\\') {
2023 len = 2;
2024 str = "\\\\";
2025 }
2026 else if (c == '/') {
2027 len = 2;
2028 str = "\\/";
2029 }
2030 else if (c == '\b') {
2031 len = 2;
2032 str = "\\b";
2033 }
2034 else if (c == '\f') {
2035 len = 2;
2036 str = "\\f";
2037 }
2038 else if (c == '\r') {
2039 len = 2;
2040 str = "\\r";
2041 }
2042 else if (c == '\n') {
2043 len = 2;
2044 str = "\\n";
2045 }
2046 else if (c == '\t') {
2047 len = 2;
2048 str = "\\t";
2049 }
Willy Tarreau90807112020-02-25 08:16:33 +01002050 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002051 /* isprint generate a segfault if c is too big. The man says that
2052 * c must have the value of an unsigned char or EOF.
2053 */
2054 len = 6;
2055 _str[0] = '\\';
2056 _str[1] = 'u';
2057 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2058 str = _str;
2059 }
2060 else {
2061 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002062 _str[0] = c;
2063 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002064 }
2065
2066 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002067 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002068 return 0;
2069
2070 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002071 memcpy(temp->area + temp->data, str, len);
2072 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002073 }
2074
2075 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002076 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002077 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002078
2079 return 1;
2080}
2081
Emeric Brun54c4ac82014-11-03 15:32:43 +01002082/* This sample function is designed to extract some bytes from an input buffer.
2083 * First arg is the offset.
2084 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002085static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002086{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002087 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2088 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002089 return 1;
2090 }
2091
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002092 if (smp->data.u.str.size)
2093 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002094 smp->data.u.str.data -= arg_p[0].data.sint;
2095 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002096
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002097 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2098 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002099
2100 return 1;
2101}
2102
Emeric Brunf399b0d2014-11-03 17:07:03 +01002103static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2104 const char *file, int line, char **err)
2105{
2106 struct arg *arg = args;
2107
2108 if (!arg) {
2109 memprintf(err, "Unexpected empty arg list");
2110 return 0;
2111 }
2112
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002113 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002114 memprintf(err, "Unexpected arg type");
2115 return 0;
2116 }
2117
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002118 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002119 memprintf(err, "Unexpected value 0 for index");
2120 return 0;
2121 }
2122
2123 arg++;
2124
2125 if (arg->type != ARGT_STR) {
2126 memprintf(err, "Unexpected arg type");
2127 return 0;
2128 }
2129
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002130 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002131 memprintf(err, "Empty separators list");
2132 return 0;
2133 }
2134
2135 return 1;
2136}
2137
2138/* This sample function is designed to a return selected part of a string (field).
2139 * First arg is the index of the field (start at 1)
2140 * Second arg is a char list of separators (type string)
2141 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002142static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002143{
Marcin Deranek9631a282018-04-16 14:30:46 +02002144 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002145 char *start, *end;
2146 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002147 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002148
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002149 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002150 return 0;
2151
Marcin Deranek9631a282018-04-16 14:30:46 +02002152 if (arg_p[0].data.sint < 0) {
2153 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002154 end = start = smp->data.u.str.area + smp->data.u.str.data;
2155 while (start > smp->data.u.str.area) {
2156 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2157 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002158 if (field == arg_p[0].data.sint) {
2159 if (count == 1)
2160 goto found;
2161 else if (count > 1)
2162 count--;
2163 } else {
2164 end = start-1;
2165 field--;
2166 }
2167 break;
2168 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002169 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002170 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002171 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002172 } else {
2173 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 end = start = smp->data.u.str.area;
2175 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2176 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2177 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002178 if (field == arg_p[0].data.sint) {
2179 if (count == 1)
2180 goto found;
2181 else if (count > 1)
2182 count--;
2183 } else {
2184 start = end+1;
2185 field++;
2186 }
2187 break;
2188 }
2189 }
2190 end++;
2191 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002192 }
2193
2194 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002195 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002196 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002197 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002198 }
2199found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002200 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002201 /* If ret string is len 0, no need to
2202 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002203 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002204 return 1;
2205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002206 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002207
2208 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002209 Note: smp->data.u.str.size cannot be set to 0 */
2210 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002212
2213 return 1;
2214}
2215
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002216/* This sample function is designed to return a word from a string.
2217 * First arg is the index of the word (start at 1)
2218 * Second arg is a char list of words separators (type string)
2219 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002220static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002221{
Marcin Deranek9631a282018-04-16 14:30:46 +02002222 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002223 char *start, *end;
2224 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002225 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002226
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002227 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002228 return 0;
2229
2230 word = 0;
2231 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002232 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002233 end = start = smp->data.u.str.area + smp->data.u.str.data;
2234 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002235 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002236 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2237 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002238 issep = 1;
2239 break;
2240 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002241 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002242 if (!inword) {
2243 if (!issep) {
2244 if (word != arg_p[0].data.sint) {
2245 word--;
2246 end = start;
2247 }
2248 inword = 1;
2249 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002250 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002251 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002252 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002253 if (count == 1)
2254 goto found;
2255 else if (count > 1)
2256 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002257 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002258 inword = 0;
2259 }
2260 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002261 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002262 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002263 end = start = smp->data.u.str.area;
2264 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002265 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002266 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2267 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002268 issep = 1;
2269 break;
2270 }
2271 }
2272 if (!inword) {
2273 if (!issep) {
2274 if (word != arg_p[0].data.sint) {
2275 word++;
2276 start = end;
2277 }
2278 inword = 1;
2279 }
2280 }
2281 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002282 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002283 if (count == 1)
2284 goto found;
2285 else if (count > 1)
2286 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002287 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002288 inword = 0;
2289 }
2290 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002291 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002292 }
2293
2294 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002295 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002296 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002297 return 1;
2298 }
2299found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002301 /* If ret string is len 0, no need to
2302 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002304 return 1;
2305
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002307
2308 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002309 Note: smp->data.u.str.size cannot be set to 0 */
2310 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002311 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002312
2313 return 1;
2314}
2315
Willy Tarreau7eda8492015-01-20 19:47:06 +01002316static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2317 const char *file, int line, char **err)
2318{
2319 struct arg *arg = args;
2320 char *p;
2321 int len;
2322
2323 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2324 arg[0].type_flags = 0;
2325
2326 if (arg[2].type != ARGT_STR)
2327 return 1;
2328
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002329 p = arg[2].data.str.area;
2330 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002331 while (len) {
2332 if (*p == 'i') {
2333 arg[0].type_flags |= ARGF_REG_ICASE;
2334 }
2335 else if (*p == 'g') {
2336 arg[0].type_flags |= ARGF_REG_GLOB;
2337 }
2338 else {
2339 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2340 return 0;
2341 }
2342 p++;
2343 len--;
2344 }
2345 return 1;
2346}
2347
2348/* This sample function is designed to do the equivalent of s/match/replace/ on
2349 * the input string. It applies a regex and restarts from the last matched
2350 * location until nothing matches anymore. First arg is the regex to apply to
2351 * the input string, second arg is the replacement expression.
2352 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002353static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002354{
2355 char *start, *end;
2356 struct my_regex *reg = arg_p[0].data.reg;
2357 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002358 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002359 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002360 int flag, max;
2361 int found;
2362
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002363 start = smp->data.u.str.area;
2364 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002365
2366 flag = 0;
2367 while (1) {
2368 /* check for last round which is used to copy remaining parts
2369 * when not running in global replacement mode.
2370 */
2371 found = 0;
2372 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2373 /* Note: we can have start == end on empty strings or at the end */
2374 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2375 }
2376
2377 if (!found)
2378 pmatch[0].rm_so = end - start;
2379
2380 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002381 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002382 if (max && pmatch[0].rm_so > 0) {
2383 if (max > pmatch[0].rm_so)
2384 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002385 memcpy(trash->area + trash->data, start, max);
2386 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002387 }
2388
2389 if (!found)
2390 break;
2391
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002392 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002393 if (!output)
2394 break;
2395
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002396 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2397
Willy Tarreau7eda8492015-01-20 19:47:06 +01002398 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002399 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002400 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002401 if (max > output->data)
2402 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002403 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002404 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002405 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002406 }
2407
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002408 free_trash_chunk(output);
2409
Willy Tarreau7eda8492015-01-20 19:47:06 +01002410 /* stop here if we're done with this string */
2411 if (start >= end)
2412 break;
2413
2414 /* We have a special case for matches of length 0 (eg: "x*y*").
2415 * These ones are considered to match in front of a character,
2416 * so we have to copy that character and skip to the next one.
2417 */
2418 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002419 if (trash->data < trash->size)
2420 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002421 pmatch[0].rm_eo++;
2422 }
2423
2424 start += pmatch[0].rm_eo;
2425 flag |= REG_NOTBOL;
2426 }
2427
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002428 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002429 return 1;
2430}
2431
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002432/* This function check an operator entry. It expects a string.
2433 * The string can be an integer or a variable name.
2434 */
2435static int check_operator(struct arg *args, struct sample_conv *conv,
2436 const char *file, int line, char **err)
2437{
2438 const char *str;
2439 const char *end;
2440
2441 /* Try to decode a variable. */
2442 if (vars_check_arg(&args[0], NULL))
2443 return 1;
2444
2445 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002446 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002447 end = str + strlen(str);
2448 args[0].data.sint = read_int64(&str, end);
2449 if (*str != '\0') {
2450 memprintf(err, "expects an integer or a variable name");
2451 return 0;
2452 }
2453 args[0].type = ARGT_SINT;
2454 return 1;
2455}
2456
Willy Tarreau6204cd92016-03-10 16:33:04 +01002457/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002458 * If the arg contain an integer, the integer is returned in the
2459 * sample. If the arg contains a variable descriptor, it returns the
2460 * variable value.
2461 *
2462 * This function returns 0 if an error occurs, otherwise it returns 1.
2463 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002464static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002465{
2466 switch (arg->type) {
2467 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002468 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002469 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002470 return 1;
2471 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002472 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002473 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002474 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002475 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002476 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002477 return 0;
2478 return 1;
2479 default:
2480 return 0;
2481 }
2482}
2483
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002484/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002485 * result.
2486 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002487static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002488{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002489 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002490 return 1;
2491}
2492
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002493/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002494 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002495 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002496static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002497{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002498 struct sample tmp;
2499
Willy Tarreau7560dd42016-03-10 16:28:58 +01002500 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002501 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002502 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002503 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002504 return 1;
2505}
2506
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002507/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002508 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002509 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002510static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002511{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002512 struct sample tmp;
2513
Willy Tarreau7560dd42016-03-10 16:28:58 +01002514 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002515 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002516 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002517 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002518 return 1;
2519}
2520
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002521/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002522 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002523 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002524static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002525{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002526 struct sample tmp;
2527
Willy Tarreau7560dd42016-03-10 16:28:58 +01002528 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002529 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002530 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002531 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002532 return 1;
2533}
2534
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002535static inline long long int arith_add(long long int a, long long int b)
2536{
2537 /* Prevent overflow and makes capped calculus.
2538 * We must ensure that the check calculus doesn't
2539 * exceed the signed 64 bits limits.
2540 *
2541 * +----------+----------+
2542 * | a<0 | a>=0 |
2543 * +------+----------+----------+
2544 * | b<0 | MIN-a>b | no check |
2545 * +------+----------+----------+
2546 * | b>=0 | no check | MAX-a<b |
2547 * +------+----------+----------+
2548 */
2549 if ((a ^ b) >= 0) {
2550 /* signs are differents. */
2551 if (a < 0) {
2552 if (LLONG_MIN - a > b)
2553 return LLONG_MIN;
2554 }
2555 if (LLONG_MAX - a < b)
2556 return LLONG_MAX;
2557 }
2558 return a + b;
2559}
2560
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002561/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002562 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002563 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002564static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002565{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002566 struct sample tmp;
2567
Willy Tarreau7560dd42016-03-10 16:28:58 +01002568 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002569 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002570 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002571 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002572 return 1;
2573}
2574
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002575/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002576 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002577 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002578static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002579 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002580{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002581 struct sample tmp;
2582
Willy Tarreau7560dd42016-03-10 16:28:58 +01002583 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002584 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002585 return 0;
2586
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002587 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2588 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2589 * of -LLONG_MIN and correct the result.
2590 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002591 if (tmp.data.u.sint == LLONG_MIN) {
2592 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2593 if (smp->data.u.sint < LLONG_MAX)
2594 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002595 return 1;
2596 }
2597
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002598 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002599 * the second operand.
2600 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002601 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002602 return 1;
2603}
2604
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002605/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002606 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002607 * If the result makes an overflow, then the largest possible quantity is
2608 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002609 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002610static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002611 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002612{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002613 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002614 long long int c;
2615
Willy Tarreau7560dd42016-03-10 16:28:58 +01002616 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002617 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002618 return 0;
2619
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002620 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002621 if (!smp->data.u.sint || !tmp.data.u.sint) {
2622 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002623 return 1;
2624 }
2625
2626 /* The multiply between LLONG_MIN and -1 returns a
2627 * "floting point exception".
2628 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002629 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2630 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002631 return 1;
2632 }
2633
2634 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002635 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002636
2637 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002638 if (smp->data.u.sint != c / tmp.data.u.sint) {
2639 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2640 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002641 return 1;
2642 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002643 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002644 return 1;
2645 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002646 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002647 return 1;
2648}
2649
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002650/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002651 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002652 * If arg_p makes the result overflow, then the largest possible quantity is
2653 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002654 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002655static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002656 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002657{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002658 struct sample tmp;
2659
Willy Tarreau7560dd42016-03-10 16:28:58 +01002660 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002661 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002662 return 0;
2663
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002664 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002665 /* The divide between LLONG_MIN and -1 returns a
2666 * "floting point exception".
2667 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002668 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2669 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002670 return 1;
2671 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002672 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002673 return 1;
2674 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002675 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002676 return 1;
2677}
2678
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002679/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002680 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002681 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002682 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002683static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002684 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002685{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002686 struct sample tmp;
2687
Willy Tarreau7560dd42016-03-10 16:28:58 +01002688 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002689 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002690 return 0;
2691
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002692 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002693 /* The divide between LLONG_MIN and -1 returns a
2694 * "floting point exception".
2695 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002696 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2697 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002698 return 1;
2699 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002700 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002701 return 1;
2702 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002703 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002704 return 1;
2705}
2706
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002707/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002708 * result.
2709 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002710static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002711 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002712{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002713 if (smp->data.u.sint == LLONG_MIN)
2714 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002715 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002716 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002717 return 1;
2718}
2719
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002720/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002721 * false. The output is a BOOL.
2722 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002723static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002724 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002725{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002726 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002727 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002728 return 1;
2729}
2730
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002731/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002732 * truee. The output is a BOOL.
2733 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002734static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002735 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002736{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002737 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002738 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002739 return 1;
2740}
2741
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002742/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002743 * The output is a BOOL.
2744 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002745static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002746 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002747{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002748 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002749 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002750 return 1;
2751}
2752
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002753/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002754 * The output is a BOOL.
2755 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002756static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002757 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002758{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002759 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002760 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002761 return 1;
2762}
2763
Willy Tarreau280f42b2018-02-19 15:34:12 +01002764/* appends an optional const string, an optional variable contents and another
2765 * optional const string to an existing string.
2766 */
2767static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2768{
Willy Tarreau83061a82018-07-13 11:56:34 +02002769 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002770 struct sample tmp;
2771 int max;
2772
2773 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002774 trash->data = smp->data.u.str.data;
2775 if (trash->data > trash->size - 1)
2776 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002777
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002778 memcpy(trash->area, smp->data.u.str.area, trash->data);
2779 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002780
2781 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002782 max = arg_p[0].data.str.data;
2783 if (max > trash->size - 1 - trash->data)
2784 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002785
2786 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002787 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2788 trash->data += max;
2789 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002790 }
2791
2792 /* append second string (variable) if it's found and we can turn it
2793 * into a string.
2794 */
2795 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2796 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2797 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2798 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2799
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002800 max = tmp.data.u.str.data;
2801 if (max > trash->size - 1 - trash->data)
2802 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002803
2804 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002805 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2806 max);
2807 trash->data += max;
2808 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002809 }
2810 }
2811
2812 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 max = arg_p[2].data.str.data;
2814 if (max > trash->size - 1 - trash->data)
2815 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002816
2817 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002818 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2819 trash->data += max;
2820 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002821 }
2822
2823 smp->data.u.str = *trash;
2824 smp->data.type = SMP_T_STR;
2825 return 1;
2826}
2827
2828/* This function checks the "concat" converter's arguments and extracts the
2829 * variable name and its scope.
2830 */
2831static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2832 const char *file, int line, char **err)
2833{
2834 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002835 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2836 memprintf(err, "failed to register variable name '%s'",
2837 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002838 return 0;
2839 }
2840 return 1;
2841}
2842
Tim Duesterhusca097c12018-04-27 21:18:45 +02002843/* compares string with a variable containing a string. Return value
2844 * is compatible with strcmp(3)'s return value.
2845 */
2846static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2847{
2848 struct sample tmp;
2849 int max, result;
2850
2851 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2852 if (arg_p[0].type != ARGT_VAR)
2853 return 0;
2854 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2855 return 0;
2856 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2857 return 0;
2858
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002859 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2860 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002861 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002862 if (smp->data.u.str.data != tmp.data.u.str.data) {
2863 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002864 result = -1;
2865 }
2866 else {
2867 result = 1;
2868 }
2869 }
2870 }
2871
2872 smp->data.u.sint = result;
2873 smp->data.type = SMP_T_SINT;
2874 return 1;
2875}
2876
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002877#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2878#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2879#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2880
2881/*
2882 * Extract the field value of an input binary sample. Takes a mandatory argument:
2883 * the protocol buffers field identifier (dotted notation) internally represented
2884 * as an array of unsigned integers and its size.
2885 * Return 1 if the field was found, 0 if not.
2886 */
2887static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2888{
2889 unsigned char *pos;
2890 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002891
2892 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002893 grpc_left = smp->data.u.str.data;
2894
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002895 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002896 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002897
2898 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2899
2900 pos += GRPC_MSG_HEADER_SZ;
2901 grpc_left -= GRPC_MSG_HEADER_SZ;
2902
2903 if (grpc_left < left)
2904 return 0;
2905
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002906 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2907 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002908
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002909 grpc_left -= grpc_msg_len;
2910 }
2911
2912 return 0;
2913}
2914
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002915static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2916{
2917 unsigned char *pos;
2918 size_t left;
2919
2920 pos = (unsigned char *)smp->data.u.str.area;
2921 left = smp->data.u.str.data;
2922
2923 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2924}
2925
2926static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2927 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002928{
2929 if (!args[1].type) {
2930 args[1].type = ARGT_SINT;
2931 args[1].data.sint = PBUF_T_BINARY;
2932 }
2933 else {
2934 int pbuf_type;
2935
2936 pbuf_type = protobuf_type(args[1].data.str.area);
2937 if (pbuf_type == -1) {
2938 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2939 return 0;
2940 }
2941
2942 args[1].type = ARGT_SINT;
2943 args[1].data.sint = pbuf_type;
2944 }
2945
2946 return 1;
2947}
2948
Tim Duesterhusca097c12018-04-27 21:18:45 +02002949/* This function checks the "strcmp" converter's arguments and extracts the
2950 * variable name and its scope.
2951 */
2952static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2953 const char *file, int line, char **err)
2954{
2955 /* Try to decode a variable. */
2956 if (vars_check_arg(&args[0], NULL))
2957 return 1;
2958
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002959 memprintf(err, "failed to register variable name '%s'",
2960 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002961 return 0;
2962}
2963
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002964/************************************************************************/
2965/* All supported sample fetch functions must be declared here */
2966/************************************************************************/
2967
2968/* force TRUE to be returned at the fetch level */
2969static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002970smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002971{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002972 if (!smp_make_rw(smp))
2973 return 0;
2974
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002975 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002976 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002977 return 1;
2978}
2979
2980/* force FALSE to be returned at the fetch level */
2981static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002982smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002983{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002984 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002985 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002986 return 1;
2987}
2988
2989/* retrieve environment variable $1 as a string */
2990static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002991smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002992{
2993 char *env;
2994
2995 if (!args || args[0].type != ARGT_STR)
2996 return 0;
2997
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002999 if (!env)
3000 return 0;
3001
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003002 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003003 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003004 smp->data.u.str.area = env;
3005 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003006 return 1;
3007}
3008
Damien Claisseae6f1252019-10-30 15:57:28 +00003009/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3010 * optional string representing the unit of the result: "s" for seconds, "ms" for
3011 * milliseconds and "us" for microseconds.
3012 * Returns 0 on error and non-zero if OK.
3013 */
3014int smp_check_date_unit(struct arg *args, char **err)
3015{
3016 if (args[1].type == ARGT_STR) {
3017 if (strcmp(args[1].data.str.area, "s") == 0) {
3018 args[1].data.sint = TIME_UNIT_S;
3019 }
3020 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3021 args[1].data.sint = TIME_UNIT_MS;
3022 }
3023 else if (strcmp(args[1].data.str.area, "us") == 0) {
3024 args[1].data.sint = TIME_UNIT_US;
3025 }
3026 else {
3027 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3028 args[1].data.str.area);
3029 return 0;
3030 }
3031 free(args[1].data.str.area);
3032 args[1].data.str.area = NULL;
3033 args[1].type = ARGT_SINT;
3034 }
3035 else if (args[1].type != ARGT_STOP) {
3036 memprintf(err, "Unexpected arg type");
3037 return 0;
3038 }
3039
3040 return 1;
3041}
3042
3043/* retrieve the current local date in epoch time, converts it to milliseconds
3044 * or microseconds if asked to in optional args[1] unit param, and applies an
3045 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003046 */
3047static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003048smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003049{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003050 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003051
Damien Claisseae6f1252019-10-30 15:57:28 +00003052 /* report in milliseconds */
3053 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3054 smp->data.u.sint *= 1000;
3055 smp->data.u.sint += date.tv_usec / 1000;
3056 }
3057 /* report in microseconds */
3058 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3059 smp->data.u.sint *= 1000000;
3060 smp->data.u.sint += date.tv_usec;
3061 }
3062
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003063 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003064 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003066
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003067 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003068 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3069 return 1;
3070}
3071
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003072/* retrieve the current microsecond part of the date */
3073static int
3074smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3075{
3076 smp->data.u.sint = date.tv_usec;
3077 smp->data.type = SMP_T_SINT;
3078 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3079 return 1;
3080}
3081
3082
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003083/* returns the hostname */
3084static int
3085smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3086{
3087 smp->data.type = SMP_T_STR;
3088 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003089 smp->data.u.str.area = hostname;
3090 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003091 return 1;
3092}
3093
Willy Tarreau0f30d262014-11-24 16:02:05 +01003094/* returns the number of processes */
3095static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003096smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003097{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003098 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003099 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003100 return 1;
3101}
3102
3103/* returns the number of the current process (between 1 and nbproc */
3104static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003105smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003106{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003107 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003108 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003109 return 1;
3110}
3111
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003112/* returns the number of the current thread (between 1 and nbthread */
3113static int
3114smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3115{
3116 smp->data.type = SMP_T_SINT;
3117 smp->data.u.sint = tid;
3118 return 1;
3119}
3120
Willy Tarreau84310e22014-02-14 11:59:04 +01003121/* generate a random 32-bit integer for whatever purpose, with an optional
3122 * range specified in argument.
3123 */
3124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003125smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003126{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003127 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003128
3129 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003130 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003131 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003132
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003133 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003134 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3135 return 1;
3136}
3137
Willy Tarreau0f30d262014-11-24 16:02:05 +01003138/* returns true if the current process is stopping */
3139static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003140smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003141{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003142 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003143 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003144 return 1;
3145}
3146
Willy Tarreau70fe9442018-11-22 16:07:39 +01003147/* returns the number of calls of the current stream's process_stream() */
3148static int
3149smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3150{
3151 smp->data.type = SMP_T_SINT;
3152 smp->data.u.sint = smp->strm->task->calls;
3153 return 1;
3154}
3155
3156/* returns the average number of nanoseconds spent processing the stream per call */
3157static int
3158smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3159{
3160 smp->data.type = SMP_T_SINT;
3161 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3162 return 1;
3163}
3164
3165/* returns the total number of nanoseconds spent processing the stream */
3166static int
3167smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3168{
3169 smp->data.type = SMP_T_SINT;
3170 smp->data.u.sint = smp->strm->task->cpu_time;
3171 return 1;
3172}
3173
3174/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3175static int
3176smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3177{
3178 smp->data.type = SMP_T_SINT;
3179 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3180 return 1;
3181}
3182
3183/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3184static int
3185smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3186{
3187 smp->data.type = SMP_T_SINT;
3188 smp->data.u.sint = smp->strm->task->lat_time;
3189 return 1;
3190}
3191
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003192static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3193{
3194 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003195 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003196 smp->data.u.str.area = args[0].data.str.area;
3197 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003198 return 1;
3199}
3200
3201static int smp_check_const_bool(struct arg *args, char **err)
3202{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3204 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003205 args[0].type = ARGT_SINT;
3206 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003207 return 1;
3208 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003209 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3210 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003211 args[0].type = ARGT_SINT;
3212 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003213 return 1;
3214 }
3215 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3216 return 0;
3217}
3218
3219static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3220{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003221 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003222 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003223 return 1;
3224}
3225
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003226static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003227{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003228 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003229 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003230 return 1;
3231}
3232
3233static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3234{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003235 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003236 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003237 return 1;
3238}
3239
3240static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3241{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003242 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003243 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003244 return 1;
3245}
3246
3247static int smp_check_const_bin(struct arg *args, char **err)
3248{
David Carlier64a16ab2016-04-08 10:37:02 +01003249 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003250 int binstrlen;
3251
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003252 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003253 return 0;
3254 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003255 args[0].data.str.area = binstr;
3256 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003257 return 1;
3258}
3259
3260static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3261{
3262 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003263 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003264 smp->data.u.str.area = args[0].data.str.area;
3265 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003266 return 1;
3267}
3268
3269static int smp_check_const_meth(struct arg *args, char **err)
3270{
3271 enum http_meth_t meth;
3272 int i;
3273
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003274 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003275 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003276 args[0].type = ARGT_SINT;
3277 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003278 } else {
3279 /* Check method avalaibility. A methos is a token defined as :
3280 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3281 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3282 * token = 1*tchar
3283 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003284 for (i = 0; i < args[0].data.str.data; i++) {
3285 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003286 memprintf(err, "expects valid method.");
3287 return 0;
3288 }
3289 }
3290 }
3291 return 1;
3292}
3293
3294static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3295{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003296 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003297 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003298 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003299 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003300 smp->data.u.meth.str.area = "";
3301 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003302 } else {
3303 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003304 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003305 smp->data.u.meth.str.area = args[0].data.str.area;
3306 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003307 }
3308 return 1;
3309}
3310
Luca Schimweg8a694b82019-09-10 15:42:52 +02003311// This function checks the "uuid" sample's arguments.
3312// Function won't get called when no parameter is specified (maybe a bug?)
3313static int smp_check_uuid(struct arg *args, char **err)
3314{
3315 if (!args[0].type) {
3316 args[0].type = ARGT_SINT;
3317 args[0].data.sint = 4;
3318 }
3319 else if (args[0].data.sint != 4) {
3320 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3321 return 0;
3322 }
3323
3324 return 1;
3325}
3326
3327// Generate a RFC4122 UUID (default is v4 = fully random)
3328static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3329{
3330 if (args[0].data.sint == 4 || !args[0].type) {
3331 uint32_t rnd[4] = { 0, 0, 0, 0 };
3332 uint64_t last = 0;
3333 int byte = 0;
3334 uint8_t bits = 0;
3335 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3336
3337 while (byte < 4) {
3338 while (bits < 32) {
3339 last |= (uint64_t)random() << bits;
3340 bits += rand_max_bits;
3341 }
3342 rnd[byte++] = last;
3343 last >>= 32u;
3344 bits -= 32;
3345 }
3346
3347 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3348 rnd[0],
3349 rnd[1] & 0xFFFF,
3350 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3351 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3352 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3353 );
3354
3355 smp->data.type = SMP_T_STR;
3356 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3357 smp->data.u.str = trash;
3358 return 1;
3359 }
3360
3361 // more implementations of other uuid formats possible here
3362 return 0;
3363}
3364
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003365/* Note: must not be declared <const> as its list will be overwritten.
3366 * Note: fetches that may return multiple types must be declared as the lowest
3367 * common denominator, the type that can be casted into all other ones. For
3368 * instance IPv4/IPv6 must be declared IPv4.
3369 */
3370static struct sample_fetch_kw_list smp_kws = {ILH, {
3371 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3372 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003373 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003374 { "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 +01003375 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003376 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003377 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3378 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003379 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003380 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003381 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003382 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003383 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003384
3385 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3386 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3387 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3388 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3389 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003390
3391 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3392 { "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 +02003393 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003394 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3395 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3396 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3397 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3398
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003399 { /* END */ },
3400}};
3401
Willy Tarreau0108d902018-11-25 19:14:37 +01003402INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3403
Emeric Brun107ca302010-01-04 16:16:05 +01003404/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003405static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003406 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003407 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003408 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003409 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3410 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003411 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003412 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003413 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003414 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003415 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3416 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003417 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003418 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003419 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3420 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3421 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003422 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3423 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003424 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003425 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003426 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3427 { "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 +01003428 { "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 +02003429 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003430#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003431 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003432#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003433 { "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 +02003434 { "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 +01003435
3436 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003437 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3438 { "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 +01003439
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003440 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3441 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3442 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3443 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3444 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3445 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3446 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3447 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3448 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3449 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3450 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3451 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3452 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3453 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003454
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003455 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003456}};
3457
Willy Tarreau0108d902018-11-25 19:14:37 +01003458INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);