blob: 9c20469c5aa53a624786f08c37932dbca85ccb87 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020025#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020026#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020027#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020028
Willy Tarreau9fcb9842012-04-20 14:45:49 +020029#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020030#include <proto/auth.h>
31#include <proto/log.h>
32#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010033#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020034#include <proto/sample.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020035#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020036#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010037
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020038#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010039#include <import/xxhash.h>
40
Willy Tarreau1cf8f082014-02-07 12:14:54 +010041/* sample type names */
42const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020043 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010044 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010045 [SMP_T_SINT] = "sint",
46 [SMP_T_ADDR] = "addr",
47 [SMP_T_IPV4] = "ipv4",
48 [SMP_T_IPV6] = "ipv6",
49 [SMP_T_STR] = "str",
50 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020051 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010052};
53
Willy Tarreau12785782012-04-27 21:37:17 +020054/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020055static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010056
Willy Tarreau12785782012-04-27 21:37:17 +020057/* list head of all known sample fetch keywords */
58static struct sample_fetch_kw_list sample_fetches = {
59 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010060};
61
Willy Tarreau12785782012-04-27 21:37:17 +020062/* list head of all known sample format conversion keywords */
63static struct sample_conv_kw_list sample_convs = {
64 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010065};
66
Willy Tarreau80aca902013-01-07 15:42:20 +010067const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
68 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
69 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
70 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
71 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
72 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
73 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
74 SMP_VAL_FE_LOG_END),
75
76 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
77 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
78 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
79 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
80 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
81 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
82 SMP_VAL_FE_LOG_END),
83
84 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
85 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
86 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
87 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
88 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
89 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
90 SMP_VAL_FE_LOG_END),
91
92 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
93 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
94 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
95 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
96 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
97 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
98 SMP_VAL_FE_LOG_END),
99
100 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
101 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
102 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
103 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
104 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
105 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
106 SMP_VAL_FE_LOG_END),
107
108 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
109 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
110 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
111 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
112 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
113 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
114 SMP_VAL_FE_LOG_END),
115
116 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
117 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
118 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
119 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
120 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
121 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
122 SMP_VAL___________),
123
124 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
125 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
126 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
127 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
128 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
129 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
130 SMP_VAL___________),
131
132 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
133 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
134 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
135 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
136 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
137 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
138 SMP_VAL_FE_LOG_END),
139
140 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
141 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
142 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
143 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
144 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
145 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
146 SMP_VAL___________),
147
148 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
149 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
150 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
151 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
152 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
153 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
154 SMP_VAL_FE_LOG_END),
155
156 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
157 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
158 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
160 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
161 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
162 SMP_VAL_FE_LOG_END),
163
164 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
165 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
166 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
168 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
169 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
170 SMP_VAL_FE_LOG_END),
171
172 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
173 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
174 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
176 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
177 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
178 SMP_VAL_FE_LOG_END),
179
180 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
181 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
182 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
184 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
185 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
186 SMP_VAL___________),
187
188 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
189 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
190 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
192 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
193 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
194 SMP_VAL___________),
195
196 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
197 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
198 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
200 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
201 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
202 SMP_VAL_FE_LOG_END),
203
204 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
205 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
206 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
209 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
210 SMP_VAL___________),
211
212 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
213 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
214 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL_FE_LOG_END),
219
220 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
221 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
222 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL_FE_LOG_END),
227
228 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
229 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
230 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL_FE_LOG_END),
235
236 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
237 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
238 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL_FE_LOG_END),
243};
244
245static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
246 [SMP_SRC_INTRN] = "internal state",
247 [SMP_SRC_LISTN] = "listener",
248 [SMP_SRC_FTEND] = "frontend",
249 [SMP_SRC_L4CLI] = "client address",
250 [SMP_SRC_L5CLI] = "client-side connection",
251 [SMP_SRC_TRACK] = "track counters",
252 [SMP_SRC_L6REQ] = "request buffer",
253 [SMP_SRC_HRQHV] = "HTTP request headers",
254 [SMP_SRC_HRQHP] = "HTTP request",
255 [SMP_SRC_HRQBO] = "HTTP request body",
256 [SMP_SRC_BKEND] = "backend",
257 [SMP_SRC_SERVR] = "server",
258 [SMP_SRC_L4SRV] = "server address",
259 [SMP_SRC_L5SRV] = "server-side connection",
260 [SMP_SRC_L6RES] = "response buffer",
261 [SMP_SRC_HRSHV] = "HTTP response headers",
262 [SMP_SRC_HRSHP] = "HTTP response",
263 [SMP_SRC_HRSBO] = "HTTP response body",
264 [SMP_SRC_RQFIN] = "request buffer statistics",
265 [SMP_SRC_RSFIN] = "response buffer statistics",
266 [SMP_SRC_TXFIN] = "transaction statistics",
267 [SMP_SRC_SSFIN] = "session statistics",
268};
269
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100270static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
271 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
272 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
273 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
274 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
275 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
276 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
277 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
278 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
279 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
280 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
281 [SMP_CKP_BE_SRV_CON] = "server source selection",
282 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
283 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
284 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
285 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
286 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
287 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
288 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
289 [SMP_CKP_FE_LOG_END] = "logs",
290};
291
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100292/* This function returns the type of the data returned by the sample_expr.
293 * It assumes that the <expr> and all of its converters are properly
294 * initialized.
295 */
296inline
297int smp_expr_output_type(struct sample_expr *expr)
298{
299 struct sample_conv_expr *smp_expr;
300
301 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
302 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
303 return smp_expr->conv->out_type;
304 }
305 return expr->fetch->out_type;
306}
307
308
Willy Tarreau80aca902013-01-07 15:42:20 +0100309/* fill the trash with a comma-delimited list of source names for the <use> bit
310 * field which must be composed of a non-null set of SMP_USE_* flags. The return
311 * value is the pointer to the string in the trash buffer.
312 */
313const char *sample_src_names(unsigned int use)
314{
315 int bit;
316
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200317 trash.data = 0;
318 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100319 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
320 if (!(use & ~((1 << bit) - 1)))
321 break; /* no more bits */
322
323 if (!(use & (1 << bit)))
324 continue; /* bit not set */
325
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200326 trash.data += snprintf(trash.area + trash.data,
327 trash.size - trash.data, "%s%s",
328 (use & ((1 << bit) - 1)) ? "," : "",
329 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200331 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100332}
333
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100334/* return a pointer to the correct sample checkpoint name, or "unknown" when
335 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
336 * if set.
337 */
338const char *sample_ckp_names(unsigned int use)
339{
340 int bit;
341
342 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
343 if (use & (1 << bit))
344 return fetch_ckp_names[bit];
345 return "unknown sample check place, please report this bug";
346}
347
Emeric Brun107ca302010-01-04 16:16:05 +0100348/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100349 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
350 * for next parsing sessions. The fetch keywords capabilities are also computed
351 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100352 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100353void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100354{
Willy Tarreau80aca902013-01-07 15:42:20 +0100355 struct sample_fetch *sf;
356 int bit;
357
358 for (sf = kwl->kw; sf->kw != NULL; sf++) {
359 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
360 if (sf->use & (1 << bit))
361 sf->val |= fetch_cap[bit];
362 }
363 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100364}
365
366/*
Willy Tarreau12785782012-04-27 21:37:17 +0200367 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100368 * parsing sessions.
369 */
Willy Tarreau12785782012-04-27 21:37:17 +0200370void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100371{
Willy Tarreau12785782012-04-27 21:37:17 +0200372 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100373}
374
375/*
Willy Tarreau12785782012-04-27 21:37:17 +0200376 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100377 * string of <len> in buffer <kw>.
378 *
379 */
Willy Tarreau12785782012-04-27 21:37:17 +0200380struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100381{
382 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200383 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100384
Willy Tarreau12785782012-04-27 21:37:17 +0200385 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100386 for (index = 0; kwl->kw[index].kw != NULL; index++) {
387 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
388 kwl->kw[index].kw[len] == '\0')
389 return &kwl->kw[index];
390 }
391 }
392 return NULL;
393}
394
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100395/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100396 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100397 * <idx> is the index of the next sample fetch entry. It is used as private
398 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100401 * the list is reached.
402 */
403struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
404{
405 struct sample_fetch_kw_list *kwl;
406 struct sample_fetch *base;
407
408 if (!current) {
409 /* Get first kwl entry. */
410 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
411 (*idx) = 0;
412 } else {
413 /* Get kwl corresponding to the curret entry. */
414 base = current + 1 - (*idx);
415 kwl = container_of(base, struct sample_fetch_kw_list, kw);
416 }
417
418 while (1) {
419
420 /* Check if kwl is the last entry. */
421 if (&kwl->list == &sample_fetches.list)
422 return NULL;
423
424 /* idx contain the next keyword. If it is available, return it. */
425 if (kwl->kw[*idx].kw) {
426 (*idx)++;
427 return &kwl->kw[(*idx)-1];
428 }
429
430 /* get next entry in the main list, and return NULL if the end is reached. */
431 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
432
433 /* Set index to 0, ans do one other loop. */
434 (*idx) = 0;
435 }
436}
437
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100438/* This function browses the list of available converters. <current> is
439 * the last used converter. If it is the first call, it must set to NULL.
440 * <idx> is the index of the next converter entry. It is used as private
441 * value. It is useless to initiate it.
442 *
443 * It returns always the next sample_conv entry, and NULL when the end of
444 * the list is reached.
445 */
446struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
447{
448 struct sample_conv_kw_list *kwl;
449 struct sample_conv *base;
450
451 if (!current) {
452 /* Get first kwl entry. */
453 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
454 (*idx) = 0;
455 } else {
456 /* Get kwl corresponding to the curret entry. */
457 base = current + 1 - (*idx);
458 kwl = container_of(base, struct sample_conv_kw_list, kw);
459 }
460
461 while (1) {
462 /* Check if kwl is the last entry. */
463 if (&kwl->list == &sample_convs.list)
464 return NULL;
465
466 /* idx contain the next keyword. If it is available, return it. */
467 if (kwl->kw[*idx].kw) {
468 (*idx)++;
469 return &kwl->kw[(*idx)-1];
470 }
471
472 /* get next entry in the main list, and return NULL if the end is reached. */
473 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
474
475 /* Set index to 0, ans do one other loop. */
476 (*idx) = 0;
477 }
478}
479
Emeric Brun107ca302010-01-04 16:16:05 +0100480/*
Willy Tarreau12785782012-04-27 21:37:17 +0200481 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100482 * string of <len> in buffer <kw>.
483 *
484 */
Willy Tarreau12785782012-04-27 21:37:17 +0200485struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100486{
487 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200488 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100489
Willy Tarreau12785782012-04-27 21:37:17 +0200490 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100491 for (index = 0; kwl->kw[index].kw != NULL; index++) {
492 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
493 kwl->kw[index].kw[len] == '\0')
494 return &kwl->kw[index];
495 }
496 }
497 return NULL;
498}
499
Emeric Brun107ca302010-01-04 16:16:05 +0100500/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200501/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100502/******************************************************************/
503
Willy Tarreau342acb42012-04-23 22:03:39 +0200504static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100505{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200506 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200507 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100508 return 1;
509}
510
Willy Tarreau342acb42012-04-23 22:03:39 +0200511static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100512{
Willy Tarreau83061a82018-07-13 11:56:34 +0200513 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100514
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200515 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100516 return 0;
517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200519 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200520 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100521 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100522
523 return 1;
524}
525
Willy Tarreau342acb42012-04-23 22:03:39 +0200526static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100527{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200528 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200529 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100530 return 1;
531}
532
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200533static int c_ipv62ip(struct sample *smp)
534{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200535 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100537 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200538 return 1;
539}
540
Willy Tarreau342acb42012-04-23 22:03:39 +0200541static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100542{
Willy Tarreau83061a82018-07-13 11:56:34 +0200543 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100544
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200545 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100546 return 0;
547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200549 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100551 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100552 return 1;
553}
554
555/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200556static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100557{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200558 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100559}
560*/
561
Willy Tarreau342acb42012-04-23 22:03:39 +0200562static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100563{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200564 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200565 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100566 return 1;
567}
568
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200569static int c_int2ipv6(struct sample *smp)
570{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200571 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
572 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200573 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200574 return 1;
575}
576
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100577static int c_str2addr(struct sample *smp)
578{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200579 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
580 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100581 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200582 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100583 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100585 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200586 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100587 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 return 1;
589}
590
Willy Tarreau342acb42012-04-23 22:03:39 +0200591static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100592{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200593 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100594 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200595 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100596 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 1;
598}
599
Willy Tarreau342acb42012-04-23 22:03:39 +0200600static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100601{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100603 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200604 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100605 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100607}
608
Emeric Brun4b9e8022014-11-03 18:17:10 +0100609/*
610 * The NULL char always enforces the end of string if it is met.
611 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100612 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200613static int c_bin2str(struct sample *smp)
614{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100615 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200617 for (i = 0; i < smp->data.u.str.data; i++) {
618 if (!smp->data.u.str.area[i]) {
619 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100620 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100621 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200622 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200623 return 1;
624}
625
Willy Tarreau342acb42012-04-23 22:03:39 +0200626static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100627{
Willy Tarreau83061a82018-07-13 11:56:34 +0200628 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100629 char *pos;
630
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200631 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100632 if (!pos)
633 return 0;
634
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200635 trash->size = trash->size - (pos - trash->area);
636 trash->area = pos;
637 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200638 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200639 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100640 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100641 return 1;
642}
643
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800644/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200645 * For strings and binary blocks, it also provides a known allocated size with
646 * a length that is capped to the size, and ensures a trailing zero is always
647 * appended for strings. This is necessary for some operations which may
648 * require to extend the length. It returns 0 if it fails, 1 on success.
649 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100650int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200651{
Willy Tarreau83061a82018-07-13 11:56:34 +0200652 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200653
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200654 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100655 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100656 case SMP_T_SINT:
657 case SMP_T_ADDR:
658 case SMP_T_IPV4:
659 case SMP_T_IPV6:
660 /* These type are not const. */
661 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200662
Christopher Fauletec100512017-07-24 15:38:41 +0200663 case SMP_T_METH:
664 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
665 break;
666 /* Fall through */
667
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100668 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200669 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100670 trash->data = smp->data.type == SMP_T_STR ?
671 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200672 if (trash->data > trash->size - 1)
673 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200674
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100675 memcpy(trash->area, smp->data.type == SMP_T_STR ?
676 smp->data.u.str.area : smp->data.u.meth.str.area,
677 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200678 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200679 smp->data.u.str = *trash;
680 break;
681
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100682 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100683 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200684 trash->data = smp->data.u.str.data;
685 if (trash->data > trash->size)
686 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200687
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200688 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200689 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100690 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200691
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100692 default:
693 /* Other cases are unexpected. */
694 return 0;
695 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100696
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100697 /* remove const flag */
698 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200699 return 1;
700}
701
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100702int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100703{
704 return 1;
705}
706
Willy Tarreau342acb42012-04-23 22:03:39 +0200707static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100708{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200709 const char *str;
710 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100711
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200712 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100713 return 0;
714
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 str = smp->data.u.str.area;
716 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100717
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200718 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200719 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100720 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100721 return 1;
722}
723
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100724static int c_str2meth(struct sample *smp)
725{
726 enum http_meth_t meth;
727 int len;
728
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200729 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100730 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200731 len = smp->data.u.str.data;
732 smp->data.u.meth.str.area = smp->data.u.str.area;
733 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100734 }
735 else
736 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200737 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100739 return 1;
740}
741
742static int c_meth2str(struct sample *smp)
743{
744 int len;
745 enum http_meth_t meth;
746
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200747 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100748 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200749 len = smp->data.u.meth.str.data;
750 smp->data.u.str.area = smp->data.u.meth.str.area;
751 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200752 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100753 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200754 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100755 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200756 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200757 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200758 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100759 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200760 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100761 }
762 else {
763 /* Unknown method */
764 return 0;
765 }
766 return 1;
767}
768
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200769static int c_addr2bin(struct sample *smp)
770{
Willy Tarreau83061a82018-07-13 11:56:34 +0200771 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200773 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200774 chk->data = 4;
775 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200776 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200777 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200778 chk->data = 16;
779 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200780 }
781 else
782 return 0;
783
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200784 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200785 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200786 return 1;
787}
788
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200789static int c_int2bin(struct sample *smp)
790{
Willy Tarreau83061a82018-07-13 11:56:34 +0200791 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200792
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200793 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
794 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200795
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200796 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200797 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200798 return 1;
799}
800
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200801
Emeric Brun107ca302010-01-04 16:16:05 +0100802/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200803/* Sample casts matrix: */
804/* sample_casts[from type][to type] */
805/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100806/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100807
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100808sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200809/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
810/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
811/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200812/* 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 +0200813/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
814/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200815/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200816/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
817/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
818/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200819};
Emeric Brun107ca302010-01-04 16:16:05 +0100820
Emeric Brun107ca302010-01-04 16:16:05 +0100821/*
Willy Tarreau12785782012-04-27 21:37:17 +0200822 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100823 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200824 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200825 * The caller must have set al->ctx.
Emeric Brun107ca302010-01-04 16:16:05 +0100826 */
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +0100827struct 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 +0100828{
Willy Tarreau833cc792013-07-24 15:34:19 +0200829 const char *begw; /* beginning of word */
830 const char *endw; /* end of word */
831 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200832 struct sample_expr *expr;
833 struct sample_fetch *fetch;
834 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100835 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200836 char *fkw = NULL;
837 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100838 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100839
Willy Tarreau833cc792013-07-24 15:34:19 +0200840 begw = str[*idx];
841 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
842
843 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100844 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100845 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200846 }
Emeric Brun107ca302010-01-04 16:16:05 +0100847
Willy Tarreau833cc792013-07-24 15:34:19 +0200848 /* keep a copy of the current fetch keyword for error reporting */
849 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100850
Willy Tarreau833cc792013-07-24 15:34:19 +0200851 fetch = find_sample_fetch(begw, endw - begw);
852 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100853 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100854 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200855 }
Emeric Brun107ca302010-01-04 16:16:05 +0100856
Willy Tarreau833cc792013-07-24 15:34:19 +0200857 endt = endw;
858 if (*endt == '(') {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100859 /* look for the end of this term and skip the opening parenthesis */
860 endt = ++endw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200861 while (*endt && *endt != ')')
862 endt++;
863 if (*endt != ')') {
Willy Tarreau975c1782013-12-12 23:16:54 +0100864 memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200865 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200866 }
Emeric Brun485479d2010-09-23 18:02:19 +0200867 }
Emeric Brun107ca302010-01-04 16:16:05 +0100868
Willy Tarreau833cc792013-07-24 15:34:19 +0200869 /* At this point, we have :
870 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100871 * - endw : end of the keyword, first character not part of keyword
872 * nor the opening parenthesis (so first character of args
873 * if present).
Willy Tarreau833cc792013-07-24 15:34:19 +0200874 * - endt : end of the term (=endw or last parenthesis if args are present)
875 */
876
877 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100878 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100879 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200880 }
Emeric Brun107ca302010-01-04 16:16:05 +0100881 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200882
Vincent Bernat02779b62016-04-03 13:48:43 +0200883 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200884 if (!expr)
885 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100886
887 LIST_INIT(&(expr->conv_exprs));
888 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200889 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100890
Willy Tarreau689a1df2013-12-13 00:40:11 +0100891 /* Note that we call the argument parser even with an empty string,
892 * this allows it to automatically create entries for mandatory
893 * implicit arguments (eg: local proxy name).
894 */
895 al->kw = expr->fetch->kw;
896 al->conv = NULL;
897 if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
898 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
899 goto out_error;
900 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200901
Willy Tarreau689a1df2013-12-13 00:40:11 +0100902 if (!expr->arg_p) {
903 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200904 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100905 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
906 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200907 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100908 }
909
Willy Tarreau833cc792013-07-24 15:34:19 +0200910 /* Now process the converters if any. We have two supported syntaxes
911 * for the converters, which can be combined :
912 * - comma-delimited list of converters just after the keyword and args ;
913 * - one converter per keyword
914 * The combination allows to have each keyword being a comma-delimited
915 * series of converters.
916 *
917 * We want to process the former first, then the latter. For this we start
918 * from the beginning of the supposed place in the exiting conv chain, which
919 * starts at the last comma (endt).
920 */
921
922 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200923 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +0100924
Willy Tarreau833cc792013-07-24 15:34:19 +0200925 if (*endt == ')') /* skip last closing parenthesis */
926 endt++;
Emeric Brun107ca302010-01-04 16:16:05 +0100927
Willy Tarreau833cc792013-07-24 15:34:19 +0200928 if (*endt && *endt != ',') {
929 if (ckw)
Willy Tarreau97108e02016-11-25 07:33:24 +0100930 memprintf(err_msg, "missing comma after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200931 else
Willy Tarreau975c1782013-12-12 23:16:54 +0100932 memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100933 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200934 }
Emeric Brun107ca302010-01-04 16:16:05 +0100935
Willy Tarreau833cc792013-07-24 15:34:19 +0200936 while (*endt == ',') /* then trailing commas */
937 endt++;
938
Willy Tarreau97108e02016-11-25 07:33:24 +0100939 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200940
941 if (!*begw) {
942 /* none ? skip to next string */
943 (*idx)++;
944 begw = str[*idx];
945 if (!begw || !*begw)
946 break;
947 }
948
949 for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++);
950
951 free(ckw);
952 ckw = my_strndup(begw, endw - begw);
953
954 conv = find_sample_conv(begw, endw - begw);
955 if (!conv) {
956 /* we found an isolated keyword that we don't know, it's not ours */
957 if (begw == str[*idx])
958 break;
Willy Tarreau97108e02016-11-25 07:33:24 +0100959 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200960 goto out_error;
961 }
Emeric Brun107ca302010-01-04 16:16:05 +0100962
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 endt = endw;
964 if (*endt == '(') {
965 /* look for the end of this term */
966 while (*endt && *endt != ')')
967 endt++;
968 if (*endt != ')') {
Willy Tarreau97108e02016-11-25 07:33:24 +0100969 memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200970 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200971 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200972 }
973
974 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100975 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100976 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200977 }
Emeric Brun107ca302010-01-04 16:16:05 +0100978
979 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200980 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100981 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100982 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200983 }
Emeric Brun107ca302010-01-04 16:16:05 +0100984
985 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200986 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200987 if (!conv_expr)
988 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100989
990 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
991 conv_expr->conv = conv;
992
Willy Tarreau833cc792013-07-24 15:34:19 +0200993 if (endt != endw) {
Willy Tarreaub27c0d32012-04-20 16:04:47 +0200994 int err_arg;
Willy Tarreau21d68a62012-04-20 15:52:36 +0200995
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200996 if (!conv->arg_mask) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100997 memprintf(err_msg, "converter '%s' does not support any args", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +0200998 goto out_error;
999 }
Willy Tarreau9e92d322010-01-26 17:58:06 +01001000
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001001 al->kw = expr->fetch->kw;
1002 al->conv = conv_expr->conv->kw;
Willy Tarreau975c1782013-12-12 23:16:54 +01001003 if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001004 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
Willy Tarreau21d68a62012-04-20 15:52:36 +02001005 goto out_error;
1006 }
1007
Willy Tarreau2e845be2012-10-19 19:49:09 +02001008 if (!conv_expr->arg_p)
1009 conv_expr->arg_p = empty_arg_list;
1010
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01001011 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001012 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001013 goto out_error;
1014 }
1015 }
Willy Tarreau833cc792013-07-24 15:34:19 +02001016 else if (ARGM(conv->arg_mask)) {
Willy Tarreau97108e02016-11-25 07:33:24 +01001017 memprintf(err_msg, "missing args for converter '%s'", ckw);
Emeric Brun485479d2010-09-23 18:02:19 +02001018 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001019 }
1020 }
Emeric Brun485479d2010-09-23 18:02:19 +02001021
Willy Tarreau833cc792013-07-24 15:34:19 +02001022 out:
1023 free(fkw);
1024 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001025 return expr;
1026
1027out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001028 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001029 expr = NULL;
1030 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001031}
1032
1033/*
Willy Tarreau12785782012-04-27 21:37:17 +02001034 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001035 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001036 * Returns a pointer on a typed sample structure containing the result or NULL if
1037 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001038 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001039 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001040 *
1041 * Note: the fetch functions are required to properly set the return type. The
1042 * conversion functions must do so too. However the cast functions do not need
1043 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001044 *
1045 * The caller may indicate in <opt> if it considers the result final or not.
1046 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1047 * if the result is stable or not, according to the following table :
1048 *
1049 * return MAY_CHANGE FINAL Meaning for the sample
1050 * NULL 0 * Not present and will never be (eg: header)
1051 * NULL 1 0 Not present yet, could change (eg: POST param)
1052 * NULL 1 1 Not present yet, will not change anymore
1053 * smp 0 * Present and will not change (eg: header)
1054 * smp 1 0 Present, may change (eg: request length)
1055 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001056 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001057struct sample *sample_process(struct proxy *px, struct session *sess,
1058 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001059 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001060{
Willy Tarreau12785782012-04-27 21:37:17 +02001061 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001062
Willy Tarreau18387e22013-07-25 12:02:38 +02001063 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001064 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001065 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001066 }
Emeric Brun107ca302010-01-04 16:16:05 +01001067
Willy Tarreau1777ea62016-03-10 16:15:46 +01001068 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001069 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001070 return NULL;
1071
Emeric Brun107ca302010-01-04 16:16:05 +01001072 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001073 /* we want to ensure that p->type can be casted into
1074 * conv_expr->conv->in_type. We have 3 possibilities :
1075 * - NULL => not castable.
1076 * - c_none => nothing to do (let's optimize it)
1077 * - other => apply cast and prepare to fail
1078 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001079 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001080 return NULL;
1081
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001082 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1083 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001084 return NULL;
1085
Willy Tarreau12e50112012-04-25 17:21:49 +02001086 /* OK cast succeeded */
1087
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001088 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001089 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001090 }
1091 return p;
1092}
1093
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001094/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001095 * Resolve all remaining arguments in proxy <p>. Returns the number of
1096 * errors or 0 if everything is fine.
1097 */
1098int smp_resolve_args(struct proxy *p)
1099{
1100 struct arg_list *cur, *bak;
1101 const char *ctx, *where;
1102 const char *conv_ctx, *conv_pre, *conv_pos;
1103 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001104 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001105 struct arg *arg;
1106 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001107 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001108
1109 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1110 struct proxy *px;
1111 struct server *srv;
1112 char *pname, *sname;
Willy Tarreau46947782015-01-19 19:00:58 +01001113 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001114
1115 arg = cur->arg;
1116
1117 /* prepare output messages */
1118 conv_pre = conv_pos = conv_ctx = "";
1119 if (cur->conv) {
1120 conv_ctx = cur->conv;
1121 conv_pre = "conversion keyword '";
1122 conv_pos = "' for ";
1123 }
1124
1125 where = "in";
1126 ctx = "sample fetch keyword";
1127 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001128 case ARGC_STK: where = "in stick rule in"; break;
1129 case ARGC_TRK: where = "in tracking rule in"; break;
1130 case ARGC_LOG: where = "in log-format string in"; break;
1131 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1132 case ARGC_HRQ: where = "in http-request header format string in"; break;
1133 case ARGC_HRS: where = "in http-response header format string in"; break;
1134 case ARGC_UIF: where = "in unique-id-format string in"; break;
1135 case ARGC_RDR: where = "in redirect format string in"; break;
1136 case ARGC_CAP: where = "in capture rule in"; break;
1137 case ARGC_ACL: ctx = "ACL keyword"; break;
1138 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001139 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001140 }
1141
1142 /* set a few default settings */
1143 px = p;
1144 pname = p->id;
1145
1146 switch (arg->type) {
1147 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001148 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001149 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1150 cur->file, cur->line,
1151 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152 cfgerr++;
1153 continue;
1154 }
1155
1156 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001157 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158
1159 if (sname) {
1160 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001161 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001163 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001164 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001165 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1166 cur->file, cur->line, pname,
1167 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001168 cfgerr++;
1169 break;
1170 }
1171 }
1172 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001173 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174
1175 srv = findserver(px, sname);
1176 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001177 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",
1178 cur->file, cur->line, sname, pname,
1179 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001180 cfgerr++;
1181 break;
1182 }
1183
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001184 free(arg->data.str.area);
1185 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001186 arg->unresolved = 0;
1187 arg->data.srv = srv;
1188 break;
1189
1190 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001191 if (arg->data.str.data) {
1192 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001193 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 }
1195
1196 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001197 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1198 cur->file, cur->line, pname,
1199 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001200 cfgerr++;
1201 break;
1202 }
1203
1204 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001205 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",
1206 cur->file, cur->line, pname,
1207 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 cfgerr++;
1209 break;
1210 }
1211
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001212 free(arg->data.str.area);
1213 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001214 arg->unresolved = 0;
1215 arg->data.prx = px;
1216 break;
1217
1218 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001219 if (arg->data.str.data) {
1220 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001221 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222 }
1223
1224 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1226 cur->file, cur->line, pname,
1227 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001228 cfgerr++;
1229 break;
1230 }
1231
1232 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001233 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",
1234 cur->file, cur->line, pname,
1235 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001236 cfgerr++;
1237 break;
1238 }
1239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001240 free(arg->data.str.area);
1241 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001242 arg->unresolved = 0;
1243 arg->data.prx = px;
1244 break;
1245
1246 case ARGT_TAB:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001247 if (arg->data.str.data) {
1248 pname = arg->data.str.area;
Willy Tarreaue2dc1fa2015-05-26 12:08:07 +02001249 px = proxy_tbl_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001250 }
1251
1252 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001253 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1254 cur->file, cur->line, pname,
1255 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001256 cfgerr++;
1257 break;
1258 }
1259
1260 if (!px->table.size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001261 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1262 cur->file, cur->line, pname,
1263 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001264 cfgerr++;
1265 break;
1266 }
1267
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001268 if (p->bind_proc & ~px->bind_proc) {
1269 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
1270 cur->file, cur->line, px->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001271 cfgerr++;
1272 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001273 }
1274
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001275 free(arg->data.str.area);
1276 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001277 arg->unresolved = 0;
1278 arg->data.prx = px;
1279 break;
1280
1281 case ARGT_USR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001282 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001283 ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1284 cur->file, cur->line,
1285 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001286 cfgerr++;
1287 break;
1288 }
1289
1290 if (p->uri_auth && p->uri_auth->userlist &&
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001291 !strcmp(p->uri_auth->userlist->name, arg->data.str.area))
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001292 ul = p->uri_auth->userlist;
1293 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001294 ul = auth_find_userlist(arg->data.str.area);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001295
1296 if (!ul) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001297 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 +02001298 cur->file, cur->line,
1299 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001300 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001301 cfgerr++;
1302 break;
1303 }
1304
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001305 free(arg->data.str.area);
1306 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001307 arg->unresolved = 0;
1308 arg->data.usr = ul;
1309 break;
Willy Tarreau46947782015-01-19 19:00:58 +01001310
1311 case ARGT_REG:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001312 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001313 ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1314 cur->file, cur->line,
1315 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001316 cfgerr++;
1317 continue;
1318 }
1319
1320 reg = calloc(1, sizeof(*reg));
1321 if (!reg) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001322 ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1323 cur->file, cur->line,
1324 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreau46947782015-01-19 19:00:58 +01001325 cfgerr++;
1326 continue;
1327 }
1328
1329 rflags = 0;
1330 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1331 err = NULL;
1332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 if (!regex_comp(arg->data.str.area, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001334 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1335 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001336 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001337 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001338 cfgerr++;
1339 continue;
1340 }
1341
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001342 free(arg->data.str.area);
1343 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001344 arg->unresolved = 0;
1345 arg->data.reg = reg;
1346 break;
1347
1348
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001349 }
1350
1351 LIST_DEL(&cur->list);
1352 free(cur);
1353 } /* end of args processing */
1354
1355 return cfgerr;
1356}
1357
1358/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001359 * Process a fetch + format conversion as defined by the sample expression
1360 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001361 * not explicitly set to <smp_type>, but shall be compatible with it as
1362 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1363 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001364 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1365 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1366 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1367 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001368 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1369 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1370 * still hope to get it after waiting longer, and is not converted to string.
1371 * The possible output combinations are the following :
1372 *
1373 * return MAY_CHANGE FINAL Meaning for the sample
1374 * NULL * * Not present and will never be (eg: header)
1375 * smp 0 * Final value converted (eg: header)
1376 * smp 1 0 Not present yet, may appear later (eg: header)
1377 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001378 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001379struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001380 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001381 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001382{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001383 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001384
Willy Tarreau6c616e02014-06-25 16:56:41 +02001385 memset(smp, 0, sizeof(*smp));
1386
Willy Tarreau192252e2015-04-04 01:47:55 +02001387 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001388 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1389 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001390 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001391 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001392
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001393 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001394 return NULL;
1395
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001396 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001397 return NULL;
1398
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001399 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001400 return smp;
1401}
1402
Christopher Faulet476e5d02016-10-26 11:34:47 +02001403static void release_sample_arg(struct arg *p)
1404{
1405 struct arg *p_back = p;
1406
1407 if (!p)
1408 return;
1409
1410 while (p->type != ARGT_STOP) {
1411 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001412 free(p->data.str.area);
1413 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001414 p->unresolved = 0;
1415 }
1416 else if (p->type == ARGT_REG) {
1417 if (p->data.reg) {
1418 regex_free(p->data.reg);
1419 free(p->data.reg);
1420 p->data.reg = NULL;
1421 }
1422 }
1423 p++;
1424 }
1425
1426 if (p_back != empty_arg_list)
1427 free(p_back);
1428}
1429
1430void release_sample_expr(struct sample_expr *expr)
1431{
1432 struct sample_conv_expr *conv_expr, *conv_exprb;
1433
1434 if (!expr)
1435 return;
1436
1437 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1438 release_sample_arg(conv_expr->arg_p);
1439 release_sample_arg(expr->arg_p);
1440 free(expr);
1441}
1442
Emeric Brun107ca302010-01-04 16:16:05 +01001443/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001444/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001445/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001446/*****************************************************************/
1447
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448#ifdef DEBUG_EXPR
1449static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1450{
1451 int i;
1452 struct sample tmp;
1453
1454 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001455 fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]);
1456 if (!sample_casts[smp->data.type][SMP_T_STR]) {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001457 fprintf(stderr, "(undisplayable)");
1458 } else {
1459
1460 /* Copy sample fetch. This put the sample as const, the
1461 * cast will copy data if a transformation is required.
1462 */
1463 memcpy(&tmp, smp, sizeof(struct sample));
1464 tmp.flags = SMP_F_CONST;
1465
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001466 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001467 fprintf(stderr, "(undisplayable)");
1468
1469 else {
1470 /* Display the displayable chars*. */
1471 fprintf(stderr, "<");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001472 for (i = 0; i < tmp.data.u.str.data; i++) {
1473 if (isprint(tmp.data.u.str.area[i]))
1474 fputc(tmp.data.u.str.area[i],
1475 stderr);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001476 else
1477 fputc('.', stderr);
1478 }
1479 }
1480 fprintf(stderr, ">\n");
1481 }
1482 }
1483 return 1;
1484}
1485#endif
1486
Holger Just1bfc24b2017-05-06 00:56:53 +02001487static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1488{
Willy Tarreau83061a82018-07-13 11:56:34 +02001489 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001490 int bin_len;
1491
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001492 trash->data = 0;
1493 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1494 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001495 if (bin_len < 0)
1496 return 0;
1497
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001498 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001499 smp->data.u.str = *trash;
1500 smp->data.type = SMP_T_BIN;
1501 smp->flags &= ~SMP_F_CONST;
1502 return 1;
1503}
1504
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001505static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001506{
Willy Tarreau83061a82018-07-13 11:56:34 +02001507 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001508 int b64_len;
1509
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001510 trash->data = 0;
1511 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1512 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001513 if (b64_len < 0)
1514 return 0;
1515
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001516 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001517 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001518 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001519 smp->flags &= ~SMP_F_CONST;
1520 return 1;
1521}
1522
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001523static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1524{
1525 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001526 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001527
1528 memset(&ctx, 0, sizeof(ctx));
1529
1530 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001531 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1532 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001533
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001534 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001535 smp->data.u.str = *trash;
1536 smp->data.type = SMP_T_BIN;
1537 smp->flags &= ~SMP_F_CONST;
1538 return 1;
1539}
1540
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001541static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001542{
Willy Tarreau83061a82018-07-13 11:56:34 +02001543 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001544 unsigned char c;
1545 int ptr = 0;
1546
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001547 trash->data = 0;
1548 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1549 c = smp->data.u.str.area[ptr++];
1550 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1551 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001552 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001553 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001554 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001555 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001556 return 1;
1557}
1558
Dragan Dosen3f957b22017-10-24 09:27:34 +02001559static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1560{
1561 long long int n = 0;
1562 int i, c;
1563
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001564 for (i = 0; i < smp->data.u.str.data; i++) {
1565 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001566 return 0;
1567 n = (n << 4) + c;
1568 }
1569
1570 smp->data.u.sint = n;
1571 smp->data.type = SMP_T_SINT;
1572 smp->flags &= ~SMP_F_CONST;
1573 return 1;
1574}
1575
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001576/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001577static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001578{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001579 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1580 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001581 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001582 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001583 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001584 return 1;
1585}
1586
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001587static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001588{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001589 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001590 smp->data.u.sint = i;
1591 smp->data.type = SMP_T_SINT;
1592 return 1;
1593}
1594
1595
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001596static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001597{
1598 int i;
1599
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001600 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001601 return 0;
1602
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001603 for (i = 0; i < smp->data.u.str.data; i++) {
1604 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1605 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001606 }
1607 return 1;
1608}
1609
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001610static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001611{
1612 int i;
1613
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001614 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001615 return 0;
1616
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001617 for (i = 0; i < smp->data.u.str.data; i++) {
1618 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1619 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001620 }
1621 return 1;
1622}
1623
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001624/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1625static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001626{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001627 /* Attempt to convert to IPv4 to apply the correct mask. */
1628 c_ipv62ip(smp);
1629
1630 if (smp->data.type == SMP_T_IPV4) {
1631 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1632 smp->data.type = SMP_T_IPV4;
1633 }
1634 else if (smp->data.type == SMP_T_IPV6) {
1635 /* IPv6 cannot be converted without an IPv6 mask. */
1636 if (args[1].type != ARGT_IPV6)
1637 return 0;
1638
1639 *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0];
1640 *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4];
1641 *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8];
1642 *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12];
1643 smp->data.type = SMP_T_IPV6;
1644 }
1645
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001646 return 1;
1647}
1648
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001649/* takes an UINT value on input supposed to represent the time since EPOCH,
1650 * adds an optional offset found in args[1] and emits a string representing
1651 * the local time in the format specified in args[1] using strftime().
1652 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001653static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001654{
Willy Tarreau83061a82018-07-13 11:56:34 +02001655 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001656 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001657 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001658 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001659
1660 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001661 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001662 curr_date += args[1].data.sint;
1663
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001664 tm = localtime(&curr_date);
1665 if (!tm)
1666 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001667 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001668 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1669 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001670 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001671 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001672 return 1;
1673}
1674
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001675/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001676static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001677{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001678 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1679 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001680 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001681 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001682 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001683 return 1;
1684}
1685
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001686/* takes an UINT value on input supposed to represent the time since EPOCH,
1687 * adds an optional offset found in args[1] and emits a string representing
1688 * the UTC date in the format specified in args[1] using strftime().
1689 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001690static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001691{
Willy Tarreau83061a82018-07-13 11:56:34 +02001692 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001693 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001694 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001695 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001696
1697 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001698 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001699 curr_date += args[1].data.sint;
1700
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001701 tm = gmtime(&curr_date);
1702 if (!tm)
1703 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001704 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001705 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1706 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001707 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001708 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001709 return 1;
1710}
1711
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001712/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001713static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001714{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001715 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1716 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001717 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001718 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001719 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001720 return 1;
1721}
1722
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001723/* hashes the binary input into a 32-bit unsigned int using xxh.
1724 * The seed of the hash defaults to 0 but can be changd in argument 1.
1725 */
1726static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1727{
1728 unsigned int seed;
1729
1730 if (arg_p && arg_p->data.sint)
1731 seed = arg_p->data.sint;
1732 else
1733 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001734 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1735 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001736 smp->data.type = SMP_T_SINT;
1737 return 1;
1738}
1739
1740/* hashes the binary input into a 64-bit unsigned int using xxh.
1741 * In fact, the function returns a 64 bit unsigned, but the sample
1742 * storage of haproxy only proposes 64-bits signed, so the value is
1743 * cast as signed. This cast doesn't impact the hash repartition.
1744 * The seed of the hash defaults to 0 but can be changd in argument 1.
1745 */
1746static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1747{
1748 unsigned long long int seed;
1749
1750 if (arg_p && arg_p->data.sint)
1751 seed = (unsigned long long int)arg_p->data.sint;
1752 else
1753 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001754 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1755 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001756 smp->data.type = SMP_T_SINT;
1757 return 1;
1758}
1759
Willy Tarreau80599772015-01-20 19:35:24 +01001760/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001761static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001762{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001763 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1764 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001765 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001766 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001767 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001768 return 1;
1769}
1770
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001771/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1772static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1773{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001774 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1775 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001776 if (arg_p && arg_p->data.sint)
1777 smp->data.u.sint = full_hash(smp->data.u.sint);
1778 smp->data.type = SMP_T_SINT;
1779 return 1;
1780}
1781
Frédéric Lécaillefd95c622019-02-26 14:09:08 +01001782/* Decode an unsigned protocol buffers varint */
1783static int sample_conv_varint(const struct arg *arg_p, struct sample *smp, void *private)
1784{
1785 uint64_t varint;
1786
1787 if (!protobuf_varint(&varint, (unsigned char *)smp->data.u.str.area, smp->data.u.str.data))
1788 return 0;
1789
1790 smp->data.u.sint = varint;
1791 smp->data.type = SMP_T_SINT;
1792 return 1;
1793}
1794
1795/* Decode a signed protocol buffers varint encoded as (zigzag + varint). */
1796static int sample_conv_svarint(const struct arg *arg_p, struct sample *smp, void *private)
1797{
1798 uint64_t varint;
1799
1800 if (!protobuf_varint(&varint, (unsigned char *)smp->data.u.str.area, smp->data.u.str.data))
1801 return 0;
1802
1803 /* zigzag decoding. */
1804 smp->data.u.sint = (varint >> 1) ^ -(varint & 1);
1805 smp->data.type = SMP_T_SINT;
1806 return 1;
1807}
1808
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001809/* This function escape special json characters. The returned string can be
1810 * safely set between two '"' and used as json string. The json string is
1811 * defined like this:
1812 *
1813 * any Unicode character except '"' or '\' or control character
1814 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1815 *
1816 * The enum input_type contain all the allowed mode for decoding the input
1817 * string.
1818 */
1819enum input_type {
1820 IT_ASCII = 0,
1821 IT_UTF8,
1822 IT_UTF8S,
1823 IT_UTF8P,
1824 IT_UTF8PS,
1825};
1826static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1827 const char *file, int line, char **err)
1828{
1829 if (!arg) {
1830 memprintf(err, "Unexpected empty arg list");
1831 return 0;
1832 }
1833
1834 if (arg->type != ARGT_STR) {
1835 memprintf(err, "Unexpected arg type");
1836 return 0;
1837 }
1838
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001839 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001840 arg->type = ARGT_SINT;
1841 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001842 return 1;
1843 }
1844
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001845 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001846 arg->type = ARGT_SINT;
1847 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001848 return 1;
1849 }
1850
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001852 arg->type = ARGT_SINT;
1853 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001854 return 1;
1855 }
1856
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001857 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001858 arg->type = ARGT_SINT;
1859 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001860 return 1;
1861 }
1862
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001863 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001864 arg->type = ARGT_SINT;
1865 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001866 return 1;
1867 }
1868
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001869 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001870 arg->type = ARGT_SINT;
1871 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001872 return 1;
1873 }
1874
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001875 memprintf(err, "Unexpected input code type. "
1876 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001877 return 0;
1878}
1879
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001880static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001881{
Willy Tarreau83061a82018-07-13 11:56:34 +02001882 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001883 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1884 const char *str;
1885 int len;
1886 enum input_type input_type = IT_ASCII;
1887 unsigned int c;
1888 unsigned int ret;
1889 char *p;
1890
1891 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001892 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001893
1894 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001895 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001897 p = smp->data.u.str.area;
1898 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001899
1900 if (input_type == IT_ASCII) {
1901 /* Read input as ASCII. */
1902 c = *(unsigned char *)p;
1903 p++;
1904 }
1905 else {
1906 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001907 ret = utf8_next(p,
1908 smp->data.u.str.data - ( p - smp->data.u.str.area),
1909 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001910 p += utf8_return_length(ret);
1911
1912 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
1913 return 0;
1914 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
1915 continue;
1916 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1917 return 0;
1918 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
1919 continue;
1920
1921 /* Check too big values. */
1922 if ((unsigned int)c > 0xffff) {
1923 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
1924 return 0;
1925 continue;
1926 }
1927 }
1928
1929 /* Convert character. */
1930 if (c == '"') {
1931 len = 2;
1932 str = "\\\"";
1933 }
1934 else if (c == '\\') {
1935 len = 2;
1936 str = "\\\\";
1937 }
1938 else if (c == '/') {
1939 len = 2;
1940 str = "\\/";
1941 }
1942 else if (c == '\b') {
1943 len = 2;
1944 str = "\\b";
1945 }
1946 else if (c == '\f') {
1947 len = 2;
1948 str = "\\f";
1949 }
1950 else if (c == '\r') {
1951 len = 2;
1952 str = "\\r";
1953 }
1954 else if (c == '\n') {
1955 len = 2;
1956 str = "\\n";
1957 }
1958 else if (c == '\t') {
1959 len = 2;
1960 str = "\\t";
1961 }
1962 else if (c > 0xff || !isprint(c)) {
1963 /* isprint generate a segfault if c is too big. The man says that
1964 * c must have the value of an unsigned char or EOF.
1965 */
1966 len = 6;
1967 _str[0] = '\\';
1968 _str[1] = 'u';
1969 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
1970 str = _str;
1971 }
1972 else {
1973 len = 1;
1974 str = (char *)&c;
1975 }
1976
1977 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001978 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001979 return 0;
1980
1981 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001982 memcpy(temp->area + temp->data, str, len);
1983 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001984 }
1985
1986 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001987 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001988 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001989
1990 return 1;
1991}
1992
Emeric Brun54c4ac82014-11-03 15:32:43 +01001993/* This sample function is designed to extract some bytes from an input buffer.
1994 * First arg is the offset.
1995 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001996static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01001997{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001998 if (smp->data.u.str.data <= arg_p[0].data.sint) {
1999 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002000 return 1;
2001 }
2002
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002003 if (smp->data.u.str.size)
2004 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002005 smp->data.u.str.data -= arg_p[0].data.sint;
2006 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002007
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002008 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2009 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002010
2011 return 1;
2012}
2013
Emeric Brunf399b0d2014-11-03 17:07:03 +01002014static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2015 const char *file, int line, char **err)
2016{
2017 struct arg *arg = args;
2018
2019 if (!arg) {
2020 memprintf(err, "Unexpected empty arg list");
2021 return 0;
2022 }
2023
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002024 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002025 memprintf(err, "Unexpected arg type");
2026 return 0;
2027 }
2028
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002029 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002030 memprintf(err, "Unexpected value 0 for index");
2031 return 0;
2032 }
2033
2034 arg++;
2035
2036 if (arg->type != ARGT_STR) {
2037 memprintf(err, "Unexpected arg type");
2038 return 0;
2039 }
2040
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002041 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002042 memprintf(err, "Empty separators list");
2043 return 0;
2044 }
2045
2046 return 1;
2047}
2048
2049/* This sample function is designed to a return selected part of a string (field).
2050 * First arg is the index of the field (start at 1)
2051 * Second arg is a char list of separators (type string)
2052 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002053static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002054{
Marcin Deranek9631a282018-04-16 14:30:46 +02002055 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002056 char *start, *end;
2057 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002058 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002059
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002060 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002061 return 0;
2062
Marcin Deranek9631a282018-04-16 14:30:46 +02002063 if (arg_p[0].data.sint < 0) {
2064 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002065 end = start = smp->data.u.str.area + smp->data.u.str.data;
2066 while (start > smp->data.u.str.area) {
2067 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2068 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002069 if (field == arg_p[0].data.sint) {
2070 if (count == 1)
2071 goto found;
2072 else if (count > 1)
2073 count--;
2074 } else {
2075 end = start-1;
2076 field--;
2077 }
2078 break;
2079 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002080 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002081 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002082 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002083 } else {
2084 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002085 end = start = smp->data.u.str.area;
2086 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2087 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2088 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002089 if (field == arg_p[0].data.sint) {
2090 if (count == 1)
2091 goto found;
2092 else if (count > 1)
2093 count--;
2094 } else {
2095 start = end+1;
2096 field++;
2097 }
2098 break;
2099 }
2100 }
2101 end++;
2102 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002103 }
2104
2105 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002106 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002107 smp->data.u.str.data = 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002108 return 1;
2109 }
2110found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002111 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002112 /* If ret string is len 0, no need to
2113 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002114 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002115 return 1;
2116
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002117 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002118
2119 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002120 Note: smp->data.u.str.size cannot be set to 0 */
2121 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002122 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002123
2124 return 1;
2125}
2126
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002127/* This sample function is designed to return a word from a string.
2128 * First arg is the index of the word (start at 1)
2129 * Second arg is a char list of words separators (type string)
2130 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002131static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002132{
Marcin Deranek9631a282018-04-16 14:30:46 +02002133 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002134 char *start, *end;
2135 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002136 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002137
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002138 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002139 return 0;
2140
2141 word = 0;
2142 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002143 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002144 end = start = smp->data.u.str.area + smp->data.u.str.data;
2145 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002146 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002147 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2148 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002149 issep = 1;
2150 break;
2151 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002152 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002153 if (!inword) {
2154 if (!issep) {
2155 if (word != arg_p[0].data.sint) {
2156 word--;
2157 end = start;
2158 }
2159 inword = 1;
2160 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002161 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002162 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002163 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002164 if (count == 1)
2165 goto found;
2166 else if (count > 1)
2167 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002168 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002169 inword = 0;
2170 }
2171 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002172 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002173 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002174 end = start = smp->data.u.str.area;
2175 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002176 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002177 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2178 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002179 issep = 1;
2180 break;
2181 }
2182 }
2183 if (!inword) {
2184 if (!issep) {
2185 if (word != arg_p[0].data.sint) {
2186 word++;
2187 start = end;
2188 }
2189 inword = 1;
2190 }
2191 }
2192 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002193 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002194 if (count == 1)
2195 goto found;
2196 else if (count > 1)
2197 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002198 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002199 inword = 0;
2200 }
2201 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002202 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002203 }
2204
2205 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002206 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002208 return 1;
2209 }
2210found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002211 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002212 /* If ret string is len 0, no need to
2213 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002214 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002215 return 1;
2216
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002217 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002218
2219 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002220 Note: smp->data.u.str.size cannot be set to 0 */
2221 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002222 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002223
2224 return 1;
2225}
2226
Willy Tarreau7eda8492015-01-20 19:47:06 +01002227static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2228 const char *file, int line, char **err)
2229{
2230 struct arg *arg = args;
2231 char *p;
2232 int len;
2233
2234 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2235 arg[0].type_flags = 0;
2236
2237 if (arg[2].type != ARGT_STR)
2238 return 1;
2239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002240 p = arg[2].data.str.area;
2241 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002242 while (len) {
2243 if (*p == 'i') {
2244 arg[0].type_flags |= ARGF_REG_ICASE;
2245 }
2246 else if (*p == 'g') {
2247 arg[0].type_flags |= ARGF_REG_GLOB;
2248 }
2249 else {
2250 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2251 return 0;
2252 }
2253 p++;
2254 len--;
2255 }
2256 return 1;
2257}
2258
2259/* This sample function is designed to do the equivalent of s/match/replace/ on
2260 * the input string. It applies a regex and restarts from the last matched
2261 * location until nothing matches anymore. First arg is the regex to apply to
2262 * the input string, second arg is the replacement expression.
2263 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002264static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002265{
2266 char *start, *end;
2267 struct my_regex *reg = arg_p[0].data.reg;
2268 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002269 struct buffer *trash = get_trash_chunk();
Willy Tarreau7eda8492015-01-20 19:47:06 +01002270 int flag, max;
2271 int found;
2272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002273 start = smp->data.u.str.area;
2274 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002275
2276 flag = 0;
2277 while (1) {
2278 /* check for last round which is used to copy remaining parts
2279 * when not running in global replacement mode.
2280 */
2281 found = 0;
2282 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2283 /* Note: we can have start == end on empty strings or at the end */
2284 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2285 }
2286
2287 if (!found)
2288 pmatch[0].rm_so = end - start;
2289
2290 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002291 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002292 if (max && pmatch[0].rm_so > 0) {
2293 if (max > pmatch[0].rm_so)
2294 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 memcpy(trash->area + trash->data, start, max);
2296 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002297 }
2298
2299 if (!found)
2300 break;
2301
2302 /* replace the matching part */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002303 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002304 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002305 if (max > arg_p[1].data.str.data)
2306 max = arg_p[1].data.str.data;
2307 memcpy(trash->area + trash->data,
2308 arg_p[1].data.str.area, max);
2309 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002310 }
2311
2312 /* stop here if we're done with this string */
2313 if (start >= end)
2314 break;
2315
2316 /* We have a special case for matches of length 0 (eg: "x*y*").
2317 * These ones are considered to match in front of a character,
2318 * so we have to copy that character and skip to the next one.
2319 */
2320 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002321 if (trash->data < trash->size)
2322 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002323 pmatch[0].rm_eo++;
2324 }
2325
2326 start += pmatch[0].rm_eo;
2327 flag |= REG_NOTBOL;
2328 }
2329
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002330 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002331 return 1;
2332}
2333
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002334/* This function check an operator entry. It expects a string.
2335 * The string can be an integer or a variable name.
2336 */
2337static int check_operator(struct arg *args, struct sample_conv *conv,
2338 const char *file, int line, char **err)
2339{
2340 const char *str;
2341 const char *end;
2342
2343 /* Try to decode a variable. */
2344 if (vars_check_arg(&args[0], NULL))
2345 return 1;
2346
2347 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002348 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002349 end = str + strlen(str);
2350 args[0].data.sint = read_int64(&str, end);
2351 if (*str != '\0') {
2352 memprintf(err, "expects an integer or a variable name");
2353 return 0;
2354 }
2355 args[0].type = ARGT_SINT;
2356 return 1;
2357}
2358
Willy Tarreau6204cd92016-03-10 16:33:04 +01002359/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002360 * If the arg contain an integer, the integer is returned in the
2361 * sample. If the arg contains a variable descriptor, it returns the
2362 * variable value.
2363 *
2364 * This function returns 0 if an error occurs, otherwise it returns 1.
2365 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002366static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002367{
2368 switch (arg->type) {
2369 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002370 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002371 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002372 return 1;
2373 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002374 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002375 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002376 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002377 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002378 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002379 return 0;
2380 return 1;
2381 default:
2382 return 0;
2383 }
2384}
2385
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002386/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002387 * result.
2388 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002389static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002390{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002391 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002392 return 1;
2393}
2394
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002395/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002396 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002397 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002398static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002399{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002400 struct sample tmp;
2401
Willy Tarreau7560dd42016-03-10 16:28:58 +01002402 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002403 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002404 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002405 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002406 return 1;
2407}
2408
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002409/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002410 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002411 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002412static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002413{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002414 struct sample tmp;
2415
Willy Tarreau7560dd42016-03-10 16:28:58 +01002416 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002417 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002418 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002419 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002420 return 1;
2421}
2422
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002423/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002424 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002425 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002426static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002427{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002428 struct sample tmp;
2429
Willy Tarreau7560dd42016-03-10 16:28:58 +01002430 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002431 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002432 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002433 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002434 return 1;
2435}
2436
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002437static inline long long int arith_add(long long int a, long long int b)
2438{
2439 /* Prevent overflow and makes capped calculus.
2440 * We must ensure that the check calculus doesn't
2441 * exceed the signed 64 bits limits.
2442 *
2443 * +----------+----------+
2444 * | a<0 | a>=0 |
2445 * +------+----------+----------+
2446 * | b<0 | MIN-a>b | no check |
2447 * +------+----------+----------+
2448 * | b>=0 | no check | MAX-a<b |
2449 * +------+----------+----------+
2450 */
2451 if ((a ^ b) >= 0) {
2452 /* signs are differents. */
2453 if (a < 0) {
2454 if (LLONG_MIN - a > b)
2455 return LLONG_MIN;
2456 }
2457 if (LLONG_MAX - a < b)
2458 return LLONG_MAX;
2459 }
2460 return a + b;
2461}
2462
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002463/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002464 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002465 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002466static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002467{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002468 struct sample tmp;
2469
Willy Tarreau7560dd42016-03-10 16:28:58 +01002470 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002471 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002472 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002473 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002474 return 1;
2475}
2476
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002477/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002478 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002479 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002480static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002481 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002482{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002483 struct sample tmp;
2484
Willy Tarreau7560dd42016-03-10 16:28:58 +01002485 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002486 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002487 return 0;
2488
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002489 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2490 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2491 * of -LLONG_MIN and correct the result.
2492 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002493 if (tmp.data.u.sint == LLONG_MIN) {
2494 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2495 if (smp->data.u.sint < LLONG_MAX)
2496 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002497 return 1;
2498 }
2499
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002500 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002501 * the second operand.
2502 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002503 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002504 return 1;
2505}
2506
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002507/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002508 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002509 * If the result makes an overflow, then the largest possible quantity is
2510 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002511 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002512static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002513 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002514{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002515 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002516 long long int c;
2517
Willy Tarreau7560dd42016-03-10 16:28:58 +01002518 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002519 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002520 return 0;
2521
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002522 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002523 if (!smp->data.u.sint || !tmp.data.u.sint) {
2524 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002525 return 1;
2526 }
2527
2528 /* The multiply between LLONG_MIN and -1 returns a
2529 * "floting point exception".
2530 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002531 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2532 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002533 return 1;
2534 }
2535
2536 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002537 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002538
2539 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002540 if (smp->data.u.sint != c / tmp.data.u.sint) {
2541 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2542 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002543 return 1;
2544 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002545 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002546 return 1;
2547 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002548 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002549 return 1;
2550}
2551
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002552/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002553 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002554 * If arg_p makes the result overflow, then the largest possible quantity is
2555 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002556 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002557static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002558 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002559{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002560 struct sample tmp;
2561
Willy Tarreau7560dd42016-03-10 16:28:58 +01002562 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002563 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002564 return 0;
2565
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002566 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002567 /* The divide between LLONG_MIN and -1 returns a
2568 * "floting point exception".
2569 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002570 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2571 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002572 return 1;
2573 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002574 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002575 return 1;
2576 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002577 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002578 return 1;
2579}
2580
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002581/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002582 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002583 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002584 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002585static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002586 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002587{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002588 struct sample tmp;
2589
Willy Tarreau7560dd42016-03-10 16:28:58 +01002590 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002591 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002592 return 0;
2593
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002594 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002595 /* The divide between LLONG_MIN and -1 returns a
2596 * "floting point exception".
2597 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002598 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2599 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002600 return 1;
2601 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002602 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002603 return 1;
2604 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002605 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002606 return 1;
2607}
2608
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002609/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002610 * result.
2611 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002612static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002613 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002614{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002615 if (smp->data.u.sint == LLONG_MIN)
2616 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002617 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002618 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002619 return 1;
2620}
2621
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002622/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002623 * false. The output is a BOOL.
2624 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002625static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002626 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002627{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002628 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002629 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002630 return 1;
2631}
2632
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002633/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002634 * truee. The output is a BOOL.
2635 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002636static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002637 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002638{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002639 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002640 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002641 return 1;
2642}
2643
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002644/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002645 * The output is a BOOL.
2646 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002647static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002648 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002649{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002650 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002651 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002652 return 1;
2653}
2654
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002655/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002656 * The output is a BOOL.
2657 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002658static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002659 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002660{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002661 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002662 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002663 return 1;
2664}
2665
Willy Tarreau280f42b2018-02-19 15:34:12 +01002666/* appends an optional const string, an optional variable contents and another
2667 * optional const string to an existing string.
2668 */
2669static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2670{
Willy Tarreau83061a82018-07-13 11:56:34 +02002671 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002672 struct sample tmp;
2673 int max;
2674
2675 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002676 trash->data = smp->data.u.str.data;
2677 if (trash->data > trash->size - 1)
2678 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002679
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002680 memcpy(trash->area, smp->data.u.str.area, trash->data);
2681 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002682
2683 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002684 max = arg_p[0].data.str.data;
2685 if (max > trash->size - 1 - trash->data)
2686 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002687
2688 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002689 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2690 trash->data += max;
2691 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002692 }
2693
2694 /* append second string (variable) if it's found and we can turn it
2695 * into a string.
2696 */
2697 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2698 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2699 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2700 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2701
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002702 max = tmp.data.u.str.data;
2703 if (max > trash->size - 1 - trash->data)
2704 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002705
2706 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002707 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2708 max);
2709 trash->data += max;
2710 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002711 }
2712 }
2713
2714 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002715 max = arg_p[2].data.str.data;
2716 if (max > trash->size - 1 - trash->data)
2717 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002718
2719 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002720 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2721 trash->data += max;
2722 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002723 }
2724
2725 smp->data.u.str = *trash;
2726 smp->data.type = SMP_T_STR;
2727 return 1;
2728}
2729
2730/* This function checks the "concat" converter's arguments and extracts the
2731 * variable name and its scope.
2732 */
2733static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2734 const char *file, int line, char **err)
2735{
2736 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002737 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2738 memprintf(err, "failed to register variable name '%s'",
2739 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002740 return 0;
2741 }
2742 return 1;
2743}
2744
Tim Duesterhusca097c12018-04-27 21:18:45 +02002745/* compares string with a variable containing a string. Return value
2746 * is compatible with strcmp(3)'s return value.
2747 */
2748static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2749{
2750 struct sample tmp;
2751 int max, result;
2752
2753 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2754 if (arg_p[0].type != ARGT_VAR)
2755 return 0;
2756 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2757 return 0;
2758 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2759 return 0;
2760
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002761 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2762 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002763 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002764 if (smp->data.u.str.data != tmp.data.u.str.data) {
2765 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002766 result = -1;
2767 }
2768 else {
2769 result = 1;
2770 }
2771 }
2772 }
2773
2774 smp->data.u.sint = result;
2775 smp->data.type = SMP_T_SINT;
2776 return 1;
2777}
2778
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002779#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2780#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2781#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2782
2783/*
2784 * Extract the field value of an input binary sample. Takes a mandatory argument:
2785 * the protocol buffers field identifier (dotted notation) internally represented
2786 * as an array of unsigned integers and its size.
2787 * Return 1 if the field was found, 0 if not.
2788 */
2789static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2790{
2791 unsigned char *pos;
2792 size_t grpc_left;
2793 unsigned int *fid;
2794 size_t fid_sz;
2795
2796 if (!smp->strm)
2797 return 0;
2798
2799 fid = arg_p[0].data.fid.ids;
2800 fid_sz = arg_p[0].data.fid.sz;
2801
2802 pos = (unsigned char *)smp->data.u.str.area;
2803 /* Remaining bytes in the body to be parsed. */
2804 grpc_left = smp->data.u.str.data;
2805
2806 while (grpc_left > GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ) {
2807 int next_field, found;
2808 size_t grpc_msg_len, left;
2809 unsigned int wire_type, field_number;
2810 uint64_t key, elen;
2811
2812 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2813
2814 pos += GRPC_MSG_HEADER_SZ;
2815 grpc_left -= GRPC_MSG_HEADER_SZ;
2816
2817 if (grpc_left < left)
2818 return 0;
2819
2820 found = 1;
2821 /* Length of the length-delimited messages if any. */
2822 elen = 0;
2823
2824 /* Message decoding: there may be serveral key+value protobuf pairs by
2825 * gRPC message.
2826 */
2827 next_field = 0;
2828 while (next_field < fid_sz) {
2829 uint64_t sleft;
2830
2831 if ((ssize_t)left <= 0)
2832 return 0;
2833
2834 /* Remaining bytes saving. */
2835 sleft = left;
2836
2837 /* Key decoding */
2838 if (!protobuf_decode_varint(&key, &pos, &left))
2839 return 0;
2840
2841 wire_type = key & 0x7;
2842 field_number = key >> 3;
2843 found = field_number == fid[next_field];
2844
2845 if (found && field_number != fid[next_field])
2846 found = 0;
2847
2848 switch (wire_type) {
2849 case PBUF_TYPE_VARINT:
2850 {
2851 if (!found) {
2852 protobuf_skip_varint(&pos, &left);
2853 } else if (next_field == fid_sz - 1) {
2854 int varint_len;
2855 unsigned char *spos = pos;
2856
2857 varint_len = protobuf_varint_getlen(&pos, &left);
2858 if (varint_len == -1)
2859 return 0;
2860
2861 smp->data.type = SMP_T_BIN;
2862 smp->data.u.str.area = (char *)spos;
2863 smp->data.u.str.data = varint_len;
2864 smp->flags = SMP_F_VOL_TEST;
2865 return 1;
2866 }
2867 break;
2868 }
2869
2870 case PBUF_TYPE_64BIT:
2871 {
2872 if (!found) {
2873 pos += sizeof(uint64_t);
2874 left -= sizeof(uint64_t);
2875 } else if (next_field == fid_sz - 1) {
2876 smp->data.type = SMP_T_BIN;
2877 smp->data.u.str.area = (char *)pos;
2878 smp->data.u.str.data = sizeof(uint64_t);
2879 smp->flags = SMP_F_VOL_TEST;
2880 return 1;
2881 }
2882 break;
2883 }
2884
2885 case PBUF_TYPE_LENGTH_DELIMITED:
2886 {
2887 /* Decode the length of this length-delimited field. */
2888 if (!protobuf_decode_varint(&elen, &pos, &left))
2889 return 0;
2890
2891 if (elen > left)
2892 return 0;
2893
2894 /* The size of the current field is computed from here do skip
2895 * the bytes to encode the previous lenght.*
2896 */
2897 sleft = left;
2898 if (!found) {
2899 /* Skip the current length-delimited field. */
2900 pos += elen;
2901 left -= elen;
2902 break;
2903 } else if (next_field == fid_sz - 1) {
2904 smp->data.type = SMP_T_BIN;
2905 smp->data.u.str.area = (char *)pos;
2906 smp->data.u.str.data = elen;
2907 smp->flags = SMP_F_VOL_TEST;
2908 return 1;
2909 }
2910 break;
2911 }
2912
2913 case PBUF_TYPE_32BIT:
2914 {
2915 if (!found) {
2916 pos += sizeof(uint32_t);
2917 left -= sizeof(uint32_t);
2918 } else if (next_field == fid_sz - 1) {
2919 smp->data.type = SMP_T_BIN;
2920 smp->data.u.str.area = (char *)pos;
2921 smp->data.u.str.data = sizeof(uint32_t);
2922 smp->flags = SMP_F_VOL_TEST;
2923 return 1;
2924 }
2925 break;
2926 }
2927
2928 default:
2929 return 0;
2930 }
2931
2932 if ((ssize_t)(elen) > 0)
2933 elen -= sleft - left;
2934
2935 if (found) {
2936 next_field++;
2937 }
2938 else if ((ssize_t)elen <= 0) {
2939 next_field = 0;
2940 }
2941 }
2942 grpc_left -= grpc_msg_len;
2943 }
2944
2945 return 0;
2946}
2947
Tim Duesterhusca097c12018-04-27 21:18:45 +02002948/* This function checks the "strcmp" converter's arguments and extracts the
2949 * variable name and its scope.
2950 */
2951static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2952 const char *file, int line, char **err)
2953{
2954 /* Try to decode a variable. */
2955 if (vars_check_arg(&args[0], NULL))
2956 return 1;
2957
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002958 memprintf(err, "failed to register variable name '%s'",
2959 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002960 return 0;
2961}
2962
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002963/************************************************************************/
2964/* All supported sample fetch functions must be declared here */
2965/************************************************************************/
2966
2967/* force TRUE to be returned at the fetch level */
2968static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002969smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002970{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002971 if (!smp_make_rw(smp))
2972 return 0;
2973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002974 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002975 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002976 return 1;
2977}
2978
2979/* force FALSE to be returned at the fetch level */
2980static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002981smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002982{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002983 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002984 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002985 return 1;
2986}
2987
2988/* retrieve environment variable $1 as a string */
2989static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002990smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002991{
2992 char *env;
2993
2994 if (!args || args[0].type != ARGT_STR)
2995 return 0;
2996
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002998 if (!env)
2999 return 0;
3000
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003001 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003002 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003003 smp->data.u.str.area = env;
3004 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003005 return 1;
3006}
3007
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003008/* retrieve the current local date in epoch time, and applies an optional offset
3009 * of args[0] seconds.
3010 */
3011static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003012smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003013{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003014 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003015
3016 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003017 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003018 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003019
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003020 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003021 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3022 return 1;
3023}
3024
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003025/* retrieve the current microsecond part of the date */
3026static int
3027smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3028{
3029 smp->data.u.sint = date.tv_usec;
3030 smp->data.type = SMP_T_SINT;
3031 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3032 return 1;
3033}
3034
3035
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003036/* returns the hostname */
3037static int
3038smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3039{
3040 smp->data.type = SMP_T_STR;
3041 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003042 smp->data.u.str.area = hostname;
3043 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003044 return 1;
3045}
3046
Willy Tarreau0f30d262014-11-24 16:02:05 +01003047/* returns the number of processes */
3048static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003049smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003050{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003051 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003052 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003053 return 1;
3054}
3055
3056/* returns the number of the current process (between 1 and nbproc */
3057static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003058smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003059{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003060 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003061 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003062 return 1;
3063}
3064
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003065/* returns the number of the current thread (between 1 and nbthread */
3066static int
3067smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3068{
3069 smp->data.type = SMP_T_SINT;
3070 smp->data.u.sint = tid;
3071 return 1;
3072}
3073
Willy Tarreau84310e22014-02-14 11:59:04 +01003074/* generate a random 32-bit integer for whatever purpose, with an optional
3075 * range specified in argument.
3076 */
3077static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003078smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003079{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003080 smp->data.u.sint = random();
Willy Tarreau84310e22014-02-14 11:59:04 +01003081
3082 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003083 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003084 smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1);
Willy Tarreau84310e22014-02-14 11:59:04 +01003085
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003086 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003087 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3088 return 1;
3089}
3090
Willy Tarreau0f30d262014-11-24 16:02:05 +01003091/* returns true if the current process is stopping */
3092static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003093smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003094{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003095 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003096 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003097 return 1;
3098}
3099
Willy Tarreau70fe9442018-11-22 16:07:39 +01003100/* returns the number of calls of the current stream's process_stream() */
3101static int
3102smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3103{
3104 smp->data.type = SMP_T_SINT;
3105 smp->data.u.sint = smp->strm->task->calls;
3106 return 1;
3107}
3108
3109/* returns the average number of nanoseconds spent processing the stream per call */
3110static int
3111smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3112{
3113 smp->data.type = SMP_T_SINT;
3114 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3115 return 1;
3116}
3117
3118/* returns the total number of nanoseconds spent processing the stream */
3119static int
3120smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3121{
3122 smp->data.type = SMP_T_SINT;
3123 smp->data.u.sint = smp->strm->task->cpu_time;
3124 return 1;
3125}
3126
3127/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3128static int
3129smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3130{
3131 smp->data.type = SMP_T_SINT;
3132 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3133 return 1;
3134}
3135
3136/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3137static int
3138smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3139{
3140 smp->data.type = SMP_T_SINT;
3141 smp->data.u.sint = smp->strm->task->lat_time;
3142 return 1;
3143}
3144
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003145static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3146{
3147 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003148 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003149 smp->data.u.str.area = args[0].data.str.area;
3150 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003151 return 1;
3152}
3153
3154static int smp_check_const_bool(struct arg *args, char **err)
3155{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003156 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3157 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003158 args[0].type = ARGT_SINT;
3159 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003160 return 1;
3161 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003162 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3163 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003164 args[0].type = ARGT_SINT;
3165 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003166 return 1;
3167 }
3168 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3169 return 0;
3170}
3171
3172static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3173{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003174 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003175 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003176 return 1;
3177}
3178
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003179static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003180{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003181 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003182 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003183 return 1;
3184}
3185
3186static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3187{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003188 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003189 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003190 return 1;
3191}
3192
3193static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3194{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003195 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003196 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003197 return 1;
3198}
3199
3200static int smp_check_const_bin(struct arg *args, char **err)
3201{
David Carlier64a16ab2016-04-08 10:37:02 +01003202 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003203 int binstrlen;
3204
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003205 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003206 return 0;
3207 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003208 args[0].data.str.area = binstr;
3209 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003210 return 1;
3211}
3212
3213static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3214{
3215 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003216 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003217 smp->data.u.str.area = args[0].data.str.area;
3218 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003219 return 1;
3220}
3221
3222static int smp_check_const_meth(struct arg *args, char **err)
3223{
3224 enum http_meth_t meth;
3225 int i;
3226
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003227 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003228 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003229 args[0].type = ARGT_SINT;
3230 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003231 } else {
3232 /* Check method avalaibility. A methos is a token defined as :
3233 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3234 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3235 * token = 1*tchar
3236 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003237 for (i = 0; i < args[0].data.str.data; i++) {
3238 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003239 memprintf(err, "expects valid method.");
3240 return 0;
3241 }
3242 }
3243 }
3244 return 1;
3245}
3246
3247static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3248{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003249 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003250 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003251 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003252 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 smp->data.u.meth.str.area = "";
3254 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003255 } else {
3256 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003257 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003258 smp->data.u.meth.str.area = args[0].data.str.area;
3259 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003260 }
3261 return 1;
3262}
3263
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003264/* Note: must not be declared <const> as its list will be overwritten.
3265 * Note: fetches that may return multiple types must be declared as the lowest
3266 * common denominator, the type that can be casted into all other ones. For
3267 * instance IPv4/IPv6 must be declared IPv4.
3268 */
3269static struct sample_fetch_kw_list smp_kws = {ILH, {
3270 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3271 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003272 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003273 { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003274 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003275 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003276 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3277 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003278 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003279 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003280 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003281 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3282
3283 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3284 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3285 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3286 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3287 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003288
3289 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3290 { "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 +02003291 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003292 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3293 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3294 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3295 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3296
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003297 { /* END */ },
3298}};
3299
Willy Tarreau0108d902018-11-25 19:14:37 +01003300INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3301
Emeric Brun107ca302010-01-04 16:16:05 +01003302/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003303static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIER9687c772015-05-07 15:46:29 +02003304#ifdef DEBUG_EXPR
3305 { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY },
3306#endif
3307
Holger Just1bfc24b2017-05-06 00:56:53 +02003308 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003309 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003310 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3311 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003312 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003313 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003314 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003315 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003316 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3317 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003318 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003319 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003320 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3321 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3322 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003323 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3324 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003325 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003326 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003327 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3328 { "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 +01003329 { "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 +02003330 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau280f42b2018-02-19 15:34:12 +01003331 { "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 +02003332 { "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 +01003333
3334 /* gRPC converters. */
3335 { "ungrpc", sample_conv_ungrpc, ARG1(1,PBUF_FNUM), NULL, SMP_T_BIN, SMP_T_BIN },
Frédéric Lécaillefd95c622019-02-26 14:09:08 +01003336 { "varint", sample_conv_varint, 0, NULL, SMP_T_BIN, SMP_T_SINT },
3337 { "svarint", sample_conv_svarint, 0, NULL, SMP_T_BIN, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003338
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003339 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3340 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3341 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3342 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3343 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3344 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3345 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3346 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3347 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3348 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3349 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3350 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3351 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3352 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003353
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003354 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003355}};
3356
Willy Tarreau0108d902018-11-25 19:14:37 +01003357INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);