blob: 24d21100e08b3cb1c9c73fba764096ef0b102085 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010035#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020036#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020037#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010038
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020039#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010040#include <import/xxhash.h>
41
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042/* sample type names */
43const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020044 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_SINT] = "sint",
47 [SMP_T_ADDR] = "addr",
48 [SMP_T_IPV4] = "ipv4",
49 [SMP_T_IPV6] = "ipv6",
50 [SMP_T_STR] = "str",
51 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020052 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020056static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010057
Willy Tarreau12785782012-04-27 21:37:17 +020058/* list head of all known sample fetch keywords */
59static struct sample_fetch_kw_list sample_fetches = {
60 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010061};
62
Willy Tarreau12785782012-04-27 21:37:17 +020063/* list head of all known sample format conversion keywords */
64static struct sample_conv_kw_list sample_convs = {
65 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010066};
67
Willy Tarreau80aca902013-01-07 15:42:20 +010068const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
69 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
115 SMP_VAL_FE_LOG_END),
116
117 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________),
132
133 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
134 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
137 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
138 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
139 SMP_VAL_FE_LOG_END),
140
141 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________),
148
149 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
152 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL_FE_LOG_END),
164
165 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL_FE_LOG_END),
172
173 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL_FE_LOG_END),
180
181 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL___________),
196
197 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
201 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL_FE_LOG_END),
204
205 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
210 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
211 SMP_VAL___________),
212
213 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220
221 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236
237 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
243 SMP_VAL_FE_LOG_END),
244};
245
246static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
247 [SMP_SRC_INTRN] = "internal state",
248 [SMP_SRC_LISTN] = "listener",
249 [SMP_SRC_FTEND] = "frontend",
250 [SMP_SRC_L4CLI] = "client address",
251 [SMP_SRC_L5CLI] = "client-side connection",
252 [SMP_SRC_TRACK] = "track counters",
253 [SMP_SRC_L6REQ] = "request buffer",
254 [SMP_SRC_HRQHV] = "HTTP request headers",
255 [SMP_SRC_HRQHP] = "HTTP request",
256 [SMP_SRC_HRQBO] = "HTTP request body",
257 [SMP_SRC_BKEND] = "backend",
258 [SMP_SRC_SERVR] = "server",
259 [SMP_SRC_L4SRV] = "server address",
260 [SMP_SRC_L5SRV] = "server-side connection",
261 [SMP_SRC_L6RES] = "response buffer",
262 [SMP_SRC_HRSHV] = "HTTP response headers",
263 [SMP_SRC_HRSHP] = "HTTP response",
264 [SMP_SRC_HRSBO] = "HTTP response body",
265 [SMP_SRC_RQFIN] = "request buffer statistics",
266 [SMP_SRC_RSFIN] = "response buffer statistics",
267 [SMP_SRC_TXFIN] = "transaction statistics",
268 [SMP_SRC_SSFIN] = "session statistics",
269};
270
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100271static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
272 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
273 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
274 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
275 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
276 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
277 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
278 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
279 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
280 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
281 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
282 [SMP_CKP_BE_SRV_CON] = "server source selection",
283 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
284 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
285 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
286 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
287 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
288 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
289 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
290 [SMP_CKP_FE_LOG_END] = "logs",
291};
292
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100293/* This function returns the type of the data returned by the sample_expr.
294 * It assumes that the <expr> and all of its converters are properly
295 * initialized.
296 */
297inline
298int smp_expr_output_type(struct sample_expr *expr)
299{
300 struct sample_conv_expr *smp_expr;
301
302 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
303 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
304 return smp_expr->conv->out_type;
305 }
306 return expr->fetch->out_type;
307}
308
309
Willy Tarreau80aca902013-01-07 15:42:20 +0100310/* fill the trash with a comma-delimited list of source names for the <use> bit
311 * field which must be composed of a non-null set of SMP_USE_* flags. The return
312 * value is the pointer to the string in the trash buffer.
313 */
314const char *sample_src_names(unsigned int use)
315{
316 int bit;
317
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200318 trash.data = 0;
319 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100320 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
321 if (!(use & ~((1 << bit) - 1)))
322 break; /* no more bits */
323
324 if (!(use & (1 << bit)))
325 continue; /* bit not set */
326
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200327 trash.data += snprintf(trash.area + trash.data,
328 trash.size - trash.data, "%s%s",
329 (use & ((1 << bit) - 1)) ? "," : "",
330 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100331 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200332 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100333}
334
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100335/* return a pointer to the correct sample checkpoint name, or "unknown" when
336 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
337 * if set.
338 */
339const char *sample_ckp_names(unsigned int use)
340{
341 int bit;
342
343 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
344 if (use & (1 << bit))
345 return fetch_ckp_names[bit];
346 return "unknown sample check place, please report this bug";
347}
348
Emeric Brun107ca302010-01-04 16:16:05 +0100349/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100350 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
351 * for next parsing sessions. The fetch keywords capabilities are also computed
352 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100353 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100354void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100355{
Willy Tarreau80aca902013-01-07 15:42:20 +0100356 struct sample_fetch *sf;
357 int bit;
358
359 for (sf = kwl->kw; sf->kw != NULL; sf++) {
360 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
361 if (sf->use & (1 << bit))
362 sf->val |= fetch_cap[bit];
363 }
364 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * parsing sessions.
370 */
Willy Tarreau12785782012-04-27 21:37:17 +0200371void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100372{
Willy Tarreau12785782012-04-27 21:37:17 +0200373 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100374}
375
376/*
Willy Tarreau12785782012-04-27 21:37:17 +0200377 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100378 * string of <len> in buffer <kw>.
379 *
380 */
Willy Tarreau12785782012-04-27 21:37:17 +0200381struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100382{
383 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200384 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100385
Willy Tarreau12785782012-04-27 21:37:17 +0200386 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100387 for (index = 0; kwl->kw[index].kw != NULL; index++) {
388 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
389 kwl->kw[index].kw[len] == '\0')
390 return &kwl->kw[index];
391 }
392 }
393 return NULL;
394}
395
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100396/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100397 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398 * <idx> is the index of the next sample fetch entry. It is used as private
399 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100400 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100401 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 * the list is reached.
403 */
404struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
405{
406 struct sample_fetch_kw_list *kwl;
407 struct sample_fetch *base;
408
409 if (!current) {
410 /* Get first kwl entry. */
411 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
412 (*idx) = 0;
413 } else {
414 /* Get kwl corresponding to the curret entry. */
415 base = current + 1 - (*idx);
416 kwl = container_of(base, struct sample_fetch_kw_list, kw);
417 }
418
419 while (1) {
420
421 /* Check if kwl is the last entry. */
422 if (&kwl->list == &sample_fetches.list)
423 return NULL;
424
425 /* idx contain the next keyword. If it is available, return it. */
426 if (kwl->kw[*idx].kw) {
427 (*idx)++;
428 return &kwl->kw[(*idx)-1];
429 }
430
431 /* get next entry in the main list, and return NULL if the end is reached. */
432 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
433
434 /* Set index to 0, ans do one other loop. */
435 (*idx) = 0;
436 }
437}
438
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100439/* This function browses the list of available converters. <current> is
440 * the last used converter. If it is the first call, it must set to NULL.
441 * <idx> is the index of the next converter entry. It is used as private
442 * value. It is useless to initiate it.
443 *
444 * It returns always the next sample_conv entry, and NULL when the end of
445 * the list is reached.
446 */
447struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
448{
449 struct sample_conv_kw_list *kwl;
450 struct sample_conv *base;
451
452 if (!current) {
453 /* Get first kwl entry. */
454 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
455 (*idx) = 0;
456 } else {
457 /* Get kwl corresponding to the curret entry. */
458 base = current + 1 - (*idx);
459 kwl = container_of(base, struct sample_conv_kw_list, kw);
460 }
461
462 while (1) {
463 /* Check if kwl is the last entry. */
464 if (&kwl->list == &sample_convs.list)
465 return NULL;
466
467 /* idx contain the next keyword. If it is available, return it. */
468 if (kwl->kw[*idx].kw) {
469 (*idx)++;
470 return &kwl->kw[(*idx)-1];
471 }
472
473 /* get next entry in the main list, and return NULL if the end is reached. */
474 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
475
476 /* Set index to 0, ans do one other loop. */
477 (*idx) = 0;
478 }
479}
480
Emeric Brun107ca302010-01-04 16:16:05 +0100481/*
Willy Tarreau12785782012-04-27 21:37:17 +0200482 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100483 * string of <len> in buffer <kw>.
484 *
485 */
Willy Tarreau12785782012-04-27 21:37:17 +0200486struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100487{
488 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200489 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100490
Willy Tarreau12785782012-04-27 21:37:17 +0200491 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100492 for (index = 0; kwl->kw[index].kw != NULL; index++) {
493 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
494 kwl->kw[index].kw[len] == '\0')
495 return &kwl->kw[index];
496 }
497 }
498 return NULL;
499}
500
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200502/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100503/******************************************************************/
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100506{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200508 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100509 return 1;
510}
511
Willy Tarreau342acb42012-04-23 22:03:39 +0200512static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100513{
Willy Tarreau83061a82018-07-13 11:56:34 +0200514 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100517 return 0;
518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200521 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100522 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100523
524 return 1;
525}
526
Willy Tarreau342acb42012-04-23 22:03:39 +0200527static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100528{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200529 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100531 return 1;
532}
533
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534static int c_ipv62ip(struct sample *smp)
535{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200536 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100538 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539 return 1;
540}
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100543{
Willy Tarreau83061a82018-07-13 11:56:34 +0200544 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100545
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100547 return 0;
548
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200551 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100552 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100553 return 1;
554}
555
556/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200557static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100558{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100560}
561*/
562
Willy Tarreau342acb42012-04-23 22:03:39 +0200563static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100567 return 1;
568}
569
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570static int c_int2ipv6(struct sample *smp)
571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
573 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 1;
576}
577
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100578static int c_str2addr(struct sample *smp)
579{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
581 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200583 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100584 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100585 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100586 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100589 return 1;
590}
591
Willy Tarreau342acb42012-04-23 22:03:39 +0200592static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100593{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200594 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100595 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200596 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100597 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100598 return 1;
599}
600
Willy Tarreau342acb42012-04-23 22:03:39 +0200601static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100602{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100604 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100606 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100607 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100608}
609
Emeric Brun4b9e8022014-11-03 18:17:10 +0100610/*
611 * The NULL char always enforces the end of string if it is met.
612 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100613 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614static int c_bin2str(struct sample *smp)
615{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100616 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200617
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 for (i = 0; i < smp->data.u.str.data; i++) {
619 if (!smp->data.u.str.area[i]) {
620 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100621 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200624 return 1;
625}
626
Willy Tarreau342acb42012-04-23 22:03:39 +0200627static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100628{
Willy Tarreau83061a82018-07-13 11:56:34 +0200629 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100630 char *pos;
631
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100633 if (!pos)
634 return 0;
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 trash->size = trash->size - (pos - trash->area);
637 trash->area = pos;
638 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200639 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100641 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100642 return 1;
643}
644
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800645/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200646 * For strings and binary blocks, it also provides a known allocated size with
647 * a length that is capped to the size, and ensures a trailing zero is always
648 * appended for strings. This is necessary for some operations which may
649 * require to extend the length. It returns 0 if it fails, 1 on success.
650 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200652{
Willy Tarreau83061a82018-07-13 11:56:34 +0200653 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200654
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200655 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_SINT:
658 case SMP_T_ADDR:
659 case SMP_T_IPV4:
660 case SMP_T_IPV6:
661 /* These type are not const. */
662 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200663
Christopher Fauletec100512017-07-24 15:38:41 +0200664 case SMP_T_METH:
665 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
666 break;
667 /* Fall through */
668
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100669 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200670 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100671 trash->data = smp->data.type == SMP_T_STR ?
672 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 if (trash->data > trash->size - 1)
674 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200675
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100676 memcpy(trash->area, smp->data.type == SMP_T_STR ?
677 smp->data.u.str.area : smp->data.u.meth.str.area,
678 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200680 smp->data.u.str = *trash;
681 break;
682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 trash->data = smp->data.u.str.data;
686 if (trash->data > trash->size)
687 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200688
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200690 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 default:
694 /* Other cases are unexpected. */
695 return 0;
696 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100697
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 /* remove const flag */
699 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200700 return 1;
701}
702
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100703int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
705 return 1;
706}
707
Willy Tarreau342acb42012-04-23 22:03:39 +0200708static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100709{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200710 const char *str;
711 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100712
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100714 return 0;
715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 str = smp->data.u.str.area;
717 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200719 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200720 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100722 return 1;
723}
724
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725static int c_str2meth(struct sample *smp)
726{
727 enum http_meth_t meth;
728 int len;
729
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100731 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 len = smp->data.u.str.data;
733 smp->data.u.meth.str.area = smp->data.u.str.area;
734 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 }
736 else
737 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200739 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100740 return 1;
741}
742
743static int c_meth2str(struct sample *smp)
744{
745 int len;
746 enum http_meth_t meth;
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 len = smp->data.u.meth.str.data;
751 smp->data.u.str.area = smp->data.u.meth.str.area;
752 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200755 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200758 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200761 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 }
763 else {
764 /* Unknown method */
765 return 0;
766 }
767 return 1;
768}
769
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770static int c_addr2bin(struct sample *smp)
771{
Willy Tarreau83061a82018-07-13 11:56:34 +0200772 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 chk->data = 4;
776 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 chk->data = 16;
780 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 }
782 else
783 return 0;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200786 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787 return 1;
788}
789
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200790static int c_int2bin(struct sample *smp)
791{
Willy Tarreau83061a82018-07-13 11:56:34 +0200792 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
795 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799 return 1;
800}
801
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200802
Emeric Brun107ca302010-01-04 16:16:05 +0100803/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200804/* Sample casts matrix: */
805/* sample_casts[from type][to type] */
806/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100807/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100808
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100809sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200810/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
811/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
812/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200813/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200814/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
815/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200816/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200817/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
818/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
819/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200820};
Emeric Brun107ca302010-01-04 16:16:05 +0100821
Emeric Brun107ca302010-01-04 16:16:05 +0100822/*
Willy Tarreau12785782012-04-27 21:37:17 +0200823 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100824 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200825 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200826 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100827 * If <endptr> is non-nul, it will be set to the first unparsed character
828 * (which may be the final '\0') on success. If it is nul, the expression
829 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100830 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100831struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
Emeric Brun107ca302010-01-04 16:16:05 +0100832{
Willy Tarreau833cc792013-07-24 15:34:19 +0200833 const char *begw; /* beginning of word */
834 const char *endw; /* end of word */
835 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200836 struct sample_expr *expr;
837 struct sample_fetch *fetch;
838 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100839 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 char *fkw = NULL;
841 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100842 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100843
Willy Tarreau833cc792013-07-24 15:34:19 +0200844 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100845 for (endw = begw; is_idchar(*endw); endw++)
846 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200847
848 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100849 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100850 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200851 }
Emeric Brun107ca302010-01-04 16:16:05 +0100852
Willy Tarreau833cc792013-07-24 15:34:19 +0200853 /* keep a copy of the current fetch keyword for error reporting */
854 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100855
Willy Tarreau833cc792013-07-24 15:34:19 +0200856 fetch = find_sample_fetch(begw, endw - begw);
857 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100858 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100859 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200860 }
Emeric Brun107ca302010-01-04 16:16:05 +0100861
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 /* At this point, we have :
863 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100864 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 */
866
867 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100868 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100869 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200870 }
Emeric Brun107ca302010-01-04 16:16:05 +0100871 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200872
Vincent Bernat02779b62016-04-03 13:48:43 +0200873 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200874 if (!expr)
875 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100876
877 LIST_INIT(&(expr->conv_exprs));
878 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200879 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100880
Willy Tarreau689a1df2013-12-13 00:40:11 +0100881 /* Note that we call the argument parser even with an empty string,
882 * this allows it to automatically create entries for mandatory
883 * implicit arguments (eg: local proxy name).
884 */
885 al->kw = expr->fetch->kw;
886 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100887 if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100888 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
889 goto out_error;
890 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200891
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100892 /* now endt is our first char not part of the arg list, typically the
893 * comma after the sample fetch name or after the closing parenthesis,
894 * or the NUL char.
895 */
896
Willy Tarreau689a1df2013-12-13 00:40:11 +0100897 if (!expr->arg_p) {
898 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200899 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100900 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
901 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200902 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100903 }
904
Willy Tarreau833cc792013-07-24 15:34:19 +0200905 /* Now process the converters if any. We have two supported syntaxes
906 * for the converters, which can be combined :
907 * - comma-delimited list of converters just after the keyword and args ;
908 * - one converter per keyword
909 * The combination allows to have each keyword being a comma-delimited
910 * series of converters.
911 *
912 * We want to process the former first, then the latter. For this we start
913 * from the beginning of the supposed place in the exiting conv chain, which
914 * starts at the last comma (endt).
915 */
916
917 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200918 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100919 int err_arg;
920 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100921
Willy Tarreau833cc792013-07-24 15:34:19 +0200922 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100923 if (endptr) {
924 /* end found, let's stop here */
925 break;
926 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200927 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100928 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200929 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100930 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100931 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200932 }
Emeric Brun107ca302010-01-04 16:16:05 +0100933
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100934 /* FIXME: how long should we support such idiocies ? Maybe we
935 * should already warn ?
936 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200937 while (*endt == ',') /* then trailing commas */
938 endt++;
939
Willy Tarreau97108e02016-11-25 07:33:24 +0100940 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200941
942 if (!*begw) {
943 /* none ? skip to next string */
944 (*idx)++;
945 begw = str[*idx];
946 if (!begw || !*begw)
947 break;
948 }
949
Willy Tarreaued2c6622020-02-14 18:27:10 +0100950 for (endw = begw; is_idchar(*endw); endw++)
951 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200952
953 free(ckw);
954 ckw = my_strndup(begw, endw - begw);
955
956 conv = find_sample_conv(begw, endw - begw);
957 if (!conv) {
958 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100959 if (begw == str[*idx]) {
960 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200961 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100962 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100963 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200964 goto out_error;
965 }
Emeric Brun107ca302010-01-04 16:16:05 +0100966
Willy Tarreau833cc792013-07-24 15:34:19 +0200967 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100968 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100969 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200970 }
Emeric Brun107ca302010-01-04 16:16:05 +0100971
972 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200973 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100974 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100975 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200976 }
Emeric Brun107ca302010-01-04 16:16:05 +0100977
978 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200979 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200980 if (!conv_expr)
981 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100982
983 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
984 conv_expr->conv = conv;
985
Willy Tarreau46dfd782019-12-17 10:25:29 +0100986 al->kw = expr->fetch->kw;
987 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100988 argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
Willy Tarreau46dfd782019-12-17 10:25:29 +0100989 if (argcnt < 0) {
990 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
991 goto out_error;
992 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100993
Willy Tarreau46dfd782019-12-17 10:25:29 +0100994 if (argcnt && !conv->arg_mask) {
995 memprintf(err_msg, "converter '%s' does not support any args", ckw);
996 goto out_error;
997 }
Willy Tarreau21d68a62012-04-20 15:52:36 +0200998
Willy Tarreau46dfd782019-12-17 10:25:29 +0100999 if (!conv_expr->arg_p)
1000 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001001
Willy Tarreau46dfd782019-12-17 10:25:29 +01001002 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1003 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001004 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001005 }
1006 }
Emeric Brun485479d2010-09-23 18:02:19 +02001007
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001008 if (endptr) {
1009 /* end found, let's stop here */
1010 *endptr = (char *)endt;
1011 }
1012
Willy Tarreau833cc792013-07-24 15:34:19 +02001013 out:
1014 free(fkw);
1015 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001016 return expr;
1017
1018out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001019 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001020 expr = NULL;
1021 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001022}
1023
1024/*
Willy Tarreau12785782012-04-27 21:37:17 +02001025 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001026 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001027 * Returns a pointer on a typed sample structure containing the result or NULL if
1028 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001029 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001030 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001031 *
1032 * Note: the fetch functions are required to properly set the return type. The
1033 * conversion functions must do so too. However the cast functions do not need
1034 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001035 *
1036 * The caller may indicate in <opt> if it considers the result final or not.
1037 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1038 * if the result is stable or not, according to the following table :
1039 *
1040 * return MAY_CHANGE FINAL Meaning for the sample
1041 * NULL 0 * Not present and will never be (eg: header)
1042 * NULL 1 0 Not present yet, could change (eg: POST param)
1043 * NULL 1 1 Not present yet, will not change anymore
1044 * smp 0 * Present and will not change (eg: header)
1045 * smp 1 0 Present, may change (eg: request length)
1046 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001047 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001048struct sample *sample_process(struct proxy *px, struct session *sess,
1049 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001050 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001051{
Willy Tarreau12785782012-04-27 21:37:17 +02001052 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001053
Willy Tarreau18387e22013-07-25 12:02:38 +02001054 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001055 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001056 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001057 }
Emeric Brun107ca302010-01-04 16:16:05 +01001058
Willy Tarreau1777ea62016-03-10 16:15:46 +01001059 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001060 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001061 return NULL;
1062
Emeric Brun107ca302010-01-04 16:16:05 +01001063 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001064 /* we want to ensure that p->type can be casted into
1065 * conv_expr->conv->in_type. We have 3 possibilities :
1066 * - NULL => not castable.
1067 * - c_none => nothing to do (let's optimize it)
1068 * - other => apply cast and prepare to fail
1069 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001070 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001071 return NULL;
1072
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001073 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1074 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001075 return NULL;
1076
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 /* OK cast succeeded */
1078
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001079 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001080 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001081 }
1082 return p;
1083}
1084
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001085/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001086 * Resolve all remaining arguments in proxy <p>. Returns the number of
1087 * errors or 0 if everything is fine.
1088 */
1089int smp_resolve_args(struct proxy *p)
1090{
1091 struct arg_list *cur, *bak;
1092 const char *ctx, *where;
1093 const char *conv_ctx, *conv_pre, *conv_pos;
1094 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001095 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001096 struct arg *arg;
1097 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001098 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001099
1100 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1101 struct proxy *px;
1102 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001103 struct stktable *t;
1104 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001105 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001106
1107 arg = cur->arg;
1108
1109 /* prepare output messages */
1110 conv_pre = conv_pos = conv_ctx = "";
1111 if (cur->conv) {
1112 conv_ctx = cur->conv;
1113 conv_pre = "conversion keyword '";
1114 conv_pos = "' for ";
1115 }
1116
1117 where = "in";
1118 ctx = "sample fetch keyword";
1119 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001120 case ARGC_STK: where = "in stick rule in"; break;
1121 case ARGC_TRK: where = "in tracking rule in"; break;
1122 case ARGC_LOG: where = "in log-format string in"; break;
1123 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1124 case ARGC_HRQ: where = "in http-request header format string in"; break;
1125 case ARGC_HRS: where = "in http-response header format string in"; break;
1126 case ARGC_UIF: where = "in unique-id-format string in"; break;
1127 case ARGC_RDR: where = "in redirect format string in"; break;
1128 case ARGC_CAP: where = "in capture rule in"; break;
1129 case ARGC_ACL: ctx = "ACL keyword"; break;
1130 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001131 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001132 }
1133
1134 /* set a few default settings */
1135 px = p;
1136 pname = p->id;
1137
1138 switch (arg->type) {
1139 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001140 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001141 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1142 cur->file, cur->line,
1143 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001144 cfgerr++;
1145 continue;
1146 }
1147
1148 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001149 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001150
1151 if (sname) {
1152 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001153 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001154
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001155 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001157 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1158 cur->file, cur->line, pname,
1159 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160 cfgerr++;
1161 break;
1162 }
1163 }
1164 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001165 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001166
1167 srv = findserver(px, sname);
1168 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001169 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",
1170 cur->file, cur->line, sname, pname,
1171 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172 cfgerr++;
1173 break;
1174 }
1175
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001176 free(arg->data.str.area);
1177 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001178 arg->unresolved = 0;
1179 arg->data.srv = srv;
1180 break;
1181
1182 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001183 if (arg->data.str.data) {
1184 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001185 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 }
1187
1188 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001189 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1190 cur->file, cur->line, pname,
1191 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001192 cfgerr++;
1193 break;
1194 }
1195
1196 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 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",
1198 cur->file, cur->line, pname,
1199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001204 free(arg->data.str.area);
1205 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 arg->unresolved = 0;
1207 arg->data.prx = px;
1208 break;
1209
1210 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001211 if (arg->data.str.data) {
1212 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001213 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 }
1215
1216 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001217 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1218 cur->file, cur->line, pname,
1219 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 cfgerr++;
1221 break;
1222 }
1223
1224 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 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",
1226 cur->file, cur->line, pname,
1227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228 cfgerr++;
1229 break;
1230 }
1231
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001232 free(arg->data.str.area);
1233 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001234 arg->unresolved = 0;
1235 arg->data.prx = px;
1236 break;
1237
1238 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001239 if (arg->data.str.data)
1240 stktname = arg->data.str.area;
1241 else
1242 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001243
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001244 t = stktable_find_by_name(stktname);
1245 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001246 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 +01001247 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001248 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249 cfgerr++;
1250 break;
1251 }
1252
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001253 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 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 +01001255 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001256 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001257 cfgerr++;
1258 break;
1259 }
1260
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001261 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001262 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 +01001263 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001264 cfgerr++;
1265 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001266 }
1267
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001268 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001269 p->next_stkt_ref = t->proxies_list;
1270 t->proxies_list = p;
1271 }
1272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001273 free(arg->data.str.area);
1274 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001275 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001276 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001277 break;
1278
1279 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001280 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001281 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1282 cur->file, cur->line,
1283 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001284 cfgerr++;
1285 break;
1286 }
1287
1288 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001289 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 ul = p->uri_auth->userlist;
1291 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001292 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001293
1294 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001295 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 +02001296 cur->file, cur->line,
1297 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001298 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299 cfgerr++;
1300 break;
1301 }
1302
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001303 free(arg->data.str.area);
1304 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001305 arg->unresolved = 0;
1306 arg->data.usr = ul;
1307 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001308
1309 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001310 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001311 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1312 cur->file, cur->line,
1313 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001314 cfgerr++;
1315 continue;
1316 }
1317
Willy Tarreau46947782015-01-19 19:00:58 +01001318 rflags = 0;
1319 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1320 err = NULL;
1321
Dragan Dosen26743032019-04-30 15:54:36 +02001322 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001323 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1324 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001325 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001326 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001327 cfgerr++;
1328 continue;
1329 }
1330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 free(arg->data.str.area);
1332 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001333 arg->unresolved = 0;
1334 arg->data.reg = reg;
1335 break;
1336
1337
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001338 }
1339
1340 LIST_DEL(&cur->list);
1341 free(cur);
1342 } /* end of args processing */
1343
1344 return cfgerr;
1345}
1346
1347/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001348 * Process a fetch + format conversion as defined by the sample expression
1349 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001350 * not explicitly set to <smp_type>, but shall be compatible with it as
1351 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1352 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001353 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1354 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1355 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1356 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001357 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1358 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1359 * still hope to get it after waiting longer, and is not converted to string.
1360 * The possible output combinations are the following :
1361 *
1362 * return MAY_CHANGE FINAL Meaning for the sample
1363 * NULL * * Not present and will never be (eg: header)
1364 * smp 0 * Final value converted (eg: header)
1365 * smp 1 0 Not present yet, may appear later (eg: header)
1366 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001367 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001368struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001369 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001370 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001371{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001372 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373
Willy Tarreau6c616e02014-06-25 16:56:41 +02001374 memset(smp, 0, sizeof(*smp));
1375
Willy Tarreau192252e2015-04-04 01:47:55 +02001376 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001377 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1378 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001380 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001382 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383 return NULL;
1384
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001385 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001386 return NULL;
1387
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001388 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return smp;
1390}
1391
Christopher Faulet476e5d02016-10-26 11:34:47 +02001392static void release_sample_arg(struct arg *p)
1393{
1394 struct arg *p_back = p;
1395
1396 if (!p)
1397 return;
1398
1399 while (p->type != ARGT_STOP) {
1400 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001401 free(p->data.str.area);
1402 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001403 p->unresolved = 0;
1404 }
1405 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001406 regex_free(p->data.reg);
1407 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001408 }
1409 p++;
1410 }
1411
1412 if (p_back != empty_arg_list)
1413 free(p_back);
1414}
1415
1416void release_sample_expr(struct sample_expr *expr)
1417{
1418 struct sample_conv_expr *conv_expr, *conv_exprb;
1419
1420 if (!expr)
1421 return;
1422
1423 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1424 release_sample_arg(conv_expr->arg_p);
1425 release_sample_arg(expr->arg_p);
1426 free(expr);
1427}
1428
Emeric Brun107ca302010-01-04 16:16:05 +01001429/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001430/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001431/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001432/*****************************************************************/
1433
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001434static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1435{
1436 int i;
1437 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001438 struct buffer *buf;
1439 struct sink *sink;
1440 struct ist line;
1441 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001442
Willy Tarreau0851fd52019-12-17 10:07:25 +01001443 buf = alloc_trash_chunk();
1444 if (!buf)
1445 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001446
Willy Tarreau0851fd52019-12-17 10:07:25 +01001447 sink = (struct sink *)arg_p[1].data.str.area;
1448 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001449
Willy Tarreau0851fd52019-12-17 10:07:25 +01001450 pfx = arg_p[0].data.str.area;
1451 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452
Willy Tarreau0851fd52019-12-17 10:07:25 +01001453 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1454 if (!sample_casts[smp->data.type][SMP_T_STR])
1455 goto nocast;
1456
1457 /* Copy sample fetch. This puts the sample as const, the
1458 * cast will copy data if a transformation is required.
1459 */
1460 memcpy(&tmp, smp, sizeof(struct sample));
1461 tmp.flags = SMP_F_CONST;
1462
1463 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1464 goto nocast;
1465
1466 /* Display the displayable chars*. */
1467 b_putchr(buf, '<');
1468 for (i = 0; i < tmp.data.u.str.data; i++) {
1469 if (isprint(tmp.data.u.str.area[i]))
1470 b_putchr(buf, tmp.data.u.str.area[i]);
1471 else
1472 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001473 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001474 b_putchr(buf, '>');
1475
1476 done:
1477 line = ist2(buf->area, buf->data);
1478 sink_write(sink, &line, 1);
1479 end:
1480 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001481 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001482 nocast:
1483 chunk_appendf(buf, "(undisplayable)");
1484 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001485}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001486
1487// This function checks the "debug" converter's arguments.
1488static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1489 const char *file, int line, char **err)
1490{
1491 const char *name = "buf0";
1492 struct sink *sink = NULL;
1493
1494 if (args[0].type != ARGT_STR) {
1495 /* optional prefix */
1496 args[0].data.str.area = "";
1497 args[0].data.str.data = 0;
1498 }
1499
1500 if (args[1].type == ARGT_STR)
1501 name = args[1].data.str.area;
1502
1503 sink = sink_find(name);
1504 if (!sink) {
1505 memprintf(err, "No such sink '%s'", name);
1506 return 0;
1507 }
1508
1509 args[1].data.str.area = (char *)sink;
1510 args[1].data.str.data = 0; // that's not a string anymore
1511 return 1;
1512}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001513
Holger Just1bfc24b2017-05-06 00:56:53 +02001514static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1515{
Willy Tarreau83061a82018-07-13 11:56:34 +02001516 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001517 int bin_len;
1518
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001519 trash->data = 0;
1520 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1521 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001522 if (bin_len < 0)
1523 return 0;
1524
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001525 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001526 smp->data.u.str = *trash;
1527 smp->data.type = SMP_T_BIN;
1528 smp->flags &= ~SMP_F_CONST;
1529 return 1;
1530}
1531
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001532static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001533{
Willy Tarreau83061a82018-07-13 11:56:34 +02001534 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001535 int b64_len;
1536
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001537 trash->data = 0;
1538 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1539 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001540 if (b64_len < 0)
1541 return 0;
1542
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001543 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001544 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001545 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001546 smp->flags &= ~SMP_F_CONST;
1547 return 1;
1548}
1549
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001550static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1551{
1552 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001553 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001554
1555 memset(&ctx, 0, sizeof(ctx));
1556
1557 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001558 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1559 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001560
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001561 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001562 smp->data.u.str = *trash;
1563 smp->data.type = SMP_T_BIN;
1564 smp->flags &= ~SMP_F_CONST;
1565 return 1;
1566}
1567
Tim Duesterhusd4376302019-06-17 12:41:44 +02001568#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001569static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1570 const char *file, int line, char **err)
1571{
1572 if (args[0].type == ARGT_STOP)
1573 return 1;
1574 if (args[0].type != ARGT_SINT) {
1575 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1576 return 0;
1577 }
1578
1579 switch (args[0].data.sint) {
1580 case 224:
1581 case 256:
1582 case 384:
1583 case 512:
1584 /* this is okay */
1585 return 1;
1586 default:
1587 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1588 return 0;
1589 }
1590}
1591
Tim Duesterhusd4376302019-06-17 12:41:44 +02001592static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1593{
1594 struct buffer *trash = get_trash_chunk();
1595 int bits = 256;
1596 if (arg_p && arg_p->data.sint)
1597 bits = arg_p->data.sint;
1598
1599 switch (bits) {
1600 case 224: {
1601 SHA256_CTX ctx;
1602
1603 memset(&ctx, 0, sizeof(ctx));
1604
1605 SHA224_Init(&ctx);
1606 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1607 SHA224_Final((unsigned char *) trash->area, &ctx);
1608 trash->data = SHA224_DIGEST_LENGTH;
1609 break;
1610 }
1611 case 256: {
1612 SHA256_CTX ctx;
1613
1614 memset(&ctx, 0, sizeof(ctx));
1615
1616 SHA256_Init(&ctx);
1617 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1618 SHA256_Final((unsigned char *) trash->area, &ctx);
1619 trash->data = SHA256_DIGEST_LENGTH;
1620 break;
1621 }
1622 case 384: {
1623 SHA512_CTX ctx;
1624
1625 memset(&ctx, 0, sizeof(ctx));
1626
1627 SHA384_Init(&ctx);
1628 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1629 SHA384_Final((unsigned char *) trash->area, &ctx);
1630 trash->data = SHA384_DIGEST_LENGTH;
1631 break;
1632 }
1633 case 512: {
1634 SHA512_CTX ctx;
1635
1636 memset(&ctx, 0, sizeof(ctx));
1637
1638 SHA512_Init(&ctx);
1639 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1640 SHA512_Final((unsigned char *) trash->area, &ctx);
1641 trash->data = SHA512_DIGEST_LENGTH;
1642 break;
1643 }
1644 default:
1645 return 0;
1646 }
1647
1648 smp->data.u.str = *trash;
1649 smp->data.type = SMP_T_BIN;
1650 smp->flags &= ~SMP_F_CONST;
1651 return 1;
1652}
1653#endif
1654
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001655static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001656{
Willy Tarreau83061a82018-07-13 11:56:34 +02001657 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001658 unsigned char c;
1659 int ptr = 0;
1660
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001661 trash->data = 0;
1662 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1663 c = smp->data.u.str.area[ptr++];
1664 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1665 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001666 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001667 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001668 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001669 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001670 return 1;
1671}
1672
Dragan Dosen3f957b22017-10-24 09:27:34 +02001673static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1674{
1675 long long int n = 0;
1676 int i, c;
1677
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001678 for (i = 0; i < smp->data.u.str.data; i++) {
1679 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001680 return 0;
1681 n = (n << 4) + c;
1682 }
1683
1684 smp->data.u.sint = n;
1685 smp->data.type = SMP_T_SINT;
1686 smp->flags &= ~SMP_F_CONST;
1687 return 1;
1688}
1689
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001690/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001691static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001692{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001693 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1694 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001695 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001696 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001697 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001698 return 1;
1699}
1700
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001701static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001702{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001703 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001704 smp->data.u.sint = i;
1705 smp->data.type = SMP_T_SINT;
1706 return 1;
1707}
1708
1709
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001710static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001711{
1712 int i;
1713
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001714 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001715 return 0;
1716
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001717 for (i = 0; i < smp->data.u.str.data; i++) {
1718 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1719 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001720 }
1721 return 1;
1722}
1723
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001724static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001725{
1726 int i;
1727
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001728 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001729 return 0;
1730
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001731 for (i = 0; i < smp->data.u.str.data; i++) {
1732 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1733 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001734 }
1735 return 1;
1736}
1737
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001738/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1739static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001740{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001741 /* Attempt to convert to IPv4 to apply the correct mask. */
1742 c_ipv62ip(smp);
1743
1744 if (smp->data.type == SMP_T_IPV4) {
1745 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1746 smp->data.type = SMP_T_IPV4;
1747 }
1748 else if (smp->data.type == SMP_T_IPV6) {
1749 /* IPv6 cannot be converted without an IPv6 mask. */
1750 if (args[1].type != ARGT_IPV6)
1751 return 0;
1752
1753 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1754 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1755 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1756 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1757 smp->data.type = SMP_T_IPV6;
1758 }
1759
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001760 return 1;
1761}
1762
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001763/* takes an UINT value on input supposed to represent the time since EPOCH,
1764 * adds an optional offset found in args[1] and emits a string representing
1765 * the local time in the format specified in args[1] using strftime().
1766 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001767static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001768{
Willy Tarreau83061a82018-07-13 11:56:34 +02001769 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001770 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001771 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001772 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001773
1774 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001775 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001776 curr_date += args[1].data.sint;
1777
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001778 tm = localtime(&curr_date);
1779 if (!tm)
1780 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001781 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001782 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1783 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001784 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001785 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001786 return 1;
1787}
1788
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001789/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001790static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001791{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001792 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1793 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001794 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001795 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001796 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001797 return 1;
1798}
1799
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001800/* takes an UINT value on input supposed to represent the time since EPOCH,
1801 * adds an optional offset found in args[1] and emits a string representing
1802 * the UTC date in the format specified in args[1] using strftime().
1803 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001804static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001805{
Willy Tarreau83061a82018-07-13 11:56:34 +02001806 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001807 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001808 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001809 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001810
1811 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001812 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001813 curr_date += args[1].data.sint;
1814
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001815 tm = gmtime(&curr_date);
1816 if (!tm)
1817 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001818 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001819 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1820 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001821 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001822 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001823 return 1;
1824}
1825
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001826/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001827static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001828{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001829 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1830 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001831 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001832 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001833 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001834 return 1;
1835}
1836
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001837/* hashes the binary input into a 32-bit unsigned int using xxh.
1838 * The seed of the hash defaults to 0 but can be changd in argument 1.
1839 */
1840static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1841{
1842 unsigned int seed;
1843
1844 if (arg_p && arg_p->data.sint)
1845 seed = arg_p->data.sint;
1846 else
1847 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001848 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1849 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001850 smp->data.type = SMP_T_SINT;
1851 return 1;
1852}
1853
1854/* hashes the binary input into a 64-bit unsigned int using xxh.
1855 * In fact, the function returns a 64 bit unsigned, but the sample
1856 * storage of haproxy only proposes 64-bits signed, so the value is
1857 * cast as signed. This cast doesn't impact the hash repartition.
1858 * The seed of the hash defaults to 0 but can be changd in argument 1.
1859 */
1860static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1861{
1862 unsigned long long int seed;
1863
1864 if (arg_p && arg_p->data.sint)
1865 seed = (unsigned long long int)arg_p->data.sint;
1866 else
1867 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001868 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1869 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001870 smp->data.type = SMP_T_SINT;
1871 return 1;
1872}
1873
Willy Tarreau80599772015-01-20 19:35:24 +01001874/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001875static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001876{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001877 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1878 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001879 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001880 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001881 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001882 return 1;
1883}
1884
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001885/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1886static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1887{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001888 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1889 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001890 if (arg_p && arg_p->data.sint)
1891 smp->data.u.sint = full_hash(smp->data.u.sint);
1892 smp->data.type = SMP_T_SINT;
1893 return 1;
1894}
1895
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001896/* This function escape special json characters. The returned string can be
1897 * safely set between two '"' and used as json string. The json string is
1898 * defined like this:
1899 *
1900 * any Unicode character except '"' or '\' or control character
1901 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1902 *
1903 * The enum input_type contain all the allowed mode for decoding the input
1904 * string.
1905 */
1906enum input_type {
1907 IT_ASCII = 0,
1908 IT_UTF8,
1909 IT_UTF8S,
1910 IT_UTF8P,
1911 IT_UTF8PS,
1912};
1913static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1914 const char *file, int line, char **err)
1915{
1916 if (!arg) {
1917 memprintf(err, "Unexpected empty arg list");
1918 return 0;
1919 }
1920
1921 if (arg->type != ARGT_STR) {
1922 memprintf(err, "Unexpected arg type");
1923 return 0;
1924 }
1925
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001926 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001927 arg->type = ARGT_SINT;
1928 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001929 return 1;
1930 }
1931
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001932 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001933 arg->type = ARGT_SINT;
1934 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001935 return 1;
1936 }
1937
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001938 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001939 arg->type = ARGT_SINT;
1940 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001941 return 1;
1942 }
1943
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001944 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001945 arg->type = ARGT_SINT;
1946 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001947 return 1;
1948 }
1949
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001950 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001951 arg->type = ARGT_SINT;
1952 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001953 return 1;
1954 }
1955
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001956 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001957 arg->type = ARGT_SINT;
1958 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001959 return 1;
1960 }
1961
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001962 memprintf(err, "Unexpected input code type. "
1963 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001964 return 0;
1965}
1966
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001967static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001968{
Willy Tarreau83061a82018-07-13 11:56:34 +02001969 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001970 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1971 const char *str;
1972 int len;
1973 enum input_type input_type = IT_ASCII;
1974 unsigned int c;
1975 unsigned int ret;
1976 char *p;
1977
1978 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001979 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001980
1981 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001982 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001983
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001984 p = smp->data.u.str.area;
1985 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001986
1987 if (input_type == IT_ASCII) {
1988 /* Read input as ASCII. */
1989 c = *(unsigned char *)p;
1990 p++;
1991 }
1992 else {
1993 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001994 ret = utf8_next(p,
1995 smp->data.u.str.data - ( p - smp->data.u.str.area),
1996 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001997 p += utf8_return_length(ret);
1998
1999 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2000 return 0;
2001 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2002 continue;
2003 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2004 return 0;
2005 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2006 continue;
2007
2008 /* Check too big values. */
2009 if ((unsigned int)c > 0xffff) {
2010 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2011 return 0;
2012 continue;
2013 }
2014 }
2015
2016 /* Convert character. */
2017 if (c == '"') {
2018 len = 2;
2019 str = "\\\"";
2020 }
2021 else if (c == '\\') {
2022 len = 2;
2023 str = "\\\\";
2024 }
2025 else if (c == '/') {
2026 len = 2;
2027 str = "\\/";
2028 }
2029 else if (c == '\b') {
2030 len = 2;
2031 str = "\\b";
2032 }
2033 else if (c == '\f') {
2034 len = 2;
2035 str = "\\f";
2036 }
2037 else if (c == '\r') {
2038 len = 2;
2039 str = "\\r";
2040 }
2041 else if (c == '\n') {
2042 len = 2;
2043 str = "\\n";
2044 }
2045 else if (c == '\t') {
2046 len = 2;
2047 str = "\\t";
2048 }
2049 else if (c > 0xff || !isprint(c)) {
2050 /* isprint generate a segfault if c is too big. The man says that
2051 * c must have the value of an unsigned char or EOF.
2052 */
2053 len = 6;
2054 _str[0] = '\\';
2055 _str[1] = 'u';
2056 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2057 str = _str;
2058 }
2059 else {
2060 len = 1;
2061 str = (char *)&c;
2062 }
2063
2064 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002065 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002066 return 0;
2067
2068 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002069 memcpy(temp->area + temp->data, str, len);
2070 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002071 }
2072
2073 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002074 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002075 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002076
2077 return 1;
2078}
2079
Emeric Brun54c4ac82014-11-03 15:32:43 +01002080/* This sample function is designed to extract some bytes from an input buffer.
2081 * First arg is the offset.
2082 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002083static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002084{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2086 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002087 return 1;
2088 }
2089
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002090 if (smp->data.u.str.size)
2091 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002092 smp->data.u.str.data -= arg_p[0].data.sint;
2093 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002094
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002095 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2096 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002097
2098 return 1;
2099}
2100
Emeric Brunf399b0d2014-11-03 17:07:03 +01002101static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2102 const char *file, int line, char **err)
2103{
2104 struct arg *arg = args;
2105
2106 if (!arg) {
2107 memprintf(err, "Unexpected empty arg list");
2108 return 0;
2109 }
2110
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002111 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002112 memprintf(err, "Unexpected arg type");
2113 return 0;
2114 }
2115
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002116 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002117 memprintf(err, "Unexpected value 0 for index");
2118 return 0;
2119 }
2120
2121 arg++;
2122
2123 if (arg->type != ARGT_STR) {
2124 memprintf(err, "Unexpected arg type");
2125 return 0;
2126 }
2127
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002128 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002129 memprintf(err, "Empty separators list");
2130 return 0;
2131 }
2132
2133 return 1;
2134}
2135
2136/* This sample function is designed to a return selected part of a string (field).
2137 * First arg is the index of the field (start at 1)
2138 * Second arg is a char list of separators (type string)
2139 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002140static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002141{
Marcin Deranek9631a282018-04-16 14:30:46 +02002142 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002143 char *start, *end;
2144 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002145 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002146
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002147 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002148 return 0;
2149
Marcin Deranek9631a282018-04-16 14:30:46 +02002150 if (arg_p[0].data.sint < 0) {
2151 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002152 end = start = smp->data.u.str.area + smp->data.u.str.data;
2153 while (start > smp->data.u.str.area) {
2154 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2155 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002156 if (field == arg_p[0].data.sint) {
2157 if (count == 1)
2158 goto found;
2159 else if (count > 1)
2160 count--;
2161 } else {
2162 end = start-1;
2163 field--;
2164 }
2165 break;
2166 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002167 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002168 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002169 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002170 } else {
2171 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002172 end = start = smp->data.u.str.area;
2173 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2174 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2175 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002176 if (field == arg_p[0].data.sint) {
2177 if (count == 1)
2178 goto found;
2179 else if (count > 1)
2180 count--;
2181 } else {
2182 start = end+1;
2183 field++;
2184 }
2185 break;
2186 }
2187 }
2188 end++;
2189 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002190 }
2191
2192 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002193 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002194 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002195 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002196 }
2197found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002198 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002199 /* If ret string is len 0, no need to
2200 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002201 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002202 return 1;
2203
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002205
2206 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002207 Note: smp->data.u.str.size cannot be set to 0 */
2208 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002209 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002210
2211 return 1;
2212}
2213
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002214/* This sample function is designed to return a word from a string.
2215 * First arg is the index of the word (start at 1)
2216 * Second arg is a char list of words separators (type string)
2217 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002218static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002219{
Marcin Deranek9631a282018-04-16 14:30:46 +02002220 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002221 char *start, *end;
2222 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002223 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002224
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002225 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002226 return 0;
2227
2228 word = 0;
2229 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002230 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002231 end = start = smp->data.u.str.area + smp->data.u.str.data;
2232 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002233 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2235 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002236 issep = 1;
2237 break;
2238 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002239 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002240 if (!inword) {
2241 if (!issep) {
2242 if (word != arg_p[0].data.sint) {
2243 word--;
2244 end = start;
2245 }
2246 inword = 1;
2247 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002248 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002249 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002250 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002251 if (count == 1)
2252 goto found;
2253 else if (count > 1)
2254 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002255 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002256 inword = 0;
2257 }
2258 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002259 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002260 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002261 end = start = smp->data.u.str.area;
2262 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002263 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002264 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2265 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002266 issep = 1;
2267 break;
2268 }
2269 }
2270 if (!inword) {
2271 if (!issep) {
2272 if (word != arg_p[0].data.sint) {
2273 word++;
2274 start = end;
2275 }
2276 inword = 1;
2277 }
2278 }
2279 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002280 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002281 if (count == 1)
2282 goto found;
2283 else if (count > 1)
2284 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002285 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002286 inword = 0;
2287 }
2288 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002289 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002290 }
2291
2292 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002293 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002294 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002295 return 1;
2296 }
2297found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002298 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002299 /* If ret string is len 0, no need to
2300 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002301 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002302 return 1;
2303
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002304 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002305
2306 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002307 Note: smp->data.u.str.size cannot be set to 0 */
2308 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002309 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002310
2311 return 1;
2312}
2313
Willy Tarreau7eda8492015-01-20 19:47:06 +01002314static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2315 const char *file, int line, char **err)
2316{
2317 struct arg *arg = args;
2318 char *p;
2319 int len;
2320
2321 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2322 arg[0].type_flags = 0;
2323
2324 if (arg[2].type != ARGT_STR)
2325 return 1;
2326
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 p = arg[2].data.str.area;
2328 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002329 while (len) {
2330 if (*p == 'i') {
2331 arg[0].type_flags |= ARGF_REG_ICASE;
2332 }
2333 else if (*p == 'g') {
2334 arg[0].type_flags |= ARGF_REG_GLOB;
2335 }
2336 else {
2337 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2338 return 0;
2339 }
2340 p++;
2341 len--;
2342 }
2343 return 1;
2344}
2345
2346/* This sample function is designed to do the equivalent of s/match/replace/ on
2347 * the input string. It applies a regex and restarts from the last matched
2348 * location until nothing matches anymore. First arg is the regex to apply to
2349 * the input string, second arg is the replacement expression.
2350 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002351static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002352{
2353 char *start, *end;
2354 struct my_regex *reg = arg_p[0].data.reg;
2355 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002356 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002357 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002358 int flag, max;
2359 int found;
2360
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002361 start = smp->data.u.str.area;
2362 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002363
2364 flag = 0;
2365 while (1) {
2366 /* check for last round which is used to copy remaining parts
2367 * when not running in global replacement mode.
2368 */
2369 found = 0;
2370 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2371 /* Note: we can have start == end on empty strings or at the end */
2372 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2373 }
2374
2375 if (!found)
2376 pmatch[0].rm_so = end - start;
2377
2378 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002379 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002380 if (max && pmatch[0].rm_so > 0) {
2381 if (max > pmatch[0].rm_so)
2382 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002383 memcpy(trash->area + trash->data, start, max);
2384 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002385 }
2386
2387 if (!found)
2388 break;
2389
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002390 output = alloc_trash_chunk();
2391 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2392
Willy Tarreau7eda8492015-01-20 19:47:06 +01002393 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002394 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002395 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002396 if (max > output->data)
2397 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002399 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002400 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002401 }
2402
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002403 free_trash_chunk(output);
2404
Willy Tarreau7eda8492015-01-20 19:47:06 +01002405 /* stop here if we're done with this string */
2406 if (start >= end)
2407 break;
2408
2409 /* We have a special case for matches of length 0 (eg: "x*y*").
2410 * These ones are considered to match in front of a character,
2411 * so we have to copy that character and skip to the next one.
2412 */
2413 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002414 if (trash->data < trash->size)
2415 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002416 pmatch[0].rm_eo++;
2417 }
2418
2419 start += pmatch[0].rm_eo;
2420 flag |= REG_NOTBOL;
2421 }
2422
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002423 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002424 return 1;
2425}
2426
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002427/* This function check an operator entry. It expects a string.
2428 * The string can be an integer or a variable name.
2429 */
2430static int check_operator(struct arg *args, struct sample_conv *conv,
2431 const char *file, int line, char **err)
2432{
2433 const char *str;
2434 const char *end;
2435
2436 /* Try to decode a variable. */
2437 if (vars_check_arg(&args[0], NULL))
2438 return 1;
2439
2440 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002441 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002442 end = str + strlen(str);
2443 args[0].data.sint = read_int64(&str, end);
2444 if (*str != '\0') {
2445 memprintf(err, "expects an integer or a variable name");
2446 return 0;
2447 }
2448 args[0].type = ARGT_SINT;
2449 return 1;
2450}
2451
Willy Tarreau6204cd92016-03-10 16:33:04 +01002452/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002453 * If the arg contain an integer, the integer is returned in the
2454 * sample. If the arg contains a variable descriptor, it returns the
2455 * variable value.
2456 *
2457 * This function returns 0 if an error occurs, otherwise it returns 1.
2458 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002459static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002460{
2461 switch (arg->type) {
2462 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002463 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002464 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002465 return 1;
2466 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002467 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002468 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002469 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002470 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002471 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002472 return 0;
2473 return 1;
2474 default:
2475 return 0;
2476 }
2477}
2478
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002479/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002480 * result.
2481 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002482static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002483{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002484 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002485 return 1;
2486}
2487
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002488/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002489 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002490 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002491static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002492{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002493 struct sample tmp;
2494
Willy Tarreau7560dd42016-03-10 16:28:58 +01002495 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002496 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002497 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002498 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002499 return 1;
2500}
2501
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002502/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002503 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002504 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002505static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002506{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002507 struct sample tmp;
2508
Willy Tarreau7560dd42016-03-10 16:28:58 +01002509 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002510 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002511 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002512 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002513 return 1;
2514}
2515
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002516/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002517 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002518 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002519static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002520{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002521 struct sample tmp;
2522
Willy Tarreau7560dd42016-03-10 16:28:58 +01002523 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002524 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002525 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002526 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002527 return 1;
2528}
2529
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002530static inline long long int arith_add(long long int a, long long int b)
2531{
2532 /* Prevent overflow and makes capped calculus.
2533 * We must ensure that the check calculus doesn't
2534 * exceed the signed 64 bits limits.
2535 *
2536 * +----------+----------+
2537 * | a<0 | a>=0 |
2538 * +------+----------+----------+
2539 * | b<0 | MIN-a>b | no check |
2540 * +------+----------+----------+
2541 * | b>=0 | no check | MAX-a<b |
2542 * +------+----------+----------+
2543 */
2544 if ((a ^ b) >= 0) {
2545 /* signs are differents. */
2546 if (a < 0) {
2547 if (LLONG_MIN - a > b)
2548 return LLONG_MIN;
2549 }
2550 if (LLONG_MAX - a < b)
2551 return LLONG_MAX;
2552 }
2553 return a + b;
2554}
2555
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002556/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002557 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002558 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002559static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002560{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002561 struct sample tmp;
2562
Willy Tarreau7560dd42016-03-10 16:28:58 +01002563 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002564 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002565 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002566 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002567 return 1;
2568}
2569
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002570/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002571 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002572 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002573static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002574 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002575{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002576 struct sample tmp;
2577
Willy Tarreau7560dd42016-03-10 16:28:58 +01002578 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002579 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002580 return 0;
2581
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002582 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2583 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2584 * of -LLONG_MIN and correct the result.
2585 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002586 if (tmp.data.u.sint == LLONG_MIN) {
2587 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2588 if (smp->data.u.sint < LLONG_MAX)
2589 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002590 return 1;
2591 }
2592
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002593 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002594 * the second operand.
2595 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002596 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002597 return 1;
2598}
2599
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002600/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002601 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002602 * If the result makes an overflow, then the largest possible quantity is
2603 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002604 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002605static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002606 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002607{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002608 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002609 long long int c;
2610
Willy Tarreau7560dd42016-03-10 16:28:58 +01002611 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002612 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002613 return 0;
2614
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002615 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002616 if (!smp->data.u.sint || !tmp.data.u.sint) {
2617 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002618 return 1;
2619 }
2620
2621 /* The multiply between LLONG_MIN and -1 returns a
2622 * "floting point exception".
2623 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002624 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2625 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002626 return 1;
2627 }
2628
2629 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002630 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002631
2632 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002633 if (smp->data.u.sint != c / tmp.data.u.sint) {
2634 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2635 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002636 return 1;
2637 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002638 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002639 return 1;
2640 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002641 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002642 return 1;
2643}
2644
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002645/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002646 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002647 * If arg_p makes the result overflow, then the largest possible quantity is
2648 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002649 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002650static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002651 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002652{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002653 struct sample tmp;
2654
Willy Tarreau7560dd42016-03-10 16:28:58 +01002655 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002656 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002657 return 0;
2658
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002659 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002660 /* The divide between LLONG_MIN and -1 returns a
2661 * "floting point exception".
2662 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002663 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2664 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002665 return 1;
2666 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002667 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002668 return 1;
2669 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002670 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002671 return 1;
2672}
2673
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002674/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002675 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002676 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002677 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002678static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002679 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002680{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002681 struct sample tmp;
2682
Willy Tarreau7560dd42016-03-10 16:28:58 +01002683 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002684 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002685 return 0;
2686
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002687 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002688 /* The divide between LLONG_MIN and -1 returns a
2689 * "floting point exception".
2690 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002691 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2692 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002693 return 1;
2694 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002695 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002696 return 1;
2697 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002698 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002699 return 1;
2700}
2701
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002702/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002703 * result.
2704 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002705static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002706 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002707{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002708 if (smp->data.u.sint == LLONG_MIN)
2709 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002710 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002711 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002712 return 1;
2713}
2714
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002715/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002716 * false. The output is a BOOL.
2717 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002718static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002719 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002720{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002721 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002722 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002723 return 1;
2724}
2725
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002726/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002727 * truee. The output is a BOOL.
2728 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002729static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002730 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002731{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002732 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002733 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002734 return 1;
2735}
2736
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002737/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002738 * The output is a BOOL.
2739 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002740static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002741 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002742{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002743 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002744 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002745 return 1;
2746}
2747
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002748/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002749 * The output is a BOOL.
2750 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002751static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002752 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002753{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002754 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002755 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002756 return 1;
2757}
2758
Willy Tarreau280f42b2018-02-19 15:34:12 +01002759/* appends an optional const string, an optional variable contents and another
2760 * optional const string to an existing string.
2761 */
2762static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2763{
Willy Tarreau83061a82018-07-13 11:56:34 +02002764 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002765 struct sample tmp;
2766 int max;
2767
2768 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 trash->data = smp->data.u.str.data;
2770 if (trash->data > trash->size - 1)
2771 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002772
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002773 memcpy(trash->area, smp->data.u.str.area, trash->data);
2774 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002775
2776 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002777 max = arg_p[0].data.str.data;
2778 if (max > trash->size - 1 - trash->data)
2779 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002780
2781 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002782 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2783 trash->data += max;
2784 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002785 }
2786
2787 /* append second string (variable) if it's found and we can turn it
2788 * into a string.
2789 */
2790 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2791 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2792 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2793 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2794
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002795 max = tmp.data.u.str.data;
2796 if (max > trash->size - 1 - trash->data)
2797 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002798
2799 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002800 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2801 max);
2802 trash->data += max;
2803 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002804 }
2805 }
2806
2807 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002808 max = arg_p[2].data.str.data;
2809 if (max > trash->size - 1 - trash->data)
2810 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002811
2812 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002813 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2814 trash->data += max;
2815 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002816 }
2817
2818 smp->data.u.str = *trash;
2819 smp->data.type = SMP_T_STR;
2820 return 1;
2821}
2822
2823/* This function checks the "concat" converter's arguments and extracts the
2824 * variable name and its scope.
2825 */
2826static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2827 const char *file, int line, char **err)
2828{
2829 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002830 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2831 memprintf(err, "failed to register variable name '%s'",
2832 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002833 return 0;
2834 }
2835 return 1;
2836}
2837
Tim Duesterhusca097c12018-04-27 21:18:45 +02002838/* compares string with a variable containing a string. Return value
2839 * is compatible with strcmp(3)'s return value.
2840 */
2841static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2842{
2843 struct sample tmp;
2844 int max, result;
2845
2846 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2847 if (arg_p[0].type != ARGT_VAR)
2848 return 0;
2849 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2850 return 0;
2851 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2852 return 0;
2853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002854 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2855 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002856 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002857 if (smp->data.u.str.data != tmp.data.u.str.data) {
2858 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002859 result = -1;
2860 }
2861 else {
2862 result = 1;
2863 }
2864 }
2865 }
2866
2867 smp->data.u.sint = result;
2868 smp->data.type = SMP_T_SINT;
2869 return 1;
2870}
2871
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002872#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2873#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2874#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2875
2876/*
2877 * Extract the field value of an input binary sample. Takes a mandatory argument:
2878 * the protocol buffers field identifier (dotted notation) internally represented
2879 * as an array of unsigned integers and its size.
2880 * Return 1 if the field was found, 0 if not.
2881 */
2882static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2883{
2884 unsigned char *pos;
2885 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002886
2887 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002888 grpc_left = smp->data.u.str.data;
2889
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002890 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002891 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002892
2893 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2894
2895 pos += GRPC_MSG_HEADER_SZ;
2896 grpc_left -= GRPC_MSG_HEADER_SZ;
2897
2898 if (grpc_left < left)
2899 return 0;
2900
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002901 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2902 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002903
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002904 grpc_left -= grpc_msg_len;
2905 }
2906
2907 return 0;
2908}
2909
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002910static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2911{
2912 unsigned char *pos;
2913 size_t left;
2914
2915 pos = (unsigned char *)smp->data.u.str.area;
2916 left = smp->data.u.str.data;
2917
2918 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2919}
2920
2921static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2922 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002923{
2924 if (!args[1].type) {
2925 args[1].type = ARGT_SINT;
2926 args[1].data.sint = PBUF_T_BINARY;
2927 }
2928 else {
2929 int pbuf_type;
2930
2931 pbuf_type = protobuf_type(args[1].data.str.area);
2932 if (pbuf_type == -1) {
2933 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2934 return 0;
2935 }
2936
2937 args[1].type = ARGT_SINT;
2938 args[1].data.sint = pbuf_type;
2939 }
2940
2941 return 1;
2942}
2943
Tim Duesterhusca097c12018-04-27 21:18:45 +02002944/* This function checks the "strcmp" converter's arguments and extracts the
2945 * variable name and its scope.
2946 */
2947static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2948 const char *file, int line, char **err)
2949{
2950 /* Try to decode a variable. */
2951 if (vars_check_arg(&args[0], NULL))
2952 return 1;
2953
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002954 memprintf(err, "failed to register variable name '%s'",
2955 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002956 return 0;
2957}
2958
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002959/************************************************************************/
2960/* All supported sample fetch functions must be declared here */
2961/************************************************************************/
2962
2963/* force TRUE to be returned at the fetch level */
2964static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002965smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002966{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002967 if (!smp_make_rw(smp))
2968 return 0;
2969
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002970 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002971 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002972 return 1;
2973}
2974
2975/* force FALSE to be returned at the fetch level */
2976static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002977smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002978{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002979 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002980 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002981 return 1;
2982}
2983
2984/* retrieve environment variable $1 as a string */
2985static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002986smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002987{
2988 char *env;
2989
2990 if (!args || args[0].type != ARGT_STR)
2991 return 0;
2992
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002993 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002994 if (!env)
2995 return 0;
2996
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002997 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01002998 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002999 smp->data.u.str.area = env;
3000 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003001 return 1;
3002}
3003
Damien Claisseae6f1252019-10-30 15:57:28 +00003004/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3005 * optional string representing the unit of the result: "s" for seconds, "ms" for
3006 * milliseconds and "us" for microseconds.
3007 * Returns 0 on error and non-zero if OK.
3008 */
3009int smp_check_date_unit(struct arg *args, char **err)
3010{
3011 if (args[1].type == ARGT_STR) {
3012 if (strcmp(args[1].data.str.area, "s") == 0) {
3013 args[1].data.sint = TIME_UNIT_S;
3014 }
3015 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3016 args[1].data.sint = TIME_UNIT_MS;
3017 }
3018 else if (strcmp(args[1].data.str.area, "us") == 0) {
3019 args[1].data.sint = TIME_UNIT_US;
3020 }
3021 else {
3022 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3023 args[1].data.str.area);
3024 return 0;
3025 }
3026 free(args[1].data.str.area);
3027 args[1].data.str.area = NULL;
3028 args[1].type = ARGT_SINT;
3029 }
3030 else if (args[1].type != ARGT_STOP) {
3031 memprintf(err, "Unexpected arg type");
3032 return 0;
3033 }
3034
3035 return 1;
3036}
3037
3038/* retrieve the current local date in epoch time, converts it to milliseconds
3039 * or microseconds if asked to in optional args[1] unit param, and applies an
3040 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003041 */
3042static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003043smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003044{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003045 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003046
Damien Claisseae6f1252019-10-30 15:57:28 +00003047 /* report in milliseconds */
3048 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3049 smp->data.u.sint *= 1000;
3050 smp->data.u.sint += date.tv_usec / 1000;
3051 }
3052 /* report in microseconds */
3053 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3054 smp->data.u.sint *= 1000000;
3055 smp->data.u.sint += date.tv_usec;
3056 }
3057
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003058 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003059 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003060 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003061
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003062 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003063 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3064 return 1;
3065}
3066
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003067/* retrieve the current microsecond part of the date */
3068static int
3069smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3070{
3071 smp->data.u.sint = date.tv_usec;
3072 smp->data.type = SMP_T_SINT;
3073 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3074 return 1;
3075}
3076
3077
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003078/* returns the hostname */
3079static int
3080smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3081{
3082 smp->data.type = SMP_T_STR;
3083 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003084 smp->data.u.str.area = hostname;
3085 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003086 return 1;
3087}
3088
Willy Tarreau0f30d262014-11-24 16:02:05 +01003089/* returns the number of processes */
3090static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003091smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003092{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003093 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003094 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003095 return 1;
3096}
3097
3098/* returns the number of the current process (between 1 and nbproc */
3099static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003100smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003101{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003102 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003103 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003104 return 1;
3105}
3106
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003107/* returns the number of the current thread (between 1 and nbthread */
3108static int
3109smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3110{
3111 smp->data.type = SMP_T_SINT;
3112 smp->data.u.sint = tid;
3113 return 1;
3114}
3115
Willy Tarreau84310e22014-02-14 11:59:04 +01003116/* generate a random 32-bit integer for whatever purpose, with an optional
3117 * range specified in argument.
3118 */
3119static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003120smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003121{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003122 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003123
3124 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003125 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003126 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003127
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003128 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003129 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3130 return 1;
3131}
3132
Willy Tarreau0f30d262014-11-24 16:02:05 +01003133/* returns true if the current process is stopping */
3134static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003135smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003136{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003137 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003138 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003139 return 1;
3140}
3141
Willy Tarreau70fe9442018-11-22 16:07:39 +01003142/* returns the number of calls of the current stream's process_stream() */
3143static int
3144smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3145{
3146 smp->data.type = SMP_T_SINT;
3147 smp->data.u.sint = smp->strm->task->calls;
3148 return 1;
3149}
3150
3151/* returns the average number of nanoseconds spent processing the stream per call */
3152static int
3153smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3154{
3155 smp->data.type = SMP_T_SINT;
3156 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3157 return 1;
3158}
3159
3160/* returns the total number of nanoseconds spent processing the stream */
3161static int
3162smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3163{
3164 smp->data.type = SMP_T_SINT;
3165 smp->data.u.sint = smp->strm->task->cpu_time;
3166 return 1;
3167}
3168
3169/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3170static int
3171smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3172{
3173 smp->data.type = SMP_T_SINT;
3174 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3175 return 1;
3176}
3177
3178/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3179static int
3180smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3181{
3182 smp->data.type = SMP_T_SINT;
3183 smp->data.u.sint = smp->strm->task->lat_time;
3184 return 1;
3185}
3186
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003187static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3188{
3189 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003190 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003191 smp->data.u.str.area = args[0].data.str.area;
3192 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003193 return 1;
3194}
3195
3196static int smp_check_const_bool(struct arg *args, char **err)
3197{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003198 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3199 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003200 args[0].type = ARGT_SINT;
3201 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003202 return 1;
3203 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003204 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3205 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003206 args[0].type = ARGT_SINT;
3207 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003208 return 1;
3209 }
3210 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3211 return 0;
3212}
3213
3214static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3215{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003216 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003217 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003218 return 1;
3219}
3220
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003221static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003222{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003223 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003224 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003225 return 1;
3226}
3227
3228static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3229{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003230 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003231 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003232 return 1;
3233}
3234
3235static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3236{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003237 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003238 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003239 return 1;
3240}
3241
3242static int smp_check_const_bin(struct arg *args, char **err)
3243{
David Carlier64a16ab2016-04-08 10:37:02 +01003244 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003245 int binstrlen;
3246
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003247 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003248 return 0;
3249 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003250 args[0].data.str.area = binstr;
3251 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003252 return 1;
3253}
3254
3255static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3256{
3257 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003258 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003259 smp->data.u.str.area = args[0].data.str.area;
3260 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003261 return 1;
3262}
3263
3264static int smp_check_const_meth(struct arg *args, char **err)
3265{
3266 enum http_meth_t meth;
3267 int i;
3268
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003269 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003270 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003271 args[0].type = ARGT_SINT;
3272 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003273 } else {
3274 /* Check method avalaibility. A methos is a token defined as :
3275 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3276 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3277 * token = 1*tchar
3278 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003279 for (i = 0; i < args[0].data.str.data; i++) {
3280 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003281 memprintf(err, "expects valid method.");
3282 return 0;
3283 }
3284 }
3285 }
3286 return 1;
3287}
3288
3289static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3290{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003291 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003292 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003293 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003294 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003295 smp->data.u.meth.str.area = "";
3296 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003297 } else {
3298 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003299 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003300 smp->data.u.meth.str.area = args[0].data.str.area;
3301 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003302 }
3303 return 1;
3304}
3305
Luca Schimweg8a694b82019-09-10 15:42:52 +02003306// This function checks the "uuid" sample's arguments.
3307// Function won't get called when no parameter is specified (maybe a bug?)
3308static int smp_check_uuid(struct arg *args, char **err)
3309{
3310 if (!args[0].type) {
3311 args[0].type = ARGT_SINT;
3312 args[0].data.sint = 4;
3313 }
3314 else if (args[0].data.sint != 4) {
3315 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3316 return 0;
3317 }
3318
3319 return 1;
3320}
3321
3322// Generate a RFC4122 UUID (default is v4 = fully random)
3323static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3324{
3325 if (args[0].data.sint == 4 || !args[0].type) {
3326 uint32_t rnd[4] = { 0, 0, 0, 0 };
3327 uint64_t last = 0;
3328 int byte = 0;
3329 uint8_t bits = 0;
3330 unsigned int rand_max_bits = my_flsl(RAND_MAX);
3331
3332 while (byte < 4) {
3333 while (bits < 32) {
3334 last |= (uint64_t)random() << bits;
3335 bits += rand_max_bits;
3336 }
3337 rnd[byte++] = last;
3338 last >>= 32u;
3339 bits -= 32;
3340 }
3341
3342 chunk_printf(&trash, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
3343 rnd[0],
3344 rnd[1] & 0xFFFF,
3345 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
3346 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
3347 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
3348 );
3349
3350 smp->data.type = SMP_T_STR;
3351 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3352 smp->data.u.str = trash;
3353 return 1;
3354 }
3355
3356 // more implementations of other uuid formats possible here
3357 return 0;
3358}
3359
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003360/* Note: must not be declared <const> as its list will be overwritten.
3361 * Note: fetches that may return multiple types must be declared as the lowest
3362 * common denominator, the type that can be casted into all other ones. For
3363 * instance IPv4/IPv6 must be declared IPv4.
3364 */
3365static struct sample_fetch_kw_list smp_kws = {ILH, {
3366 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3367 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003368 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003369 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003370 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003371 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003372 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3373 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003374 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003375 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003376 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003377 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003378 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003379
3380 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3381 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3382 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3383 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3384 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003385
3386 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3387 { "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 +02003388 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003389 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3390 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3391 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3392 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3393
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003394 { /* END */ },
3395}};
3396
Willy Tarreau0108d902018-11-25 19:14:37 +01003397INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3398
Emeric Brun107ca302010-01-04 16:16:05 +01003399/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003400static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003401 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003402 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003403 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003404 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3405 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003406 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003407 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003408 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003409 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003410 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3411 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003412 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003413 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003414 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3415 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3416 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003417 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3418 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003419 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003420 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003421 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3422 { "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 +01003423 { "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 +02003424 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003425#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003426 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003427#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003428 { "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 +02003429 { "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 +01003430
3431 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003432 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3433 { "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 +01003434
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003435 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3436 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3437 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3438 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3439 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3440 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3441 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3442 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3443 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3444 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3445 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3446 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3447 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3448 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003449
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003450 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003451}};
3452
Willy Tarreau0108d902018-11-25 19:14:37 +01003453INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);