blob: 6cf805aed9adb99bf5e27ee866d1fe2ac870e267 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * Sample management functions.
Emeric Brun107ca302010-01-04 16:16:05 +01003 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02005 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +01006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Thierry FOURNIER317e1c42014-08-12 10:20:47 +020014#include <ctype.h>
Emeric Brun107ca302010-01-04 16:16:05 +010015#include <string.h>
16#include <arpa/inet.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020017#include <stdio.h>
Emeric Brun107ca302010-01-04 16:16:05 +010018
Willy Tarreau7e2c6472012-10-29 20:44:36 +010019#include <types/global.h>
20
Willy Tarreauc7e42382012-08-24 19:22:53 +020021#include <common/chunk.h>
Willy Tarreau23ec4ca2014-07-15 20:15:37 +020022#include <common/hash.h>
Willy Tarreau35b51c62018-09-10 15:38:55 +020023#include <common/http.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010024#include <common/initcall.h>
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +010025#include <common/net_helper.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020026#include <common/standard.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020027#include <common/uri_auth.h>
Emeric Brun53d1a982014-04-30 18:21:37 +020028#include <common/base64.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029
Willy Tarreau9fcb9842012-04-20 14:45:49 +020030#include <proto/arg.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020031#include <proto/auth.h>
32#include <proto/log.h>
33#include <proto/proxy.h>
Frédéric Lécaillefd95c622019-02-26 14:09:08 +010034#include <proto/protocol_buffers.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020035#include <proto/sample.h>
Willy Tarreau0851fd52019-12-17 10:07:25 +010036#include <proto/sink.h>
Willy Tarreaua4312fa2013-04-02 16:34:32 +020037#include <proto/stick_table.h>
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +020038#include <proto/vars.h>
Emeric Brun107ca302010-01-04 16:16:05 +010039
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +020040#include <import/sha1.h>
Thierry FOURNIER01e09742016-12-26 11:46:11 +010041#include <import/xxhash.h>
42
Willy Tarreau1cf8f082014-02-07 12:14:54 +010043/* sample type names */
44const char *smp_to_type[SMP_TYPES] = {
Thierry FOURNIER9c627e82015-06-03 20:12:35 +020045 [SMP_T_ANY] = "any",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010046 [SMP_T_BOOL] = "bool",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010047 [SMP_T_SINT] = "sint",
48 [SMP_T_ADDR] = "addr",
49 [SMP_T_IPV4] = "ipv4",
50 [SMP_T_IPV6] = "ipv6",
51 [SMP_T_STR] = "str",
52 [SMP_T_BIN] = "bin",
Thierry FOURNIER4c2479e2015-06-03 20:12:04 +020053 [SMP_T_METH] = "meth",
Willy Tarreau1cf8f082014-02-07 12:14:54 +010054};
55
Willy Tarreau12785782012-04-27 21:37:17 +020056/* static sample used in sample_process() when <p> is NULL */
Emeric Brune5c918b2017-06-14 14:15:36 +020057static THREAD_LOCAL struct sample temp_smp;
Emeric Brun107ca302010-01-04 16:16:05 +010058
Willy Tarreau12785782012-04-27 21:37:17 +020059/* list head of all known sample fetch keywords */
60static struct sample_fetch_kw_list sample_fetches = {
61 .list = LIST_HEAD_INIT(sample_fetches.list)
Emeric Brun107ca302010-01-04 16:16:05 +010062};
63
Willy Tarreau12785782012-04-27 21:37:17 +020064/* list head of all known sample format conversion keywords */
65static struct sample_conv_kw_list sample_convs = {
66 .list = LIST_HEAD_INIT(sample_convs.list)
Emeric Brun107ca302010-01-04 16:16:05 +010067};
68
Willy Tarreau80aca902013-01-07 15:42:20 +010069const unsigned int fetch_cap[SMP_SRC_ENTRIES] = {
70 [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
71 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
72 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
73 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
74 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
75 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010076 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +010077
78 [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
79 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
80 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
81 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
82 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
83 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010084 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010085
86 [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
87 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
88 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
89 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
90 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
91 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +010092 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +010093
94 [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
95 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
96 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
97 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
98 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
99 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100100 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100101
102 [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
103 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
104 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
105 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
106 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
107 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100108 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100109
110 [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT |
111 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
112 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
113 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
114 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
115 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100116 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100117
118 [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
119 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
120 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
121 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
122 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
123 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100124 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100125
126 [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
127 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
128 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
129 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
130 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
131 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100132 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100133
134 [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT |
135 SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
136 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
137 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
138 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
139 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100140 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100141
142 [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
143 SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK |
144 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
145 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ |
146 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
147 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100148 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100149
150 [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
151 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
152 SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY |
153 SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
154 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
155 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100156 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100157
158 [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
159 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
160 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
161 SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT |
162 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
163 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100164 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100165
166 [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
167 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
168 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
169 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
170 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
171 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100172 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100173
174 [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
175 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
176 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
177 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
178 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
179 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100180 SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL),
Willy Tarreau80aca902013-01-07 15:42:20 +0100181
182 [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
183 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
184 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
185 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
186 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
187 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100188 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100189
190 [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
191 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
192 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
193 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
194 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
195 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100196 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100197
198 [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
199 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
200 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
201 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT |
202 SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
203 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100204 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100205
206 [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
207 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
208 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
209 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
210 SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL |
211 SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100212 SMP_VAL___________ | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100213
214 [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
215 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
216 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
217 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
218 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
219 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100220 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100221
222 [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
223 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
224 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
225 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
226 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
227 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100228 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100229
230 [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
231 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
232 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
233 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
234 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
235 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100236 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100237
238 [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
239 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
240 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
241 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
242 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
243 SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ |
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100244 SMP_VAL_FE_LOG_END | SMP_VAL___________),
Willy Tarreau80aca902013-01-07 15:42:20 +0100245};
246
247static const char *fetch_src_names[SMP_SRC_ENTRIES] = {
248 [SMP_SRC_INTRN] = "internal state",
249 [SMP_SRC_LISTN] = "listener",
250 [SMP_SRC_FTEND] = "frontend",
251 [SMP_SRC_L4CLI] = "client address",
252 [SMP_SRC_L5CLI] = "client-side connection",
253 [SMP_SRC_TRACK] = "track counters",
254 [SMP_SRC_L6REQ] = "request buffer",
255 [SMP_SRC_HRQHV] = "HTTP request headers",
256 [SMP_SRC_HRQHP] = "HTTP request",
257 [SMP_SRC_HRQBO] = "HTTP request body",
258 [SMP_SRC_BKEND] = "backend",
259 [SMP_SRC_SERVR] = "server",
260 [SMP_SRC_L4SRV] = "server address",
261 [SMP_SRC_L5SRV] = "server-side connection",
262 [SMP_SRC_L6RES] = "response buffer",
263 [SMP_SRC_HRSHV] = "HTTP response headers",
264 [SMP_SRC_HRSHP] = "HTTP response",
265 [SMP_SRC_HRSBO] = "HTTP response body",
266 [SMP_SRC_RQFIN] = "request buffer statistics",
267 [SMP_SRC_RSFIN] = "response buffer statistics",
268 [SMP_SRC_TXFIN] = "transaction statistics",
269 [SMP_SRC_SSFIN] = "session statistics",
270};
271
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100272static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = {
273 [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule",
274 [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule",
275 [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule",
276 [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule",
277 [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule",
278 [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule",
279 [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule",
280 [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule",
281 [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule",
282 [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule",
283 [SMP_CKP_BE_SRV_CON] = "server source selection",
284 [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule",
285 [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule",
286 [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule",
287 [SMP_CKP_BE_STO_RUL] = "backend stick-store rule",
288 [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule",
289 [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule",
290 [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule",
291 [SMP_CKP_FE_LOG_END] = "logs",
Gaetan Rivet707b52f2020-02-21 18:14:59 +0100292 [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule",
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100293};
294
Thierry FOURNIER348971e2013-11-21 10:50:10 +0100295/* This function returns the type of the data returned by the sample_expr.
296 * It assumes that the <expr> and all of its converters are properly
297 * initialized.
298 */
299inline
300int smp_expr_output_type(struct sample_expr *expr)
301{
302 struct sample_conv_expr *smp_expr;
303
304 if (!LIST_ISEMPTY(&expr->conv_exprs)) {
305 smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list);
306 return smp_expr->conv->out_type;
307 }
308 return expr->fetch->out_type;
309}
310
311
Willy Tarreau80aca902013-01-07 15:42:20 +0100312/* fill the trash with a comma-delimited list of source names for the <use> bit
313 * field which must be composed of a non-null set of SMP_USE_* flags. The return
314 * value is the pointer to the string in the trash buffer.
315 */
316const char *sample_src_names(unsigned int use)
317{
318 int bit;
319
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200320 trash.data = 0;
321 trash.area[0] = '\0';
Willy Tarreau80aca902013-01-07 15:42:20 +0100322 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) {
323 if (!(use & ~((1 << bit) - 1)))
324 break; /* no more bits */
325
326 if (!(use & (1 << bit)))
327 continue; /* bit not set */
328
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200329 trash.data += snprintf(trash.area + trash.data,
330 trash.size - trash.data, "%s%s",
331 (use & ((1 << bit) - 1)) ? "," : "",
332 fetch_src_names[bit]);
Willy Tarreau80aca902013-01-07 15:42:20 +0100333 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200334 return trash.area;
Willy Tarreau80aca902013-01-07 15:42:20 +0100335}
336
Willy Tarreaubf8e2512013-03-25 14:52:41 +0100337/* return a pointer to the correct sample checkpoint name, or "unknown" when
338 * the flags are invalid. Only the lowest bit is used, higher bits are ignored
339 * if set.
340 */
341const char *sample_ckp_names(unsigned int use)
342{
343 int bit;
344
345 for (bit = 0; bit < SMP_CKP_ENTRIES; bit++)
346 if (use & (1 << bit))
347 return fetch_ckp_names[bit];
348 return "unknown sample check place, please report this bug";
349}
350
Emeric Brun107ca302010-01-04 16:16:05 +0100351/*
Willy Tarreau80aca902013-01-07 15:42:20 +0100352 * Registers the sample fetch keyword list <kwl> as a list of valid keywords
353 * for next parsing sessions. The fetch keywords capabilities are also computed
354 * from their ->use field.
Emeric Brun107ca302010-01-04 16:16:05 +0100355 */
Willy Tarreau80aca902013-01-07 15:42:20 +0100356void sample_register_fetches(struct sample_fetch_kw_list *kwl)
Emeric Brun107ca302010-01-04 16:16:05 +0100357{
Willy Tarreau80aca902013-01-07 15:42:20 +0100358 struct sample_fetch *sf;
359 int bit;
360
361 for (sf = kwl->kw; sf->kw != NULL; sf++) {
362 for (bit = 0; bit < SMP_SRC_ENTRIES; bit++)
363 if (sf->use & (1 << bit))
364 sf->val |= fetch_cap[bit];
365 }
366 LIST_ADDQ(&sample_fetches.list, &kwl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100367}
368
369/*
Willy Tarreau12785782012-04-27 21:37:17 +0200370 * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next
Emeric Brun107ca302010-01-04 16:16:05 +0100371 * parsing sessions.
372 */
Willy Tarreau12785782012-04-27 21:37:17 +0200373void sample_register_convs(struct sample_conv_kw_list *pckl)
Emeric Brun107ca302010-01-04 16:16:05 +0100374{
Willy Tarreau12785782012-04-27 21:37:17 +0200375 LIST_ADDQ(&sample_convs.list, &pckl->list);
Emeric Brun107ca302010-01-04 16:16:05 +0100376}
377
378/*
Willy Tarreau12785782012-04-27 21:37:17 +0200379 * Returns the pointer on sample fetch keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100380 * string of <len> in buffer <kw>.
381 *
382 */
Willy Tarreau12785782012-04-27 21:37:17 +0200383struct sample_fetch *find_sample_fetch(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100384{
385 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200386 struct sample_fetch_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100387
Willy Tarreau12785782012-04-27 21:37:17 +0200388 list_for_each_entry(kwl, &sample_fetches.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100389 for (index = 0; kwl->kw[index].kw != NULL; index++) {
390 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
391 kwl->kw[index].kw[len] == '\0')
392 return &kwl->kw[index];
393 }
394 }
395 return NULL;
396}
397
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100398/* This function browses the list of available sample fetches. <current> is
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100399 * the last used sample fetch. If it is the first call, it must set to NULL.
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100400 * <idx> is the index of the next sample fetch entry. It is used as private
401 * value. It is useless to initiate it.
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100402 *
Tim Duesterhusc555ee02018-01-25 16:24:44 +0100403 * It returns always the new fetch_sample entry, and NULL when the end of
Thierry FOURNIER4d9a1d12014-12-08 14:49:19 +0100404 * the list is reached.
405 */
406struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx)
407{
408 struct sample_fetch_kw_list *kwl;
409 struct sample_fetch *base;
410
411 if (!current) {
412 /* Get first kwl entry. */
413 kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list);
414 (*idx) = 0;
415 } else {
416 /* Get kwl corresponding to the curret entry. */
417 base = current + 1 - (*idx);
418 kwl = container_of(base, struct sample_fetch_kw_list, kw);
419 }
420
421 while (1) {
422
423 /* Check if kwl is the last entry. */
424 if (&kwl->list == &sample_fetches.list)
425 return NULL;
426
427 /* idx contain the next keyword. If it is available, return it. */
428 if (kwl->kw[*idx].kw) {
429 (*idx)++;
430 return &kwl->kw[(*idx)-1];
431 }
432
433 /* get next entry in the main list, and return NULL if the end is reached. */
434 kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list);
435
436 /* Set index to 0, ans do one other loop. */
437 (*idx) = 0;
438 }
439}
440
Thierry FOURNIER8fd13762015-03-10 23:56:48 +0100441/* This function browses the list of available converters. <current> is
442 * the last used converter. If it is the first call, it must set to NULL.
443 * <idx> is the index of the next converter entry. It is used as private
444 * value. It is useless to initiate it.
445 *
446 * It returns always the next sample_conv entry, and NULL when the end of
447 * the list is reached.
448 */
449struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx)
450{
451 struct sample_conv_kw_list *kwl;
452 struct sample_conv *base;
453
454 if (!current) {
455 /* Get first kwl entry. */
456 kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list);
457 (*idx) = 0;
458 } else {
459 /* Get kwl corresponding to the curret entry. */
460 base = current + 1 - (*idx);
461 kwl = container_of(base, struct sample_conv_kw_list, kw);
462 }
463
464 while (1) {
465 /* Check if kwl is the last entry. */
466 if (&kwl->list == &sample_convs.list)
467 return NULL;
468
469 /* idx contain the next keyword. If it is available, return it. */
470 if (kwl->kw[*idx].kw) {
471 (*idx)++;
472 return &kwl->kw[(*idx)-1];
473 }
474
475 /* get next entry in the main list, and return NULL if the end is reached. */
476 kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list);
477
478 /* Set index to 0, ans do one other loop. */
479 (*idx) = 0;
480 }
481}
482
Emeric Brun107ca302010-01-04 16:16:05 +0100483/*
Willy Tarreau12785782012-04-27 21:37:17 +0200484 * Returns the pointer on sample format conversion keyword structure identified by
Emeric Brun107ca302010-01-04 16:16:05 +0100485 * string of <len> in buffer <kw>.
486 *
487 */
Willy Tarreau12785782012-04-27 21:37:17 +0200488struct sample_conv *find_sample_conv(const char *kw, int len)
Emeric Brun107ca302010-01-04 16:16:05 +0100489{
490 int index;
Willy Tarreau12785782012-04-27 21:37:17 +0200491 struct sample_conv_kw_list *kwl;
Emeric Brun107ca302010-01-04 16:16:05 +0100492
Willy Tarreau12785782012-04-27 21:37:17 +0200493 list_for_each_entry(kwl, &sample_convs.list, list) {
Emeric Brun107ca302010-01-04 16:16:05 +0100494 for (index = 0; kwl->kw[index].kw != NULL; index++) {
495 if (strncmp(kwl->kw[index].kw, kw, len) == 0 &&
496 kwl->kw[index].kw[len] == '\0')
497 return &kwl->kw[index];
498 }
499 }
500 return NULL;
501}
502
Emeric Brun107ca302010-01-04 16:16:05 +0100503/******************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200504/* Sample casts functions */
Emeric Brun107ca302010-01-04 16:16:05 +0100505/******************************************************************/
506
Willy Tarreau342acb42012-04-23 22:03:39 +0200507static int c_ip2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100508{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200509 smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200510 smp->data.type = SMP_T_SINT;
Emeric Brun107ca302010-01-04 16:16:05 +0100511 return 1;
512}
513
Willy Tarreau342acb42012-04-23 22:03:39 +0200514static int c_ip2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100515{
Willy Tarreau83061a82018-07-13 11:56:34 +0200516 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100517
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200518 if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size))
Emeric Brun107ca302010-01-04 16:16:05 +0100519 return 0;
520
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200521 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200522 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200523 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100524 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100525
526 return 1;
527}
528
Willy Tarreau342acb42012-04-23 22:03:39 +0200529static int c_ip2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100530{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200531 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200532 smp->data.type = SMP_T_IPV6;
David du Colombier4f92d322011-03-24 11:09:31 +0100533 return 1;
534}
535
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200536static int c_ipv62ip(struct sample *smp)
537{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200538 if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6))
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200539 return 0;
Tim Duesterhusbf5ce022018-01-25 16:24:46 +0100540 smp->data.type = SMP_T_IPV4;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200541 return 1;
542}
543
Willy Tarreau342acb42012-04-23 22:03:39 +0200544static int c_ipv62str(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100545{
Willy Tarreau83061a82018-07-13 11:56:34 +0200546 struct buffer *trash = get_trash_chunk();
David du Colombier4f92d322011-03-24 11:09:31 +0100547
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200548 if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size))
David du Colombier4f92d322011-03-24 11:09:31 +0100549 return 0;
550
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200551 trash->data = strlen(trash->area);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200552 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200553 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100554 smp->flags &= ~SMP_F_CONST;
David du Colombier4f92d322011-03-24 11:09:31 +0100555 return 1;
556}
557
558/*
Willy Tarreau342acb42012-04-23 22:03:39 +0200559static int c_ipv62ip(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100560{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200561 return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6);
David du Colombier4f92d322011-03-24 11:09:31 +0100562}
563*/
564
Willy Tarreau342acb42012-04-23 22:03:39 +0200565static int c_int2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100566{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200567 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200568 smp->data.type = SMP_T_IPV4;
Emeric Brun107ca302010-01-04 16:16:05 +0100569 return 1;
570}
571
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200572static int c_int2ipv6(struct sample *smp)
573{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200574 smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint);
575 v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200576 smp->data.type = SMP_T_IPV6;
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200577 return 1;
578}
579
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100580static int c_str2addr(struct sample *smp)
581{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200582 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) {
583 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100584 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200585 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100586 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100587 return 1;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100588 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200589 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100590 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100591 return 1;
592}
593
Willy Tarreau342acb42012-04-23 22:03:39 +0200594static int c_str2ip(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100595{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200596 if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4))
Emeric Brun107ca302010-01-04 16:16:05 +0100597 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200598 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100599 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100600 return 1;
601}
602
Willy Tarreau342acb42012-04-23 22:03:39 +0200603static int c_str2ipv6(struct sample *smp)
David du Colombier4f92d322011-03-24 11:09:31 +0100604{
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200605 if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6))
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100606 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200607 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100608 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIERfd139902013-12-11 12:38:57 +0100609 return 1;
David du Colombier4f92d322011-03-24 11:09:31 +0100610}
611
Emeric Brun4b9e8022014-11-03 18:17:10 +0100612/*
613 * The NULL char always enforces the end of string if it is met.
614 * Data is never changed, so we can ignore the CONST case
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100615 */
Emeric Brun8ac33d92012-10-17 13:36:06 +0200616static int c_bin2str(struct sample *smp)
617{
Emeric Brun4b9e8022014-11-03 18:17:10 +0100618 int i;
Emeric Brun8ac33d92012-10-17 13:36:06 +0200619
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200620 for (i = 0; i < smp->data.u.str.data; i++) {
621 if (!smp->data.u.str.area[i]) {
622 smp->data.u.str.data = i;
Thierry FOURNIERe87cac12014-03-12 15:07:59 +0100623 break;
Emeric Brun4b9e8022014-11-03 18:17:10 +0100624 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200625 }
Emeric Brun8ac33d92012-10-17 13:36:06 +0200626 return 1;
627}
628
Willy Tarreau342acb42012-04-23 22:03:39 +0200629static int c_int2str(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100630{
Willy Tarreau83061a82018-07-13 11:56:34 +0200631 struct buffer *trash = get_trash_chunk();
Emeric Brun107ca302010-01-04 16:16:05 +0100632 char *pos;
633
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200634 pos = lltoa_r(smp->data.u.sint, trash->area, trash->size);
Emeric Brun107ca302010-01-04 16:16:05 +0100635 if (!pos)
636 return 0;
637
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200638 trash->size = trash->size - (pos - trash->area);
639 trash->area = pos;
640 trash->data = strlen(pos);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200641 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200642 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100643 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100644 return 1;
645}
646
Joseph Herlant757f5ad2018-11-15 12:14:56 -0800647/* This function unconditionally duplicates data and removes the "const" flag.
Willy Tarreauad635822016-08-08 19:21:09 +0200648 * For strings and binary blocks, it also provides a known allocated size with
649 * a length that is capped to the size, and ensures a trailing zero is always
650 * appended for strings. This is necessary for some operations which may
651 * require to extend the length. It returns 0 if it fails, 1 on success.
652 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100653int smp_dup(struct sample *smp)
Emeric Brun485479d2010-09-23 18:02:19 +0200654{
Willy Tarreau83061a82018-07-13 11:56:34 +0200655 struct buffer *trash;
Emeric Brun485479d2010-09-23 18:02:19 +0200656
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200657 switch (smp->data.type) {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100658 case SMP_T_BOOL:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100659 case SMP_T_SINT:
660 case SMP_T_ADDR:
661 case SMP_T_IPV4:
662 case SMP_T_IPV6:
663 /* These type are not const. */
664 break;
Willy Tarreauad635822016-08-08 19:21:09 +0200665
Christopher Fauletec100512017-07-24 15:38:41 +0200666 case SMP_T_METH:
667 if (smp->data.u.meth.meth != HTTP_METH_OTHER)
668 break;
669 /* Fall through */
670
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100671 case SMP_T_STR:
Willy Tarreauad635822016-08-08 19:21:09 +0200672 trash = get_trash_chunk();
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100673 trash->data = smp->data.type == SMP_T_STR ?
674 smp->data.u.str.data : smp->data.u.meth.str.data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200675 if (trash->data > trash->size - 1)
676 trash->data = trash->size - 1;
Willy Tarreauad635822016-08-08 19:21:09 +0200677
Olivier Houchard4468f1c2018-12-07 15:23:41 +0100678 memcpy(trash->area, smp->data.type == SMP_T_STR ?
679 smp->data.u.str.area : smp->data.u.meth.str.area,
680 trash->data);
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200681 trash->area[trash->data] = 0;
Willy Tarreauad635822016-08-08 19:21:09 +0200682 smp->data.u.str = *trash;
683 break;
684
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100685 case SMP_T_BIN:
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100686 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200687 trash->data = smp->data.u.str.data;
688 if (trash->data > trash->size)
689 trash->data = trash->size;
Willy Tarreauad635822016-08-08 19:21:09 +0200690
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200691 memcpy(trash->area, smp->data.u.str.area, trash->data);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200692 smp->data.u.str = *trash;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100693 break;
Christopher Fauletec100512017-07-24 15:38:41 +0200694
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100695 default:
696 /* Other cases are unexpected. */
697 return 0;
698 }
Thierry FOURNIERb805f712013-11-26 20:47:54 +0100699
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100700 /* remove const flag */
701 smp->flags &= ~SMP_F_CONST;
Emeric Brun485479d2010-09-23 18:02:19 +0200702 return 1;
703}
704
Thierry FOURNIER0e9af552013-12-14 14:55:04 +0100705int c_none(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100706{
707 return 1;
708}
709
Willy Tarreau342acb42012-04-23 22:03:39 +0200710static int c_str2int(struct sample *smp)
Emeric Brun107ca302010-01-04 16:16:05 +0100711{
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200712 const char *str;
713 const char *end;
Emeric Brun107ca302010-01-04 16:16:05 +0100714
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200715 if (smp->data.u.str.data == 0)
Thierry FOURNIER60bb0202014-01-27 18:20:48 +0100716 return 0;
717
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200718 str = smp->data.u.str.area;
719 end = smp->data.u.str.area + smp->data.u.str.data;
Emeric Brun107ca302010-01-04 16:16:05 +0100720
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200721 smp->data.u.sint = read_int64(&str, end);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200722 smp->data.type = SMP_T_SINT;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +0100723 smp->flags &= ~SMP_F_CONST;
Emeric Brun107ca302010-01-04 16:16:05 +0100724 return 1;
725}
726
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100727static int c_str2meth(struct sample *smp)
728{
729 enum http_meth_t meth;
730 int len;
731
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200732 meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100733 if (meth == HTTP_METH_OTHER) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200734 len = smp->data.u.str.data;
735 smp->data.u.meth.str.area = smp->data.u.str.area;
736 smp->data.u.meth.str.data = len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100737 }
738 else
739 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200740 smp->data.u.meth.meth = meth;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200741 smp->data.type = SMP_T_METH;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100742 return 1;
743}
744
745static int c_meth2str(struct sample *smp)
746{
747 int len;
748 enum http_meth_t meth;
749
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200750 if (smp->data.u.meth.meth == HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100751 /* The method is unknown. Copy the original pointer. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200752 len = smp->data.u.meth.str.data;
753 smp->data.u.str.area = smp->data.u.meth.str.area;
754 smp->data.u.str.data = len;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200755 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100756 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200757 else if (smp->data.u.meth.meth < HTTP_METH_OTHER) {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100758 /* The method is known, copy the pointer containing the string. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200759 meth = smp->data.u.meth.meth;
Willy Tarreau35b51c62018-09-10 15:38:55 +0200760 smp->data.u.str.area = http_known_methods[meth].ptr;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200761 smp->data.u.str.data = http_known_methods[meth].len;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100762 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200763 smp->data.type = SMP_T_STR;
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100764 }
765 else {
766 /* Unknown method */
767 return 0;
768 }
769 return 1;
770}
771
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200772static int c_addr2bin(struct sample *smp)
773{
Willy Tarreau83061a82018-07-13 11:56:34 +0200774 struct buffer *chk = get_trash_chunk();
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200775
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200776 if (smp->data.type == SMP_T_IPV4) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200777 chk->data = 4;
778 memcpy(chk->area, &smp->data.u.ipv4, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200779 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200780 else if (smp->data.type == SMP_T_IPV6) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200781 chk->data = 16;
782 memcpy(chk->area, &smp->data.u.ipv6, chk->data);
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200783 }
784 else
785 return 0;
786
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200787 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200788 smp->data.type = SMP_T_BIN;
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200789 return 1;
790}
791
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200792static int c_int2bin(struct sample *smp)
793{
Willy Tarreau83061a82018-07-13 11:56:34 +0200794 struct buffer *chk = get_trash_chunk();
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200795
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200796 *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint);
797 chk->data = 8;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200798
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200799 smp->data.u.str = *chk;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200800 smp->data.type = SMP_T_BIN;
Willy Tarreaubbfd1a22014-07-15 21:19:08 +0200801 return 1;
802}
803
Willy Tarreau9700e5c2014-07-15 21:03:26 +0200804
Emeric Brun107ca302010-01-04 16:16:05 +0100805/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +0200806/* Sample casts matrix: */
807/* sample_casts[from type][to type] */
808/* NULL pointer used for impossible sample casts */
Emeric Brun107ca302010-01-04 16:16:05 +0100809/*****************************************************************/
Emeric Brun107ca302010-01-04 16:16:05 +0100810
Thierry FOURNIER8af6ff12013-11-21 10:53:12 +0100811sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = {
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200812/* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */
813/* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, },
814/* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200815/* 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 +0200816/* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, },
817/* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, },
Thierry FOURNIERcc4d1712015-07-24 09:04:56 +0200818/* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +0200819/* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, },
820/* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, },
821/* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, }
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200822};
Emeric Brun107ca302010-01-04 16:16:05 +0100823
Emeric Brun107ca302010-01-04 16:16:05 +0100824/*
Willy Tarreau12785782012-04-27 21:37:17 +0200825 * Parse a sample expression configuration:
Emeric Brun107ca302010-01-04 16:16:05 +0100826 * fetch keyword followed by format conversion keywords.
Willy Tarreau12785782012-04-27 21:37:17 +0200827 * Returns a pointer on allocated sample expression structure.
Willy Tarreaua4312fa2013-04-02 16:34:32 +0200828 * The caller must have set al->ctx.
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100829 * If <endptr> is non-nul, it will be set to the first unparsed character
830 * (which may be the final '\0') on success. If it is nul, the expression
831 * must be properly terminated by a '\0' otherwise an error is reported.
Emeric Brun107ca302010-01-04 16:16:05 +0100832 */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100833struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr)
Emeric Brun107ca302010-01-04 16:16:05 +0100834{
Willy Tarreau833cc792013-07-24 15:34:19 +0200835 const char *begw; /* beginning of word */
836 const char *endw; /* end of word */
837 const char *endt; /* end of term */
Willy Tarreau12785782012-04-27 21:37:17 +0200838 struct sample_expr *expr;
839 struct sample_fetch *fetch;
840 struct sample_conv *conv;
Emeric Brun107ca302010-01-04 16:16:05 +0100841 unsigned long prev_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200842 char *fkw = NULL;
843 char *ckw = NULL;
Willy Tarreau689a1df2013-12-13 00:40:11 +0100844 int err_arg;
Emeric Brun107ca302010-01-04 16:16:05 +0100845
Willy Tarreau833cc792013-07-24 15:34:19 +0200846 begw = str[*idx];
Willy Tarreaued2c6622020-02-14 18:27:10 +0100847 for (endw = begw; is_idchar(*endw); endw++)
848 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200849
850 if (endw == begw) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100851 memprintf(err_msg, "missing fetch method");
Emeric Brun107ca302010-01-04 16:16:05 +0100852 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200853 }
Emeric Brun107ca302010-01-04 16:16:05 +0100854
Willy Tarreau833cc792013-07-24 15:34:19 +0200855 /* keep a copy of the current fetch keyword for error reporting */
856 fkw = my_strndup(begw, endw - begw);
Emeric Brun107ca302010-01-04 16:16:05 +0100857
Willy Tarreau833cc792013-07-24 15:34:19 +0200858 fetch = find_sample_fetch(begw, endw - begw);
859 if (!fetch) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100860 memprintf(err_msg, "unknown fetch method '%s'", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100861 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200862 }
Emeric Brun107ca302010-01-04 16:16:05 +0100863
Willy Tarreau833cc792013-07-24 15:34:19 +0200864 /* At this point, we have :
865 * - begw : beginning of the keyword
Willy Tarreau689a1df2013-12-13 00:40:11 +0100866 * - endw : end of the keyword, first character not part of keyword
Willy Tarreau833cc792013-07-24 15:34:19 +0200867 */
868
869 if (fetch->out_type >= SMP_TYPES) {
Willy Tarreau975c1782013-12-12 23:16:54 +0100870 memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw);
Emeric Brun107ca302010-01-04 16:16:05 +0100871 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200872 }
Emeric Brun107ca302010-01-04 16:16:05 +0100873 prev_type = fetch->out_type;
Willy Tarreau833cc792013-07-24 15:34:19 +0200874
Vincent Bernat02779b62016-04-03 13:48:43 +0200875 expr = calloc(1, sizeof(*expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200876 if (!expr)
877 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100878
879 LIST_INIT(&(expr->conv_exprs));
880 expr->fetch = fetch;
Willy Tarreau2e845be2012-10-19 19:49:09 +0200881 expr->arg_p = empty_arg_list;
Emeric Brun107ca302010-01-04 16:16:05 +0100882
Willy Tarreau689a1df2013-12-13 00:40:11 +0100883 /* Note that we call the argument parser even with an empty string,
884 * this allows it to automatically create entries for mandatory
885 * implicit arguments (eg: local proxy name).
886 */
887 al->kw = expr->fetch->kw;
888 al->conv = NULL;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100889 if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) {
Willy Tarreau689a1df2013-12-13 00:40:11 +0100890 memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg);
891 goto out_error;
892 }
Willy Tarreau2e845be2012-10-19 19:49:09 +0200893
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100894 /* now endt is our first char not part of the arg list, typically the
895 * comma after the sample fetch name or after the closing parenthesis,
896 * or the NUL char.
897 */
898
Willy Tarreau689a1df2013-12-13 00:40:11 +0100899 if (!expr->arg_p) {
900 expr->arg_p = empty_arg_list;
Emeric Brun485479d2010-09-23 18:02:19 +0200901 }
Willy Tarreau689a1df2013-12-13 00:40:11 +0100902 else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) {
903 memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +0200904 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100905 }
906
Willy Tarreau833cc792013-07-24 15:34:19 +0200907 /* Now process the converters if any. We have two supported syntaxes
908 * for the converters, which can be combined :
909 * - comma-delimited list of converters just after the keyword and args ;
910 * - one converter per keyword
911 * The combination allows to have each keyword being a comma-delimited
912 * series of converters.
913 *
914 * We want to process the former first, then the latter. For this we start
915 * from the beginning of the supposed place in the exiting conv chain, which
916 * starts at the last comma (endt).
917 */
918
919 while (1) {
Willy Tarreau12785782012-04-27 21:37:17 +0200920 struct sample_conv_expr *conv_expr;
Willy Tarreau46dfd782019-12-17 10:25:29 +0100921 int err_arg;
922 int argcnt;
Emeric Brun107ca302010-01-04 16:16:05 +0100923
Willy Tarreau833cc792013-07-24 15:34:19 +0200924 if (*endt && *endt != ',') {
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100925 if (endptr) {
926 /* end found, let's stop here */
927 break;
928 }
Willy Tarreau833cc792013-07-24 15:34:19 +0200929 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 Tarreau80b53ff2020-02-14 08:40:37 +0100936 /* FIXME: how long should we support such idiocies ? Maybe we
937 * should already warn ?
938 */
Willy Tarreau833cc792013-07-24 15:34:19 +0200939 while (*endt == ',') /* then trailing commas */
940 endt++;
941
Willy Tarreau97108e02016-11-25 07:33:24 +0100942 begw = endt; /* start of converter */
Willy Tarreau833cc792013-07-24 15:34:19 +0200943
944 if (!*begw) {
945 /* none ? skip to next string */
946 (*idx)++;
947 begw = str[*idx];
948 if (!begw || !*begw)
949 break;
950 }
951
Willy Tarreaued2c6622020-02-14 18:27:10 +0100952 for (endw = begw; is_idchar(*endw); endw++)
953 ;
Willy Tarreau833cc792013-07-24 15:34:19 +0200954
955 free(ckw);
956 ckw = my_strndup(begw, endw - begw);
957
958 conv = find_sample_conv(begw, endw - begw);
959 if (!conv) {
960 /* we found an isolated keyword that we don't know, it's not ours */
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100961 if (begw == str[*idx]) {
962 endt = begw;
Willy Tarreau833cc792013-07-24 15:34:19 +0200963 break;
Willy Tarreaue3b57bf2020-02-14 16:50:14 +0100964 }
Willy Tarreau97108e02016-11-25 07:33:24 +0100965 memprintf(err_msg, "unknown converter '%s'", ckw);
Willy Tarreau833cc792013-07-24 15:34:19 +0200966 goto out_error;
967 }
Emeric Brun107ca302010-01-04 16:16:05 +0100968
Willy Tarreau833cc792013-07-24 15:34:19 +0200969 if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100970 memprintf(err_msg, "returns type of converter '%s' is unknown", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100971 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200972 }
Emeric Brun107ca302010-01-04 16:16:05 +0100973
974 /* If impossible type conversion */
Willy Tarreau12785782012-04-27 21:37:17 +0200975 if (!sample_casts[prev_type][conv->in_type]) {
Willy Tarreau97108e02016-11-25 07:33:24 +0100976 memprintf(err_msg, "converter '%s' cannot be applied", ckw);
Emeric Brun107ca302010-01-04 16:16:05 +0100977 goto out_error;
Emeric Brun485479d2010-09-23 18:02:19 +0200978 }
Emeric Brun107ca302010-01-04 16:16:05 +0100979
980 prev_type = conv->out_type;
Vincent Bernat02779b62016-04-03 13:48:43 +0200981 conv_expr = calloc(1, sizeof(*conv_expr));
Emeric Brun485479d2010-09-23 18:02:19 +0200982 if (!conv_expr)
983 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +0100984
985 LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list));
986 conv_expr->conv = conv;
987
Willy Tarreau46dfd782019-12-17 10:25:29 +0100988 al->kw = expr->fetch->kw;
989 al->conv = conv_expr->conv->kw;
Willy Tarreau80b53ff2020-02-14 08:40:37 +0100990 argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al);
Willy Tarreau46dfd782019-12-17 10:25:29 +0100991 if (argcnt < 0) {
992 memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg);
993 goto out_error;
994 }
Willy Tarreau9e92d322010-01-26 17:58:06 +0100995
Willy Tarreau46dfd782019-12-17 10:25:29 +0100996 if (argcnt && !conv->arg_mask) {
997 memprintf(err_msg, "converter '%s' does not support any args", ckw);
998 goto out_error;
999 }
Willy Tarreau21d68a62012-04-20 15:52:36 +02001000
Willy Tarreau46dfd782019-12-17 10:25:29 +01001001 if (!conv_expr->arg_p)
1002 conv_expr->arg_p = empty_arg_list;
Willy Tarreau2e845be2012-10-19 19:49:09 +02001003
Willy Tarreau46dfd782019-12-17 10:25:29 +01001004 if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) {
1005 memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg);
Emeric Brun485479d2010-09-23 18:02:19 +02001006 goto out_error;
Emeric Brun107ca302010-01-04 16:16:05 +01001007 }
1008 }
Emeric Brun485479d2010-09-23 18:02:19 +02001009
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001010 if (endptr) {
1011 /* end found, let's stop here */
1012 *endptr = (char *)endt;
1013 }
1014
Willy Tarreau833cc792013-07-24 15:34:19 +02001015 out:
1016 free(fkw);
1017 free(ckw);
Emeric Brun107ca302010-01-04 16:16:05 +01001018 return expr;
1019
1020out_error:
Willy Tarreau12785782012-04-27 21:37:17 +02001021 /* TODO: prune_sample_expr(expr); */
Willy Tarreau833cc792013-07-24 15:34:19 +02001022 expr = NULL;
1023 goto out;
Emeric Brun107ca302010-01-04 16:16:05 +01001024}
1025
1026/*
Willy Tarreau12785782012-04-27 21:37:17 +02001027 * Process a fetch + format conversion of defined by the sample expression <expr>
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001028 * on request or response considering the <opt> parameter.
Willy Tarreau12785782012-04-27 21:37:17 +02001029 * Returns a pointer on a typed sample structure containing the result or NULL if
1030 * sample is not found or when format conversion failed.
Emeric Brun107ca302010-01-04 16:16:05 +01001031 * If <p> is not null, function returns results in structure pointed by <p>.
Willy Tarreau12785782012-04-27 21:37:17 +02001032 * If <p> is null, functions returns a pointer on a static sample structure.
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001033 *
1034 * Note: the fetch functions are required to properly set the return type. The
1035 * conversion functions must do so too. However the cast functions do not need
1036 * to since they're made to cast mutiple types according to what is required.
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001037 *
1038 * The caller may indicate in <opt> if it considers the result final or not.
1039 * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify
1040 * if the result is stable or not, according to the following table :
1041 *
1042 * return MAY_CHANGE FINAL Meaning for the sample
1043 * NULL 0 * Not present and will never be (eg: header)
1044 * NULL 1 0 Not present yet, could change (eg: POST param)
1045 * NULL 1 1 Not present yet, will not change anymore
1046 * smp 0 * Present and will not change (eg: header)
1047 * smp 1 0 Present, may change (eg: request length)
1048 * smp 1 1 Present, last known value (eg: request length)
Emeric Brun107ca302010-01-04 16:16:05 +01001049 */
Willy Tarreau192252e2015-04-04 01:47:55 +02001050struct sample *sample_process(struct proxy *px, struct session *sess,
1051 struct stream *strm, unsigned int opt,
Willy Tarreau12785782012-04-27 21:37:17 +02001052 struct sample_expr *expr, struct sample *p)
Emeric Brun107ca302010-01-04 16:16:05 +01001053{
Willy Tarreau12785782012-04-27 21:37:17 +02001054 struct sample_conv_expr *conv_expr;
Emeric Brun107ca302010-01-04 16:16:05 +01001055
Willy Tarreau18387e22013-07-25 12:02:38 +02001056 if (p == NULL) {
Willy Tarreaub4a88f02012-04-23 21:35:11 +02001057 p = &temp_smp;
Willy Tarreau6c616e02014-06-25 16:56:41 +02001058 memset(p, 0, sizeof(*p));
Willy Tarreau18387e22013-07-25 12:02:38 +02001059 }
Emeric Brun107ca302010-01-04 16:16:05 +01001060
Willy Tarreau1777ea62016-03-10 16:15:46 +01001061 smp_set_owner(p, px, sess, strm, opt);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02001062 if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001063 return NULL;
1064
Emeric Brun107ca302010-01-04 16:16:05 +01001065 list_for_each_entry(conv_expr, &expr->conv_exprs, list) {
Willy Tarreau12e50112012-04-25 17:21:49 +02001066 /* we want to ensure that p->type can be casted into
1067 * conv_expr->conv->in_type. We have 3 possibilities :
1068 * - NULL => not castable.
1069 * - c_none => nothing to do (let's optimize it)
1070 * - other => apply cast and prepare to fail
1071 */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001072 if (!sample_casts[p->data.type][conv_expr->conv->in_type])
Willy Tarreau12e50112012-04-25 17:21:49 +02001073 return NULL;
1074
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001075 if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
1076 !sample_casts[p->data.type][conv_expr->conv->in_type](p))
Emeric Brun107ca302010-01-04 16:16:05 +01001077 return NULL;
1078
Willy Tarreau12e50112012-04-25 17:21:49 +02001079 /* OK cast succeeded */
1080
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001081 if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private))
Emeric Brun107ca302010-01-04 16:16:05 +01001082 return NULL;
Emeric Brun107ca302010-01-04 16:16:05 +01001083 }
1084 return p;
1085}
1086
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001087/*
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001088 * Resolve all remaining arguments in proxy <p>. Returns the number of
1089 * errors or 0 if everything is fine.
1090 */
1091int smp_resolve_args(struct proxy *p)
1092{
1093 struct arg_list *cur, *bak;
1094 const char *ctx, *where;
1095 const char *conv_ctx, *conv_pre, *conv_pos;
1096 struct userlist *ul;
Willy Tarreau46947782015-01-19 19:00:58 +01001097 struct my_regex *reg;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001098 struct arg *arg;
1099 int cfgerr = 0;
Willy Tarreau46947782015-01-19 19:00:58 +01001100 int rflags;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001101
1102 list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) {
1103 struct proxy *px;
1104 struct server *srv;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001105 struct stktable *t;
1106 char *pname, *sname, *stktname;
Willy Tarreau46947782015-01-19 19:00:58 +01001107 char *err;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001108
1109 arg = cur->arg;
1110
1111 /* prepare output messages */
1112 conv_pre = conv_pos = conv_ctx = "";
1113 if (cur->conv) {
1114 conv_ctx = cur->conv;
1115 conv_pre = "conversion keyword '";
1116 conv_pos = "' for ";
1117 }
1118
1119 where = "in";
1120 ctx = "sample fetch keyword";
1121 switch (cur->ctx) {
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001122 case ARGC_STK: where = "in stick rule in"; break;
1123 case ARGC_TRK: where = "in tracking rule in"; break;
1124 case ARGC_LOG: where = "in log-format string in"; break;
1125 case ARGC_LOGSD: where = "in log-format-sd string in"; break;
1126 case ARGC_HRQ: where = "in http-request header format string in"; break;
1127 case ARGC_HRS: where = "in http-response header format string in"; break;
1128 case ARGC_UIF: where = "in unique-id-format string in"; break;
1129 case ARGC_RDR: where = "in redirect format string in"; break;
1130 case ARGC_CAP: where = "in capture rule in"; break;
1131 case ARGC_ACL: ctx = "ACL keyword"; break;
1132 case ARGC_SRV: where = "in server directive in"; break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001133 case ARGC_SPOE: where = "in spoe-message directive in"; break;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001134 }
1135
1136 /* set a few default settings */
1137 px = p;
1138 pname = p->id;
1139
1140 switch (arg->type) {
1141 case ARGT_SRV:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001142 if (!arg->data.str.data) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001143 ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1144 cur->file, cur->line,
1145 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001146 cfgerr++;
1147 continue;
1148 }
1149
1150 /* we support two formats : "bck/srv" and "srv" */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001151 sname = strrchr(arg->data.str.area, '/');
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001152
1153 if (sname) {
1154 *sname++ = '\0';
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001155 pname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001156
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001157 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001158 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001159 ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1160 cur->file, cur->line, pname,
1161 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001162 cfgerr++;
1163 break;
1164 }
1165 }
1166 else
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001167 sname = arg->data.str.area;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001168
1169 srv = findserver(px, sname);
1170 if (!srv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001171 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",
1172 cur->file, cur->line, sname, pname,
1173 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001174 cfgerr++;
1175 break;
1176 }
1177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001178 free(arg->data.str.area);
1179 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001180 arg->unresolved = 0;
1181 arg->data.srv = srv;
1182 break;
1183
1184 case ARGT_FE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001185 if (arg->data.str.data) {
1186 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001187 px = proxy_fe_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001188 }
1189
1190 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001191 ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1192 cur->file, cur->line, pname,
1193 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001194 cfgerr++;
1195 break;
1196 }
1197
1198 if (!(px->cap & PR_CAP_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001199 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",
1200 cur->file, cur->line, pname,
1201 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001202 cfgerr++;
1203 break;
1204 }
1205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001206 free(arg->data.str.area);
1207 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001208 arg->unresolved = 0;
1209 arg->data.prx = px;
1210 break;
1211
1212 case ARGT_BE:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001213 if (arg->data.str.data) {
1214 pname = arg->data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001215 px = proxy_be_by_name(pname);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001216 }
1217
1218 if (!px) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001219 ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
1220 cur->file, cur->line, pname,
1221 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001222 cfgerr++;
1223 break;
1224 }
1225
1226 if (!(px->cap & PR_CAP_BE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001227 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",
1228 cur->file, cur->line, pname,
1229 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001230 cfgerr++;
1231 break;
1232 }
1233
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001234 free(arg->data.str.area);
1235 arg->data.str.area = NULL;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001236 arg->unresolved = 0;
1237 arg->data.prx = px;
1238 break;
1239
1240 case ARGT_TAB:
Frédéric Lécaille9417f452019-06-20 09:31:04 +02001241 if (arg->data.str.data)
1242 stktname = arg->data.str.area;
1243 else
1244 stktname = px->id;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001245
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001246 t = stktable_find_by_name(stktname);
1247 if (!t) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001248 ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001249 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001250 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001251 cfgerr++;
1252 break;
1253 }
1254
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001255 if (!t->size) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001256 ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001257 cur->file, cur->line, stktname,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001258 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001259 cfgerr++;
1260 break;
1261 }
1262
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001263 if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001264 ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001265 cur->file, cur->line, t->proxy->id, p->id);
Willy Tarreau1a0fe3b2019-02-06 10:25:07 +01001266 cfgerr++;
1267 break;
Willy Tarreau151e1ca2019-02-05 11:38:38 +01001268 }
1269
Frédéric Lécaillebe367932019-08-07 09:28:39 +02001270 if (!in_proxies_list(t->proxies_list, p)) {
Frédéric Lécaille015e4d72019-03-19 14:55:01 +01001271 p->next_stkt_ref = t->proxies_list;
1272 t->proxies_list = p;
1273 }
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;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001278 arg->data.t = t;
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001279 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
Willy Tarreau46947782015-01-19 19:00:58 +01001320 rflags = 0;
1321 rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
1322 err = NULL;
1323
Dragan Dosen26743032019-04-30 15:54:36 +02001324 if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001325 ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
1326 cur->file, cur->line,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001327 arg->data.str.area,
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
Willy Tarreau46947782015-01-19 19:00:58 +01001329 cfgerr++;
1330 continue;
1331 }
1332
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001333 free(arg->data.str.area);
1334 arg->data.str.area = NULL;
Willy Tarreau46947782015-01-19 19:00:58 +01001335 arg->unresolved = 0;
1336 arg->data.reg = reg;
1337 break;
1338
1339
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001340 }
1341
1342 LIST_DEL(&cur->list);
1343 free(cur);
1344 } /* end of args processing */
1345
1346 return cfgerr;
1347}
1348
1349/*
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001350 * Process a fetch + format conversion as defined by the sample expression
1351 * <expr> on request or response considering the <opt> parameter. The output is
Adis Nezirovic79beb242015-07-06 15:41:02 +02001352 * not explicitly set to <smp_type>, but shall be compatible with it as
1353 * specified by 'sample_casts' table. If a stable sample can be fetched, or an
1354 * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001355 * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found
1356 * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is
1357 * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to
1358 * take actions (eg: wait longer). If a sample could not be found or could not
Willy Tarreau6bcb0a82014-07-30 08:56:35 +02001359 * be converted, NULL is returned. The caller MUST NOT use the sample if the
1360 * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is
1361 * still hope to get it after waiting longer, and is not converted to string.
1362 * The possible output combinations are the following :
1363 *
1364 * return MAY_CHANGE FINAL Meaning for the sample
1365 * NULL * * Not present and will never be (eg: header)
1366 * smp 0 * Final value converted (eg: header)
1367 * smp 1 0 Not present yet, may appear later (eg: header)
1368 * smp 1 1 never happens (either flag is cleared on output)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001369 */
Adis Nezirovic79beb242015-07-06 15:41:02 +02001370struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
Willy Tarreau192252e2015-04-04 01:47:55 +02001371 struct stream *strm, unsigned int opt,
Adis Nezirovic79beb242015-07-06 15:41:02 +02001372 struct sample_expr *expr, int smp_type)
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001373{
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001374 struct sample *smp = &temp_smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001375
Willy Tarreau6c616e02014-06-25 16:56:41 +02001376 memset(smp, 0, sizeof(*smp));
1377
Willy Tarreau192252e2015-04-04 01:47:55 +02001378 if (!sample_process(px, sess, strm, opt, expr, smp)) {
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001379 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
1380 return smp;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001381 return NULL;
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001382 }
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001383
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001384 if (!sample_casts[smp->data.type][smp_type])
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001385 return NULL;
1386
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001387 if (!sample_casts[smp->data.type][smp_type](smp))
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001388 return NULL;
1389
Willy Tarreau5b4bf702014-06-13 16:04:35 +02001390 smp->flags &= ~SMP_F_MAY_CHANGE;
Willy Tarreaue7ad4bb2012-12-21 00:02:32 +01001391 return smp;
1392}
1393
Christopher Faulet476e5d02016-10-26 11:34:47 +02001394static void release_sample_arg(struct arg *p)
1395{
1396 struct arg *p_back = p;
1397
1398 if (!p)
1399 return;
1400
1401 while (p->type != ARGT_STOP) {
1402 if (p->type == ARGT_STR || p->unresolved) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 free(p->data.str.area);
1404 p->data.str.area = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001405 p->unresolved = 0;
1406 }
1407 else if (p->type == ARGT_REG) {
Dragan Dosen26743032019-04-30 15:54:36 +02001408 regex_free(p->data.reg);
1409 p->data.reg = NULL;
Christopher Faulet476e5d02016-10-26 11:34:47 +02001410 }
1411 p++;
1412 }
1413
1414 if (p_back != empty_arg_list)
1415 free(p_back);
1416}
1417
1418void release_sample_expr(struct sample_expr *expr)
1419{
1420 struct sample_conv_expr *conv_expr, *conv_exprb;
1421
1422 if (!expr)
1423 return;
1424
1425 list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list)
1426 release_sample_arg(conv_expr->arg_p);
1427 release_sample_arg(expr->arg_p);
1428 free(expr);
1429}
1430
Emeric Brun107ca302010-01-04 16:16:05 +01001431/*****************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02001432/* Sample format convert functions */
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02001433/* These functions set the data type on return. */
Emeric Brun107ca302010-01-04 16:16:05 +01001434/*****************************************************************/
1435
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001436static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private)
1437{
1438 int i;
1439 struct sample tmp;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001440 struct buffer *buf;
1441 struct sink *sink;
1442 struct ist line;
1443 char *pfx;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001444
Willy Tarreau0851fd52019-12-17 10:07:25 +01001445 buf = alloc_trash_chunk();
1446 if (!buf)
1447 goto end;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001448
Willy Tarreau0851fd52019-12-17 10:07:25 +01001449 sink = (struct sink *)arg_p[1].data.str.area;
1450 BUG_ON(!sink);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001451
Willy Tarreau0851fd52019-12-17 10:07:25 +01001452 pfx = arg_p[0].data.str.area;
1453 BUG_ON(!pfx);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001454
Willy Tarreau0851fd52019-12-17 10:07:25 +01001455 chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]);
1456 if (!sample_casts[smp->data.type][SMP_T_STR])
1457 goto nocast;
1458
1459 /* Copy sample fetch. This puts the sample as const, the
1460 * cast will copy data if a transformation is required.
1461 */
1462 memcpy(&tmp, smp, sizeof(struct sample));
1463 tmp.flags = SMP_F_CONST;
1464
1465 if (!sample_casts[smp->data.type][SMP_T_STR](&tmp))
1466 goto nocast;
1467
1468 /* Display the displayable chars*. */
1469 b_putchr(buf, '<');
1470 for (i = 0; i < tmp.data.u.str.data; i++) {
Willy Tarreau90807112020-02-25 08:16:33 +01001471 if (isprint((unsigned char)tmp.data.u.str.area[i]))
Willy Tarreau0851fd52019-12-17 10:07:25 +01001472 b_putchr(buf, tmp.data.u.str.area[i]);
1473 else
1474 b_putchr(buf, '.');
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001475 }
Willy Tarreau0851fd52019-12-17 10:07:25 +01001476 b_putchr(buf, '>');
1477
1478 done:
1479 line = ist2(buf->area, buf->data);
1480 sink_write(sink, &line, 1);
1481 end:
1482 free_trash_chunk(buf);
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001483 return 1;
Willy Tarreau0851fd52019-12-17 10:07:25 +01001484 nocast:
1485 chunk_appendf(buf, "(undisplayable)");
1486 goto done;
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001487}
Willy Tarreau0851fd52019-12-17 10:07:25 +01001488
1489// This function checks the "debug" converter's arguments.
1490static int smp_check_debug(struct arg *args, struct sample_conv *conv,
1491 const char *file, int line, char **err)
1492{
1493 const char *name = "buf0";
1494 struct sink *sink = NULL;
1495
1496 if (args[0].type != ARGT_STR) {
1497 /* optional prefix */
1498 args[0].data.str.area = "";
1499 args[0].data.str.data = 0;
1500 }
1501
1502 if (args[1].type == ARGT_STR)
1503 name = args[1].data.str.area;
1504
1505 sink = sink_find(name);
1506 if (!sink) {
1507 memprintf(err, "No such sink '%s'", name);
1508 return 0;
1509 }
1510
1511 args[1].data.str.area = (char *)sink;
1512 args[1].data.str.data = 0; // that's not a string anymore
1513 return 1;
1514}
Thierry FOURNIER9687c772015-05-07 15:46:29 +02001515
Holger Just1bfc24b2017-05-06 00:56:53 +02001516static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private)
1517{
Willy Tarreau83061a82018-07-13 11:56:34 +02001518 struct buffer *trash = get_trash_chunk();
Holger Just1bfc24b2017-05-06 00:56:53 +02001519 int bin_len;
1520
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001521 trash->data = 0;
1522 bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data,
1523 trash->area, trash->size);
Holger Just1bfc24b2017-05-06 00:56:53 +02001524 if (bin_len < 0)
1525 return 0;
1526
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 trash->data = bin_len;
Holger Just1bfc24b2017-05-06 00:56:53 +02001528 smp->data.u.str = *trash;
1529 smp->data.type = SMP_T_BIN;
1530 smp->flags &= ~SMP_F_CONST;
1531 return 1;
1532}
1533
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001534static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun53d1a982014-04-30 18:21:37 +02001535{
Willy Tarreau83061a82018-07-13 11:56:34 +02001536 struct buffer *trash = get_trash_chunk();
Emeric Brun53d1a982014-04-30 18:21:37 +02001537 int b64_len;
1538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001539 trash->data = 0;
1540 b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data,
1541 trash->area, trash->size);
Emeric Brun53d1a982014-04-30 18:21:37 +02001542 if (b64_len < 0)
1543 return 0;
1544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001545 trash->data = b64_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001546 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001547 smp->data.type = SMP_T_STR;
Emeric Brun53d1a982014-04-30 18:21:37 +02001548 smp->flags &= ~SMP_F_CONST;
1549 return 1;
1550}
1551
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001552static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private)
1553{
1554 blk_SHA_CTX ctx;
Willy Tarreau83061a82018-07-13 11:56:34 +02001555 struct buffer *trash = get_trash_chunk();
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001556
1557 memset(&ctx, 0, sizeof(ctx));
1558
1559 blk_SHA1_Init(&ctx);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001560 blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1561 blk_SHA1_Final((unsigned char *) trash->area, &ctx);
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001562
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001563 trash->data = 20;
Dragan Dosen6e5a9ca2017-10-24 09:18:23 +02001564 smp->data.u.str = *trash;
1565 smp->data.type = SMP_T_BIN;
1566 smp->flags &= ~SMP_F_CONST;
1567 return 1;
1568}
1569
Tim Duesterhusd4376302019-06-17 12:41:44 +02001570#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01001571static int smp_check_sha2(struct arg *args, struct sample_conv *conv,
1572 const char *file, int line, char **err)
1573{
1574 if (args[0].type == ARGT_STOP)
1575 return 1;
1576 if (args[0].type != ARGT_SINT) {
1577 memprintf(err, "Invalid type '%s'", arg_type_names[args[0].type]);
1578 return 0;
1579 }
1580
1581 switch (args[0].data.sint) {
1582 case 224:
1583 case 256:
1584 case 384:
1585 case 512:
1586 /* this is okay */
1587 return 1;
1588 default:
1589 memprintf(err, "Unsupported number of bits: '%lld'", args[0].data.sint);
1590 return 0;
1591 }
1592}
1593
Tim Duesterhusd4376302019-06-17 12:41:44 +02001594static int sample_conv_sha2(const struct arg *arg_p, struct sample *smp, void *private)
1595{
1596 struct buffer *trash = get_trash_chunk();
1597 int bits = 256;
1598 if (arg_p && arg_p->data.sint)
1599 bits = arg_p->data.sint;
1600
1601 switch (bits) {
1602 case 224: {
1603 SHA256_CTX ctx;
1604
1605 memset(&ctx, 0, sizeof(ctx));
1606
1607 SHA224_Init(&ctx);
1608 SHA224_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1609 SHA224_Final((unsigned char *) trash->area, &ctx);
1610 trash->data = SHA224_DIGEST_LENGTH;
1611 break;
1612 }
1613 case 256: {
1614 SHA256_CTX ctx;
1615
1616 memset(&ctx, 0, sizeof(ctx));
1617
1618 SHA256_Init(&ctx);
1619 SHA256_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1620 SHA256_Final((unsigned char *) trash->area, &ctx);
1621 trash->data = SHA256_DIGEST_LENGTH;
1622 break;
1623 }
1624 case 384: {
1625 SHA512_CTX ctx;
1626
1627 memset(&ctx, 0, sizeof(ctx));
1628
1629 SHA384_Init(&ctx);
1630 SHA384_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1631 SHA384_Final((unsigned char *) trash->area, &ctx);
1632 trash->data = SHA384_DIGEST_LENGTH;
1633 break;
1634 }
1635 case 512: {
1636 SHA512_CTX ctx;
1637
1638 memset(&ctx, 0, sizeof(ctx));
1639
1640 SHA512_Init(&ctx);
1641 SHA512_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data);
1642 SHA512_Final((unsigned char *) trash->area, &ctx);
1643 trash->data = SHA512_DIGEST_LENGTH;
1644 break;
1645 }
1646 default:
1647 return 0;
1648 }
1649
1650 smp->data.u.str = *trash;
1651 smp->data.type = SMP_T_BIN;
1652 smp->flags &= ~SMP_F_CONST;
1653 return 1;
1654}
1655#endif
1656
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001657static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001658{
Willy Tarreau83061a82018-07-13 11:56:34 +02001659 struct buffer *trash = get_trash_chunk();
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001660 unsigned char c;
1661 int ptr = 0;
1662
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001663 trash->data = 0;
1664 while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) {
1665 c = smp->data.u.str.area[ptr++];
1666 trash->area[trash->data++] = hextab[(c >> 4) & 0xF];
1667 trash->area[trash->data++] = hextab[c & 0xF];
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001668 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001669 smp->data.u.str = *trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001670 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001671 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01001672 return 1;
1673}
1674
Dragan Dosen3f957b22017-10-24 09:27:34 +02001675static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private)
1676{
1677 long long int n = 0;
1678 int i, c;
1679
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001680 for (i = 0; i < smp->data.u.str.data; i++) {
1681 if ((c = hex2i(smp->data.u.str.area[i])) < 0)
Dragan Dosen3f957b22017-10-24 09:27:34 +02001682 return 0;
1683 n = (n << 4) + c;
1684 }
1685
1686 smp->data.u.sint = n;
1687 smp->data.type = SMP_T_SINT;
1688 smp->flags &= ~SMP_F_CONST;
1689 return 1;
1690}
1691
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001692/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001693static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001694{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001695 smp->data.u.sint = hash_djb2(smp->data.u.str.area,
1696 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001697 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001698 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001699 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001700 return 1;
1701}
1702
Willy Tarreau60a2ee72017-12-15 07:13:48 +01001703static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private)
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001704{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001705 int i = smp->data.u.str.data;
Etienne Carriereed0d24e2017-12-13 13:41:34 +01001706 smp->data.u.sint = i;
1707 smp->data.type = SMP_T_SINT;
1708 return 1;
1709}
1710
1711
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001712static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001713{
1714 int i;
1715
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001716 if (!smp_make_rw(smp))
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01001717 return 0;
1718
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001719 for (i = 0; i < smp->data.u.str.data; i++) {
1720 if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z'))
1721 smp->data.u.str.area[i] += 'a' - 'A';
Emeric Brun107ca302010-01-04 16:16:05 +01001722 }
1723 return 1;
1724}
1725
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001726static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun107ca302010-01-04 16:16:05 +01001727{
1728 int i;
1729
Willy Tarreauf0645dc2016-08-09 14:29:38 +02001730 if (!smp_make_rw(smp))
Emeric Brun485479d2010-09-23 18:02:19 +02001731 return 0;
1732
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001733 for (i = 0; i < smp->data.u.str.data; i++) {
1734 if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z'))
1735 smp->data.u.str.area[i] += 'A' - 'a';
Emeric Brun107ca302010-01-04 16:16:05 +01001736 }
1737 return 1;
1738}
1739
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001740/* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */
1741static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private)
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001742{
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001743 /* Attempt to convert to IPv4 to apply the correct mask. */
1744 c_ipv62ip(smp);
1745
1746 if (smp->data.type == SMP_T_IPV4) {
1747 smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr;
1748 smp->data.type = SMP_T_IPV4;
1749 }
1750 else if (smp->data.type == SMP_T_IPV6) {
1751 /* IPv6 cannot be converted without an IPv6 mask. */
1752 if (args[1].type != ARGT_IPV6)
1753 return 0;
1754
Willy Tarreaua8b7ecd2020-02-25 09:43:22 +01001755 write_u64(&smp->data.u.ipv6.s6_addr[0],
1756 read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0]));
1757 write_u64(&smp->data.u.ipv6.s6_addr[8],
1758 read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8]));
Tim Duesterhus1478aa72018-01-25 16:24:51 +01001759 smp->data.type = SMP_T_IPV6;
1760 }
1761
Willy Tarreaud31d6eb2010-01-26 18:01:41 +01001762 return 1;
1763}
1764
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001765/* takes an UINT value on input supposed to represent the time since EPOCH,
1766 * adds an optional offset found in args[1] and emits a string representing
1767 * the local time in the format specified in args[1] using strftime().
1768 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001769static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001770{
Willy Tarreau83061a82018-07-13 11:56:34 +02001771 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001772 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001773 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001774 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001775
1776 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001777 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001778 curr_date += args[1].data.sint;
1779
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001780 tm = localtime(&curr_date);
1781 if (!tm)
1782 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001783 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001784 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1785 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001786 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001787 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001788 return 1;
1789}
1790
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001791/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001792static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001793{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001794 smp->data.u.sint = hash_sdbm(smp->data.u.str.area,
1795 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001796 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001797 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001798 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001799 return 1;
1800}
1801
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001802/* takes an UINT value on input supposed to represent the time since EPOCH,
1803 * adds an optional offset found in args[1] and emits a string representing
1804 * the UTC date in the format specified in args[1] using strftime().
1805 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001806static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001807{
Willy Tarreau83061a82018-07-13 11:56:34 +02001808 struct buffer *temp;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001809 /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001810 time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL;
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001811 struct tm *tm;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001812
1813 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001814 if (args[1].type == ARGT_SINT)
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001815 curr_date += args[1].data.sint;
1816
Thierry FOURNIERfac9ccf2015-07-08 00:15:20 +02001817 tm = gmtime(&curr_date);
1818 if (!tm)
1819 return 0;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001820 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001821 temp->data = strftime(temp->area, temp->size, args[0].data.str.area,
1822 tm);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001823 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001824 smp->data.type = SMP_T_STR;
Willy Tarreau0dbfdba2014-07-10 16:37:47 +02001825 return 1;
1826}
1827
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001828/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001829static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001830{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001831 smp->data.u.sint = hash_wt6(smp->data.u.str.area,
1832 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001833 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001834 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001835 smp->data.type = SMP_T_SINT;
Willy Tarreau23ec4ca2014-07-15 20:15:37 +02001836 return 1;
1837}
1838
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001839/* hashes the binary input into a 32-bit unsigned int using xxh.
1840 * The seed of the hash defaults to 0 but can be changd in argument 1.
1841 */
1842static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private)
1843{
1844 unsigned int seed;
1845
1846 if (arg_p && arg_p->data.sint)
1847 seed = arg_p->data.sint;
1848 else
1849 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001850 smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data,
1851 seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001852 smp->data.type = SMP_T_SINT;
1853 return 1;
1854}
1855
1856/* hashes the binary input into a 64-bit unsigned int using xxh.
1857 * In fact, the function returns a 64 bit unsigned, but the sample
1858 * storage of haproxy only proposes 64-bits signed, so the value is
1859 * cast as signed. This cast doesn't impact the hash repartition.
1860 * The seed of the hash defaults to 0 but can be changd in argument 1.
1861 */
1862static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private)
1863{
1864 unsigned long long int seed;
1865
1866 if (arg_p && arg_p->data.sint)
1867 seed = (unsigned long long int)arg_p->data.sint;
1868 else
1869 seed = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001870 smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area,
1871 smp->data.u.str.data, seed);
Thierry FOURNIER01e09742016-12-26 11:46:11 +01001872 smp->data.type = SMP_T_SINT;
1873 return 1;
1874}
1875
Willy Tarreau80599772015-01-20 19:35:24 +01001876/* hashes the binary input into a 32-bit unsigned int */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001877static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau80599772015-01-20 19:35:24 +01001878{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001879 smp->data.u.sint = hash_crc32(smp->data.u.str.area,
1880 smp->data.u.str.data);
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001881 if (arg_p && arg_p->data.sint)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001882 smp->data.u.sint = full_hash(smp->data.u.sint);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001883 smp->data.type = SMP_T_SINT;
Willy Tarreau80599772015-01-20 19:35:24 +01001884 return 1;
1885}
1886
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001887/* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */
1888static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private)
1889{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001890 smp->data.u.sint = hash_crc32c(smp->data.u.str.area,
1891 smp->data.u.str.data);
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01001892 if (arg_p && arg_p->data.sint)
1893 smp->data.u.sint = full_hash(smp->data.u.sint);
1894 smp->data.type = SMP_T_SINT;
1895 return 1;
1896}
1897
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001898/* This function escape special json characters. The returned string can be
1899 * safely set between two '"' and used as json string. The json string is
1900 * defined like this:
1901 *
1902 * any Unicode character except '"' or '\' or control character
1903 * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits
1904 *
1905 * The enum input_type contain all the allowed mode for decoding the input
1906 * string.
1907 */
1908enum input_type {
1909 IT_ASCII = 0,
1910 IT_UTF8,
1911 IT_UTF8S,
1912 IT_UTF8P,
1913 IT_UTF8PS,
1914};
1915static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv,
1916 const char *file, int line, char **err)
1917{
1918 if (!arg) {
1919 memprintf(err, "Unexpected empty arg list");
1920 return 0;
1921 }
1922
1923 if (arg->type != ARGT_STR) {
1924 memprintf(err, "Unexpected arg type");
1925 return 0;
1926 }
1927
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001928 if (strcmp(arg->data.str.area, "") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001929 arg->type = ARGT_SINT;
1930 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001931 return 1;
1932 }
1933
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001934 else if (strcmp(arg->data.str.area, "ascii") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001935 arg->type = ARGT_SINT;
1936 arg->data.sint = IT_ASCII;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001937 return 1;
1938 }
1939
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001940 else if (strcmp(arg->data.str.area, "utf8") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001941 arg->type = ARGT_SINT;
1942 arg->data.sint = IT_UTF8;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001943 return 1;
1944 }
1945
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001946 else if (strcmp(arg->data.str.area, "utf8s") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001947 arg->type = ARGT_SINT;
1948 arg->data.sint = IT_UTF8S;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001949 return 1;
1950 }
1951
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001952 else if (strcmp(arg->data.str.area, "utf8p") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001953 arg->type = ARGT_SINT;
1954 arg->data.sint = IT_UTF8P;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001955 return 1;
1956 }
1957
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 else if (strcmp(arg->data.str.area, "utf8ps") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001959 arg->type = ARGT_SINT;
1960 arg->data.sint = IT_UTF8PS;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001961 return 1;
1962 }
1963
Thierry FOURNIER / OZON.IOa69c9122016-11-23 01:13:57 +01001964 memprintf(err, "Unexpected input code type. "
1965 "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'");
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001966 return 0;
1967}
1968
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001969static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001970{
Willy Tarreau83061a82018-07-13 11:56:34 +02001971 struct buffer *temp;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001972 char _str[7]; /* \u + 4 hex digit + null char for sprintf. */
1973 const char *str;
1974 int len;
1975 enum input_type input_type = IT_ASCII;
1976 unsigned int c;
1977 unsigned int ret;
1978 char *p;
1979
1980 if (arg_p)
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02001981 input_type = arg_p->data.sint;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001982
1983 temp = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001984 temp->data = 0;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001985
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001986 p = smp->data.u.str.area;
1987 while (p < smp->data.u.str.area + smp->data.u.str.data) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001988
1989 if (input_type == IT_ASCII) {
1990 /* Read input as ASCII. */
1991 c = *(unsigned char *)p;
1992 p++;
1993 }
1994 else {
1995 /* Read input as UTF8. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001996 ret = utf8_next(p,
1997 smp->data.u.str.data - ( p - smp->data.u.str.area),
1998 &c);
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02001999 p += utf8_return_length(ret);
2000
2001 if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK)
2002 return 0;
2003 if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK)
2004 continue;
2005 if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2006 return 0;
2007 if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ))
2008 continue;
2009
2010 /* Check too big values. */
2011 if ((unsigned int)c > 0xffff) {
2012 if (input_type == IT_UTF8 || input_type == IT_UTF8P)
2013 return 0;
2014 continue;
2015 }
2016 }
2017
2018 /* Convert character. */
2019 if (c == '"') {
2020 len = 2;
2021 str = "\\\"";
2022 }
2023 else if (c == '\\') {
2024 len = 2;
2025 str = "\\\\";
2026 }
2027 else if (c == '/') {
2028 len = 2;
2029 str = "\\/";
2030 }
2031 else if (c == '\b') {
2032 len = 2;
2033 str = "\\b";
2034 }
2035 else if (c == '\f') {
2036 len = 2;
2037 str = "\\f";
2038 }
2039 else if (c == '\r') {
2040 len = 2;
2041 str = "\\r";
2042 }
2043 else if (c == '\n') {
2044 len = 2;
2045 str = "\\n";
2046 }
2047 else if (c == '\t') {
2048 len = 2;
2049 str = "\\t";
2050 }
Willy Tarreau90807112020-02-25 08:16:33 +01002051 else if (c > 0xff || !isprint((unsigned char)c)) {
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002052 /* isprint generate a segfault if c is too big. The man says that
2053 * c must have the value of an unsigned char or EOF.
2054 */
2055 len = 6;
2056 _str[0] = '\\';
2057 _str[1] = 'u';
2058 snprintf(&_str[2], 5, "%04x", (unsigned short)c);
2059 str = _str;
2060 }
2061 else {
2062 len = 1;
Willy Tarreau5715da22020-02-25 08:37:37 +01002063 _str[0] = c;
2064 str = _str;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002065 }
2066
2067 /* Check length */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002068 if (temp->data + len > temp->size)
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002069 return 0;
2070
2071 /* Copy string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002072 memcpy(temp->area + temp->data, str, len);
2073 temp->data += len;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002074 }
2075
2076 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002077 smp->data.u.str = *temp;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002078 smp->data.type = SMP_T_STR;
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002079
2080 return 1;
2081}
2082
Emeric Brun54c4ac82014-11-03 15:32:43 +01002083/* This sample function is designed to extract some bytes from an input buffer.
2084 * First arg is the offset.
2085 * Optional second arg is the length to truncate */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002086static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brun54c4ac82014-11-03 15:32:43 +01002087{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002088 if (smp->data.u.str.data <= arg_p[0].data.sint) {
2089 smp->data.u.str.data = 0;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002090 return 1;
2091 }
2092
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002093 if (smp->data.u.str.size)
2094 smp->data.u.str.size -= arg_p[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002095 smp->data.u.str.data -= arg_p[0].data.sint;
2096 smp->data.u.str.area += arg_p[0].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002097
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002098 if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data))
2099 smp->data.u.str.data = arg_p[1].data.sint;
Emeric Brun54c4ac82014-11-03 15:32:43 +01002100
2101 return 1;
2102}
2103
Emeric Brunf399b0d2014-11-03 17:07:03 +01002104static int sample_conv_field_check(struct arg *args, struct sample_conv *conv,
2105 const char *file, int line, char **err)
2106{
2107 struct arg *arg = args;
2108
2109 if (!arg) {
2110 memprintf(err, "Unexpected empty arg list");
2111 return 0;
2112 }
2113
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002114 if (arg->type != ARGT_SINT) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002115 memprintf(err, "Unexpected arg type");
2116 return 0;
2117 }
2118
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002119 if (!arg->data.sint) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002120 memprintf(err, "Unexpected value 0 for index");
2121 return 0;
2122 }
2123
2124 arg++;
2125
2126 if (arg->type != ARGT_STR) {
2127 memprintf(err, "Unexpected arg type");
2128 return 0;
2129 }
2130
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002131 if (!arg->data.str.data) {
Emeric Brunf399b0d2014-11-03 17:07:03 +01002132 memprintf(err, "Empty separators list");
2133 return 0;
2134 }
2135
2136 return 1;
2137}
2138
2139/* This sample function is designed to a return selected part of a string (field).
2140 * First arg is the index of the field (start at 1)
2141 * Second arg is a char list of separators (type string)
2142 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002143static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002144{
Marcin Deranek9631a282018-04-16 14:30:46 +02002145 int field;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002146 char *start, *end;
2147 int i;
Marcin Deranek9631a282018-04-16 14:30:46 +02002148 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002149
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002150 if (!arg_p[0].data.sint)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002151 return 0;
2152
Marcin Deranek9631a282018-04-16 14:30:46 +02002153 if (arg_p[0].data.sint < 0) {
2154 field = -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002155 end = start = smp->data.u.str.area + smp->data.u.str.data;
2156 while (start > smp->data.u.str.area) {
2157 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2158 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002159 if (field == arg_p[0].data.sint) {
2160 if (count == 1)
2161 goto found;
2162 else if (count > 1)
2163 count--;
2164 } else {
2165 end = start-1;
2166 field--;
2167 }
2168 break;
2169 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002170 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002171 start--;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002172 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002173 } else {
2174 field = 1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002175 end = start = smp->data.u.str.area;
2176 while (end - smp->data.u.str.area < smp->data.u.str.data) {
2177 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 if (field == arg_p[0].data.sint) {
2180 if (count == 1)
2181 goto found;
2182 else if (count > 1)
2183 count--;
2184 } else {
2185 start = end+1;
2186 field++;
2187 }
2188 break;
2189 }
2190 }
2191 end++;
2192 }
Emeric Brunf399b0d2014-11-03 17:07:03 +01002193 }
2194
2195 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002196 if (field != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002197 smp->data.u.str.data = 0;
Tim Duesterhus4381d262019-10-16 15:11:15 +02002198 return 0;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002199 }
2200found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002201 smp->data.u.str.data = end - start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002202 /* If ret string is len 0, no need to
2203 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002204 if (!smp->data.u.str.data)
Emeric Brunf399b0d2014-11-03 17:07:03 +01002205 return 1;
2206
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 smp->data.u.str.area = start;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002208
2209 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002210 Note: smp->data.u.str.size cannot be set to 0 */
2211 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002212 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunf399b0d2014-11-03 17:07:03 +01002213
2214 return 1;
2215}
2216
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002217/* This sample function is designed to return a word from a string.
2218 * First arg is the index of the word (start at 1)
2219 * Second arg is a char list of words separators (type string)
2220 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002221static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002222{
Marcin Deranek9631a282018-04-16 14:30:46 +02002223 int word;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002224 char *start, *end;
2225 int i, issep, inword;
Marcin Deranek9631a282018-04-16 14:30:46 +02002226 int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002227
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002228 if (!arg_p[0].data.sint)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002229 return 0;
2230
2231 word = 0;
2232 inword = 0;
Marcin Deranek9631a282018-04-16 14:30:46 +02002233 if (arg_p[0].data.sint < 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002234 end = start = smp->data.u.str.area + smp->data.u.str.data;
2235 while (start > smp->data.u.str.area) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002236 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002237 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2238 if (*(start-1) == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002239 issep = 1;
2240 break;
2241 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002242 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002243 if (!inword) {
2244 if (!issep) {
2245 if (word != arg_p[0].data.sint) {
2246 word--;
2247 end = start;
2248 }
2249 inword = 1;
2250 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002251 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002252 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002253 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002254 if (count == 1)
2255 goto found;
2256 else if (count > 1)
2257 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002258 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002259 inword = 0;
2260 }
2261 start--;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002262 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002263 } else {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002264 end = start = smp->data.u.str.area;
2265 while (end - smp->data.u.str.area < smp->data.u.str.data) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002266 issep = 0;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002267 for (i = 0 ; i < arg_p[1].data.str.data; i++) {
2268 if (*end == arg_p[1].data.str.area[i]) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002269 issep = 1;
2270 break;
2271 }
2272 }
2273 if (!inword) {
2274 if (!issep) {
2275 if (word != arg_p[0].data.sint) {
2276 word++;
2277 start = end;
2278 }
2279 inword = 1;
2280 }
2281 }
2282 else if (issep) {
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002283 if (word == arg_p[0].data.sint) {
Marcin Deranek9631a282018-04-16 14:30:46 +02002284 if (count == 1)
2285 goto found;
2286 else if (count > 1)
2287 count--;
Willy Tarreau9eb2a4a2018-04-19 10:33:28 +02002288 }
Marcin Deranek9631a282018-04-16 14:30:46 +02002289 inword = 0;
2290 }
2291 end++;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002292 }
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002293 }
2294
2295 /* Field not found */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002296 if (word != arg_p[0].data.sint) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002297 smp->data.u.str.data = 0;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002298 return 1;
2299 }
2300found:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002301 smp->data.u.str.data = end - start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002302 /* If ret string is len 0, no need to
2303 change pointers or to update size */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002304 if (!smp->data.u.str.data)
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002305 return 1;
2306
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002307 smp->data.u.str.area = start;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002308
2309 /* Compute remaining size if needed
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002310 Note: smp->data.u.str.size cannot be set to 0 */
2311 if (smp->data.u.str.size)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002312 smp->data.u.str.size -= start - smp->data.u.str.area;
Emeric Brunc9a0f6d2014-11-25 14:09:01 +01002313
2314 return 1;
2315}
2316
Willy Tarreau7eda8492015-01-20 19:47:06 +01002317static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv,
2318 const char *file, int line, char **err)
2319{
2320 struct arg *arg = args;
2321 char *p;
2322 int len;
2323
2324 /* arg0 is a regex, it uses type_flag for ICASE and global match */
2325 arg[0].type_flags = 0;
2326
2327 if (arg[2].type != ARGT_STR)
2328 return 1;
2329
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002330 p = arg[2].data.str.area;
2331 len = arg[2].data.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002332 while (len) {
2333 if (*p == 'i') {
2334 arg[0].type_flags |= ARGF_REG_ICASE;
2335 }
2336 else if (*p == 'g') {
2337 arg[0].type_flags |= ARGF_REG_GLOB;
2338 }
2339 else {
2340 memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p);
2341 return 0;
2342 }
2343 p++;
2344 len--;
2345 }
2346 return 1;
2347}
2348
2349/* This sample function is designed to do the equivalent of s/match/replace/ on
2350 * the input string. It applies a regex and restarts from the last matched
2351 * location until nothing matches anymore. First arg is the regex to apply to
2352 * the input string, second arg is the replacement expression.
2353 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002354static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau7eda8492015-01-20 19:47:06 +01002355{
2356 char *start, *end;
2357 struct my_regex *reg = arg_p[0].data.reg;
2358 regmatch_t pmatch[MAX_MATCH];
Willy Tarreau83061a82018-07-13 11:56:34 +02002359 struct buffer *trash = get_trash_chunk();
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002360 struct buffer *output;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002361 int flag, max;
2362 int found;
2363
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002364 start = smp->data.u.str.area;
2365 end = start + smp->data.u.str.data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002366
2367 flag = 0;
2368 while (1) {
2369 /* check for last round which is used to copy remaining parts
2370 * when not running in global replacement mode.
2371 */
2372 found = 0;
2373 if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) {
2374 /* Note: we can have start == end on empty strings or at the end */
2375 found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag);
2376 }
2377
2378 if (!found)
2379 pmatch[0].rm_so = end - start;
2380
2381 /* copy the heading non-matching part (which may also be the tail if nothing matches) */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002382 max = trash->size - trash->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002383 if (max && pmatch[0].rm_so > 0) {
2384 if (max > pmatch[0].rm_so)
2385 max = pmatch[0].rm_so;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002386 memcpy(trash->area + trash->data, start, max);
2387 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002388 }
2389
2390 if (!found)
2391 break;
2392
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002393 output = alloc_trash_chunk();
Willy Tarreau23997da2020-02-18 14:27:44 +01002394 if (!output)
2395 break;
2396
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002397 output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch);
2398
Willy Tarreau7eda8492015-01-20 19:47:06 +01002399 /* replace the matching part */
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002400 max = output->size - output->data;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002401 if (max) {
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002402 if (max > output->data)
2403 max = output->data;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002404 memcpy(trash->area + trash->data,
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002405 output->area, max);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002406 trash->data += max;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002407 }
2408
Jerome Magnin07e1e3c2020-02-16 19:20:19 +01002409 free_trash_chunk(output);
2410
Willy Tarreau7eda8492015-01-20 19:47:06 +01002411 /* stop here if we're done with this string */
2412 if (start >= end)
2413 break;
2414
2415 /* We have a special case for matches of length 0 (eg: "x*y*").
2416 * These ones are considered to match in front of a character,
2417 * so we have to copy that character and skip to the next one.
2418 */
2419 if (!pmatch[0].rm_eo) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002420 if (trash->data < trash->size)
2421 trash->area[trash->data++] = start[pmatch[0].rm_eo];
Willy Tarreau7eda8492015-01-20 19:47:06 +01002422 pmatch[0].rm_eo++;
2423 }
2424
2425 start += pmatch[0].rm_eo;
2426 flag |= REG_NOTBOL;
2427 }
2428
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002429 smp->data.u.str = *trash;
Willy Tarreau7eda8492015-01-20 19:47:06 +01002430 return 1;
2431}
2432
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002433/* This function check an operator entry. It expects a string.
2434 * The string can be an integer or a variable name.
2435 */
2436static int check_operator(struct arg *args, struct sample_conv *conv,
2437 const char *file, int line, char **err)
2438{
2439 const char *str;
2440 const char *end;
2441
2442 /* Try to decode a variable. */
2443 if (vars_check_arg(&args[0], NULL))
2444 return 1;
2445
2446 /* Try to convert an integer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002447 str = args[0].data.str.area;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002448 end = str + strlen(str);
2449 args[0].data.sint = read_int64(&str, end);
2450 if (*str != '\0') {
2451 memprintf(err, "expects an integer or a variable name");
2452 return 0;
2453 }
2454 args[0].type = ARGT_SINT;
2455 return 1;
2456}
2457
Willy Tarreau6204cd92016-03-10 16:33:04 +01002458/* This function returns a sample struct filled with an arg content.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002459 * If the arg contain an integer, the integer is returned in the
2460 * sample. If the arg contains a variable descriptor, it returns the
2461 * variable value.
2462 *
2463 * This function returns 0 if an error occurs, otherwise it returns 1.
2464 */
Willy Tarreau6204cd92016-03-10 16:33:04 +01002465static inline int sample_conv_var2smp(const struct arg *arg, struct sample *smp)
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002466{
2467 switch (arg->type) {
2468 case ARGT_SINT:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002469 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002470 smp->data.u.sint = arg->data.sint;
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002471 return 1;
2472 case ARGT_VAR:
Willy Tarreau6204cd92016-03-10 16:33:04 +01002473 if (!vars_get_by_desc(&arg->data.var, smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002474 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002475 if (!sample_casts[smp->data.type][SMP_T_SINT])
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002476 return 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002477 if (!sample_casts[smp->data.type][SMP_T_SINT](smp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002478 return 0;
2479 return 1;
2480 default:
2481 return 0;
2482 }
2483}
2484
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002485/* Takes a SINT on input, applies a binary twos complement and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002486 * result.
2487 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002488static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002489{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002490 smp->data.u.sint = ~smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002491 return 1;
2492}
2493
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002494/* Takes a SINT on input, applies a binary "and" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002495 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002496 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002497static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002498{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002499 struct sample tmp;
2500
Willy Tarreau7560dd42016-03-10 16:28:58 +01002501 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002502 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002503 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002504 smp->data.u.sint &= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002505 return 1;
2506}
2507
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002508/* Takes a SINT on input, applies a binary "or" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002509 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002510 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002511static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002512{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002513 struct sample tmp;
2514
Willy Tarreau7560dd42016-03-10 16:28:58 +01002515 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002516 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002517 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002518 smp->data.u.sint |= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002519 return 1;
2520}
2521
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002522/* Takes a SINT on input, applies a binary "xor" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002523 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002524 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002525static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002526{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002527 struct sample tmp;
2528
Willy Tarreau7560dd42016-03-10 16:28:58 +01002529 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002530 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002531 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002532 smp->data.u.sint ^= tmp.data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002533 return 1;
2534}
2535
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002536static inline long long int arith_add(long long int a, long long int b)
2537{
2538 /* Prevent overflow and makes capped calculus.
2539 * We must ensure that the check calculus doesn't
2540 * exceed the signed 64 bits limits.
2541 *
2542 * +----------+----------+
2543 * | a<0 | a>=0 |
2544 * +------+----------+----------+
2545 * | b<0 | MIN-a>b | no check |
2546 * +------+----------+----------+
2547 * | b>=0 | no check | MAX-a<b |
2548 * +------+----------+----------+
2549 */
2550 if ((a ^ b) >= 0) {
2551 /* signs are differents. */
2552 if (a < 0) {
2553 if (LLONG_MIN - a > b)
2554 return LLONG_MIN;
2555 }
2556 if (LLONG_MAX - a < b)
2557 return LLONG_MAX;
2558 }
2559 return a + b;
2560}
2561
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002562/* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002563 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002564 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002565static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002566{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002567 struct sample tmp;
2568
Willy Tarreau7560dd42016-03-10 16:28:58 +01002569 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002570 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002571 return 0;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002572 smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002573 return 1;
2574}
2575
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002576/* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002577 * arg_p or in the variable described in arg_p, and returns the SINT result.
Willy Tarreau97707872015-01-27 15:12:13 +01002578 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002579static int sample_conv_arith_sub(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002580 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002581{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002582 struct sample tmp;
2583
Willy Tarreau7560dd42016-03-10 16:28:58 +01002584 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002585 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002586 return 0;
2587
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002588 /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater
2589 * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place
2590 * of -LLONG_MIN and correct the result.
2591 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002592 if (tmp.data.u.sint == LLONG_MIN) {
2593 smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX);
2594 if (smp->data.u.sint < LLONG_MAX)
2595 smp->data.u.sint++;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002596 return 1;
2597 }
2598
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002599 /* standard subtraction: we use the "add" function and negate
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002600 * the second operand.
2601 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002602 smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint);
Willy Tarreau97707872015-01-27 15:12:13 +01002603 return 1;
2604}
2605
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002606/* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002607 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002608 * If the result makes an overflow, then the largest possible quantity is
2609 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002610 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002611static int sample_conv_arith_mul(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002612 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002613{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002614 struct sample tmp;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002615 long long int c;
2616
Willy Tarreau7560dd42016-03-10 16:28:58 +01002617 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002618 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002619 return 0;
2620
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002621 /* prevent divide by 0 during the check */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002622 if (!smp->data.u.sint || !tmp.data.u.sint) {
2623 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002624 return 1;
2625 }
2626
2627 /* The multiply between LLONG_MIN and -1 returns a
2628 * "floting point exception".
2629 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002630 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2631 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002632 return 1;
2633 }
2634
2635 /* execute standard multiplication. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002636 c = smp->data.u.sint * tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002637
2638 /* check for overflow and makes capped multiply. */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002639 if (smp->data.u.sint != c / tmp.data.u.sint) {
2640 if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) {
2641 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002642 return 1;
2643 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002644 smp->data.u.sint = LLONG_MIN;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002645 return 1;
2646 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002647 smp->data.u.sint = c;
Willy Tarreau97707872015-01-27 15:12:13 +01002648 return 1;
2649}
2650
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002651/* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002652 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002653 * If arg_p makes the result overflow, then the largest possible quantity is
2654 * returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002655 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002656static int sample_conv_arith_div(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002657 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002658{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002659 struct sample tmp;
2660
Willy Tarreau7560dd42016-03-10 16:28:58 +01002661 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002662 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002663 return 0;
2664
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002665 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002666 /* The divide between LLONG_MIN and -1 returns a
2667 * "floting point exception".
2668 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002669 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2670 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002671 return 1;
2672 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002673 smp->data.u.sint /= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002674 return 1;
2675 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002676 smp->data.u.sint = LLONG_MAX;
Willy Tarreau97707872015-01-27 15:12:13 +01002677 return 1;
2678}
2679
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002680/* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in
Joseph Herlant757f5ad2018-11-15 12:14:56 -08002681 * arg_p or in the variable described in arg_p, and returns the SINT result.
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002682 * If arg_p makes the result overflow, then 0 is returned.
Willy Tarreau97707872015-01-27 15:12:13 +01002683 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002684static int sample_conv_arith_mod(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002685 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002686{
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002687 struct sample tmp;
2688
Willy Tarreau7560dd42016-03-10 16:28:58 +01002689 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
Willy Tarreau6204cd92016-03-10 16:33:04 +01002690 if (!sample_conv_var2smp(arg_p, &tmp))
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02002691 return 0;
2692
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002693 if (tmp.data.u.sint) {
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002694 /* The divide between LLONG_MIN and -1 returns a
2695 * "floting point exception".
2696 */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002697 if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) {
2698 smp->data.u.sint = 0;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002699 return 1;
2700 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002701 smp->data.u.sint %= tmp.data.u.sint;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002702 return 1;
2703 }
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002704 smp->data.u.sint = 0;
Willy Tarreau97707872015-01-27 15:12:13 +01002705 return 1;
2706}
2707
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002708/* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT
Willy Tarreau97707872015-01-27 15:12:13 +01002709 * result.
2710 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002711static int sample_conv_arith_neg(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002712 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002713{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002714 if (smp->data.u.sint == LLONG_MIN)
2715 smp->data.u.sint = LLONG_MAX;
Thierry FOURNIER00c005c2015-07-08 01:10:21 +02002716 else
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002717 smp->data.u.sint = -smp->data.u.sint;
Willy Tarreau97707872015-01-27 15:12:13 +01002718 return 1;
2719}
2720
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002721/* Takes a SINT on input, returns true is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002722 * false. The output is a BOOL.
2723 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002724static int sample_conv_arith_bool(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002725 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002726{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002727 smp->data.u.sint = !!smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002728 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002729 return 1;
2730}
2731
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002732/* Takes a SINT on input, returns false is the value is non-null, otherwise
Willy Tarreau97707872015-01-27 15:12:13 +01002733 * truee. The output is a BOOL.
2734 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002735static int sample_conv_arith_not(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002736 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002737{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002738 smp->data.u.sint = !smp->data.u.sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002739 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002740 return 1;
2741}
2742
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002743/* Takes a SINT on input, returns true is the value is odd, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002744 * The output is a BOOL.
2745 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002746static int sample_conv_arith_odd(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002747 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002748{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002749 smp->data.u.sint = smp->data.u.sint & 1;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002750 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002751 return 1;
2752}
2753
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002754/* Takes a SINT on input, returns true is the value is even, otherwise false.
Willy Tarreau97707872015-01-27 15:12:13 +01002755 * The output is a BOOL.
2756 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002757static int sample_conv_arith_even(const struct arg *arg_p,
Thierry FOURNIER68a556e2015-02-23 15:11:11 +01002758 struct sample *smp, void *private)
Willy Tarreau97707872015-01-27 15:12:13 +01002759{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002760 smp->data.u.sint = !(smp->data.u.sint & 1);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002761 smp->data.type = SMP_T_BOOL;
Willy Tarreau97707872015-01-27 15:12:13 +01002762 return 1;
2763}
2764
Willy Tarreau280f42b2018-02-19 15:34:12 +01002765/* appends an optional const string, an optional variable contents and another
2766 * optional const string to an existing string.
2767 */
2768static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private)
2769{
Willy Tarreau83061a82018-07-13 11:56:34 +02002770 struct buffer *trash;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002771 struct sample tmp;
2772 int max;
2773
2774 trash = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002775 trash->data = smp->data.u.str.data;
2776 if (trash->data > trash->size - 1)
2777 trash->data = trash->size - 1;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002778
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002779 memcpy(trash->area, smp->data.u.str.area, trash->data);
2780 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002781
2782 /* append first string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002783 max = arg_p[0].data.str.data;
2784 if (max > trash->size - 1 - trash->data)
2785 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002786
2787 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002788 memcpy(trash->area + trash->data, arg_p[0].data.str.area, max);
2789 trash->data += max;
2790 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002791 }
2792
2793 /* append second string (variable) if it's found and we can turn it
2794 * into a string.
2795 */
2796 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2797 if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp) &&
2798 (sample_casts[tmp.data.type][SMP_T_STR] == c_none ||
2799 sample_casts[tmp.data.type][SMP_T_STR](&tmp))) {
2800
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002801 max = tmp.data.u.str.data;
2802 if (max > trash->size - 1 - trash->data)
2803 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002804
2805 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002806 memcpy(trash->area + trash->data, tmp.data.u.str.area,
2807 max);
2808 trash->data += max;
2809 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002810 }
2811 }
2812
2813 /* append third string */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002814 max = arg_p[2].data.str.data;
2815 if (max > trash->size - 1 - trash->data)
2816 max = trash->size - 1 - trash->data;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002817
2818 if (max) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002819 memcpy(trash->area + trash->data, arg_p[2].data.str.area, max);
2820 trash->data += max;
2821 trash->area[trash->data] = 0;
Willy Tarreau280f42b2018-02-19 15:34:12 +01002822 }
2823
2824 smp->data.u.str = *trash;
2825 smp->data.type = SMP_T_STR;
2826 return 1;
2827}
2828
2829/* This function checks the "concat" converter's arguments and extracts the
2830 * variable name and its scope.
2831 */
2832static int smp_check_concat(struct arg *args, struct sample_conv *conv,
2833 const char *file, int line, char **err)
2834{
2835 /* Try to decode a variable. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002836 if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) {
2837 memprintf(err, "failed to register variable name '%s'",
2838 args[1].data.str.area);
Willy Tarreau280f42b2018-02-19 15:34:12 +01002839 return 0;
2840 }
2841 return 1;
2842}
2843
Tim Duesterhusca097c12018-04-27 21:18:45 +02002844/* compares string with a variable containing a string. Return value
2845 * is compatible with strcmp(3)'s return value.
2846 */
2847static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private)
2848{
2849 struct sample tmp;
2850 int max, result;
2851
2852 smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt);
2853 if (arg_p[0].type != ARGT_VAR)
2854 return 0;
2855 if (!vars_get_by_desc(&arg_p[0].data.var, &tmp))
2856 return 0;
2857 if (!sample_casts[tmp.data.type][SMP_T_STR](&tmp))
2858 return 0;
2859
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002860 max = MIN(smp->data.u.str.data, tmp.data.u.str.data);
2861 result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002862 if (result == 0) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002863 if (smp->data.u.str.data != tmp.data.u.str.data) {
2864 if (smp->data.u.str.data < tmp.data.u.str.data) {
Tim Duesterhusca097c12018-04-27 21:18:45 +02002865 result = -1;
2866 }
2867 else {
2868 result = 1;
2869 }
2870 }
2871 }
2872
2873 smp->data.u.sint = result;
2874 smp->data.type = SMP_T_SINT;
2875 return 1;
2876}
2877
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002878#define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */
2879#define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */
2880#define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ)
2881
2882/*
2883 * Extract the field value of an input binary sample. Takes a mandatory argument:
2884 * the protocol buffers field identifier (dotted notation) internally represented
2885 * as an array of unsigned integers and its size.
2886 * Return 1 if the field was found, 0 if not.
2887 */
2888static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private)
2889{
2890 unsigned char *pos;
2891 size_t grpc_left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002892
2893 pos = (unsigned char *)smp->data.u.str.area;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002894 grpc_left = smp->data.u.str.data;
2895
Frédéric Lécaille7c93e882019-03-04 07:33:41 +01002896 while (grpc_left > GRPC_MSG_HEADER_SZ) {
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002897 size_t grpc_msg_len, left;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002898
2899 grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ));
2900
2901 pos += GRPC_MSG_HEADER_SZ;
2902 grpc_left -= GRPC_MSG_HEADER_SZ;
2903
2904 if (grpc_left < left)
2905 return 0;
2906
Frédéric Lécaille5f33f852019-03-06 08:03:44 +01002907 if (protobuf_field_lookup(arg_p, smp, &pos, &left))
2908 return 1;
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002909
Frédéric Lécaille50290fb2019-02-27 14:34:51 +01002910 grpc_left -= grpc_msg_len;
2911 }
2912
2913 return 0;
2914}
2915
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01002916static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private)
2917{
2918 unsigned char *pos;
2919 size_t left;
2920
2921 pos = (unsigned char *)smp->data.u.str.area;
2922 left = smp->data.u.str.data;
2923
2924 return protobuf_field_lookup(arg_p, smp, &pos, &left);
2925}
2926
2927static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv,
2928 const char *file, int line, char **err)
Frédéric Lécaille756d97f2019-03-04 19:03:48 +01002929{
2930 if (!args[1].type) {
2931 args[1].type = ARGT_SINT;
2932 args[1].data.sint = PBUF_T_BINARY;
2933 }
2934 else {
2935 int pbuf_type;
2936
2937 pbuf_type = protobuf_type(args[1].data.str.area);
2938 if (pbuf_type == -1) {
2939 memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area);
2940 return 0;
2941 }
2942
2943 args[1].type = ARGT_SINT;
2944 args[1].data.sint = pbuf_type;
2945 }
2946
2947 return 1;
2948}
2949
Tim Duesterhusca097c12018-04-27 21:18:45 +02002950/* This function checks the "strcmp" converter's arguments and extracts the
2951 * variable name and its scope.
2952 */
2953static int smp_check_strcmp(struct arg *args, struct sample_conv *conv,
2954 const char *file, int line, char **err)
2955{
2956 /* Try to decode a variable. */
2957 if (vars_check_arg(&args[0], NULL))
2958 return 1;
2959
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002960 memprintf(err, "failed to register variable name '%s'",
2961 args[0].data.str.area);
Tim Duesterhusca097c12018-04-27 21:18:45 +02002962 return 0;
2963}
2964
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002965/************************************************************************/
2966/* All supported sample fetch functions must be declared here */
2967/************************************************************************/
2968
2969/* force TRUE to be returned at the fetch level */
2970static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002971smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002972{
Willy Tarreau280f42b2018-02-19 15:34:12 +01002973 if (!smp_make_rw(smp))
2974 return 0;
2975
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002976 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002977 smp->data.u.sint = 1;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002978 return 1;
2979}
2980
2981/* force FALSE to be returned at the fetch level */
2982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002983smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002984{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002985 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002986 smp->data.u.sint = 0;
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002987 return 1;
2988}
2989
2990/* retrieve environment variable $1 as a string */
2991static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002992smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau5b8ad222013-07-25 12:17:57 +02002993{
2994 char *env;
2995
2996 if (!args || args[0].type != ARGT_STR)
2997 return 0;
2998
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002999 env = getenv(args[0].data.str.area);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003000 if (!env)
3001 return 0;
3002
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003003 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003004 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003005 smp->data.u.str.area = env;
3006 smp->data.u.str.data = strlen(env);
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003007 return 1;
3008}
3009
Damien Claisseae6f1252019-10-30 15:57:28 +00003010/* Validates the data unit argument passed to "date" fetch. Argument 1 support an
3011 * optional string representing the unit of the result: "s" for seconds, "ms" for
3012 * milliseconds and "us" for microseconds.
3013 * Returns 0 on error and non-zero if OK.
3014 */
3015int smp_check_date_unit(struct arg *args, char **err)
3016{
3017 if (args[1].type == ARGT_STR) {
3018 if (strcmp(args[1].data.str.area, "s") == 0) {
3019 args[1].data.sint = TIME_UNIT_S;
3020 }
3021 else if (strcmp(args[1].data.str.area, "ms") == 0) {
3022 args[1].data.sint = TIME_UNIT_MS;
3023 }
3024 else if (strcmp(args[1].data.str.area, "us") == 0) {
3025 args[1].data.sint = TIME_UNIT_US;
3026 }
3027 else {
3028 memprintf(err, "expects 's', 'ms' or 'us', got '%s'",
3029 args[1].data.str.area);
3030 return 0;
3031 }
3032 free(args[1].data.str.area);
3033 args[1].data.str.area = NULL;
3034 args[1].type = ARGT_SINT;
3035 }
3036 else if (args[1].type != ARGT_STOP) {
3037 memprintf(err, "Unexpected arg type");
3038 return 0;
3039 }
3040
3041 return 1;
3042}
3043
3044/* retrieve the current local date in epoch time, converts it to milliseconds
3045 * or microseconds if asked to in optional args[1] unit param, and applies an
3046 * optional args[0] offset.
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003047 */
3048static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003049smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003050{
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003051 smp->data.u.sint = date.tv_sec;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003052
Damien Claisseae6f1252019-10-30 15:57:28 +00003053 /* report in milliseconds */
3054 if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) {
3055 smp->data.u.sint *= 1000;
3056 smp->data.u.sint += date.tv_usec / 1000;
3057 }
3058 /* report in microseconds */
3059 else if (args && args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) {
3060 smp->data.u.sint *= 1000000;
3061 smp->data.u.sint += date.tv_usec;
3062 }
3063
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003064 /* add offset */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003065 if (args && args[0].type == ARGT_SINT)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003066 smp->data.u.sint += args[0].data.sint;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003067
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003068 smp->data.type = SMP_T_SINT;
Willy Tarreau6236d3a2013-07-25 14:28:25 +02003069 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3070 return 1;
3071}
3072
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003073/* retrieve the current microsecond part of the date */
3074static int
3075smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private)
3076{
3077 smp->data.u.sint = date.tv_usec;
3078 smp->data.type = SMP_T_SINT;
3079 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3080 return 1;
3081}
3082
3083
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003084/* returns the hostname */
3085static int
3086smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private)
3087{
3088 smp->data.type = SMP_T_STR;
3089 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003090 smp->data.u.str.area = hostname;
3091 smp->data.u.str.data = strlen(hostname);
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003092 return 1;
3093}
3094
Willy Tarreau0f30d262014-11-24 16:02:05 +01003095/* returns the number of processes */
3096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003097smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003098{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003099 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003100 smp->data.u.sint = global.nbproc;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003101 return 1;
3102}
3103
3104/* returns the number of the current process (between 1 and nbproc */
3105static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003106smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003107{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003108 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003109 smp->data.u.sint = relative_pid;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003110 return 1;
3111}
3112
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003113/* returns the number of the current thread (between 1 and nbthread */
3114static int
3115smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private)
3116{
3117 smp->data.type = SMP_T_SINT;
3118 smp->data.u.sint = tid;
3119 return 1;
3120}
3121
Willy Tarreau84310e22014-02-14 11:59:04 +01003122/* generate a random 32-bit integer for whatever purpose, with an optional
3123 * range specified in argument.
3124 */
3125static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003126smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau84310e22014-02-14 11:59:04 +01003127{
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003128 smp->data.u.sint = ha_random32();
Willy Tarreau84310e22014-02-14 11:59:04 +01003129
3130 /* reduce if needed. Don't do a modulo, use all bits! */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003131 if (args && args[0].type == ARGT_SINT)
Willy Tarreauaa8bbc12020-03-08 18:01:10 +01003132 smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32;
Willy Tarreau84310e22014-02-14 11:59:04 +01003133
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003134 smp->data.type = SMP_T_SINT;
Willy Tarreau84310e22014-02-14 11:59:04 +01003135 smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3136 return 1;
3137}
3138
Willy Tarreau0f30d262014-11-24 16:02:05 +01003139/* returns true if the current process is stopping */
3140static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003141smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau0f30d262014-11-24 16:02:05 +01003142{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003143 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003144 smp->data.u.sint = stopping;
Willy Tarreau0f30d262014-11-24 16:02:05 +01003145 return 1;
3146}
3147
Willy Tarreau70fe9442018-11-22 16:07:39 +01003148/* returns the number of calls of the current stream's process_stream() */
3149static int
3150smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private)
3151{
3152 smp->data.type = SMP_T_SINT;
3153 smp->data.u.sint = smp->strm->task->calls;
3154 return 1;
3155}
3156
3157/* returns the average number of nanoseconds spent processing the stream per call */
3158static int
3159smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3160{
3161 smp->data.type = SMP_T_SINT;
3162 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0;
3163 return 1;
3164}
3165
3166/* returns the total number of nanoseconds spent processing the stream */
3167static int
3168smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3169{
3170 smp->data.type = SMP_T_SINT;
3171 smp->data.u.sint = smp->strm->task->cpu_time;
3172 return 1;
3173}
3174
3175/* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */
3176static int
3177smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private)
3178{
3179 smp->data.type = SMP_T_SINT;
3180 smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0;
3181 return 1;
3182}
3183
3184/* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */
3185static int
3186smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private)
3187{
3188 smp->data.type = SMP_T_SINT;
3189 smp->data.u.sint = smp->strm->task->lat_time;
3190 return 1;
3191}
3192
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003193static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
3194{
3195 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003196 smp->data.type = SMP_T_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 smp->data.u.str.area = args[0].data.str.area;
3198 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003199 return 1;
3200}
3201
3202static int smp_check_const_bool(struct arg *args, char **err)
3203{
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003204 if (strcasecmp(args[0].data.str.area, "true") == 0 ||
3205 strcasecmp(args[0].data.str.area, "1") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003206 args[0].type = ARGT_SINT;
3207 args[0].data.sint = 1;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003208 return 1;
3209 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003210 if (strcasecmp(args[0].data.str.area, "false") == 0 ||
3211 strcasecmp(args[0].data.str.area, "0") == 0) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003212 args[0].type = ARGT_SINT;
3213 args[0].data.sint = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003214 return 1;
3215 }
3216 memprintf(err, "Expects 'true', 'false', '0' or '1'");
3217 return 0;
3218}
3219
3220static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private)
3221{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003222 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003223 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003224 return 1;
3225}
3226
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003227static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private)
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003228{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003229 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003230 smp->data.u.sint = args[0].data.sint;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003231 return 1;
3232}
3233
3234static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private)
3235{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003236 smp->data.type = SMP_T_IPV4;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003237 smp->data.u.ipv4 = args[0].data.ipv4;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003238 return 1;
3239}
3240
3241static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private)
3242{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003243 smp->data.type = SMP_T_IPV6;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003244 smp->data.u.ipv6 = args[0].data.ipv6;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003245 return 1;
3246}
3247
3248static int smp_check_const_bin(struct arg *args, char **err)
3249{
David Carlier64a16ab2016-04-08 10:37:02 +01003250 char *binstr = NULL;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003251 int binstrlen;
3252
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003253 if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err))
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003254 return 0;
3255 args[0].type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003256 args[0].data.str.area = binstr;
3257 args[0].data.str.data = binstrlen;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003258 return 1;
3259}
3260
3261static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
3262{
3263 smp->flags |= SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003264 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003265 smp->data.u.str.area = args[0].data.str.area;
3266 smp->data.u.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003267 return 1;
3268}
3269
3270static int smp_check_const_meth(struct arg *args, char **err)
3271{
3272 enum http_meth_t meth;
3273 int i;
3274
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003275 meth = find_http_meth(args[0].data.str.area, args[0].data.str.data);
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003276 if (meth != HTTP_METH_OTHER) {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003277 args[0].type = ARGT_SINT;
3278 args[0].data.sint = meth;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003279 } else {
3280 /* Check method avalaibility. A methos is a token defined as :
3281 * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." /
3282 * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA
3283 * token = 1*tchar
3284 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003285 for (i = 0; i < args[0].data.str.data; i++) {
3286 if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003287 memprintf(err, "expects valid method.");
3288 return 0;
3289 }
3290 }
3291 }
3292 return 1;
3293}
3294
3295static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private)
3296{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003297 smp->data.type = SMP_T_METH;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003298 if (args[0].type == ARGT_SINT) {
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003299 smp->flags &= ~SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003300 smp->data.u.meth.meth = args[0].data.sint;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003301 smp->data.u.meth.str.area = "";
3302 smp->data.u.meth.str.data = 0;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003303 } else {
3304 smp->flags |= SMP_F_CONST;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003305 smp->data.u.meth.meth = HTTP_METH_OTHER;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003306 smp->data.u.meth.str.area = args[0].data.str.area;
3307 smp->data.u.meth.str.data = args[0].data.str.data;
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003308 }
3309 return 1;
3310}
3311
Luca Schimweg8a694b82019-09-10 15:42:52 +02003312// This function checks the "uuid" sample's arguments.
3313// Function won't get called when no parameter is specified (maybe a bug?)
3314static int smp_check_uuid(struct arg *args, char **err)
3315{
3316 if (!args[0].type) {
3317 args[0].type = ARGT_SINT;
3318 args[0].data.sint = 4;
3319 }
3320 else if (args[0].data.sint != 4) {
3321 memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint);
3322 return 0;
3323 }
3324
3325 return 1;
3326}
3327
3328// Generate a RFC4122 UUID (default is v4 = fully random)
3329static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private)
3330{
3331 if (args[0].data.sint == 4 || !args[0].type) {
Willy Tarreauee3bcdd2020-03-08 17:48:17 +01003332 ha_generate_uuid(&trash);
Luca Schimweg8a694b82019-09-10 15:42:52 +02003333 smp->data.type = SMP_T_STR;
3334 smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE;
3335 smp->data.u.str = trash;
3336 return 1;
3337 }
3338
3339 // more implementations of other uuid formats possible here
3340 return 0;
3341}
3342
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003343/* Note: must not be declared <const> as its list will be overwritten.
3344 * Note: fetches that may return multiple types must be declared as the lowest
3345 * common denominator, the type that can be casted into all other ones. For
3346 * instance IPv4/IPv6 must be declared IPv4.
3347 */
3348static struct sample_fetch_kw_list smp_kws = {ILH, {
3349 { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
3350 { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01003351 { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN },
Damien Claisseae6f1252019-10-30 15:57:28 +00003352 { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_INTRN },
Etienne Carrierea792a0a2018-01-17 13:43:24 +01003353 { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Nenad Merdanovic807a6e72017-03-12 22:00:00 +01003354 { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003355 { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3356 { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Christopher Faulet34adb2a2017-11-21 21:45:38 +01003357 { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003358 { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN },
Willy Tarreau0f30d262014-11-24 16:02:05 +01003359 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003360 { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN },
Luca Schimweg8a694b82019-09-10 15:42:52 +02003361 { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_INTRN },
Willy Tarreau70fe9442018-11-22 16:07:39 +01003362
3363 { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3364 { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3365 { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3366 { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
3367 { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003368
3369 { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN },
3370 { "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 +02003371 { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_INTRN },
Thierry FOURNIERcc103292015-06-06 19:30:17 +02003372 { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN },
3373 { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN },
3374 { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN },
3375 { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN },
3376
Willy Tarreau5b8ad222013-07-25 12:17:57 +02003377 { /* END */ },
3378}};
3379
Willy Tarreau0108d902018-11-25 19:14:37 +01003380INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws);
3381
Emeric Brun107ca302010-01-04 16:16:05 +01003382/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003383static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau0851fd52019-12-17 10:07:25 +01003384 { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY },
Holger Just1bfc24b2017-05-06 00:56:53 +02003385 { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN },
Emeric Brun53d1a982014-04-30 18:21:37 +02003386 { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR },
Willy Tarreau12785782012-04-27 21:37:17 +02003387 { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR },
3388 { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR },
Willy Tarreau60a2ee72017-12-15 07:13:48 +01003389 { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER2f49d6d2014-03-12 15:01:52 +01003390 { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR },
Dragan Dosen3f957b22017-10-24 09:27:34 +02003391 { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT },
Tim Duesterhus1478aa72018-01-25 16:24:51 +01003392 { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003393 { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
3394 { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003395 { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Emmanuel Hocdet50791a72018-03-21 11:19:01 +01003396 { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003397 { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3398 { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3399 { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER01e09742016-12-26 11:46:11 +01003400 { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
3401 { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT },
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02003402 { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003403 { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN },
Marcin Deranek9631a282018-04-16 14:30:46 +02003404 { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR },
3405 { "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 +01003406 { "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 +02003407 { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003408#ifdef USE_OPENSSL
Tim Duesterhuscd373242019-12-17 12:31:20 +01003409 { "sha2", sample_conv_sha2, ARG1(0, SINT), smp_check_sha2, SMP_T_BIN, SMP_T_BIN },
Tim Duesterhusd4376302019-06-17 12:41:44 +02003410#endif
Willy Tarreau280f42b2018-02-19 15:34:12 +01003411 { "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 +02003412 { "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 +01003413
3414 /* gRPC converters. */
Frédéric Lécaillebfe61382019-03-06 14:34:36 +01003415 { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
3416 { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN },
Willy Tarreau97707872015-01-27 15:12:13 +01003417
Thierry FOURNIER5d86fae2015-07-07 21:10:16 +02003418 { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3419 { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3420 { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3421 { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT },
3422 { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3423 { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3424 { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3425 { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL },
3426 { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3427 { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3428 { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3429 { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3430 { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT },
3431 { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT },
Willy Tarreau97707872015-01-27 15:12:13 +01003432
Willy Tarreau9fcb9842012-04-20 14:45:49 +02003433 { NULL, NULL, 0, 0, 0 },
Emeric Brun107ca302010-01-04 16:16:05 +01003434}};
3435
Willy Tarreau0108d902018-11-25 19:14:37 +01003436INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws);