blob: 5bd46f8caa6c6b3ab6b404b201e144535dcfee48 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020035#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020036#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010037
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020038#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010039#include <import/xxhash.h>
40
Willy Tarreau1cf8f082014-02-07 12:14:54 +010041/* sample type names */
42const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020043 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_SINT] = "sint",
46 [SMP_T_ADDR] = "addr",
47 [SMP_T_IPV4] = "ipv4",
48 [SMP_T_IPV6] = "ipv6",
49 [SMP_T_STR] = "str",
50 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020051 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052};
53
Willy Tarreau12785782012-04-27 21:37:17 +020054/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020055static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010056
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample fetch keywords */
58static struct sample_fetch_kw_list sample_fetches = {
59 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample format conversion keywords */
63static struct sample_conv_kw_list sample_convs = {
64 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau80aca902013-01-07 15:42:20 +010067const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
68 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
69 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
70 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
71 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
72 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
73 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
74 SMP_VAL_FE_LOG_END),
75
76 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
77 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
78 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
79 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
80 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
81 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
82 SMP_VAL_FE_LOG_END),
83
84 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
85 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
86 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
87 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
88 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
89 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
90 SMP_VAL_FE_LOG_END),
91
92 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
98 SMP_VAL_FE_LOG_END),
99
100 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
106 SMP_VAL_FE_LOG_END),
107
108 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
109 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
110 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
111 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
112 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
113 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
114 SMP_VAL_FE_LOG_END),
115
116 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
117 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
118 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
119 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________),
123
124 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
125 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
126 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
127 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________),
131
132 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
133 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
134 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
135 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
136 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
137 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
138 SMP_VAL_FE_LOG_END),
139
140 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
142 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
143 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________),
147
148 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
151 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
152 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
153 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
154 SMP_VAL_FE_LOG_END),
155
156 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
160 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
161 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
162 SMP_VAL_FE_LOG_END),
163
164 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
168 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
169 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
170 SMP_VAL_FE_LOG_END),
171
172 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
178 SMP_VAL_FE_LOG_END),
179
180 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
184 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
185 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
186 SMP_VAL___________),
187
188 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
192 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
193 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
194 SMP_VAL___________),
195
196 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
200 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
201 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
202 SMP_VAL_FE_LOG_END),
203
204 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
209 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
210 SMP_VAL___________),
211
212 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL_FE_LOG_END),
219
220 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL_FE_LOG_END),
227
228 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL_FE_LOG_END),
235
236 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL_FE_LOG_END),
243};
244
245static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
246 [SMP_SRC_INTRN] = "internal state",
247 [SMP_SRC_LISTN] = "listener",
248 [SMP_SRC_FTEND] = "frontend",
249 [SMP_SRC_L4CLI] = "client address",
250 [SMP_SRC_L5CLI] = "client-side connection",
251 [SMP_SRC_TRACK] = "track counters",
252 [SMP_SRC_L6REQ] = "request buffer",
253 [SMP_SRC_HRQHV] = "HTTP request headers",
254 [SMP_SRC_HRQHP] = "HTTP request",
255 [SMP_SRC_HRQBO] = "HTTP request body",
256 [SMP_SRC_BKEND] = "backend",
257 [SMP_SRC_SERVR] = "server",
258 [SMP_SRC_L4SRV] = "server address",
259 [SMP_SRC_L5SRV] = "server-side connection",
260 [SMP_SRC_L6RES] = "response buffer",
261 [SMP_SRC_HRSHV] = "HTTP response headers",
262 [SMP_SRC_HRSHP] = "HTTP response",
263 [SMP_SRC_HRSBO] = "HTTP response body",
264 [SMP_SRC_RQFIN] = "request buffer statistics",
265 [SMP_SRC_RSFIN] = "response buffer statistics",
266 [SMP_SRC_TXFIN] = "transaction statistics",
267 [SMP_SRC_SSFIN] = "session statistics",
268};
269
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100270static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
271 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
272 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
273 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
274 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
275 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
276 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
277 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
278 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
279 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
280 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
281 [SMP_CKP_BE_SRV_CON] = "server source selection",
282 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
283 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
284 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
285 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
286 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
287 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
288 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
289 [SMP_CKP_FE_LOG_END] = "logs",
290};
291
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292/* This function returns the type of the data returned by the sample_expr.
293 * It assumes that the <expr> and all of its converters are properly
294 * initialized.
295 */
296inline
297int smp_expr_output_type(struct sample_expr *expr)
298{
299 struct sample_conv_expr *smp_expr;
300
301 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
302 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
303 return smp_expr->conv->out_type;
304 }
305 return expr->fetch->out_type;
306}
307
308
Willy Tarreau80aca902013-01-07 15:42:20 +0100309/* fill the trash with a comma-delimited list of source names for the <use> bit
310 * field which must be composed of a non-null set of SMP_USE_* flags. The return
311 * value is the pointer to the string in the trash buffer.
312 */
313const char *sample_src_names(unsigned int use)
314{
315 int bit;
316
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200317 trash.data = 0;
318 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100319 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
320 if (!(use & ~((1 << bit) - 1)))
321 break; /* no more bits */
322
323 if (!(use & (1 << bit)))
324 continue; /* bit not set */
325
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200326 trash.data += snprintf(trash.area + trash.data,
327 trash.size - trash.data, "%s%s",
328 (use & ((1 << bit) - 1)) ? "," : "",
329 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100332}
333
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100334/* return a pointer to the correct sample checkpoint name, or "unknown" when
335 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
336 * if set.
337 */
338const char *sample_ckp_names(unsigned int use)
339{
340 int bit;
341
342 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
343 if (use & (1 << bit))
344 return fetch_ckp_names[bit];
345 return "unknown sample check place, please report this bug";
346}
347
Emeric Brun107ca302010-01-04 16:16:05 +0100348/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
350 * for next parsing sessions. The fetch keywords capabilities are also computed
351 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100352 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100353void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100354{
Willy Tarreau80aca902013-01-07 15:42:20 +0100355 struct sample_fetch *sf;
356 int bit;
357
358 for (sf = kwl->kw; sf->kw != NULL; sf++) {
359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
360 if (sf->use & (1 << bit))
361 sf->val |= fetch_cap[bit];
362 }
363 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100364}
365
366/*
Willy Tarreau12785782012-04-27 21:37:17 +0200367 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100368 * parsing sessions.
369 */
Willy Tarreau12785782012-04-27 21:37:17 +0200370void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100371{
Willy Tarreau12785782012-04-27 21:37:17 +0200372 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100373}
374
375/*
Willy Tarreau12785782012-04-27 21:37:17 +0200376 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100377 * string of <len> in buffer <kw>.
378 *
379 */
Willy Tarreau12785782012-04-27 21:37:17 +0200380struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100381{
382 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200383 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100384
Willy Tarreau12785782012-04-27 21:37:17 +0200385 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100386 for (index = 0; kwl->kw[index].kw != NULL; index++) {
387 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
388 kwl->kw[index].kw[len] == '\0')
389 return &kwl->kw[index];
390 }
391 }
392 return NULL;
393}
394
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100395/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100396 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397 * <idx> is the index of the next sample fetch entry. It is used as private
398 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100401 * the list is reached.
402 */
403struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
404{
405 struct sample_fetch_kw_list *kwl;
406 struct sample_fetch *base;
407
408 if (!current) {
409 /* Get first kwl entry. */
410 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
411 (*idx) = 0;
412 } else {
413 /* Get kwl corresponding to the curret entry. */
414 base = current + 1 - (*idx);
415 kwl = container_of(base, struct sample_fetch_kw_list, kw);
416 }
417
418 while (1) {
419
420 /* Check if kwl is the last entry. */
421 if (&kwl->list == &sample_fetches.list)
422 return NULL;
423
424 /* idx contain the next keyword. If it is available, return it. */
425 if (kwl->kw[*idx].kw) {
426 (*idx)++;
427 return &kwl->kw[(*idx)-1];
428 }
429
430 /* get next entry in the main list, and return NULL if the end is reached. */
431 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
432
433 /* Set index to 0, ans do one other loop. */
434 (*idx) = 0;
435 }
436}
437
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100438/* This function browses the list of available converters. <current> is
439 * the last used converter. If it is the first call, it must set to NULL.
440 * <idx> is the index of the next converter entry. It is used as private
441 * value. It is useless to initiate it.
442 *
443 * It returns always the next sample_conv entry, and NULL when the end of
444 * the list is reached.
445 */
446struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
447{
448 struct sample_conv_kw_list *kwl;
449 struct sample_conv *base;
450
451 if (!current) {
452 /* Get first kwl entry. */
453 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
454 (*idx) = 0;
455 } else {
456 /* Get kwl corresponding to the curret entry. */
457 base = current + 1 - (*idx);
458 kwl = container_of(base, struct sample_conv_kw_list, kw);
459 }
460
461 while (1) {
462 /* Check if kwl is the last entry. */
463 if (&kwl->list == &sample_convs.list)
464 return NULL;
465
466 /* idx contain the next keyword. If it is available, return it. */
467 if (kwl->kw[*idx].kw) {
468 (*idx)++;
469 return &kwl->kw[(*idx)-1];
470 }
471
472 /* get next entry in the main list, and return NULL if the end is reached. */
473 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
474
475 /* Set index to 0, ans do one other loop. */
476 (*idx) = 0;
477 }
478}
479
Emeric Brun107ca302010-01-04 16:16:05 +0100480/*
Willy Tarreau12785782012-04-27 21:37:17 +0200481 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100482 * string of <len> in buffer <kw>.
483 *
484 */
Willy Tarreau12785782012-04-27 21:37:17 +0200485struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100486{
487 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200488 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100489
Willy Tarreau12785782012-04-27 21:37:17 +0200490 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100491 for (index = 0; kwl->kw[index].kw != NULL; index++) {
492 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
493 kwl->kw[index].kw[len] == '\0')
494 return &kwl->kw[index];
495 }
496 }
497 return NULL;
498}
499
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200501/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
503
Willy Tarreau342acb42012-04-23 22:03:39 +0200504static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100505{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100508 return 1;
509}
510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100512{
Willy Tarreau83061a82018-07-13 11:56:34 +0200513 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100516 return 0;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200519 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100521 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100522
523 return 1;
524}
525
Willy Tarreau342acb42012-04-23 22:03:39 +0200526static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100527{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200528 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100530 return 1;
531}
532
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200533static int c_ipv62ip(struct sample *smp)
534{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200535 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100537 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200538 return 1;
539}
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100542{
Willy Tarreau83061a82018-07-13 11:56:34 +0200543 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100546 return 0;
547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100551 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100552 return 1;
553}
554
555/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200556static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100557{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100559}
560*/
561
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200565 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100566 return 1;
567}
568
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200569static int c_int2ipv6(struct sample *smp)
570{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200571 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
572 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 1;
575}
576
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100577static int c_str2addr(struct sample *smp)
578{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
580 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100581 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100585 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100587 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 return 1;
589}
590
Willy Tarreau342acb42012-04-23 22:03:39 +0200591static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100592{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100594 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200595 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100596 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 1;
598}
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100601{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100603 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200604 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100605 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100607}
608
Emeric Brun4b9e8022014-11-03 18:17:10 +0100609/*
610 * The NULL char always enforces the end of string if it is met.
611 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100612 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200613static int c_bin2str(struct sample *smp)
614{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100615 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 for (i = 0; i < smp->data.u.str.data; i++) {
618 if (!smp->data.u.str.area[i]) {
619 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100620 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 }
Christopher Fauletf7a9d4a2020-04-30 09:57:40 +0200623 smp->data.type = SMP_T_STR;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200624 return 1;
625}
626
Willy Tarreau342acb42012-04-23 22:03:39 +0200627static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100628{
Willy Tarreau83061a82018-07-13 11:56:34 +0200629 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100630 char *pos;
631
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100633 if (!pos)
634 return 0;
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 trash->size = trash->size - (pos - trash->area);
637 trash->area = pos;
638 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200639 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100641 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100642 return 1;
643}
644
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800645/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200646 * For strings and binary blocks, it also provides a known allocated size with
647 * a length that is capped to the size, and ensures a trailing zero is always
648 * appended for strings. This is necessary for some operations which may
649 * require to extend the length. It returns 0 if it fails, 1 on success.
650 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200652{
Willy Tarreau83061a82018-07-13 11:56:34 +0200653 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200654
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200655 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_SINT:
658 case SMP_T_ADDR:
659 case SMP_T_IPV4:
660 case SMP_T_IPV6:
661 /* These type are not const. */
662 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200663
Christopher Fauletec100512017-07-24 15:38:41 +0200664 case SMP_T_METH:
665 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
666 break;
667 /* Fall through */
668
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100669 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200670 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100671 trash->data = smp->data.type == SMP_T_STR ?
672 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 if (trash->data > trash->size - 1)
674 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200675
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100676 memcpy(trash->area, smp->data.type == SMP_T_STR ?
677 smp->data.u.str.area : smp->data.u.meth.str.area,
678 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200680 smp->data.u.str = *trash;
681 break;
682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 trash->data = smp->data.u.str.data;
686 if (trash->data > trash->size)
687 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200688
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200690 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 default:
694 /* Other cases are unexpected. */
695 return 0;
696 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100697
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 /* remove const flag */
699 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200700 return 1;
701}
702
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100703int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
705 return 1;
706}
707
Willy Tarreau342acb42012-04-23 22:03:39 +0200708static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100709{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200710 const char *str;
711 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100712
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100714 return 0;
715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 str = smp->data.u.str.area;
717 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200719 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200720 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100722 return 1;
723}
724
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725static int c_str2meth(struct sample *smp)
726{
727 enum http_meth_t meth;
728 int len;
729
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100731 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 len = smp->data.u.str.data;
733 smp->data.u.meth.str.area = smp->data.u.str.area;
734 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 }
736 else
737 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200739 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100740 return 1;
741}
742
743static int c_meth2str(struct sample *smp)
744{
745 int len;
746 enum http_meth_t meth;
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 len = smp->data.u.meth.str.data;
751 smp->data.u.str.area = smp->data.u.meth.str.area;
752 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200755 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200758 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200761 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 }
763 else {
764 /* Unknown method */
765 return 0;
766 }
767 return 1;
768}
769
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770static int c_addr2bin(struct sample *smp)
771{
Willy Tarreau83061a82018-07-13 11:56:34 +0200772 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 chk->data = 4;
776 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 chk->data = 16;
780 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 }
782 else
783 return 0;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200786 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787 return 1;
788}
789
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200790static int c_int2bin(struct sample *smp)
791{
Willy Tarreau83061a82018-07-13 11:56:34 +0200792 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
795 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799 return 1;
800}
801
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200802
Emeric Brun107ca302010-01-04 16:16:05 +0100803/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200804/* Sample casts matrix: */
805/* sample_casts[from type][to type] */
806/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100807/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100808
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100809sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200810/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
811/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
812/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200813/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200814/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
815/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200816/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200817/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
818/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
819/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200820};
Emeric Brun107ca302010-01-04 16:16:05 +0100821
Emeric Brun107ca302010-01-04 16:16:05 +0100822/*
Willy Tarreau12785782012-04-27 21:37:17 +0200823 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100824 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200825 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200826 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100827 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100828struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100829{
Willy Tarreau833cc792013-07-24 15:34:19 +0200830 const char *begw; /* beginning of word */
831 const char *endw; /* end of word */
832 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200833 struct sample_expr *expr;
834 struct sample_fetch *fetch;
835 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100836 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 char *fkw = NULL;
838 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100839 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100840
Willy Tarreau833cc792013-07-24 15:34:19 +0200841 begw = str[*idx];
842 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
843
844 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100845 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100846 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200847 }
Emeric Brun107ca302010-01-04 16:16:05 +0100848
Willy Tarreau833cc792013-07-24 15:34:19 +0200849 /* keep a copy of the current fetch keyword for error reporting */
850 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100851
Willy Tarreau833cc792013-07-24 15:34:19 +0200852 fetch = find_sample_fetch(begw, endw - begw);
853 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100854 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100855 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200856 }
Emeric Brun107ca302010-01-04 16:16:05 +0100857
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 endt = endw;
859 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100860 /* look for the end of this term and skip the opening parenthesis */
861 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 while (*endt && *endt != ')')
863 endt++;
864 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100865 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200866 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun485479d2010-09-23 18:02:19 +0200868 }
Emeric Brun107ca302010-01-04 16:16:05 +0100869
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 /* At this point, we have :
871 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100872 * - endw : end of the keyword, first character not part of keyword
873 * nor the opening parenthesis (so first character of args
874 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200875 * - endt : end of the term (=endw or last parenthesis if args are present)
876 */
877
878 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100879 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100880 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200881 }
Emeric Brun107ca302010-01-04 16:16:05 +0100882 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200883
Vincent Bernat02779b62016-04-03 13:48:43 +0200884 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200885 if (!expr)
886 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100887
888 LIST_INIT(&(expr->conv_exprs));
889 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200890 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100891
Willy Tarreau689a1df2013-12-13 00:40:11 +0100892 /* Note that we call the argument parser even with an empty string,
893 * this allows it to automatically create entries for mandatory
894 * implicit arguments (eg: local proxy name).
895 */
896 al->kw = expr->fetch->kw;
897 al->conv = NULL;
898 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
899 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
900 goto out_error;
901 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200902
Willy Tarreau689a1df2013-12-13 00:40:11 +0100903 if (!expr->arg_p) {
904 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200905 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100906 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
907 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200908 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100909 }
910
Willy Tarreau833cc792013-07-24 15:34:19 +0200911 /* Now process the converters if any. We have two supported syntaxes
912 * for the converters, which can be combined :
913 * - comma-delimited list of converters just after the keyword and args ;
914 * - one converter per keyword
915 * The combination allows to have each keyword being a comma-delimited
916 * series of converters.
917 *
918 * We want to process the former first, then the latter. For this we start
919 * from the beginning of the supposed place in the exiting conv chain, which
920 * starts at the last comma (endt).
921 */
922
923 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200924 struct sample_conv_expr *conv_expr;
Willy Tarreau8320e562019-12-17 10:25:29 +0100925 int err_arg;
926 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (*endt == ')') /* skip last closing parenthesis */
929 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100930
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 if (*endt && *endt != ',') {
932 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100933 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200934 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100935 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100936 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200937 }
Emeric Brun107ca302010-01-04 16:16:05 +0100938
Willy Tarreau833cc792013-07-24 15:34:19 +0200939 while (*endt == ',') /* then trailing commas */
940 endt++;
941
Willy Tarreau97108e02016-11-25 07:33:24 +0100942 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200943
944 if (!*begw) {
945 /* none ? skip to next string */
946 (*idx)++;
947 begw = str[*idx];
948 if (!begw || !*begw)
949 break;
950 }
951
952 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
953
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 */
960 if (begw == str[*idx])
961 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100962 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 goto out_error;
964 }
Emeric Brun107ca302010-01-04 16:16:05 +0100965
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 endt = endw;
967 if (*endt == '(') {
968 /* look for the end of this term */
Willy Tarreau8320e562019-12-17 10:25:29 +0100969 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 while (*endt && *endt != ')')
971 endt++;
972 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100973 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200974 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200975 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200976 }
977
978 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100979 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100980 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200981 }
Emeric Brun107ca302010-01-04 16:16:05 +0100982
983 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200984 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100985 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100986 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200987 }
Emeric Brun107ca302010-01-04 16:16:05 +0100988
989 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200990 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200991 if (!conv_expr)
992 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100993
994 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
995 conv_expr->conv = conv;
996
Willy Tarreau8320e562019-12-17 10:25:29 +0100997 al->kw = expr->fetch->kw;
998 al->conv = conv_expr->conv->kw;
999 argcnt = make_arg_list(endw, endt - endw, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al);
1000 if (argcnt < 0) {
1001 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1002 goto out_error;
1003 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001004
Willy Tarreau8320e562019-12-17 10:25:29 +01001005 if (argcnt && !conv->arg_mask) {
1006 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1007 goto out_error;
1008 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001009
Willy Tarreau8320e562019-12-17 10:25:29 +01001010 if (!conv_expr->arg_p)
1011 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001012
Willy Tarreau8320e562019-12-17 10:25:29 +01001013 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1014 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001015 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001016 }
1017 }
Emeric Brun485479d2010-09-23 18:02:19 +02001018
Willy Tarreau833cc792013-07-24 15:34:19 +02001019 out:
1020 free(fkw);
1021 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001022 return expr;
1023
1024out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001025 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001026 expr = NULL;
1027 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001028}
1029
1030/*
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001032 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * Returns a pointer on a typed sample structure containing the result or NULL if
1034 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001035 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001036 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001037 *
1038 * Note: the fetch functions are required to properly set the return type. The
1039 * conversion functions must do so too. However the cast functions do not need
1040 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001041 *
1042 * The caller may indicate in <opt> if it considers the result final or not.
1043 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1044 * if the result is stable or not, according to the following table :
1045 *
1046 * return MAY_CHANGE FINAL Meaning for the sample
1047 * NULL 0 * Not present and will never be (eg: header)
1048 * NULL 1 0 Not present yet, could change (eg: POST param)
1049 * NULL 1 1 Not present yet, will not change anymore
1050 * smp 0 * Present and will not change (eg: header)
1051 * smp 1 0 Present, may change (eg: request length)
1052 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001053 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001054struct sample *sample_process(struct proxy *px, struct session *sess,
1055 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001056 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001057{
Willy Tarreau12785782012-04-27 21:37:17 +02001058 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001059
Willy Tarreau18387e22013-07-25 12:02:38 +02001060 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001061 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001062 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001063 }
Emeric Brun107ca302010-01-04 16:16:05 +01001064
Willy Tarreau1777ea62016-03-10 16:15:46 +01001065 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001066 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001067 return NULL;
1068
Emeric Brun107ca302010-01-04 16:16:05 +01001069 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001070 /* we want to ensure that p->type can be casted into
1071 * conv_expr->conv->in_type. We have 3 possibilities :
1072 * - NULL => not castable.
1073 * - c_none => nothing to do (let's optimize it)
1074 * - other => apply cast and prepare to fail
1075 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001076 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 return NULL;
1078
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001079 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1080 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001081 return NULL;
1082
Willy Tarreau12e50112012-04-25 17:21:49 +02001083 /* OK cast succeeded */
1084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001085 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001086 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001087 }
1088 return p;
1089}
1090
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001091/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001092 * Resolve all remaining arguments in proxy <p>. Returns the number of
1093 * errors or 0 if everything is fine.
1094 */
1095int smp_resolve_args(struct proxy *p)
1096{
1097 struct arg_list *cur, *bak;
1098 const char *ctx, *where;
1099 const char *conv_ctx, *conv_pre, *conv_pos;
1100 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001101 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102 struct arg *arg;
1103 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105
1106 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1107 struct proxy *px;
1108 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001109 struct stktable *t;
1110 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001111 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001112
1113 arg = cur->arg;
1114
1115 /* prepare output messages */
1116 conv_pre = conv_pos = conv_ctx = "";
1117 if (cur->conv) {
1118 conv_ctx = cur->conv;
1119 conv_pre = "conversion keyword '";
1120 conv_pos = "' for ";
1121 }
1122
1123 where = "in";
1124 ctx = "sample fetch keyword";
1125 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001126 case ARGC_STK: where = "in stick rule in"; break;
1127 case ARGC_TRK: where = "in tracking rule in"; break;
1128 case ARGC_LOG: where = "in log-format string in"; break;
1129 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1130 case ARGC_HRQ: where = "in http-request header format string in"; break;
1131 case ARGC_HRS: where = "in http-response header format string in"; break;
1132 case ARGC_UIF: where = "in unique-id-format string in"; break;
1133 case ARGC_RDR: where = "in redirect format string in"; break;
1134 case ARGC_CAP: where = "in capture rule in"; break;
1135 case ARGC_ACL: ctx = "ACL keyword"; break;
1136 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001138 }
1139
1140 /* set a few default settings */
1141 px = p;
1142 pname = p->id;
1143
1144 switch (arg->type) {
1145 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001146 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001147 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1148 cur->file, cur->line,
1149 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001150 cfgerr++;
1151 continue;
1152 }
1153
1154 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001155 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156
1157 if (sname) {
1158 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001159 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001161 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001163 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1164 cur->file, cur->line, pname,
1165 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001166 cfgerr++;
1167 break;
1168 }
1169 }
1170 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001171 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172
1173 srv = findserver(px, sname);
1174 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 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",
1176 cur->file, cur->line, sname, pname,
1177 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001178 cfgerr++;
1179 break;
1180 }
1181
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 free(arg->data.str.area);
1183 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001184 arg->unresolved = 0;
1185 arg->data.srv = srv;
1186 break;
1187
1188 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001189 if (arg->data.str.data) {
1190 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001191 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001192 }
1193
1194 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001195 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1196 cur->file, cur->line, pname,
1197 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001198 cfgerr++;
1199 break;
1200 }
1201
1202 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 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",
1204 cur->file, cur->line, pname,
1205 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 cfgerr++;
1207 break;
1208 }
1209
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001210 free(arg->data.str.area);
1211 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 arg->unresolved = 0;
1213 arg->data.prx = px;
1214 break;
1215
1216 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001217 if (arg->data.str.data) {
1218 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001219 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 }
1221
1222 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001223 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1224 cur->file, cur->line, pname,
1225 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001226 cfgerr++;
1227 break;
1228 }
1229
1230 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 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",
1232 cur->file, cur->line, pname,
1233 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001234 cfgerr++;
1235 break;
1236 }
1237
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001238 free(arg->data.str.area);
1239 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001240 arg->unresolved = 0;
1241 arg->data.prx = px;
1242 break;
1243
1244 case ARGT_TAB:
Frédéric Lécaille9eae8932019-06-20 09:31:04 +02001245 if (arg->data.str.data)
1246 stktname = arg->data.str.area;
1247 else
1248 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001250 t = stktable_find_by_name(stktname);
1251 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 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 +01001253 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001259 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 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 +01001261 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 break;
1265 }
1266
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001268 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 +01001269 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001270 cfgerr++;
1271 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001272 }
1273
Frédéric Lécailleeca60872019-08-07 09:28:39 +02001274 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001275 p->next_stkt_ref = t->proxies_list;
1276 t->proxies_list = p;
1277 }
1278
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001279 free(arg->data.str.area);
1280 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001282 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283 break;
1284
1285 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001286 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001287 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1288 cur->file, cur->line,
1289 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 cfgerr++;
1291 break;
1292 }
1293
1294 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001295 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296 ul = p->uri_auth->userlist;
1297 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001298 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299
1300 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 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 +02001302 cur->file, cur->line,
1303 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001304 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001305 cfgerr++;
1306 break;
1307 }
1308
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001309 free(arg->data.str.area);
1310 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001311 arg->unresolved = 0;
1312 arg->data.usr = ul;
1313 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001314
1315 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001316 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001317 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1318 cur->file, cur->line,
1319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001320 cfgerr++;
1321 continue;
1322 }
1323
Willy Tarreau46947782015-01-19 19:00:58 +01001324 rflags = 0;
1325 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1326 err = NULL;
1327
Dragan Dosen26743032019-04-30 15:54:36 +02001328 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1330 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001333 cfgerr++;
1334 continue;
1335 }
1336
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001337 free(arg->data.str.area);
1338 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001339 arg->unresolved = 0;
1340 arg->data.reg = reg;
1341 break;
1342
1343
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001344 }
1345
1346 LIST_DEL(&cur->list);
1347 free(cur);
1348 } /* end of args processing */
1349
1350 return cfgerr;
1351}
1352
1353/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001354 * Process a fetch + format conversion as defined by the sample expression
1355 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001356 * not explicitly set to <smp_type>, but shall be compatible with it as
1357 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1358 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001359 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1360 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1361 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1362 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001363 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1364 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1365 * still hope to get it after waiting longer, and is not converted to string.
1366 * The possible output combinations are the following :
1367 *
1368 * return MAY_CHANGE FINAL Meaning for the sample
1369 * NULL * * Not present and will never be (eg: header)
1370 * smp 0 * Final value converted (eg: header)
1371 * smp 1 0 Not present yet, may appear later (eg: header)
1372 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001374struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001375 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001376 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001377{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001378 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379
Willy Tarreau6c616e02014-06-25 16:56:41 +02001380 memset(smp, 0, sizeof(*smp));
1381
Willy Tarreau192252e2015-04-04 01:47:55 +02001382 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1384 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001386 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001388 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return NULL;
1390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001391 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return NULL;
1393
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001394 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001395 return smp;
1396}
1397
Christopher Faulet476e5d02016-10-26 11:34:47 +02001398static void release_sample_arg(struct arg *p)
1399{
1400 struct arg *p_back = p;
1401
1402 if (!p)
1403 return;
1404
1405 while (p->type != ARGT_STOP) {
1406 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001407 free(p->data.str.area);
1408 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001409 p->unresolved = 0;
1410 }
1411 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001412 regex_free(p->data.reg);
1413 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001414 }
1415 p++;
1416 }
1417
1418 if (p_back != empty_arg_list)
1419 free(p_back);
1420}
1421
1422void release_sample_expr(struct sample_expr *expr)
1423{
1424 struct sample_conv_expr *conv_expr, *conv_exprb;
1425
1426 if (!expr)
1427 return;
1428
1429 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1430 release_sample_arg(conv_expr->arg_p);
1431 release_sample_arg(expr->arg_p);
1432 free(expr);
1433}
1434
Emeric Brun107ca302010-01-04 16:16:05 +01001435/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001436/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001437/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001438/*****************************************************************/
1439
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001440#ifdef DEBUG_EXPR
1441static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1442{
1443 int i;
1444 struct sample tmp;
1445
1446 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001447 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1448 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001449 fprintf(stderr, "(undisplayable)");
1450 } else {
1451
1452 /* Copy sample fetch. This put the sample as const, the
1453 * cast will copy data if a transformation is required.
1454 */
1455 memcpy(&tmp, smp, sizeof(struct sample));
1456 tmp.flags = SMP_F_CONST;
1457
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001458 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001459 fprintf(stderr, "(undisplayable)");
1460
1461 else {
1462 /* Display the displayable chars*. */
Willy Tarreau26e9a042019-12-17 09:00:15 +01001463 fputc('<', stderr);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 for (i = 0; i < tmp.data.u.str.data; i++) {
1465 if (isprint(tmp.data.u.str.area[i]))
1466 fputc(tmp.data.u.str.area[i],
1467 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001468 else
1469 fputc('.', stderr);
1470 }
Willy Tarreau26e9a042019-12-17 09:00:15 +01001471 fputc('>', stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001472 }
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001473 }
Willy Tarreau26e9a042019-12-17 09:00:15 +01001474 fputc('\n', stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001475 }
1476 return 1;
1477}
1478#endif
1479
Holger Just1bfc24b2017-05-06 00:56:53 +02001480static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1481{
Willy Tarreau83061a82018-07-13 11:56:34 +02001482 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001483 int bin_len;
1484
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001485 trash->data = 0;
1486 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1487 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001488 if (bin_len < 0)
1489 return 0;
1490
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001491 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001492 smp->data.u.str = *trash;
1493 smp->data.type = SMP_T_BIN;
1494 smp->flags &= ~SMP_F_CONST;
1495 return 1;
1496}
1497
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001498static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001499{
Willy Tarreau83061a82018-07-13 11:56:34 +02001500 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001501 int b64_len;
1502
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001503 trash->data = 0;
1504 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1505 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001506 if (b64_len < 0)
1507 return 0;
1508
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001509 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001510 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001511 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001512 smp->flags &= ~SMP_F_CONST;
1513 return 1;
1514}
1515
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001516static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1517{
1518 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001519 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001520
1521 memset(&ctx, 0, sizeof(ctx));
1522
1523 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001524 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1525 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001526
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001528 smp->data.u.str = *trash;
1529 smp->data.type = SMP_T_BIN;
1530 smp->flags &= ~SMP_F_CONST;
1531 return 1;
1532}
1533
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001534static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001535{
Willy Tarreau83061a82018-07-13 11:56:34 +02001536 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001537 unsigned char c;
1538 int ptr = 0;
1539
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001540 trash->data = 0;
1541 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1542 c = smp->data.u.str.area[ptr++];
1543 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1544 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001545 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001546 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001547 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001548 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001549 return 1;
1550}
1551
Dragan Dosen3f957b22017-10-24 09:27:34 +02001552static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1553{
1554 long long int n = 0;
1555 int i, c;
1556
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001557 for (i = 0; i < smp->data.u.str.data; i++) {
1558 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001559 return 0;
1560 n = (n << 4) + c;
1561 }
1562
1563 smp->data.u.sint = n;
1564 smp->data.type = SMP_T_SINT;
1565 smp->flags &= ~SMP_F_CONST;
1566 return 1;
1567}
1568
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001569/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001570static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001571{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001572 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1573 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001574 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001575 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001576 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001577 return 1;
1578}
1579
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001580static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001581{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001582 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001583 smp->data.u.sint = i;
1584 smp->data.type = SMP_T_SINT;
1585 return 1;
1586}
1587
1588
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001589static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001590{
1591 int i;
1592
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001593 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001594 return 0;
1595
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 for (i = 0; i < smp->data.u.str.data; i++) {
1597 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1598 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001599 }
1600 return 1;
1601}
1602
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001603static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001604{
1605 int i;
1606
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001607 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001608 return 0;
1609
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001610 for (i = 0; i < smp->data.u.str.data; i++) {
1611 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1612 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001613 }
1614 return 1;
1615}
1616
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001617/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1618static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001619{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001620 /* Attempt to convert to IPv4 to apply the correct mask. */
1621 c_ipv62ip(smp);
1622
1623 if (smp->data.type == SMP_T_IPV4) {
1624 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1625 smp->data.type = SMP_T_IPV4;
1626 }
1627 else if (smp->data.type == SMP_T_IPV6) {
1628 /* IPv6 cannot be converted without an IPv6 mask. */
1629 if (args[1].type != ARGT_IPV6)
1630 return 0;
1631
1632 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1633 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1634 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1635 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1636 smp->data.type = SMP_T_IPV6;
1637 }
1638
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001639 return 1;
1640}
1641
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001642/* takes an UINT value on input supposed to represent the time since EPOCH,
1643 * adds an optional offset found in args[1] and emits a string representing
1644 * the local time in the format specified in args[1] using strftime().
1645 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001646static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001647{
Willy Tarreau83061a82018-07-13 11:56:34 +02001648 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001649 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001650 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001651 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001652
1653 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001654 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001655 curr_date += args[1].data.sint;
1656
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001657 tm = localtime(&curr_date);
1658 if (!tm)
1659 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001660 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001661 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1662 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001663 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001664 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001665 return 1;
1666}
1667
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001668/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001669static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001670{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001671 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1672 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001673 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001674 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001675 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001676 return 1;
1677}
1678
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001679/* takes an UINT value on input supposed to represent the time since EPOCH,
1680 * adds an optional offset found in args[1] and emits a string representing
1681 * the UTC date in the format specified in args[1] using strftime().
1682 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001683static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001684{
Willy Tarreau83061a82018-07-13 11:56:34 +02001685 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001686 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001687 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001688 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001689
1690 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001691 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001692 curr_date += args[1].data.sint;
1693
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001694 tm = gmtime(&curr_date);
1695 if (!tm)
1696 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001697 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001698 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1699 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001700 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001701 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001702 return 1;
1703}
1704
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001705/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001706static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001707{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001708 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1709 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001710 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001711 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001712 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001713 return 1;
1714}
1715
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001716/* hashes the binary input into a 32-bit unsigned int using xxh.
1717 * The seed of the hash defaults to 0 but can be changd in argument 1.
1718 */
1719static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1720{
1721 unsigned int seed;
1722
1723 if (arg_p && arg_p->data.sint)
1724 seed = arg_p->data.sint;
1725 else
1726 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001727 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1728 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001729 smp->data.type = SMP_T_SINT;
1730 return 1;
1731}
1732
1733/* hashes the binary input into a 64-bit unsigned int using xxh.
1734 * In fact, the function returns a 64 bit unsigned, but the sample
1735 * storage of haproxy only proposes 64-bits signed, so the value is
1736 * cast as signed. This cast doesn't impact the hash repartition.
1737 * The seed of the hash defaults to 0 but can be changd in argument 1.
1738 */
1739static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1740{
1741 unsigned long long int seed;
1742
1743 if (arg_p && arg_p->data.sint)
1744 seed = (unsigned long long int)arg_p->data.sint;
1745 else
1746 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001747 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1748 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001749 smp->data.type = SMP_T_SINT;
1750 return 1;
1751}
1752
Willy Tarreau80599772015-01-20 19:35:24 +01001753/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001754static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001755{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001756 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1757 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001758 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001759 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001760 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001761 return 1;
1762}
1763
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001764/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1765static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1766{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001767 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1768 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001769 if (arg_p && arg_p->data.sint)
1770 smp->data.u.sint = full_hash(smp->data.u.sint);
1771 smp->data.type = SMP_T_SINT;
1772 return 1;
1773}
1774
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001775/* This function escape special json characters. The returned string can be
1776 * safely set between two '"' and used as json string. The json string is
1777 * defined like this:
1778 *
1779 * any Unicode character except '"' or '\' or control character
1780 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1781 *
1782 * The enum input_type contain all the allowed mode for decoding the input
1783 * string.
1784 */
1785enum input_type {
1786 IT_ASCII = 0,
1787 IT_UTF8,
1788 IT_UTF8S,
1789 IT_UTF8P,
1790 IT_UTF8PS,
1791};
1792static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1793 const char *file, int line, char **err)
1794{
1795 if (!arg) {
1796 memprintf(err, "Unexpected empty arg list");
1797 return 0;
1798 }
1799
1800 if (arg->type != ARGT_STR) {
1801 memprintf(err, "Unexpected arg type");
1802 return 0;
1803 }
1804
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001805 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001806 arg->type = ARGT_SINT;
1807 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001808 return 1;
1809 }
1810
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001811 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001812 arg->type = ARGT_SINT;
1813 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001814 return 1;
1815 }
1816
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001817 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001818 arg->type = ARGT_SINT;
1819 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001820 return 1;
1821 }
1822
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001823 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001824 arg->type = ARGT_SINT;
1825 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001826 return 1;
1827 }
1828
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001829 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001830 arg->type = ARGT_SINT;
1831 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001832 return 1;
1833 }
1834
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001836 arg->type = ARGT_SINT;
1837 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001838 return 1;
1839 }
1840
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001841 memprintf(err, "Unexpected input code type. "
1842 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001843 return 0;
1844}
1845
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001846static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001847{
Willy Tarreau83061a82018-07-13 11:56:34 +02001848 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001849 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1850 const char *str;
1851 int len;
1852 enum input_type input_type = IT_ASCII;
1853 unsigned int c;
1854 unsigned int ret;
1855 char *p;
1856
1857 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001858 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001859
1860 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001861 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001862
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001863 p = smp->data.u.str.area;
1864 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001865
1866 if (input_type == IT_ASCII) {
1867 /* Read input as ASCII. */
1868 c = *(unsigned char *)p;
1869 p++;
1870 }
1871 else {
1872 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001873 ret = utf8_next(p,
1874 smp->data.u.str.data - ( p - smp->data.u.str.area),
1875 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001876 p += utf8_return_length(ret);
1877
1878 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1879 return 0;
1880 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1881 continue;
1882 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1883 return 0;
1884 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1885 continue;
1886
1887 /* Check too big values. */
1888 if ((unsigned int)c > 0xffff) {
1889 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1890 return 0;
1891 continue;
1892 }
1893 }
1894
1895 /* Convert character. */
1896 if (c == '"') {
1897 len = 2;
1898 str = "\\\"";
1899 }
1900 else if (c == '\\') {
1901 len = 2;
1902 str = "\\\\";
1903 }
1904 else if (c == '/') {
1905 len = 2;
1906 str = "\\/";
1907 }
1908 else if (c == '\b') {
1909 len = 2;
1910 str = "\\b";
1911 }
1912 else if (c == '\f') {
1913 len = 2;
1914 str = "\\f";
1915 }
1916 else if (c == '\r') {
1917 len = 2;
1918 str = "\\r";
1919 }
1920 else if (c == '\n') {
1921 len = 2;
1922 str = "\\n";
1923 }
1924 else if (c == '\t') {
1925 len = 2;
1926 str = "\\t";
1927 }
1928 else if (c > 0xff || !isprint(c)) {
1929 /* isprint generate a segfault if c is too big. The man says that
1930 * c must have the value of an unsigned char or EOF.
1931 */
1932 len = 6;
1933 _str[0] = '\\';
1934 _str[1] = 'u';
1935 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1936 str = _str;
1937 }
1938 else {
1939 len = 1;
Willy Tarreaud90d8832020-02-25 08:37:37 +01001940 _str[0] = c;
1941 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001942 }
1943
1944 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001945 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001946 return 0;
1947
1948 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001949 memcpy(temp->area + temp->data, str, len);
1950 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001951 }
1952
1953 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001954 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001955 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001956
1957 return 1;
1958}
1959
Emeric Brun54c4ac82014-11-03 15:32:43 +01001960/* This sample function is designed to extract some bytes from an input buffer.
1961 * First arg is the offset.
1962 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001963static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001964{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001965 if (smp->data.u.str.data <= arg_p[0].data.sint) {
1966 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001967 return 1;
1968 }
1969
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001970 if (smp->data.u.str.size)
1971 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001972 smp->data.u.str.data -= arg_p[0].data.sint;
1973 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001974
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001975 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
1976 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01001977
1978 return 1;
1979}
1980
Emeric Brunf399b0d2014-11-03 17:07:03 +01001981static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
1982 const char *file, int line, char **err)
1983{
1984 struct arg *arg = args;
1985
1986 if (!arg) {
1987 memprintf(err, "Unexpected empty arg list");
1988 return 0;
1989 }
1990
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001991 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001992 memprintf(err, "Unexpected arg type");
1993 return 0;
1994 }
1995
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001996 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01001997 memprintf(err, "Unexpected value 0 for index");
1998 return 0;
1999 }
2000
2001 arg++;
2002
2003 if (arg->type != ARGT_STR) {
2004 memprintf(err, "Unexpected arg type");
2005 return 0;
2006 }
2007
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002008 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002009 memprintf(err, "Empty separators list");
2010 return 0;
2011 }
2012
2013 return 1;
2014}
2015
2016/* This sample function is designed to a return selected part of a string (field).
2017 * First arg is the index of the field (start at 1)
2018 * Second arg is a char list of separators (type string)
2019 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002020static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002021{
Marcin Deranek9631a282018-04-16 14:30:46 +02002022 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002023 char *start, *end;
2024 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002025 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002026
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002027 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002028 return 0;
2029
Marcin Deranek9631a282018-04-16 14:30:46 +02002030 if (arg_p[0].data.sint < 0) {
2031 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002032 end = start = smp->data.u.str.area + smp->data.u.str.data;
2033 while (start > smp->data.u.str.area) {
2034 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2035 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002036 if (field == arg_p[0].data.sint) {
2037 if (count == 1)
2038 goto found;
2039 else if (count > 1)
2040 count--;
2041 } else {
2042 end = start-1;
2043 field--;
2044 }
2045 break;
2046 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002047 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002048 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002049 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002050 } else {
2051 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002052 end = start = smp->data.u.str.area;
2053 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2054 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2055 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002056 if (field == arg_p[0].data.sint) {
2057 if (count == 1)
2058 goto found;
2059 else if (count > 1)
2060 count--;
2061 } else {
2062 start = end+1;
2063 field++;
2064 }
2065 break;
2066 }
2067 }
2068 end++;
2069 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002070 }
2071
2072 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002073 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002074 smp->data.u.str.data = 0;
Tim Duesterhus4fa98572019-10-16 15:11:15 +02002075 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002076 }
2077found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002078 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002079 /* If ret string is len 0, no need to
2080 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002081 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002082 return 1;
2083
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002084 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002085
2086 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002087 Note: smp->data.u.str.size cannot be set to 0 */
2088 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002089 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002090
2091 return 1;
2092}
2093
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002094/* This sample function is designed to return a word from a string.
2095 * First arg is the index of the word (start at 1)
2096 * Second arg is a char list of words separators (type string)
2097 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002098static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002099{
Marcin Deranek9631a282018-04-16 14:30:46 +02002100 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002101 char *start, *end;
2102 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002103 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002104
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002105 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002106 return 0;
2107
2108 word = 0;
2109 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002110 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002111 end = start = smp->data.u.str.area + smp->data.u.str.data;
2112 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002113 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002114 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2115 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002116 issep = 1;
2117 break;
2118 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002119 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002120 if (!inword) {
2121 if (!issep) {
2122 if (word != arg_p[0].data.sint) {
2123 word--;
2124 end = start;
2125 }
2126 inword = 1;
2127 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002128 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002129 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002130 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002131 if (count == 1)
2132 goto found;
2133 else if (count > 1)
2134 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002135 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002136 inword = 0;
2137 }
2138 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002139 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002140 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002141 end = start = smp->data.u.str.area;
2142 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002143 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002144 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2145 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002146 issep = 1;
2147 break;
2148 }
2149 }
2150 if (!inword) {
2151 if (!issep) {
2152 if (word != arg_p[0].data.sint) {
2153 word++;
2154 start = end;
2155 }
2156 inword = 1;
2157 }
2158 }
2159 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002160 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002161 if (count == 1)
2162 goto found;
2163 else if (count > 1)
2164 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002165 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002166 inword = 0;
2167 }
2168 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002169 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002170 }
2171
2172 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002173 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002175 return 1;
2176 }
2177found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002179 /* If ret string is len 0, no need to
2180 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002181 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002182 return 1;
2183
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002184 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002185
2186 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002187 Note: smp->data.u.str.size cannot be set to 0 */
2188 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002189 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002190
2191 return 1;
2192}
2193
Willy Tarreau7eda8492015-01-20 19:47:06 +01002194static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2195 const char *file, int line, char **err)
2196{
2197 struct arg *arg = args;
2198 char *p;
2199 int len;
2200
2201 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2202 arg[0].type_flags = 0;
2203
2204 if (arg[2].type != ARGT_STR)
2205 return 1;
2206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 p = arg[2].data.str.area;
2208 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002209 while (len) {
2210 if (*p == 'i') {
2211 arg[0].type_flags |= ARGF_REG_ICASE;
2212 }
2213 else if (*p == 'g') {
2214 arg[0].type_flags |= ARGF_REG_GLOB;
2215 }
2216 else {
2217 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2218 return 0;
2219 }
2220 p++;
2221 len--;
2222 }
2223 return 1;
2224}
2225
2226/* This sample function is designed to do the equivalent of s/match/replace/ on
2227 * the input string. It applies a regex and restarts from the last matched
2228 * location until nothing matches anymore. First arg is the regex to apply to
2229 * the input string, second arg is the replacement expression.
2230 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002231static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002232{
2233 char *start, *end;
2234 struct my_regex *reg = arg_p[0].data.reg;
2235 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002236 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002237 int flag, max;
2238 int found;
2239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002240 start = smp->data.u.str.area;
2241 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002242
2243 flag = 0;
2244 while (1) {
2245 /* check for last round which is used to copy remaining parts
2246 * when not running in global replacement mode.
2247 */
2248 found = 0;
2249 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2250 /* Note: we can have start == end on empty strings or at the end */
2251 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2252 }
2253
2254 if (!found)
2255 pmatch[0].rm_so = end - start;
2256
2257 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002258 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002259 if (max && pmatch[0].rm_so > 0) {
2260 if (max > pmatch[0].rm_so)
2261 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002262 memcpy(trash->area + trash->data, start, max);
2263 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002264 }
2265
2266 if (!found)
2267 break;
2268
2269 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002270 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002271 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002272 if (max > arg_p[1].data.str.data)
2273 max = arg_p[1].data.str.data;
2274 memcpy(trash->area + trash->data,
2275 arg_p[1].data.str.area, max);
2276 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002277 }
2278
2279 /* stop here if we're done with this string */
2280 if (start >= end)
2281 break;
2282
2283 /* We have a special case for matches of length 0 (eg: "x*y*").
2284 * These ones are considered to match in front of a character,
2285 * so we have to copy that character and skip to the next one.
2286 */
2287 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002288 if (trash->data < trash->size)
2289 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002290 pmatch[0].rm_eo++;
2291 }
2292
2293 start += pmatch[0].rm_eo;
2294 flag |= REG_NOTBOL;
2295 }
2296
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002297 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002298 return 1;
2299}
2300
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002301/* This function check an operator entry. It expects a string.
2302 * The string can be an integer or a variable name.
2303 */
2304static int check_operator(struct arg *args, struct sample_conv *conv,
2305 const char *file, int line, char **err)
2306{
2307 const char *str;
2308 const char *end;
2309
2310 /* Try to decode a variable. */
2311 if (vars_check_arg(&args[0], NULL))
2312 return 1;
2313
2314 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002316 end = str + strlen(str);
2317 args[0].data.sint = read_int64(&str, end);
2318 if (*str != '\0') {
2319 memprintf(err, "expects an integer or a variable name");
2320 return 0;
2321 }
2322 args[0].type = ARGT_SINT;
2323 return 1;
2324}
2325
Willy Tarreau6204cd92016-03-10 16:33:04 +01002326/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002327 * If the arg contain an integer, the integer is returned in the
2328 * sample. If the arg contains a variable descriptor, it returns the
2329 * variable value.
2330 *
2331 * This function returns 0 if an error occurs, otherwise it returns 1.
2332 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002333static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002334{
2335 switch (arg->type) {
2336 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002337 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002338 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002339 return 1;
2340 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002341 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002342 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002343 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002344 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002345 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002346 return 0;
2347 return 1;
2348 default:
2349 return 0;
2350 }
2351}
2352
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002353/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002354 * result.
2355 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002356static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002357{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002358 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002359 return 1;
2360}
2361
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002362/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002363 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002364 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002365static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002366{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002367 struct sample tmp;
2368
Willy Tarreau7560dd42016-03-10 16:28:58 +01002369 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002370 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002371 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002372 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002373 return 1;
2374}
2375
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002376/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002377 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002378 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002379static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002380{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002381 struct sample tmp;
2382
Willy Tarreau7560dd42016-03-10 16:28:58 +01002383 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002384 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002385 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002386 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002387 return 1;
2388}
2389
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002390/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002391 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002392 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002393static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002394{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002395 struct sample tmp;
2396
Willy Tarreau7560dd42016-03-10 16:28:58 +01002397 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002398 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002399 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002400 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002401 return 1;
2402}
2403
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002404static inline long long int arith_add(long long int a, long long int b)
2405{
2406 /* Prevent overflow and makes capped calculus.
2407 * We must ensure that the check calculus doesn't
2408 * exceed the signed 64 bits limits.
2409 *
2410 * +----------+----------+
2411 * | a<0 | a>=0 |
2412 * +------+----------+----------+
2413 * | b<0 | MIN-a>b | no check |
2414 * +------+----------+----------+
2415 * | b>=0 | no check | MAX-a<b |
2416 * +------+----------+----------+
2417 */
2418 if ((a ^ b) >= 0) {
2419 /* signs are differents. */
2420 if (a < 0) {
2421 if (LLONG_MIN - a > b)
2422 return LLONG_MIN;
2423 }
2424 if (LLONG_MAX - a < b)
2425 return LLONG_MAX;
2426 }
2427 return a + b;
2428}
2429
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002430/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002431 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002432 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002433static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002434{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002435 struct sample tmp;
2436
Willy Tarreau7560dd42016-03-10 16:28:58 +01002437 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002438 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002439 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002440 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002441 return 1;
2442}
2443
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002444/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002445 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002446 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002447static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002448 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002449{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002450 struct sample tmp;
2451
Willy Tarreau7560dd42016-03-10 16:28:58 +01002452 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002453 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002454 return 0;
2455
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002456 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2457 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2458 * of -LLONG_MIN and correct the result.
2459 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002460 if (tmp.data.u.sint == LLONG_MIN) {
2461 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2462 if (smp->data.u.sint < LLONG_MAX)
2463 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002464 return 1;
2465 }
2466
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002467 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002468 * the second operand.
2469 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002470 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002471 return 1;
2472}
2473
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002474/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002475 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002476 * If the result makes an overflow, then the largest possible quantity is
2477 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002478 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002479static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002480 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002481{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002482 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002483 long long int c;
2484
Willy Tarreau7560dd42016-03-10 16:28:58 +01002485 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002486 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002487 return 0;
2488
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002489 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002490 if (!smp->data.u.sint || !tmp.data.u.sint) {
2491 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002492 return 1;
2493 }
2494
2495 /* The multiply between LLONG_MIN and -1 returns a
2496 * "floting point exception".
2497 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002498 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2499 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002500 return 1;
2501 }
2502
2503 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002504 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002505
2506 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002507 if (smp->data.u.sint != c / tmp.data.u.sint) {
2508 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2509 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002510 return 1;
2511 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002512 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002513 return 1;
2514 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002515 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002516 return 1;
2517}
2518
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002519/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002520 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002521 * If arg_p makes the result overflow, then the largest possible quantity is
2522 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002523 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002524static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002525 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002526{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002527 struct sample tmp;
2528
Willy Tarreau7560dd42016-03-10 16:28:58 +01002529 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002530 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002531 return 0;
2532
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002533 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002534 /* The divide between LLONG_MIN and -1 returns a
2535 * "floting point exception".
2536 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002537 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2538 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002539 return 1;
2540 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002541 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002542 return 1;
2543 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002544 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002545 return 1;
2546}
2547
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002548/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002549 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002550 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002551 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002552static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002553 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002554{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002555 struct sample tmp;
2556
Willy Tarreau7560dd42016-03-10 16:28:58 +01002557 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002558 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002559 return 0;
2560
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002561 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002562 /* The divide between LLONG_MIN and -1 returns a
2563 * "floting point exception".
2564 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002565 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2566 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002567 return 1;
2568 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002569 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002570 return 1;
2571 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002573 return 1;
2574}
2575
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002576/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002577 * result.
2578 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002579static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002580 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002581{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002582 if (smp->data.u.sint == LLONG_MIN)
2583 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002584 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002585 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002586 return 1;
2587}
2588
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002589/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002590 * false. The output is a BOOL.
2591 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002592static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002593 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002594{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002595 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002596 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002597 return 1;
2598}
2599
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002600/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002601 * truee. The output is a BOOL.
2602 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002603static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002604 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002605{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002606 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002607 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002608 return 1;
2609}
2610
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002611/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002612 * The output is a BOOL.
2613 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002614static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002615 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002616{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002617 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002618 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002619 return 1;
2620}
2621
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002622/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002623 * The output is a BOOL.
2624 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002625static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002626 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002627{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002628 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002629 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002630 return 1;
2631}
2632
Willy Tarreau280f42b2018-02-19 15:34:12 +01002633/* appends an optional const string, an optional variable contents and another
2634 * optional const string to an existing string.
2635 */
2636static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2637{
Willy Tarreau83061a82018-07-13 11:56:34 +02002638 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002639 struct sample tmp;
2640 int max;
2641
2642 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002643 trash->data = smp->data.u.str.data;
2644 if (trash->data > trash->size - 1)
2645 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002646
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002647 memcpy(trash->area, smp->data.u.str.area, trash->data);
2648 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002649
2650 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002651 max = arg_p[0].data.str.data;
2652 if (max > trash->size - 1 - trash->data)
2653 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002654
2655 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002656 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2657 trash->data += max;
2658 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002659 }
2660
2661 /* append second string (variable) if it's found and we can turn it
2662 * into a string.
2663 */
2664 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2665 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2666 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2667 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2668
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002669 max = tmp.data.u.str.data;
2670 if (max > trash->size - 1 - trash->data)
2671 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002672
2673 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002674 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2675 max);
2676 trash->data += max;
2677 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002678 }
2679 }
2680
2681 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002682 max = arg_p[2].data.str.data;
2683 if (max > trash->size - 1 - trash->data)
2684 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002685
2686 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002687 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2688 trash->data += max;
2689 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002690 }
2691
2692 smp->data.u.str = *trash;
2693 smp->data.type = SMP_T_STR;
2694 return 1;
2695}
2696
2697/* This function checks the "concat" converter's arguments and extracts the
2698 * variable name and its scope.
2699 */
2700static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2701 const char *file, int line, char **err)
2702{
2703 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002704 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2705 memprintf(err, "failed to register variable name '%s'",
2706 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002707 return 0;
2708 }
2709 return 1;
2710}
2711
Tim Duesterhusca097c12018-04-27 21:18:45 +02002712/* compares string with a variable containing a string. Return value
2713 * is compatible with strcmp(3)'s return value.
2714 */
2715static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2716{
2717 struct sample tmp;
2718 int max, result;
2719
2720 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2721 if (arg_p[0].type != ARGT_VAR)
2722 return 0;
2723 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2724 return 0;
2725 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2726 return 0;
2727
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002728 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2729 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002730 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002731 if (smp->data.u.str.data != tmp.data.u.str.data) {
2732 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002733 result = -1;
2734 }
2735 else {
2736 result = 1;
2737 }
2738 }
2739 }
2740
2741 smp->data.u.sint = result;
2742 smp->data.type = SMP_T_SINT;
2743 return 1;
2744}
2745
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002746#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2747#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2748#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2749
2750/*
2751 * Extract the field value of an input binary sample. Takes a mandatory argument:
2752 * the protocol buffers field identifier (dotted notation) internally represented
2753 * as an array of unsigned integers and its size.
2754 * Return 1 if the field was found, 0 if not.
2755 */
2756static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2757{
2758 unsigned char *pos;
2759 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002760
2761 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002762 grpc_left = smp->data.u.str.data;
2763
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002764 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002765 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002766
2767 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2768
2769 pos += GRPC_MSG_HEADER_SZ;
2770 grpc_left -= GRPC_MSG_HEADER_SZ;
2771
2772 if (grpc_left < left)
2773 return 0;
2774
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002775 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2776 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002777
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002778 grpc_left -= grpc_msg_len;
2779 }
2780
2781 return 0;
2782}
2783
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002784static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2785{
2786 unsigned char *pos;
2787 size_t left;
2788
2789 pos = (unsigned char *)smp->data.u.str.area;
2790 left = smp->data.u.str.data;
2791
2792 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2793}
2794
2795static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2796 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002797{
2798 if (!args[1].type) {
2799 args[1].type = ARGT_SINT;
2800 args[1].data.sint = PBUF_T_BINARY;
2801 }
2802 else {
2803 int pbuf_type;
2804
2805 pbuf_type = protobuf_type(args[1].data.str.area);
2806 if (pbuf_type == -1) {
2807 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2808 return 0;
2809 }
2810
2811 args[1].type = ARGT_SINT;
2812 args[1].data.sint = pbuf_type;
2813 }
2814
2815 return 1;
2816}
2817
Tim Duesterhusca097c12018-04-27 21:18:45 +02002818/* This function checks the "strcmp" converter's arguments and extracts the
2819 * variable name and its scope.
2820 */
2821static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2822 const char *file, int line, char **err)
2823{
2824 /* Try to decode a variable. */
2825 if (vars_check_arg(&args[0], NULL))
2826 return 1;
2827
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002828 memprintf(err, "failed to register variable name '%s'",
2829 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002830 return 0;
2831}
2832
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002833/************************************************************************/
2834/* All supported sample fetch functions must be declared here */
2835/************************************************************************/
2836
2837/* force TRUE to be returned at the fetch level */
2838static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002839smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002840{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002841 if (!smp_make_rw(smp))
2842 return 0;
2843
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002844 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002846 return 1;
2847}
2848
2849/* force FALSE to be returned at the fetch level */
2850static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002851smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002852{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002853 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002854 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002855 return 1;
2856}
2857
2858/* retrieve environment variable $1 as a string */
2859static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002860smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002861{
2862 char *env;
2863
2864 if (!args || args[0].type != ARGT_STR)
2865 return 0;
2866
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002867 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002868 if (!env)
2869 return 0;
2870
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002871 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002872 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002873 smp->data.u.str.area = env;
2874 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002875 return 1;
2876}
2877
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002878/* retrieve the current local date in epoch time, and applies an optional offset
2879 * of args[0] seconds.
2880 */
2881static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002882smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002883{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002884 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002885
2886 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002887 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002888 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002889
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002890 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02002891 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2892 return 1;
2893}
2894
Etienne Carrierea792a0a2018-01-17 13:43:24 +01002895/* retrieve the current microsecond part of the date */
2896static int
2897smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
2898{
2899 smp->data.u.sint = date.tv_usec;
2900 smp->data.type = SMP_T_SINT;
2901 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2902 return 1;
2903}
2904
2905
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002906/* returns the hostname */
2907static int
2908smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
2909{
2910 smp->data.type = SMP_T_STR;
2911 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002912 smp->data.u.str.area = hostname;
2913 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01002914 return 1;
2915}
2916
Willy Tarreau0f30d262014-11-24 16:02:05 +01002917/* returns the number of processes */
2918static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002919smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002920{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002921 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002923 return 1;
2924}
2925
2926/* returns the number of the current process (between 1 and nbproc */
2927static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002928smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002929{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002930 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002931 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002932 return 1;
2933}
2934
Christopher Faulet34adb2a2017-11-21 21:45:38 +01002935/* returns the number of the current thread (between 1 and nbthread */
2936static int
2937smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
2938{
2939 smp->data.type = SMP_T_SINT;
2940 smp->data.u.sint = tid;
2941 return 1;
2942}
2943
Willy Tarreau84310e22014-02-14 11:59:04 +01002944/* generate a random 32-bit integer for whatever purpose, with an optional
2945 * range specified in argument.
2946 */
2947static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002948smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01002949{
Willy Tarreau861c4ef2020-03-08 00:42:37 +01002950 smp->data.u.sint = ha_random();
Willy Tarreau84310e22014-02-14 11:59:04 +01002951
2952 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002953 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002954 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01002955
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002956 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01002957 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
2958 return 1;
2959}
2960
Willy Tarreau0f30d262014-11-24 16:02:05 +01002961/* returns true if the current process is stopping */
2962static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002963smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01002964{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002965 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002966 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01002967 return 1;
2968}
2969
Willy Tarreau70fe9442018-11-22 16:07:39 +01002970/* returns the number of calls of the current stream's process_stream() */
2971static int
2972smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
2973{
Willy Tarreaucc746d52020-04-29 11:59:02 +02002974 if (!smp->strm)
2975 return 0;
2976
Willy Tarreau70fe9442018-11-22 16:07:39 +01002977 smp->data.type = SMP_T_SINT;
2978 smp->data.u.sint = smp->strm->task->calls;
2979 return 1;
2980}
2981
2982/* returns the average number of nanoseconds spent processing the stream per call */
2983static int
2984smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
2985{
Willy Tarreaucc746d52020-04-29 11:59:02 +02002986 if (!smp->strm)
2987 return 0;
2988
Willy Tarreau70fe9442018-11-22 16:07:39 +01002989 smp->data.type = SMP_T_SINT;
2990 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
2991 return 1;
2992}
2993
2994/* returns the total number of nanoseconds spent processing the stream */
2995static int
2996smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
2997{
Willy Tarreaucc746d52020-04-29 11:59:02 +02002998 if (!smp->strm)
2999 return 0;
3000
Willy Tarreau70fe9442018-11-22 16:07:39 +01003001 smp->data.type = SMP_T_SINT;
3002 smp->data.u.sint = smp->strm->task->cpu_time;
3003 return 1;
3004}
3005
3006/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3007static int
3008smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3009{
Willy Tarreaucc746d52020-04-29 11:59:02 +02003010 if (!smp->strm)
3011 return 0;
3012
Willy Tarreau70fe9442018-11-22 16:07:39 +01003013 smp->data.type = SMP_T_SINT;
3014 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3015 return 1;
3016}
3017
3018/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3019static int
3020smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3021{
Willy Tarreaucc746d52020-04-29 11:59:02 +02003022 if (!smp->strm)
3023 return 0;
3024
Willy Tarreau70fe9442018-11-22 16:07:39 +01003025 smp->data.type = SMP_T_SINT;
3026 smp->data.u.sint = smp->strm->task->lat_time;
3027 return 1;
3028}
3029
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003030static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3031{
3032 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003033 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003034 smp->data.u.str.area = args[0].data.str.area;
3035 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003036 return 1;
3037}
3038
3039static int smp_check_const_bool(struct arg *args, char **err)
3040{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003041 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3042 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003043 args[0].type = ARGT_SINT;
3044 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003045 return 1;
3046 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003047 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3048 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003049 args[0].type = ARGT_SINT;
3050 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003051 return 1;
3052 }
3053 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3054 return 0;
3055}
3056
3057static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3058{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003059 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003060 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003061 return 1;
3062}
3063
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003064static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003065{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003066 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003067 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003068 return 1;
3069}
3070
3071static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3072{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003073 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003074 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003075 return 1;
3076}
3077
3078static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3079{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003080 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003081 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003082 return 1;
3083}
3084
3085static int smp_check_const_bin(struct arg *args, char **err)
3086{
David Carlier64a16ab2016-04-08 10:37:02 +01003087 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003088 int binstrlen;
3089
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003090 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003091 return 0;
3092 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003093 args[0].data.str.area = binstr;
3094 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003095 return 1;
3096}
3097
3098static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3099{
3100 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003101 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003102 smp->data.u.str.area = args[0].data.str.area;
3103 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003104 return 1;
3105}
3106
3107static int smp_check_const_meth(struct arg *args, char **err)
3108{
3109 enum http_meth_t meth;
3110 int i;
3111
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003112 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003113 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003114 args[0].type = ARGT_SINT;
3115 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003116 } else {
3117 /* Check method avalaibility. A methos is a token defined as :
3118 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3119 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3120 * token = 1*tchar
3121 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003122 for (i = 0; i < args[0].data.str.data; i++) {
3123 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003124 memprintf(err, "expects valid method.");
3125 return 0;
3126 }
3127 }
3128 }
3129 return 1;
3130}
3131
3132static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3133{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003134 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003135 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003136 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003137 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 smp->data.u.meth.str.area = "";
3139 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003140 } else {
3141 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003142 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003143 smp->data.u.meth.str.area = args[0].data.str.area;
3144 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003145 }
3146 return 1;
3147}
3148
Luca Schimweg77306662019-09-10 15:42:52 +02003149// This function checks the "uuid" sample's arguments.
3150// Function won't get called when no parameter is specified (maybe a bug?)
3151static int smp_check_uuid(struct arg *args, char **err)
3152{
3153 if (!args[0].type) {
3154 args[0].type = ARGT_SINT;
3155 args[0].data.sint = 4;
3156 }
3157 else if (args[0].data.sint != 4) {
3158 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3159 return 0;
3160 }
3161
3162 return 1;
3163}
3164
3165// Generate a RFC4122 UUID (default is v4 = fully random)
3166static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3167{
3168 if (args[0].data.sint == 4 || !args[0].type) {
3169 uint32_t rnd[4] = { 0, 0, 0, 0 };
3170 uint64_t last = 0;
3171 int byte = 0;
3172 uint8_t bits = 0;
3173 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3174
3175 while (byte < 4) {
3176 while (bits < 32) {
Willy Tarreau861c4ef2020-03-08 00:42:37 +01003177 last |= (uint64_t)ha_random() << bits;
Luca Schimweg77306662019-09-10 15:42:52 +02003178 bits += rand_max_bits;
3179 }
3180 rnd[byte++] = last;
3181 last >>= 32u;
3182 bits -= 32;
3183 }
3184
3185 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3186 rnd[0],
3187 rnd[1] & 0xFFFF,
3188 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3189 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3190 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3191 );
3192
3193 smp->data.type = SMP_T_STR;
3194 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3195 smp->data.u.str = trash;
3196 return 1;
3197 }
3198
3199 // more implementations of other uuid formats possible here
3200 return 0;
3201}
3202
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003203/* Note: must not be declared <const> as its list will be overwritten.
3204 * Note: fetches that may return multiple types must be declared as the lowest
3205 * common denominator, the type that can be casted into all other ones. For
3206 * instance IPv4/IPv6 must be declared IPv4.
3207 */
3208static struct sample_fetch_kw_list smp_kws = {ILH, {
3209 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3210 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003211 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003212 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003213 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003214 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003215 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3216 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003217 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003218 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003219 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003220 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg77306662019-09-10 15:42:52 +02003221 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003222
3223 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3224 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3225 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3226 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3227 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003228
3229 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3230 { "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 +02003231 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003232 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3233 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3234 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3235 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3236
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003237 { /* END */ },
3238}};
3239
Willy Tarreau0108d902018-11-25 19:14:37 +01003240INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3241
Emeric Brun107ca302010-01-04 16:16:05 +01003242/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003243static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003244#ifdef DEBUG_EXPR
3245 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3246#endif
3247
Holger Just1bfc24b2017-05-06 00:56:53 +02003248 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003249 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003250 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3251 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003252 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003253 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003254 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003255 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003256 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3257 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003258 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003259 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003260 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3261 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3262 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003263 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3264 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003265 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003266 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003267 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3268 { "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 +01003269 { "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 +02003270 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003271 { "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 +02003272 { "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 +01003273
3274 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003275 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3276 { "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 +01003277
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003278 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3279 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3280 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3281 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3282 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3283 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3284 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3285 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3286 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3287 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3288 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3289 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3290 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3291 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003292
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003293 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003294}};
3295
Willy Tarreau0108d902018-11-25 19:14:37 +01003296INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);