blob: 820fa479694f889a879820ae0f723f8ba7322fd3 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010035#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020036#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020037#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010038
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020039#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010040#include <import/xxhash.h>
41
Willy Tarreau1cf8f082014-02-07 12:14:54 +010042/* sample type names */
43const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020044 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_SINT] = "sint",
47 [SMP_T_ADDR] = "addr",
48 [SMP_T_IPV4] = "ipv4",
49 [SMP_T_IPV6] = "ipv6",
50 [SMP_T_STR] = "str",
51 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020052 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010053};
54
Willy Tarreau12785782012-04-27 21:37:17 +020055/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020056static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010057
Willy Tarreau12785782012-04-27 21:37:17 +020058/* list head of all known sample fetch keywords */
59static struct sample_fetch_kw_list sample_fetches = {
60 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010061};
62
Willy Tarreau12785782012-04-27 21:37:17 +020063/* list head of all known sample format conversion keywords */
64static struct sample_conv_kw_list sample_convs = {
65 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010066};
67
Willy Tarreau80aca902013-01-07 15:42:20 +010068const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
69 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
70 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
71 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
72 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
73 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
74 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
75 SMP_VAL_FE_LOG_END),
76
77 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
78 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
79 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
80 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
81 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
82 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
83 SMP_VAL_FE_LOG_END),
84
85 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
86 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
87 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
88 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
89 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
90 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
91 SMP_VAL_FE_LOG_END),
92
93 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
94 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
95 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
96 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
97 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
98 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
99 SMP_VAL_FE_LOG_END),
100
101 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
102 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
103 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
104 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
105 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
106 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
107 SMP_VAL_FE_LOG_END),
108
109 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
110 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
111 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
112 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
113 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
114 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
115 SMP_VAL_FE_LOG_END),
116
117 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
118 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
119 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
120 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________),
124
125 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
126 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
127 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
128 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________),
132
133 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
134 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
135 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
136 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
137 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
138 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
139 SMP_VAL_FE_LOG_END),
140
141 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
142 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
143 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
144 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________),
148
149 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
152 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
153 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
154 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
155 SMP_VAL_FE_LOG_END),
156
157 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
161 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
162 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
163 SMP_VAL_FE_LOG_END),
164
165 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
169 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
170 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
171 SMP_VAL_FE_LOG_END),
172
173 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
177 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
178 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
179 SMP_VAL_FE_LOG_END),
180
181 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
185 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
186 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
187 SMP_VAL___________),
188
189 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
193 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
194 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
195 SMP_VAL___________),
196
197 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
201 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
202 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
203 SMP_VAL_FE_LOG_END),
204
205 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
210 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
211 SMP_VAL___________),
212
213 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL_FE_LOG_END),
220
221 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL_FE_LOG_END),
228
229 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL_FE_LOG_END),
236
237 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
243 SMP_VAL_FE_LOG_END),
244};
245
246static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
247 [SMP_SRC_INTRN] = "internal state",
248 [SMP_SRC_LISTN] = "listener",
249 [SMP_SRC_FTEND] = "frontend",
250 [SMP_SRC_L4CLI] = "client address",
251 [SMP_SRC_L5CLI] = "client-side connection",
252 [SMP_SRC_TRACK] = "track counters",
253 [SMP_SRC_L6REQ] = "request buffer",
254 [SMP_SRC_HRQHV] = "HTTP request headers",
255 [SMP_SRC_HRQHP] = "HTTP request",
256 [SMP_SRC_HRQBO] = "HTTP request body",
257 [SMP_SRC_BKEND] = "backend",
258 [SMP_SRC_SERVR] = "server",
259 [SMP_SRC_L4SRV] = "server address",
260 [SMP_SRC_L5SRV] = "server-side connection",
261 [SMP_SRC_L6RES] = "response buffer",
262 [SMP_SRC_HRSHV] = "HTTP response headers",
263 [SMP_SRC_HRSHP] = "HTTP response",
264 [SMP_SRC_HRSBO] = "HTTP response body",
265 [SMP_SRC_RQFIN] = "request buffer statistics",
266 [SMP_SRC_RSFIN] = "response buffer statistics",
267 [SMP_SRC_TXFIN] = "transaction statistics",
268 [SMP_SRC_SSFIN] = "session statistics",
269};
270
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100271static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
272 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
273 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
274 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
275 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
276 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
277 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
278 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
279 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
280 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
281 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
282 [SMP_CKP_BE_SRV_CON] = "server source selection",
283 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
284 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
285 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
286 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
287 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
288 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
289 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
290 [SMP_CKP_FE_LOG_END] = "logs",
291};
292
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100293/* This function returns the type of the data returned by the sample_expr.
294 * It assumes that the <expr> and all of its converters are properly
295 * initialized.
296 */
297inline
298int smp_expr_output_type(struct sample_expr *expr)
299{
300 struct sample_conv_expr *smp_expr;
301
302 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
303 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
304 return smp_expr->conv->out_type;
305 }
306 return expr->fetch->out_type;
307}
308
309
Willy Tarreau80aca902013-01-07 15:42:20 +0100310/* fill the trash with a comma-delimited list of source names for the <use> bit
311 * field which must be composed of a non-null set of SMP_USE_* flags. The return
312 * value is the pointer to the string in the trash buffer.
313 */
314const char *sample_src_names(unsigned int use)
315{
316 int bit;
317
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200318 trash.data = 0;
319 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100320 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
321 if (!(use & ~((1 << bit) - 1)))
322 break; /* no more bits */
323
324 if (!(use & (1 << bit)))
325 continue; /* bit not set */
326
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200327 trash.data += snprintf(trash.area + trash.data,
328 trash.size - trash.data, "%s%s",
329 (use & ((1 << bit) - 1)) ? "," : "",
330 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100331 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200332 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100333}
334
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100335/* return a pointer to the correct sample checkpoint name, or "unknown" when
336 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
337 * if set.
338 */
339const char *sample_ckp_names(unsigned int use)
340{
341 int bit;
342
343 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
344 if (use & (1 << bit))
345 return fetch_ckp_names[bit];
346 return "unknown sample check place, please report this bug";
347}
348
Emeric Brun107ca302010-01-04 16:16:05 +0100349/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100350 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
351 * for next parsing sessions. The fetch keywords capabilities are also computed
352 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100353 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100354void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100355{
Willy Tarreau80aca902013-01-07 15:42:20 +0100356 struct sample_fetch *sf;
357 int bit;
358
359 for (sf = kwl->kw; sf->kw != NULL; sf++) {
360 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
361 if (sf->use & (1 << bit))
362 sf->val |= fetch_cap[bit];
363 }
364 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100365}
366
367/*
Willy Tarreau12785782012-04-27 21:37:17 +0200368 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100369 * parsing sessions.
370 */
Willy Tarreau12785782012-04-27 21:37:17 +0200371void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100372{
Willy Tarreau12785782012-04-27 21:37:17 +0200373 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100374}
375
376/*
Willy Tarreau12785782012-04-27 21:37:17 +0200377 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100378 * string of <len> in buffer <kw>.
379 *
380 */
Willy Tarreau12785782012-04-27 21:37:17 +0200381struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100382{
383 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200384 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100385
Willy Tarreau12785782012-04-27 21:37:17 +0200386 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100387 for (index = 0; kwl->kw[index].kw != NULL; index++) {
388 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
389 kwl->kw[index].kw[len] == '\0')
390 return &kwl->kw[index];
391 }
392 }
393 return NULL;
394}
395
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100396/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100397 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398 * <idx> is the index of the next sample fetch entry. It is used as private
399 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100400 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100401 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 * the list is reached.
403 */
404struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
405{
406 struct sample_fetch_kw_list *kwl;
407 struct sample_fetch *base;
408
409 if (!current) {
410 /* Get first kwl entry. */
411 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
412 (*idx) = 0;
413 } else {
414 /* Get kwl corresponding to the curret entry. */
415 base = current + 1 - (*idx);
416 kwl = container_of(base, struct sample_fetch_kw_list, kw);
417 }
418
419 while (1) {
420
421 /* Check if kwl is the last entry. */
422 if (&kwl->list == &sample_fetches.list)
423 return NULL;
424
425 /* idx contain the next keyword. If it is available, return it. */
426 if (kwl->kw[*idx].kw) {
427 (*idx)++;
428 return &kwl->kw[(*idx)-1];
429 }
430
431 /* get next entry in the main list, and return NULL if the end is reached. */
432 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
433
434 /* Set index to 0, ans do one other loop. */
435 (*idx) = 0;
436 }
437}
438
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100439/* This function browses the list of available converters. <current> is
440 * the last used converter. If it is the first call, it must set to NULL.
441 * <idx> is the index of the next converter entry. It is used as private
442 * value. It is useless to initiate it.
443 *
444 * It returns always the next sample_conv entry, and NULL when the end of
445 * the list is reached.
446 */
447struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
448{
449 struct sample_conv_kw_list *kwl;
450 struct sample_conv *base;
451
452 if (!current) {
453 /* Get first kwl entry. */
454 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
455 (*idx) = 0;
456 } else {
457 /* Get kwl corresponding to the curret entry. */
458 base = current + 1 - (*idx);
459 kwl = container_of(base, struct sample_conv_kw_list, kw);
460 }
461
462 while (1) {
463 /* Check if kwl is the last entry. */
464 if (&kwl->list == &sample_convs.list)
465 return NULL;
466
467 /* idx contain the next keyword. If it is available, return it. */
468 if (kwl->kw[*idx].kw) {
469 (*idx)++;
470 return &kwl->kw[(*idx)-1];
471 }
472
473 /* get next entry in the main list, and return NULL if the end is reached. */
474 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
475
476 /* Set index to 0, ans do one other loop. */
477 (*idx) = 0;
478 }
479}
480
Emeric Brun107ca302010-01-04 16:16:05 +0100481/*
Willy Tarreau12785782012-04-27 21:37:17 +0200482 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100483 * string of <len> in buffer <kw>.
484 *
485 */
Willy Tarreau12785782012-04-27 21:37:17 +0200486struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100487{
488 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200489 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100490
Willy Tarreau12785782012-04-27 21:37:17 +0200491 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100492 for (index = 0; kwl->kw[index].kw != NULL; index++) {
493 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
494 kwl->kw[index].kw[len] == '\0')
495 return &kwl->kw[index];
496 }
497 }
498 return NULL;
499}
500
Emeric Brun107ca302010-01-04 16:16:05 +0100501/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200502/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100503/******************************************************************/
504
Willy Tarreau342acb42012-04-23 22:03:39 +0200505static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100506{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200507 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200508 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100509 return 1;
510}
511
Willy Tarreau342acb42012-04-23 22:03:39 +0200512static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100513{
Willy Tarreau83061a82018-07-13 11:56:34 +0200514 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100515
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200516 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100517 return 0;
518
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200519 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200521 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100522 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100523
524 return 1;
525}
526
Willy Tarreau342acb42012-04-23 22:03:39 +0200527static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100528{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200529 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200530 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100531 return 1;
532}
533
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200534static int c_ipv62ip(struct sample *smp)
535{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200536 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200537 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100538 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539 return 1;
540}
541
Willy Tarreau342acb42012-04-23 22:03:39 +0200542static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100543{
Willy Tarreau83061a82018-07-13 11:56:34 +0200544 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100545
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200546 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100547 return 0;
548
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200549 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200550 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200551 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100552 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100553 return 1;
554}
555
556/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200557static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100558{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200559 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100560}
561*/
562
Willy Tarreau342acb42012-04-23 22:03:39 +0200563static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100564{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200565 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200566 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100567 return 1;
568}
569
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200570static int c_int2ipv6(struct sample *smp)
571{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200572 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
573 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200574 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200575 return 1;
576}
577
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100578static int c_str2addr(struct sample *smp)
579{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200580 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
581 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100582 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200583 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100584 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100585 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100586 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200587 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100588 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100589 return 1;
590}
591
Willy Tarreau342acb42012-04-23 22:03:39 +0200592static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100593{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200594 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100595 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200596 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100597 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100598 return 1;
599}
600
Willy Tarreau342acb42012-04-23 22:03:39 +0200601static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100602{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200603 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100604 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200605 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100606 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100607 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100608}
609
Emeric Brun4b9e8022014-11-03 18:17:10 +0100610/*
611 * The NULL char always enforces the end of string if it is met.
612 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100613 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200614static int c_bin2str(struct sample *smp)
615{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100616 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200617
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200618 for (i = 0; i < smp->data.u.str.data; i++) {
619 if (!smp->data.u.str.area[i]) {
620 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100621 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200624 return 1;
625}
626
Willy Tarreau342acb42012-04-23 22:03:39 +0200627static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100628{
Willy Tarreau83061a82018-07-13 11:56:34 +0200629 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100630 char *pos;
631
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200632 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100633 if (!pos)
634 return 0;
635
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200636 trash->size = trash->size - (pos - trash->area);
637 trash->area = pos;
638 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200639 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200640 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100641 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100642 return 1;
643}
644
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800645/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200646 * For strings and binary blocks, it also provides a known allocated size with
647 * a length that is capped to the size, and ensures a trailing zero is always
648 * appended for strings. This is necessary for some operations which may
649 * require to extend the length. It returns 0 if it fails, 1 on success.
650 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100651int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200652{
Willy Tarreau83061a82018-07-13 11:56:34 +0200653 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200654
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200655 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100657 case SMP_T_SINT:
658 case SMP_T_ADDR:
659 case SMP_T_IPV4:
660 case SMP_T_IPV6:
661 /* These type are not const. */
662 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200663
Christopher Fauletec100512017-07-24 15:38:41 +0200664 case SMP_T_METH:
665 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
666 break;
667 /* Fall through */
668
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100669 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200670 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100671 trash->data = smp->data.type == SMP_T_STR ?
672 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200673 if (trash->data > trash->size - 1)
674 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200675
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100676 memcpy(trash->area, smp->data.type == SMP_T_STR ?
677 smp->data.u.str.area : smp->data.u.meth.str.area,
678 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200679 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200680 smp->data.u.str = *trash;
681 break;
682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100684 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200685 trash->data = smp->data.u.str.data;
686 if (trash->data > trash->size)
687 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200688
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200689 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200690 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100691 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 default:
694 /* Other cases are unexpected. */
695 return 0;
696 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100697
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100698 /* remove const flag */
699 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200700 return 1;
701}
702
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100703int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100704{
705 return 1;
706}
707
Willy Tarreau342acb42012-04-23 22:03:39 +0200708static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100709{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200710 const char *str;
711 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100712
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200713 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100714 return 0;
715
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200716 str = smp->data.u.str.area;
717 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100718
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200719 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200720 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100721 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100722 return 1;
723}
724
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100725static int c_str2meth(struct sample *smp)
726{
727 enum http_meth_t meth;
728 int len;
729
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200730 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100731 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 len = smp->data.u.str.data;
733 smp->data.u.meth.str.area = smp->data.u.str.area;
734 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100735 }
736 else
737 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200738 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200739 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100740 return 1;
741}
742
743static int c_meth2str(struct sample *smp)
744{
745 int len;
746 enum http_meth_t meth;
747
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200748 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100749 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200750 len = smp->data.u.meth.str.data;
751 smp->data.u.str.area = smp->data.u.meth.str.area;
752 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200753 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100754 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200755 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200758 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200759 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100760 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200761 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 }
763 else {
764 /* Unknown method */
765 return 0;
766 }
767 return 1;
768}
769
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200770static int c_addr2bin(struct sample *smp)
771{
Willy Tarreau83061a82018-07-13 11:56:34 +0200772 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200773
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200774 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200775 chk->data = 4;
776 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200777 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200778 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200779 chk->data = 16;
780 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200781 }
782 else
783 return 0;
784
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200786 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200787 return 1;
788}
789
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200790static int c_int2bin(struct sample *smp)
791{
Willy Tarreau83061a82018-07-13 11:56:34 +0200792 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200793
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200794 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
795 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200796
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200797 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200798 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200799 return 1;
800}
801
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200802
Emeric Brun107ca302010-01-04 16:16:05 +0100803/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200804/* Sample casts matrix: */
805/* sample_casts[from type][to type] */
806/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100807/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100808
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100809sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200810/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
811/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
812/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200813/* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200814/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
815/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200816/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200817/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
818/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
819/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200820};
Emeric Brun107ca302010-01-04 16:16:05 +0100821
Emeric Brun107ca302010-01-04 16:16:05 +0100822/*
Willy Tarreau12785782012-04-27 21:37:17 +0200823 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100824 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200825 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200826 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100827 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100828struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al)
Emeric Brun107ca302010-01-04 16:16:05 +0100829{
Willy Tarreau833cc792013-07-24 15:34:19 +0200830 const char *begw; /* beginning of word */
831 const char *endw; /* end of word */
832 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200833 struct sample_expr *expr;
834 struct sample_fetch *fetch;
835 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100836 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200837 char *fkw = NULL;
838 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100839 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100840
Willy Tarreau833cc792013-07-24 15:34:19 +0200841 begw = str[*idx];
842 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
843
844 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100845 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100846 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200847 }
Emeric Brun107ca302010-01-04 16:16:05 +0100848
Willy Tarreau833cc792013-07-24 15:34:19 +0200849 /* keep a copy of the current fetch keyword for error reporting */
850 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100851
Willy Tarreau833cc792013-07-24 15:34:19 +0200852 fetch = find_sample_fetch(begw, endw - begw);
853 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100854 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100855 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200856 }
Emeric Brun107ca302010-01-04 16:16:05 +0100857
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 endt = endw;
859 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100860 /* look for the end of this term and skip the opening parenthesis */
861 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200862 while (*endt && *endt != ')')
863 endt++;
864 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100865 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200866 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun485479d2010-09-23 18:02:19 +0200868 }
Emeric Brun107ca302010-01-04 16:16:05 +0100869
Willy Tarreau833cc792013-07-24 15:34:19 +0200870 /* At this point, we have :
871 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100872 * - endw : end of the keyword, first character not part of keyword
873 * nor the opening parenthesis (so first character of args
874 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200875 * - endt : end of the term (=endw or last parenthesis if args are present)
876 */
877
878 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100879 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100880 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200881 }
Emeric Brun107ca302010-01-04 16:16:05 +0100882 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200883
Vincent Bernat02779b62016-04-03 13:48:43 +0200884 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200885 if (!expr)
886 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100887
888 LIST_INIT(&(expr->conv_exprs));
889 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200890 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100891
Willy Tarreau689a1df2013-12-13 00:40:11 +0100892 /* Note that we call the argument parser even with an empty string,
893 * this allows it to automatically create entries for mandatory
894 * implicit arguments (eg: local proxy name).
895 */
896 al->kw = expr->fetch->kw;
897 al->conv = NULL;
898 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
899 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
900 goto out_error;
901 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200902
Willy Tarreau689a1df2013-12-13 00:40:11 +0100903 if (!expr->arg_p) {
904 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200905 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100906 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
907 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200908 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100909 }
910
Willy Tarreau833cc792013-07-24 15:34:19 +0200911 /* Now process the converters if any. We have two supported syntaxes
912 * for the converters, which can be combined :
913 * - comma-delimited list of converters just after the keyword and args ;
914 * - one converter per keyword
915 * The combination allows to have each keyword being a comma-delimited
916 * series of converters.
917 *
918 * We want to process the former first, then the latter. For this we start
919 * from the beginning of the supposed place in the exiting conv chain, which
920 * starts at the last comma (endt).
921 */
922
923 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200924 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100925 int err_arg;
926 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (*endt == ')') /* skip last closing parenthesis */
929 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100930
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 if (*endt && *endt != ',') {
932 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100933 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200934 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100935 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100936 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200937 }
Emeric Brun107ca302010-01-04 16:16:05 +0100938
Willy Tarreau833cc792013-07-24 15:34:19 +0200939 while (*endt == ',') /* then trailing commas */
940 endt++;
941
Willy Tarreau97108e02016-11-25 07:33:24 +0100942 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200943
944 if (!*begw) {
945 /* none ? skip to next string */
946 (*idx)++;
947 begw = str[*idx];
948 if (!begw || !*begw)
949 break;
950 }
951
952 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
953
954 free(ckw);
955 ckw = my_strndup(begw, endw - begw);
956
957 conv = find_sample_conv(begw, endw - begw);
958 if (!conv) {
959 /* we found an isolated keyword that we don't know, it's not ours */
960 if (begw == str[*idx])
961 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100962 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 goto out_error;
964 }
Emeric Brun107ca302010-01-04 16:16:05 +0100965
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 endt = endw;
967 if (*endt == '(') {
968 /* look for the end of this term */
Willy Tarreau46dfd782019-12-17 10:25:29 +0100969 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 while (*endt && *endt != ')')
971 endt++;
972 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100973 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200974 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200975 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200976 }
977
978 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100979 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100980 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200981 }
Emeric Brun107ca302010-01-04 16:16:05 +0100982
983 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200984 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100985 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100986 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200987 }
Emeric Brun107ca302010-01-04 16:16:05 +0100988
989 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200990 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200991 if (!conv_expr)
992 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100993
994 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
995 conv_expr->conv = conv;
996
Willy Tarreau46dfd782019-12-17 10:25:29 +0100997 al->kw = expr->fetch->kw;
998 al->conv = conv_expr->conv->kw;
999 argcnt = make_arg_list(endw, endt - endw, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al);
1000 if (argcnt < 0) {
1001 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
1002 goto out_error;
1003 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001004
Willy Tarreau46dfd782019-12-17 10:25:29 +01001005 if (argcnt && !conv->arg_mask) {
1006 memprintf(err_msg, "converter '%s' does not support any args", ckw);
1007 goto out_error;
1008 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001009
Willy Tarreau46dfd782019-12-17 10:25:29 +01001010 if (!conv_expr->arg_p)
1011 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001012
Willy Tarreau46dfd782019-12-17 10:25:29 +01001013 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1014 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001015 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001016 }
1017 }
Emeric Brun485479d2010-09-23 18:02:19 +02001018
Willy Tarreau833cc792013-07-24 15:34:19 +02001019 out:
1020 free(fkw);
1021 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001022 return expr;
1023
1024out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001025 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001026 expr = NULL;
1027 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001028}
1029
1030/*
Willy Tarreau12785782012-04-27 21:37:17 +02001031 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001032 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001033 * Returns a pointer on a typed sample structure containing the result or NULL if
1034 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001035 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001036 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001037 *
1038 * Note: the fetch functions are required to properly set the return type. The
1039 * conversion functions must do so too. However the cast functions do not need
1040 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001041 *
1042 * The caller may indicate in <opt> if it considers the result final or not.
1043 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1044 * if the result is stable or not, according to the following table :
1045 *
1046 * return MAY_CHANGE FINAL Meaning for the sample
1047 * NULL 0 * Not present and will never be (eg: header)
1048 * NULL 1 0 Not present yet, could change (eg: POST param)
1049 * NULL 1 1 Not present yet, will not change anymore
1050 * smp 0 * Present and will not change (eg: header)
1051 * smp 1 0 Present, may change (eg: request length)
1052 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001053 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001054struct sample *sample_process(struct proxy *px, struct session *sess,
1055 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001056 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001057{
Willy Tarreau12785782012-04-27 21:37:17 +02001058 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001059
Willy Tarreau18387e22013-07-25 12:02:38 +02001060 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001061 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001062 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001063 }
Emeric Brun107ca302010-01-04 16:16:05 +01001064
Willy Tarreau1777ea62016-03-10 16:15:46 +01001065 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001066 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001067 return NULL;
1068
Emeric Brun107ca302010-01-04 16:16:05 +01001069 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001070 /* we want to ensure that p->type can be casted into
1071 * conv_expr->conv->in_type. We have 3 possibilities :
1072 * - NULL => not castable.
1073 * - c_none => nothing to do (let's optimize it)
1074 * - other => apply cast and prepare to fail
1075 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001076 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001077 return NULL;
1078
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001079 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1080 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001081 return NULL;
1082
Willy Tarreau12e50112012-04-25 17:21:49 +02001083 /* OK cast succeeded */
1084
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001085 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001086 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001087 }
1088 return p;
1089}
1090
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001091/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001092 * Resolve all remaining arguments in proxy <p>. Returns the number of
1093 * errors or 0 if everything is fine.
1094 */
1095int smp_resolve_args(struct proxy *p)
1096{
1097 struct arg_list *cur, *bak;
1098 const char *ctx, *where;
1099 const char *conv_ctx, *conv_pre, *conv_pos;
1100 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001101 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001102 struct arg *arg;
1103 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105
1106 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1107 struct proxy *px;
1108 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001109 struct stktable *t;
1110 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001111 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001112
1113 arg = cur->arg;
1114
1115 /* prepare output messages */
1116 conv_pre = conv_pos = conv_ctx = "";
1117 if (cur->conv) {
1118 conv_ctx = cur->conv;
1119 conv_pre = "conversion keyword '";
1120 conv_pos = "' for ";
1121 }
1122
1123 where = "in";
1124 ctx = "sample fetch keyword";
1125 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001126 case ARGC_STK: where = "in stick rule in"; break;
1127 case ARGC_TRK: where = "in tracking rule in"; break;
1128 case ARGC_LOG: where = "in log-format string in"; break;
1129 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1130 case ARGC_HRQ: where = "in http-request header format string in"; break;
1131 case ARGC_HRS: where = "in http-response header format string in"; break;
1132 case ARGC_UIF: where = "in unique-id-format string in"; break;
1133 case ARGC_RDR: where = "in redirect format string in"; break;
1134 case ARGC_CAP: where = "in capture rule in"; break;
1135 case ARGC_ACL: ctx = "ACL keyword"; break;
1136 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001137 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001138 }
1139
1140 /* set a few default settings */
1141 px = p;
1142 pname = p->id;
1143
1144 switch (arg->type) {
1145 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001146 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001147 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1148 cur->file, cur->line,
1149 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001150 cfgerr++;
1151 continue;
1152 }
1153
1154 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001155 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156
1157 if (sname) {
1158 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001159 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001160
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001161 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001163 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1164 cur->file, cur->line, pname,
1165 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001166 cfgerr++;
1167 break;
1168 }
1169 }
1170 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001171 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001172
1173 srv = findserver(px, sname);
1174 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001175 ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1176 cur->file, cur->line, sname, pname,
1177 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001178 cfgerr++;
1179 break;
1180 }
1181
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001182 free(arg->data.str.area);
1183 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001184 arg->unresolved = 0;
1185 arg->data.srv = srv;
1186 break;
1187
1188 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001189 if (arg->data.str.data) {
1190 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001191 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001192 }
1193
1194 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001195 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1196 cur->file, cur->line, pname,
1197 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001198 cfgerr++;
1199 break;
1200 }
1201
1202 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001203 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
1204 cur->file, cur->line, pname,
1205 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001206 cfgerr++;
1207 break;
1208 }
1209
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001210 free(arg->data.str.area);
1211 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001212 arg->unresolved = 0;
1213 arg->data.prx = px;
1214 break;
1215
1216 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001217 if (arg->data.str.data) {
1218 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001219 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001220 }
1221
1222 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001223 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1224 cur->file, cur->line, pname,
1225 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001226 cfgerr++;
1227 break;
1228 }
1229
1230 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001231 ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
1232 cur->file, cur->line, pname,
1233 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001234 cfgerr++;
1235 break;
1236 }
1237
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001238 free(arg->data.str.area);
1239 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001240 arg->unresolved = 0;
1241 arg->data.prx = px;
1242 break;
1243
1244 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001245 if (arg->data.str.data)
1246 stktname = arg->data.str.area;
1247 else
1248 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001249
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001250 t = stktable_find_by_name(stktname);
1251 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001253 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001254 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001255 cfgerr++;
1256 break;
1257 }
1258
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001259 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001261 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001262 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001263 cfgerr++;
1264 break;
1265 }
1266
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001267 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001268 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001269 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001270 cfgerr++;
1271 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001272 }
1273
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001274 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001275 p->next_stkt_ref = t->proxies_list;
1276 t->proxies_list = p;
1277 }
1278
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001279 free(arg->data.str.area);
1280 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001281 arg->unresolved = 0;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001282 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001283 break;
1284
1285 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001286 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001287 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1288 cur->file, cur->line,
1289 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001290 cfgerr++;
1291 break;
1292 }
1293
1294 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001295 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001296 ul = p->uri_auth->userlist;
1297 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001298 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001299
1300 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001301 ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001302 cur->file, cur->line,
1303 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001304 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001305 cfgerr++;
1306 break;
1307 }
1308
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001309 free(arg->data.str.area);
1310 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001311 arg->unresolved = 0;
1312 arg->data.usr = ul;
1313 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001314
1315 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001316 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001317 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1318 cur->file, cur->line,
1319 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001320 cfgerr++;
1321 continue;
1322 }
1323
Willy Tarreau46947782015-01-19 19:00:58 +01001324 rflags = 0;
1325 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1326 err = NULL;
1327
Dragan Dosen26743032019-04-30 15:54:36 +02001328 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001329 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1330 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001331 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001333 cfgerr++;
1334 continue;
1335 }
1336
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001337 free(arg->data.str.area);
1338 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001339 arg->unresolved = 0;
1340 arg->data.reg = reg;
1341 break;
1342
1343
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001344 }
1345
1346 LIST_DEL(&cur->list);
1347 free(cur);
1348 } /* end of args processing */
1349
1350 return cfgerr;
1351}
1352
1353/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001354 * Process a fetch + format conversion as defined by the sample expression
1355 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001356 * not explicitly set to <smp_type>, but shall be compatible with it as
1357 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1358 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001359 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1360 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1361 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1362 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001363 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1364 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1365 * still hope to get it after waiting longer, and is not converted to string.
1366 * The possible output combinations are the following :
1367 *
1368 * return MAY_CHANGE FINAL Meaning for the sample
1369 * NULL * * Not present and will never be (eg: header)
1370 * smp 0 * Final value converted (eg: header)
1371 * smp 1 0 Not present yet, may appear later (eg: header)
1372 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001374struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001375 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001376 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001377{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001378 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001379
Willy Tarreau6c616e02014-06-25 16:56:41 +02001380 memset(smp, 0, sizeof(*smp));
1381
Willy Tarreau192252e2015-04-04 01:47:55 +02001382 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1384 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001386 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001387
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001388 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001389 return NULL;
1390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001391 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392 return NULL;
1393
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001394 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001395 return smp;
1396}
1397
Christopher Faulet476e5d02016-10-26 11:34:47 +02001398static void release_sample_arg(struct arg *p)
1399{
1400 struct arg *p_back = p;
1401
1402 if (!p)
1403 return;
1404
1405 while (p->type != ARGT_STOP) {
1406 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001407 free(p->data.str.area);
1408 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001409 p->unresolved = 0;
1410 }
1411 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001412 regex_free(p->data.reg);
1413 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001414 }
1415 p++;
1416 }
1417
1418 if (p_back != empty_arg_list)
1419 free(p_back);
1420}
1421
1422void release_sample_expr(struct sample_expr *expr)
1423{
1424 struct sample_conv_expr *conv_expr, *conv_exprb;
1425
1426 if (!expr)
1427 return;
1428
1429 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1430 release_sample_arg(conv_expr->arg_p);
1431 release_sample_arg(expr->arg_p);
1432 free(expr);
1433}
1434
Emeric Brun107ca302010-01-04 16:16:05 +01001435/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001436/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001437/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001438/*****************************************************************/
1439
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001440static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1441{
1442 int i;
1443 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001444 struct buffer *buf;
1445 struct sink *sink;
1446 struct ist line;
1447 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448
Willy Tarreau0851fd52019-12-17 10:07:25 +01001449 buf = alloc_trash_chunk();
1450 if (!buf)
1451 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001452
Willy Tarreau0851fd52019-12-17 10:07:25 +01001453 sink = (struct sink *)arg_p[1].data.str.area;
1454 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001455
Willy Tarreau0851fd52019-12-17 10:07:25 +01001456 pfx = arg_p[0].data.str.area;
1457 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001458
Willy Tarreau0851fd52019-12-17 10:07:25 +01001459 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1460 if (!sample_casts[smp->data.type][SMP_T_STR])
1461 goto nocast;
1462
1463 /* Copy sample fetch. This puts the sample as const, the
1464 * cast will copy data if a transformation is required.
1465 */
1466 memcpy(&tmp, smp, sizeof(struct sample));
1467 tmp.flags = SMP_F_CONST;
1468
1469 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1470 goto nocast;
1471
1472 /* Display the displayable chars*. */
1473 b_putchr(buf, '<');
1474 for (i = 0; i < tmp.data.u.str.data; i++) {
1475 if (isprint(tmp.data.u.str.area[i]))
1476 b_putchr(buf, tmp.data.u.str.area[i]);
1477 else
1478 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001479 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001480 b_putchr(buf, '>');
1481
1482 done:
1483 line = ist2(buf->area, buf->data);
1484 sink_write(sink, &line, 1);
1485 end:
1486 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001487 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001488 nocast:
1489 chunk_appendf(buf, "(undisplayable)");
1490 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001491}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001492
1493// This function checks the "debug" converter's arguments.
1494static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1495 const char *file, int line, char **err)
1496{
1497 const char *name = "buf0";
1498 struct sink *sink = NULL;
1499
1500 if (args[0].type != ARGT_STR) {
1501 /* optional prefix */
1502 args[0].data.str.area = "";
1503 args[0].data.str.data = 0;
1504 }
1505
1506 if (args[1].type == ARGT_STR)
1507 name = args[1].data.str.area;
1508
1509 sink = sink_find(name);
1510 if (!sink) {
1511 memprintf(err, "No such sink '%s'", name);
1512 return 0;
1513 }
1514
1515 args[1].data.str.area = (char *)sink;
1516 args[1].data.str.data = 0; // that's not a string anymore
1517 return 1;
1518}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001519
Holger Just1bfc24b2017-05-06 00:56:53 +02001520static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1521{
Willy Tarreau83061a82018-07-13 11:56:34 +02001522 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001523 int bin_len;
1524
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001525 trash->data = 0;
1526 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1527 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001528 if (bin_len < 0)
1529 return 0;
1530
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001531 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001532 smp->data.u.str = *trash;
1533 smp->data.type = SMP_T_BIN;
1534 smp->flags &= ~SMP_F_CONST;
1535 return 1;
1536}
1537
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001538static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001539{
Willy Tarreau83061a82018-07-13 11:56:34 +02001540 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001541 int b64_len;
1542
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001543 trash->data = 0;
1544 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1545 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001546 if (b64_len < 0)
1547 return 0;
1548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001549 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001550 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001551 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001552 smp->flags &= ~SMP_F_CONST;
1553 return 1;
1554}
1555
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001556static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1557{
1558 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001559 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001560
1561 memset(&ctx, 0, sizeof(ctx));
1562
1563 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1565 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001566
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001567 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001568 smp->data.u.str = *trash;
1569 smp->data.type = SMP_T_BIN;
1570 smp->flags &= ~SMP_F_CONST;
1571 return 1;
1572}
1573
Tim Duesterhusd4376302019-06-17 12:41:44 +02001574#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001575static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1576 const char *file, int line, char **err)
1577{
1578 if (args[0].type == ARGT_STOP)
1579 return 1;
1580 if (args[0].type != ARGT_SINT) {
1581 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1582 return 0;
1583 }
1584
1585 switch (args[0].data.sint) {
1586 case 224:
1587 case 256:
1588 case 384:
1589 case 512:
1590 /* this is okay */
1591 return 1;
1592 default:
1593 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1594 return 0;
1595 }
1596}
1597
Tim Duesterhusd4376302019-06-17 12:41:44 +02001598static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1599{
1600 struct buffer *trash = get_trash_chunk();
1601 int bits = 256;
1602 if (arg_p && arg_p->data.sint)
1603 bits = arg_p->data.sint;
1604
1605 switch (bits) {
1606 case 224: {
1607 SHA256_CTX ctx;
1608
1609 memset(&ctx, 0, sizeof(ctx));
1610
1611 SHA224_Init(&ctx);
1612 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1613 SHA224_Final((unsigned char *) trash->area, &ctx);
1614 trash->data = SHA224_DIGEST_LENGTH;
1615 break;
1616 }
1617 case 256: {
1618 SHA256_CTX ctx;
1619
1620 memset(&ctx, 0, sizeof(ctx));
1621
1622 SHA256_Init(&ctx);
1623 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1624 SHA256_Final((unsigned char *) trash->area, &ctx);
1625 trash->data = SHA256_DIGEST_LENGTH;
1626 break;
1627 }
1628 case 384: {
1629 SHA512_CTX ctx;
1630
1631 memset(&ctx, 0, sizeof(ctx));
1632
1633 SHA384_Init(&ctx);
1634 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1635 SHA384_Final((unsigned char *) trash->area, &ctx);
1636 trash->data = SHA384_DIGEST_LENGTH;
1637 break;
1638 }
1639 case 512: {
1640 SHA512_CTX ctx;
1641
1642 memset(&ctx, 0, sizeof(ctx));
1643
1644 SHA512_Init(&ctx);
1645 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1646 SHA512_Final((unsigned char *) trash->area, &ctx);
1647 trash->data = SHA512_DIGEST_LENGTH;
1648 break;
1649 }
1650 default:
1651 return 0;
1652 }
1653
1654 smp->data.u.str = *trash;
1655 smp->data.type = SMP_T_BIN;
1656 smp->flags &= ~SMP_F_CONST;
1657 return 1;
1658}
1659#endif
1660
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001661static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001662{
Willy Tarreau83061a82018-07-13 11:56:34 +02001663 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001664 unsigned char c;
1665 int ptr = 0;
1666
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001667 trash->data = 0;
1668 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1669 c = smp->data.u.str.area[ptr++];
1670 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1671 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001672 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001673 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001674 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001675 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001676 return 1;
1677}
1678
Dragan Dosen3f957b22017-10-24 09:27:34 +02001679static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1680{
1681 long long int n = 0;
1682 int i, c;
1683
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001684 for (i = 0; i < smp->data.u.str.data; i++) {
1685 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001686 return 0;
1687 n = (n << 4) + c;
1688 }
1689
1690 smp->data.u.sint = n;
1691 smp->data.type = SMP_T_SINT;
1692 smp->flags &= ~SMP_F_CONST;
1693 return 1;
1694}
1695
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001696/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001697static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001698{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001699 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1700 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001701 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001702 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001703 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001704 return 1;
1705}
1706
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001707static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001708{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001709 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001710 smp->data.u.sint = i;
1711 smp->data.type = SMP_T_SINT;
1712 return 1;
1713}
1714
1715
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001716static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001717{
1718 int i;
1719
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001720 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001721 return 0;
1722
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001723 for (i = 0; i < smp->data.u.str.data; i++) {
1724 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1725 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001726 }
1727 return 1;
1728}
1729
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001730static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001731{
1732 int i;
1733
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001734 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001735 return 0;
1736
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001737 for (i = 0; i < smp->data.u.str.data; i++) {
1738 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1739 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001740 }
1741 return 1;
1742}
1743
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001744/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1745static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001746{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001747 /* Attempt to convert to IPv4 to apply the correct mask. */
1748 c_ipv62ip(smp);
1749
1750 if (smp->data.type == SMP_T_IPV4) {
1751 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1752 smp->data.type = SMP_T_IPV4;
1753 }
1754 else if (smp->data.type == SMP_T_IPV6) {
1755 /* IPv6 cannot be converted without an IPv6 mask. */
1756 if (args[1].type != ARGT_IPV6)
1757 return 0;
1758
1759 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1760 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1761 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1762 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1763 smp->data.type = SMP_T_IPV6;
1764 }
1765
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001766 return 1;
1767}
1768
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001769/* takes an UINT value on input supposed to represent the time since EPOCH,
1770 * adds an optional offset found in args[1] and emits a string representing
1771 * the local time in the format specified in args[1] using strftime().
1772 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001773static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001774{
Willy Tarreau83061a82018-07-13 11:56:34 +02001775 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001776 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001777 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001778 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001779
1780 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001781 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001782 curr_date += args[1].data.sint;
1783
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001784 tm = localtime(&curr_date);
1785 if (!tm)
1786 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001787 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001788 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1789 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001790 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001791 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001792 return 1;
1793}
1794
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001795/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001796static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001797{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001798 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1799 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001800 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001801 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001802 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001803 return 1;
1804}
1805
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001806/* takes an UINT value on input supposed to represent the time since EPOCH,
1807 * adds an optional offset found in args[1] and emits a string representing
1808 * the UTC date in the format specified in args[1] using strftime().
1809 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001810static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001811{
Willy Tarreau83061a82018-07-13 11:56:34 +02001812 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001813 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001814 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001815 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001816
1817 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001818 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001819 curr_date += args[1].data.sint;
1820
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001821 tm = gmtime(&curr_date);
1822 if (!tm)
1823 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001824 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1826 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001827 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001828 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001829 return 1;
1830}
1831
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001832/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001833static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001834{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1836 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001837 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001838 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001839 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001840 return 1;
1841}
1842
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001843/* hashes the binary input into a 32-bit unsigned int using xxh.
1844 * The seed of the hash defaults to 0 but can be changd in argument 1.
1845 */
1846static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1847{
1848 unsigned int seed;
1849
1850 if (arg_p && arg_p->data.sint)
1851 seed = arg_p->data.sint;
1852 else
1853 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1855 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001856 smp->data.type = SMP_T_SINT;
1857 return 1;
1858}
1859
1860/* hashes the binary input into a 64-bit unsigned int using xxh.
1861 * In fact, the function returns a 64 bit unsigned, but the sample
1862 * storage of haproxy only proposes 64-bits signed, so the value is
1863 * cast as signed. This cast doesn't impact the hash repartition.
1864 * The seed of the hash defaults to 0 but can be changd in argument 1.
1865 */
1866static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1867{
1868 unsigned long long int seed;
1869
1870 if (arg_p && arg_p->data.sint)
1871 seed = (unsigned long long int)arg_p->data.sint;
1872 else
1873 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001874 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1875 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001876 smp->data.type = SMP_T_SINT;
1877 return 1;
1878}
1879
Willy Tarreau80599772015-01-20 19:35:24 +01001880/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001881static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001882{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001883 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1884 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001885 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001886 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001887 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001888 return 1;
1889}
1890
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001891/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1892static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1893{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001894 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1895 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001896 if (arg_p && arg_p->data.sint)
1897 smp->data.u.sint = full_hash(smp->data.u.sint);
1898 smp->data.type = SMP_T_SINT;
1899 return 1;
1900}
1901
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001902/* This function escape special json characters. The returned string can be
1903 * safely set between two '"' and used as json string. The json string is
1904 * defined like this:
1905 *
1906 * any Unicode character except '"' or '\' or control character
1907 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1908 *
1909 * The enum input_type contain all the allowed mode for decoding the input
1910 * string.
1911 */
1912enum input_type {
1913 IT_ASCII = 0,
1914 IT_UTF8,
1915 IT_UTF8S,
1916 IT_UTF8P,
1917 IT_UTF8PS,
1918};
1919static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1920 const char *file, int line, char **err)
1921{
1922 if (!arg) {
1923 memprintf(err, "Unexpected empty arg list");
1924 return 0;
1925 }
1926
1927 if (arg->type != ARGT_STR) {
1928 memprintf(err, "Unexpected arg type");
1929 return 0;
1930 }
1931
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001932 if (strcmp(arg->data.str.area, "") == 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, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001939 arg->type = ARGT_SINT;
1940 arg->data.sint = IT_ASCII;
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, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001945 arg->type = ARGT_SINT;
1946 arg->data.sint = IT_UTF8;
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, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001951 arg->type = ARGT_SINT;
1952 arg->data.sint = IT_UTF8S;
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, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001957 arg->type = ARGT_SINT;
1958 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001959 return 1;
1960 }
1961
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001962 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001963 arg->type = ARGT_SINT;
1964 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001965 return 1;
1966 }
1967
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001968 memprintf(err, "Unexpected input code type. "
1969 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001970 return 0;
1971}
1972
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001973static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001974{
Willy Tarreau83061a82018-07-13 11:56:34 +02001975 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001976 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1977 const char *str;
1978 int len;
1979 enum input_type input_type = IT_ASCII;
1980 unsigned int c;
1981 unsigned int ret;
1982 char *p;
1983
1984 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001985 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001986
1987 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001988 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001989
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001990 p = smp->data.u.str.area;
1991 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001992
1993 if (input_type == IT_ASCII) {
1994 /* Read input as ASCII. */
1995 c = *(unsigned char *)p;
1996 p++;
1997 }
1998 else {
1999 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002000 ret = utf8_next(p,
2001 smp->data.u.str.data - ( p - smp->data.u.str.area),
2002 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002003 p += utf8_return_length(ret);
2004
2005 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2006 return 0;
2007 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2008 continue;
2009 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2010 return 0;
2011 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2012 continue;
2013
2014 /* Check too big values. */
2015 if ((unsigned int)c > 0xffff) {
2016 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2017 return 0;
2018 continue;
2019 }
2020 }
2021
2022 /* Convert character. */
2023 if (c == '"') {
2024 len = 2;
2025 str = "\\\"";
2026 }
2027 else if (c == '\\') {
2028 len = 2;
2029 str = "\\\\";
2030 }
2031 else if (c == '/') {
2032 len = 2;
2033 str = "\\/";
2034 }
2035 else if (c == '\b') {
2036 len = 2;
2037 str = "\\b";
2038 }
2039 else if (c == '\f') {
2040 len = 2;
2041 str = "\\f";
2042 }
2043 else if (c == '\r') {
2044 len = 2;
2045 str = "\\r";
2046 }
2047 else if (c == '\n') {
2048 len = 2;
2049 str = "\\n";
2050 }
2051 else if (c == '\t') {
2052 len = 2;
2053 str = "\\t";
2054 }
2055 else if (c > 0xff || !isprint(c)) {
2056 /* isprint generate a segfault if c is too big. The man says that
2057 * c must have the value of an unsigned char or EOF.
2058 */
2059 len = 6;
2060 _str[0] = '\\';
2061 _str[1] = 'u';
2062 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2063 str = _str;
2064 }
2065 else {
2066 len = 1;
2067 str = (char *)&c;
2068 }
2069
2070 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002071 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002072 return 0;
2073
2074 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002075 memcpy(temp->area + temp->data, str, len);
2076 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002077 }
2078
2079 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002080 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002081 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002082
2083 return 1;
2084}
2085
Emeric Brun54c4ac82014-11-03 15:32:43 +01002086/* This sample function is designed to extract some bytes from an input buffer.
2087 * First arg is the offset.
2088 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002089static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002090{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002091 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2092 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002093 return 1;
2094 }
2095
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002096 if (smp->data.u.str.size)
2097 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 smp->data.u.str.data -= arg_p[0].data.sint;
2099 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002100
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002101 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2102 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002103
2104 return 1;
2105}
2106
Emeric Brunf399b0d2014-11-03 17:07:03 +01002107static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2108 const char *file, int line, char **err)
2109{
2110 struct arg *arg = args;
2111
2112 if (!arg) {
2113 memprintf(err, "Unexpected empty arg list");
2114 return 0;
2115 }
2116
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002117 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002118 memprintf(err, "Unexpected arg type");
2119 return 0;
2120 }
2121
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002122 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002123 memprintf(err, "Unexpected value 0 for index");
2124 return 0;
2125 }
2126
2127 arg++;
2128
2129 if (arg->type != ARGT_STR) {
2130 memprintf(err, "Unexpected arg type");
2131 return 0;
2132 }
2133
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002134 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002135 memprintf(err, "Empty separators list");
2136 return 0;
2137 }
2138
2139 return 1;
2140}
2141
2142/* This sample function is designed to a return selected part of a string (field).
2143 * First arg is the index of the field (start at 1)
2144 * Second arg is a char list of separators (type string)
2145 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002146static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002147{
Marcin Deranek9631a282018-04-16 14:30:46 +02002148 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002149 char *start, *end;
2150 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002151 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002152
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002153 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002154 return 0;
2155
Marcin Deranek9631a282018-04-16 14:30:46 +02002156 if (arg_p[0].data.sint < 0) {
2157 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002158 end = start = smp->data.u.str.area + smp->data.u.str.data;
2159 while (start > smp->data.u.str.area) {
2160 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2161 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002162 if (field == arg_p[0].data.sint) {
2163 if (count == 1)
2164 goto found;
2165 else if (count > 1)
2166 count--;
2167 } else {
2168 end = start-1;
2169 field--;
2170 }
2171 break;
2172 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002173 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002174 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002175 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002176 } else {
2177 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002178 end = start = smp->data.u.str.area;
2179 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2180 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2181 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002182 if (field == arg_p[0].data.sint) {
2183 if (count == 1)
2184 goto found;
2185 else if (count > 1)
2186 count--;
2187 } else {
2188 start = end+1;
2189 field++;
2190 }
2191 break;
2192 }
2193 }
2194 end++;
2195 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002196 }
2197
2198 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002199 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002200 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002201 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002202 }
2203found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002205 /* If ret string is len 0, no need to
2206 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002208 return 1;
2209
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002210 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002211
2212 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002213 Note: smp->data.u.str.size cannot be set to 0 */
2214 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002215 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002216
2217 return 1;
2218}
2219
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002220/* This sample function is designed to return a word from a string.
2221 * First arg is the index of the word (start at 1)
2222 * Second arg is a char list of words separators (type string)
2223 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002224static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002225{
Marcin Deranek9631a282018-04-16 14:30:46 +02002226 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002227 char *start, *end;
2228 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002229 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002230
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002231 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002232 return 0;
2233
2234 word = 0;
2235 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002236 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002237 end = start = smp->data.u.str.area + smp->data.u.str.data;
2238 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002239 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002240 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2241 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002242 issep = 1;
2243 break;
2244 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002245 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002246 if (!inword) {
2247 if (!issep) {
2248 if (word != arg_p[0].data.sint) {
2249 word--;
2250 end = start;
2251 }
2252 inword = 1;
2253 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002254 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002255 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002256 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002257 if (count == 1)
2258 goto found;
2259 else if (count > 1)
2260 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002261 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002262 inword = 0;
2263 }
2264 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002265 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002266 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002267 end = start = smp->data.u.str.area;
2268 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002269 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002270 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2271 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002272 issep = 1;
2273 break;
2274 }
2275 }
2276 if (!inword) {
2277 if (!issep) {
2278 if (word != arg_p[0].data.sint) {
2279 word++;
2280 start = end;
2281 }
2282 inword = 1;
2283 }
2284 }
2285 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002286 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002287 if (count == 1)
2288 goto found;
2289 else if (count > 1)
2290 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002291 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002292 inword = 0;
2293 }
2294 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002295 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002296 }
2297
2298 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002299 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002301 return 1;
2302 }
2303found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002304 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002305 /* If ret string is len 0, no need to
2306 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002307 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002308 return 1;
2309
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002310 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002311
2312 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002313 Note: smp->data.u.str.size cannot be set to 0 */
2314 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002315 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002316
2317 return 1;
2318}
2319
Willy Tarreau7eda8492015-01-20 19:47:06 +01002320static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2321 const char *file, int line, char **err)
2322{
2323 struct arg *arg = args;
2324 char *p;
2325 int len;
2326
2327 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2328 arg[0].type_flags = 0;
2329
2330 if (arg[2].type != ARGT_STR)
2331 return 1;
2332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002333 p = arg[2].data.str.area;
2334 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002335 while (len) {
2336 if (*p == 'i') {
2337 arg[0].type_flags |= ARGF_REG_ICASE;
2338 }
2339 else if (*p == 'g') {
2340 arg[0].type_flags |= ARGF_REG_GLOB;
2341 }
2342 else {
2343 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2344 return 0;
2345 }
2346 p++;
2347 len--;
2348 }
2349 return 1;
2350}
2351
2352/* This sample function is designed to do the equivalent of s/match/replace/ on
2353 * the input string. It applies a regex and restarts from the last matched
2354 * location until nothing matches anymore. First arg is the regex to apply to
2355 * the input string, second arg is the replacement expression.
2356 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002357static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002358{
2359 char *start, *end;
2360 struct my_regex *reg = arg_p[0].data.reg;
2361 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002362 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002363 int flag, max;
2364 int found;
2365
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002366 start = smp->data.u.str.area;
2367 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002368
2369 flag = 0;
2370 while (1) {
2371 /* check for last round which is used to copy remaining parts
2372 * when not running in global replacement mode.
2373 */
2374 found = 0;
2375 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2376 /* Note: we can have start == end on empty strings or at the end */
2377 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2378 }
2379
2380 if (!found)
2381 pmatch[0].rm_so = end - start;
2382
2383 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002384 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002385 if (max && pmatch[0].rm_so > 0) {
2386 if (max > pmatch[0].rm_so)
2387 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002388 memcpy(trash->area + trash->data, start, max);
2389 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002390 }
2391
2392 if (!found)
2393 break;
2394
2395 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002396 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002397 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002398 if (max > arg_p[1].data.str.data)
2399 max = arg_p[1].data.str.data;
2400 memcpy(trash->area + trash->data,
2401 arg_p[1].data.str.area, max);
2402 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002403 }
2404
2405 /* 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);