Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * Sample management functions. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 5 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 6 | * |
| 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 FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 14 | #include <ctype.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <arpa/inet.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 17 | #include <stdio.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 18 | |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 19 | #include <import/mjson.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 20 | #include <import/sha1.h> |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 21 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 22 | #include <haproxy/api.h> |
| 23 | #include <haproxy/arg.h> |
Willy Tarreau | ac13aea | 2020-06-04 10:36:03 +0200 | [diff] [blame] | 24 | #include <haproxy/auth.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 25 | #include <haproxy/base64.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 26 | #include <haproxy/buf.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 27 | #include <haproxy/chunk.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame^] | 28 | #include <haproxy/clock.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 29 | #include <haproxy/errors.h> |
Baptiste Assmann | e138dda | 2020-10-22 15:39:03 +0200 | [diff] [blame] | 30 | #include <haproxy/fix.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 31 | #include <haproxy/global.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 32 | #include <haproxy/hash.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 33 | #include <haproxy/http.h> |
Baptiste Assmann | e138dda | 2020-10-22 15:39:03 +0200 | [diff] [blame] | 34 | #include <haproxy/istbuf.h> |
Baptiste Assmann | e279ca6 | 2020-10-27 18:10:06 +0100 | [diff] [blame] | 35 | #include <haproxy/mqtt.h> |
Willy Tarreau | 6131d6a | 2020-06-02 16:48:09 +0200 | [diff] [blame] | 36 | #include <haproxy/net_helper.h> |
Willy Tarreau | b23e595 | 2020-06-04 16:06:59 +0200 | [diff] [blame] | 37 | #include <haproxy/protobuf.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 38 | #include <haproxy/proxy.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame] | 39 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 40 | #include <haproxy/sample.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/sink.h> |
| 42 | #include <haproxy/stick_table.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 43 | #include <haproxy/tools.h> |
Willy Tarreau | 8c42b8a | 2020-06-04 19:27:34 +0200 | [diff] [blame] | 44 | #include <haproxy/uri_auth-t.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 45 | #include <haproxy/vars.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 46 | #include <haproxy/xxhash.h> |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 47 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 48 | /* sample type names */ |
| 49 | const char *smp_to_type[SMP_TYPES] = { |
Thierry FOURNIER | 9c627e8 | 2015-06-03 20:12:35 +0200 | [diff] [blame] | 50 | [SMP_T_ANY] = "any", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 51 | [SMP_T_BOOL] = "bool", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 52 | [SMP_T_SINT] = "sint", |
| 53 | [SMP_T_ADDR] = "addr", |
| 54 | [SMP_T_IPV4] = "ipv4", |
| 55 | [SMP_T_IPV6] = "ipv6", |
| 56 | [SMP_T_STR] = "str", |
| 57 | [SMP_T_BIN] = "bin", |
Thierry FOURNIER | 4c2479e | 2015-06-03 20:12:04 +0200 | [diff] [blame] | 58 | [SMP_T_METH] = "meth", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 59 | }; |
| 60 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 61 | /* static sample used in sample_process() when <p> is NULL */ |
Emeric Brun | e5c918b | 2017-06-14 14:15:36 +0200 | [diff] [blame] | 62 | static THREAD_LOCAL struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 63 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 64 | /* list head of all known sample fetch keywords */ |
| 65 | static struct sample_fetch_kw_list sample_fetches = { |
| 66 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 69 | /* list head of all known sample format conversion keywords */ |
| 70 | static struct sample_conv_kw_list sample_convs = { |
| 71 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 74 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
Willy Tarreau | be2159b | 2021-03-26 11:56:11 +0100 | [diff] [blame] | 75 | [SMP_SRC_CONST] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 76 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 77 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 78 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 79 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 80 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 81 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL_CFG_PARSER | |
| 82 | SMP_VAL_CLI_PARSER ), |
Willy Tarreau | be2159b | 2021-03-26 11:56:11 +0100 | [diff] [blame] | 83 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 84 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 85 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 86 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 87 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 88 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 89 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 90 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 91 | SMP_VAL_CLI_PARSER ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 92 | |
| 93 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 94 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 95 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 96 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 97 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 98 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 99 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 100 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 101 | |
| 102 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | 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 | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 108 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 109 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 110 | |
| 111 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 112 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 113 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 114 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 115 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 116 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 117 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 118 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 119 | |
| 120 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 121 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 122 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 123 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 124 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 125 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 126 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 127 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 128 | |
| 129 | [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 130 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 131 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 132 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 133 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 134 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 135 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 136 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 137 | |
| 138 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 139 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 140 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 141 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 142 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 143 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 144 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 145 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 146 | |
| 147 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 148 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 149 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 150 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 151 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 152 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 153 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 154 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 155 | |
| 156 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 157 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 158 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 159 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 160 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 161 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 162 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 163 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 164 | |
| 165 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 166 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 167 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 168 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 169 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 170 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 171 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 172 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 173 | |
| 174 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 175 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 176 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 177 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | 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 | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 180 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 181 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 182 | |
| 183 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 184 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 185 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 186 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 187 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 188 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 189 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 190 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 191 | |
| 192 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 193 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 194 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 195 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 196 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 197 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 198 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 199 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 200 | |
| 201 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 202 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 203 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 204 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 205 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 206 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 207 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 208 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 209 | |
| 210 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 211 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 212 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 213 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 214 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 215 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 216 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 217 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 218 | |
| 219 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 220 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 221 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 222 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 223 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 224 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 225 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 226 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 227 | |
| 228 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 229 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 230 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 231 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 232 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 233 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 234 | SMP_VAL_FE_LOG_END | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 235 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 236 | |
| 237 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 238 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 239 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 240 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 241 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 242 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 243 | SMP_VAL___________ | SMP_VAL_BE_CHK_RUL | SMP_VAL___________ | |
| 244 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 245 | |
| 246 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 247 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 248 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 249 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 250 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 251 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 252 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 253 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 254 | |
| 255 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 256 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 257 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 258 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 259 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 260 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 261 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 262 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 263 | |
| 264 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 265 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 266 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 267 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 268 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 269 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 270 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 271 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 272 | |
| 273 | [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 274 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 275 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 276 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 277 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 278 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
Willy Tarreau | db5e0db | 2021-03-26 15:29:35 +0100 | [diff] [blame] | 279 | SMP_VAL_FE_LOG_END | SMP_VAL___________ | SMP_VAL___________ | |
| 280 | SMP_VAL___________ ), |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 284 | [SMP_SRC_INTRN] = "internal state", |
| 285 | [SMP_SRC_LISTN] = "listener", |
| 286 | [SMP_SRC_FTEND] = "frontend", |
| 287 | [SMP_SRC_L4CLI] = "client address", |
| 288 | [SMP_SRC_L5CLI] = "client-side connection", |
| 289 | [SMP_SRC_TRACK] = "track counters", |
| 290 | [SMP_SRC_L6REQ] = "request buffer", |
| 291 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 292 | [SMP_SRC_HRQHP] = "HTTP request", |
| 293 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 294 | [SMP_SRC_BKEND] = "backend", |
| 295 | [SMP_SRC_SERVR] = "server", |
| 296 | [SMP_SRC_L4SRV] = "server address", |
| 297 | [SMP_SRC_L5SRV] = "server-side connection", |
| 298 | [SMP_SRC_L6RES] = "response buffer", |
| 299 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 300 | [SMP_SRC_HRSHP] = "HTTP response", |
| 301 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 302 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 303 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 304 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 305 | [SMP_SRC_SSFIN] = "session statistics", |
| 306 | }; |
| 307 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 308 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 309 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 310 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 311 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 312 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 313 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 314 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 315 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 316 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 317 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 318 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 319 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 320 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 321 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 322 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 323 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 324 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 325 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 326 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 327 | [SMP_CKP_FE_LOG_END] = "logs", |
Gaetan Rivet | 707b52f | 2020-02-21 18:14:59 +0100 | [diff] [blame] | 328 | [SMP_CKP_BE_CHK_RUL] = "backend tcp-check rule", |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 329 | }; |
| 330 | |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 331 | /* This function returns the type of the data returned by the sample_expr. |
| 332 | * It assumes that the <expr> and all of its converters are properly |
| 333 | * initialized. |
| 334 | */ |
| 335 | inline |
| 336 | int smp_expr_output_type(struct sample_expr *expr) |
| 337 | { |
| 338 | struct sample_conv_expr *smp_expr; |
| 339 | |
| 340 | if (!LIST_ISEMPTY(&expr->conv_exprs)) { |
| 341 | smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list); |
| 342 | return smp_expr->conv->out_type; |
| 343 | } |
| 344 | return expr->fetch->out_type; |
| 345 | } |
| 346 | |
| 347 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 348 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 349 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 350 | * value is the pointer to the string in the trash buffer. |
| 351 | */ |
| 352 | const char *sample_src_names(unsigned int use) |
| 353 | { |
| 354 | int bit; |
| 355 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 356 | trash.data = 0; |
| 357 | trash.area[0] = '\0'; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 358 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 359 | if (!(use & ~((1 << bit) - 1))) |
| 360 | break; /* no more bits */ |
| 361 | |
| 362 | if (!(use & (1 << bit))) |
| 363 | continue; /* bit not set */ |
| 364 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 365 | trash.data += snprintf(trash.area + trash.data, |
| 366 | trash.size - trash.data, "%s%s", |
| 367 | (use & ((1 << bit) - 1)) ? "," : "", |
| 368 | fetch_src_names[bit]); |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 369 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 370 | return trash.area; |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 373 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 374 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 375 | * if set. |
| 376 | */ |
| 377 | const char *sample_ckp_names(unsigned int use) |
| 378 | { |
| 379 | int bit; |
| 380 | |
| 381 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 382 | if (use & (1 << bit)) |
| 383 | return fetch_ckp_names[bit]; |
| 384 | return "unknown sample check place, please report this bug"; |
| 385 | } |
| 386 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 387 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 388 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 389 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 390 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 391 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 392 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 393 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 394 | struct sample_fetch *sf; |
| 395 | int bit; |
| 396 | |
| 397 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 398 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 399 | if (sf->use & (1 << bit)) |
| 400 | sf->val |= fetch_cap[bit]; |
| 401 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 402 | LIST_APPEND(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 406 | * Registers the sample format coverstion keyword list <pckl> as a list of valid keywords for next |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 407 | * parsing sessions. |
| 408 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 409 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 410 | { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 411 | LIST_APPEND(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 415 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 416 | * string of <len> in buffer <kw>. |
| 417 | * |
| 418 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 419 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 420 | { |
| 421 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 422 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 423 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 424 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 425 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 426 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 427 | kwl->kw[index].kw[len] == '\0') |
| 428 | return &kwl->kw[index]; |
| 429 | } |
| 430 | } |
| 431 | return NULL; |
| 432 | } |
| 433 | |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 434 | /* This function browses the list of available sample fetches. <current> is |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 435 | * the last used sample fetch. If it is the first call, it must set to NULL. |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 436 | * <idx> is the index of the next sample fetch entry. It is used as private |
| 437 | * value. It is useless to initiate it. |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 438 | * |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 439 | * It returns always the new fetch_sample entry, and NULL when the end of |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 440 | * the list is reached. |
| 441 | */ |
| 442 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx) |
| 443 | { |
| 444 | struct sample_fetch_kw_list *kwl; |
| 445 | struct sample_fetch *base; |
| 446 | |
| 447 | if (!current) { |
| 448 | /* Get first kwl entry. */ |
| 449 | kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list); |
| 450 | (*idx) = 0; |
| 451 | } else { |
| 452 | /* Get kwl corresponding to the curret entry. */ |
| 453 | base = current + 1 - (*idx); |
| 454 | kwl = container_of(base, struct sample_fetch_kw_list, kw); |
| 455 | } |
| 456 | |
| 457 | while (1) { |
| 458 | |
| 459 | /* Check if kwl is the last entry. */ |
| 460 | if (&kwl->list == &sample_fetches.list) |
| 461 | return NULL; |
| 462 | |
| 463 | /* idx contain the next keyword. If it is available, return it. */ |
| 464 | if (kwl->kw[*idx].kw) { |
| 465 | (*idx)++; |
| 466 | return &kwl->kw[(*idx)-1]; |
| 467 | } |
| 468 | |
| 469 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 470 | kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list); |
| 471 | |
| 472 | /* Set index to 0, ans do one other loop. */ |
| 473 | (*idx) = 0; |
| 474 | } |
| 475 | } |
| 476 | |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 477 | /* This function browses the list of available converters. <current> is |
| 478 | * the last used converter. If it is the first call, it must set to NULL. |
| 479 | * <idx> is the index of the next converter entry. It is used as private |
| 480 | * value. It is useless to initiate it. |
| 481 | * |
| 482 | * It returns always the next sample_conv entry, and NULL when the end of |
| 483 | * the list is reached. |
| 484 | */ |
| 485 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx) |
| 486 | { |
| 487 | struct sample_conv_kw_list *kwl; |
| 488 | struct sample_conv *base; |
| 489 | |
| 490 | if (!current) { |
| 491 | /* Get first kwl entry. */ |
| 492 | kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list); |
| 493 | (*idx) = 0; |
| 494 | } else { |
| 495 | /* Get kwl corresponding to the curret entry. */ |
| 496 | base = current + 1 - (*idx); |
| 497 | kwl = container_of(base, struct sample_conv_kw_list, kw); |
| 498 | } |
| 499 | |
| 500 | while (1) { |
| 501 | /* Check if kwl is the last entry. */ |
| 502 | if (&kwl->list == &sample_convs.list) |
| 503 | return NULL; |
| 504 | |
| 505 | /* idx contain the next keyword. If it is available, return it. */ |
| 506 | if (kwl->kw[*idx].kw) { |
| 507 | (*idx)++; |
| 508 | return &kwl->kw[(*idx)-1]; |
| 509 | } |
| 510 | |
| 511 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 512 | kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list); |
| 513 | |
| 514 | /* Set index to 0, ans do one other loop. */ |
| 515 | (*idx) = 0; |
| 516 | } |
| 517 | } |
| 518 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 519 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 520 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 521 | * string of <len> in buffer <kw>. |
| 522 | * |
| 523 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 524 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 525 | { |
| 526 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 527 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 528 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 529 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 530 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 531 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 532 | kwl->kw[index].kw[len] == '\0') |
| 533 | return &kwl->kw[index]; |
| 534 | } |
| 535 | } |
| 536 | return NULL; |
| 537 | } |
| 538 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 539 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 540 | /* Sample casts functions */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 541 | /******************************************************************/ |
| 542 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 543 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 544 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 545 | smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 546 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 547 | return 1; |
| 548 | } |
| 549 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 550 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 551 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 552 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 553 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 554 | if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->area, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 555 | return 0; |
| 556 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 557 | trash->data = strlen(trash->area); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 558 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 560 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 561 | |
| 562 | return 1; |
| 563 | } |
| 564 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 565 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 566 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 567 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 569 | return 1; |
| 570 | } |
| 571 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 572 | static int c_ipv62ip(struct sample *smp) |
| 573 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 574 | if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6)) |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 575 | return 0; |
Tim Duesterhus | bf5ce02 | 2018-01-25 16:24:46 +0100 | [diff] [blame] | 576 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 577 | return 1; |
| 578 | } |
| 579 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 580 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 581 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 582 | struct buffer *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 583 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 584 | if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->area, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 585 | return 0; |
| 586 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 587 | trash->data = strlen(trash->area); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 588 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 590 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 591 | return 1; |
| 592 | } |
| 593 | |
| 594 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 595 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 596 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 597 | return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 598 | } |
| 599 | */ |
| 600 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 601 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 602 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 603 | smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 604 | smp->data.type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 605 | return 1; |
| 606 | } |
| 607 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 608 | static int c_int2ipv6(struct sample *smp) |
| 609 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 610 | smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint); |
| 611 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 612 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 613 | return 1; |
| 614 | } |
| 615 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 616 | static int c_str2addr(struct sample *smp) |
| 617 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 618 | if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) { |
| 619 | if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 620 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 621 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 622 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 623 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 624 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 625 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 626 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 627 | return 1; |
| 628 | } |
| 629 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 630 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 631 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 632 | if (!buf2ip(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 633 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 634 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 635 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 636 | return 1; |
| 637 | } |
| 638 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 639 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 640 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 641 | if (!buf2ip6(smp->data.u.str.area, smp->data.u.str.data, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 642 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 643 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 644 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 645 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 646 | } |
| 647 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 648 | /* |
| 649 | * The NULL char always enforces the end of string if it is met. |
| 650 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 651 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 652 | static int c_bin2str(struct sample *smp) |
| 653 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 654 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 655 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 656 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 657 | if (!smp->data.u.str.area[i]) { |
| 658 | smp->data.u.str.data = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 659 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 660 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 661 | } |
Christopher Faulet | 472ad51 | 2020-04-30 09:57:40 +0200 | [diff] [blame] | 662 | smp->data.type = SMP_T_STR; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 663 | return 1; |
| 664 | } |
| 665 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 666 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 667 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 668 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 669 | char *pos; |
| 670 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | pos = lltoa_r(smp->data.u.sint, trash->area, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 672 | if (!pos) |
| 673 | return 0; |
| 674 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 675 | trash->size = trash->size - (pos - trash->area); |
| 676 | trash->area = pos; |
| 677 | trash->data = strlen(pos); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 678 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 679 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 680 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 681 | return 1; |
| 682 | } |
| 683 | |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 684 | /* This function unconditionally duplicates data and removes the "const" flag. |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 685 | * For strings and binary blocks, it also provides a known allocated size with |
| 686 | * a length that is capped to the size, and ensures a trailing zero is always |
| 687 | * appended for strings. This is necessary for some operations which may |
| 688 | * require to extend the length. It returns 0 if it fails, 1 on success. |
| 689 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 690 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 691 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 692 | struct buffer *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 693 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 694 | switch (smp->data.type) { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 695 | case SMP_T_BOOL: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 696 | case SMP_T_SINT: |
| 697 | case SMP_T_ADDR: |
| 698 | case SMP_T_IPV4: |
| 699 | case SMP_T_IPV6: |
| 700 | /* These type are not const. */ |
| 701 | break; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 702 | |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 703 | case SMP_T_METH: |
| 704 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 705 | break; |
| 706 | /* Fall through */ |
| 707 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 708 | case SMP_T_STR: |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 709 | trash = get_trash_chunk(); |
Olivier Houchard | 4468f1c | 2018-12-07 15:23:41 +0100 | [diff] [blame] | 710 | trash->data = smp->data.type == SMP_T_STR ? |
| 711 | smp->data.u.str.data : smp->data.u.meth.str.data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 712 | if (trash->data > trash->size - 1) |
| 713 | trash->data = trash->size - 1; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 714 | |
Olivier Houchard | 4468f1c | 2018-12-07 15:23:41 +0100 | [diff] [blame] | 715 | memcpy(trash->area, smp->data.type == SMP_T_STR ? |
| 716 | smp->data.u.str.area : smp->data.u.meth.str.area, |
| 717 | trash->data); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 718 | trash->area[trash->data] = 0; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 719 | smp->data.u.str = *trash; |
| 720 | break; |
| 721 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 722 | case SMP_T_BIN: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 723 | trash = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 724 | trash->data = smp->data.u.str.data; |
| 725 | if (trash->data > trash->size) |
| 726 | trash->data = trash->size; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 727 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | memcpy(trash->area, smp->data.u.str.area, trash->data); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 729 | smp->data.u.str = *trash; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 730 | break; |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 731 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 732 | default: |
| 733 | /* Other cases are unexpected. */ |
| 734 | return 0; |
| 735 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 736 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 737 | /* remove const flag */ |
| 738 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 739 | return 1; |
| 740 | } |
| 741 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 742 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 743 | { |
| 744 | return 1; |
| 745 | } |
| 746 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 747 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 748 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 749 | const char *str; |
| 750 | const char *end; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 751 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 752 | if (smp->data.u.str.data == 0) |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 753 | return 0; |
| 754 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 755 | str = smp->data.u.str.area; |
| 756 | end = smp->data.u.str.area + smp->data.u.str.data; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 757 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 758 | smp->data.u.sint = read_int64(&str, end); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 759 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 760 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 761 | return 1; |
| 762 | } |
| 763 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 764 | static int c_str2meth(struct sample *smp) |
| 765 | { |
| 766 | enum http_meth_t meth; |
| 767 | int len; |
| 768 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 769 | meth = find_http_meth(smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 770 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 771 | len = smp->data.u.str.data; |
| 772 | smp->data.u.meth.str.area = smp->data.u.str.area; |
| 773 | smp->data.u.meth.str.data = len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 774 | } |
| 775 | else |
| 776 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 777 | smp->data.u.meth.meth = meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 778 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 779 | return 1; |
| 780 | } |
| 781 | |
| 782 | static int c_meth2str(struct sample *smp) |
| 783 | { |
| 784 | int len; |
| 785 | enum http_meth_t meth; |
| 786 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 787 | if (smp->data.u.meth.meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 788 | /* The method is unknown. Copy the original pointer. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 789 | len = smp->data.u.meth.str.data; |
| 790 | smp->data.u.str.area = smp->data.u.meth.str.area; |
| 791 | smp->data.u.str.data = len; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 792 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 793 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 794 | else if (smp->data.u.meth.meth < HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 795 | /* The method is known, copy the pointer containing the string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 796 | meth = smp->data.u.meth.meth; |
Willy Tarreau | 35b51c6 | 2018-09-10 15:38:55 +0200 | [diff] [blame] | 797 | smp->data.u.str.area = http_known_methods[meth].ptr; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 798 | smp->data.u.str.data = http_known_methods[meth].len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 799 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 800 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 801 | } |
| 802 | else { |
| 803 | /* Unknown method */ |
| 804 | return 0; |
| 805 | } |
| 806 | return 1; |
| 807 | } |
| 808 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 809 | static int c_addr2bin(struct sample *smp) |
| 810 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 811 | struct buffer *chk = get_trash_chunk(); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 812 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 813 | if (smp->data.type == SMP_T_IPV4) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 814 | chk->data = 4; |
| 815 | memcpy(chk->area, &smp->data.u.ipv4, chk->data); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 816 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 817 | else if (smp->data.type == SMP_T_IPV6) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 818 | chk->data = 16; |
| 819 | memcpy(chk->area, &smp->data.u.ipv6, chk->data); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 820 | } |
| 821 | else |
| 822 | return 0; |
| 823 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 824 | smp->data.u.str = *chk; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 825 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 826 | return 1; |
| 827 | } |
| 828 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 829 | static int c_int2bin(struct sample *smp) |
| 830 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 831 | struct buffer *chk = get_trash_chunk(); |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 832 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | *(unsigned long long int *) chk->area = my_htonll(smp->data.u.sint); |
| 834 | chk->data = 8; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 835 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 836 | smp->data.u.str = *chk; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 837 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 838 | return 1; |
| 839 | } |
| 840 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 841 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 842 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 843 | /* Sample casts matrix: */ |
| 844 | /* sample_casts[from type][to type] */ |
| 845 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 846 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 847 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 848 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 849 | /* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 850 | /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, }, |
| 851 | /* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, }, |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 852 | /* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, c_int2ipv6, c_int2str, c_int2bin, NULL, }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 853 | /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
| 854 | /* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 855 | /* IPV6 */ { c_none, NULL, NULL, c_none, c_ipv62ip,c_none, c_ipv62str, c_addr2bin, NULL, }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 856 | /* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 857 | /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 858 | /* METH */ { c_none, NULL, NULL, NULL, NULL, NULL, c_meth2str, c_meth2str, c_none, } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 859 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 860 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 861 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 862 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 863 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 864 | * Returns a pointer on allocated sample expression structure. |
Christopher Faulet | 35926a1 | 2021-09-30 08:48:56 +0200 | [diff] [blame] | 865 | * <al> is an arg_list serving as a list head to report missing dependencies. |
| 866 | * It may be NULL if such dependencies are not allowed. Otherwise, the caller |
| 867 | * must have set al->ctx if al is set. |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 868 | * If <endptr> is non-nul, it will be set to the first unparsed character |
| 869 | * (which may be the final '\0') on success. If it is nul, the expression |
| 870 | * must be properly terminated by a '\0' otherwise an error is reported. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 871 | */ |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 872 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al, char **endptr) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 873 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 874 | const char *begw; /* beginning of word */ |
| 875 | const char *endw; /* end of word */ |
| 876 | const char *endt; /* end of term */ |
Remi Tricot-Le Breton | 22e0d9b | 2021-01-12 14:55:12 +0100 | [diff] [blame] | 877 | struct sample_expr *expr = NULL; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 878 | struct sample_fetch *fetch; |
| 879 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 880 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 881 | char *fkw = NULL; |
| 882 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 883 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 884 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 885 | begw = str[*idx]; |
Willy Tarreau | ed2c662 | 2020-02-14 18:27:10 +0100 | [diff] [blame] | 886 | for (endw = begw; is_idchar(*endw); endw++) |
| 887 | ; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 888 | |
| 889 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 890 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 891 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 892 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 893 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 894 | /* keep a copy of the current fetch keyword for error reporting */ |
| 895 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 896 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 897 | fetch = find_sample_fetch(begw, endw - begw); |
| 898 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 899 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 900 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 901 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 902 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 903 | /* At this point, we have : |
| 904 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 905 | * - endw : end of the keyword, first character not part of keyword |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 906 | */ |
| 907 | |
| 908 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 909 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 910 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 911 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 912 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 913 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 914 | expr = calloc(1, sizeof(*expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 915 | if (!expr) |
| 916 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 917 | |
| 918 | LIST_INIT(&(expr->conv_exprs)); |
| 919 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 920 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 921 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 922 | /* Note that we call the argument parser even with an empty string, |
| 923 | * this allows it to automatically create entries for mandatory |
| 924 | * implicit arguments (eg: local proxy name). |
| 925 | */ |
Christopher Faulet | 35926a1 | 2021-09-30 08:48:56 +0200 | [diff] [blame] | 926 | if (al) { |
| 927 | al->kw = expr->fetch->kw; |
| 928 | al->conv = NULL; |
| 929 | } |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 930 | if (make_arg_list(endw, -1, fetch->arg_mask, &expr->arg_p, err_msg, &endt, &err_arg, al) < 0) { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 931 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 932 | goto out_error; |
| 933 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 934 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 935 | /* now endt is our first char not part of the arg list, typically the |
| 936 | * comma after the sample fetch name or after the closing parenthesis, |
| 937 | * or the NUL char. |
| 938 | */ |
| 939 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 940 | if (!expr->arg_p) { |
| 941 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 942 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 943 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 944 | memprintf(err_msg, "invalid args in fetch method '%s' : %s", fkw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 945 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 946 | } |
| 947 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 948 | /* Now process the converters if any. We have two supported syntaxes |
| 949 | * for the converters, which can be combined : |
| 950 | * - comma-delimited list of converters just after the keyword and args ; |
| 951 | * - one converter per keyword |
| 952 | * The combination allows to have each keyword being a comma-delimited |
| 953 | * series of converters. |
| 954 | * |
| 955 | * We want to process the former first, then the latter. For this we start |
| 956 | * from the beginning of the supposed place in the exiting conv chain, which |
| 957 | * starts at the last comma (endt). |
| 958 | */ |
| 959 | |
| 960 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 961 | struct sample_conv_expr *conv_expr; |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 962 | int err_arg; |
| 963 | int argcnt; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 964 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 965 | if (*endt && *endt != ',') { |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 966 | if (endptr) { |
| 967 | /* end found, let's stop here */ |
| 968 | break; |
| 969 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 970 | if (ckw) |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 971 | memprintf(err_msg, "missing comma after converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 972 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 973 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 974 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 975 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 976 | |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 977 | /* FIXME: how long should we support such idiocies ? Maybe we |
| 978 | * should already warn ? |
| 979 | */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 980 | while (*endt == ',') /* then trailing commas */ |
| 981 | endt++; |
| 982 | |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 983 | begw = endt; /* start of converter */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 984 | |
| 985 | if (!*begw) { |
| 986 | /* none ? skip to next string */ |
| 987 | (*idx)++; |
| 988 | begw = str[*idx]; |
| 989 | if (!begw || !*begw) |
| 990 | break; |
| 991 | } |
| 992 | |
Willy Tarreau | ed2c662 | 2020-02-14 18:27:10 +0100 | [diff] [blame] | 993 | for (endw = begw; is_idchar(*endw); endw++) |
| 994 | ; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 995 | |
| 996 | free(ckw); |
| 997 | ckw = my_strndup(begw, endw - begw); |
| 998 | |
| 999 | conv = find_sample_conv(begw, endw - begw); |
| 1000 | if (!conv) { |
| 1001 | /* we found an isolated keyword that we don't know, it's not ours */ |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 1002 | if (begw == str[*idx]) { |
| 1003 | endt = begw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1004 | break; |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 1005 | } |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 1006 | memprintf(err_msg, "unknown converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1007 | goto out_error; |
| 1008 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1009 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1010 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 1011 | memprintf(err_msg, "returns type of converter '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1012 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1013 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1014 | |
| 1015 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1016 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 1017 | memprintf(err_msg, "converter '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1018 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1019 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1020 | |
| 1021 | prev_type = conv->out_type; |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1022 | conv_expr = calloc(1, sizeof(*conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1023 | if (!conv_expr) |
| 1024 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1025 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1026 | LIST_APPEND(&(expr->conv_exprs), &(conv_expr->list)); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1027 | conv_expr->conv = conv; |
| 1028 | |
Christopher Faulet | 35926a1 | 2021-09-30 08:48:56 +0200 | [diff] [blame] | 1029 | if (al) { |
| 1030 | al->kw = expr->fetch->kw; |
| 1031 | al->conv = conv_expr->conv->kw; |
| 1032 | } |
Willy Tarreau | 80b53ff | 2020-02-14 08:40:37 +0100 | [diff] [blame] | 1033 | argcnt = make_arg_list(endw, -1, conv->arg_mask, &conv_expr->arg_p, err_msg, &endt, &err_arg, al); |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1034 | if (argcnt < 0) { |
| 1035 | memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg); |
| 1036 | goto out_error; |
| 1037 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 1038 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1039 | if (argcnt && !conv->arg_mask) { |
| 1040 | memprintf(err_msg, "converter '%s' does not support any args", ckw); |
| 1041 | goto out_error; |
| 1042 | } |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 1043 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1044 | if (!conv_expr->arg_p) |
| 1045 | conv_expr->arg_p = empty_arg_list; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 1046 | |
Willy Tarreau | 46dfd78 | 2019-12-17 10:25:29 +0100 | [diff] [blame] | 1047 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
| 1048 | memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1049 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1050 | } |
| 1051 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1052 | |
Willy Tarreau | e3b57bf | 2020-02-14 16:50:14 +0100 | [diff] [blame] | 1053 | if (endptr) { |
| 1054 | /* end found, let's stop here */ |
| 1055 | *endptr = (char *)endt; |
| 1056 | } |
| 1057 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1058 | out: |
| 1059 | free(fkw); |
| 1060 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1061 | return expr; |
| 1062 | |
| 1063 | out_error: |
Remi Tricot-Le Breton | 22e0d9b | 2021-01-12 14:55:12 +0100 | [diff] [blame] | 1064 | release_sample_expr(expr); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1065 | expr = NULL; |
| 1066 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1070 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1071 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1072 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 1073 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1074 | * If <p> is not null, function returns results in structure pointed by <p>. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1075 | * If <p> is null, functions returns a pointer on a static sample structure. |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1076 | * |
| 1077 | * Note: the fetch functions are required to properly set the return type. The |
| 1078 | * conversion functions must do so too. However the cast functions do not need |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1079 | * to since they're made to cast multiple types according to what is required. |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1080 | * |
| 1081 | * The caller may indicate in <opt> if it considers the result final or not. |
| 1082 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 1083 | * if the result is stable or not, according to the following table : |
| 1084 | * |
| 1085 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1086 | * NULL 0 * Not present and will never be (eg: header) |
| 1087 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 1088 | * NULL 1 1 Not present yet, will not change anymore |
| 1089 | * smp 0 * Present and will not change (eg: header) |
| 1090 | * smp 1 0 Present, may change (eg: request length) |
| 1091 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1092 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1093 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 1094 | struct stream *strm, unsigned int opt, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1095 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1096 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1097 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1098 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1099 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 1100 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1101 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1102 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1103 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 1104 | smp_set_owner(p, px, sess, strm, opt); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1105 | if (!expr->fetch->process(expr->arg_p, p, expr->fetch->kw, expr->fetch->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1106 | return NULL; |
| 1107 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1108 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1109 | /* we want to ensure that p->type can be casted into |
| 1110 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 1111 | * - NULL => not castable. |
| 1112 | * - c_none => nothing to do (let's optimize it) |
| 1113 | * - other => apply cast and prepare to fail |
| 1114 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1115 | if (!sample_casts[p->data.type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1116 | return NULL; |
| 1117 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1118 | if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none && |
| 1119 | !sample_casts[p->data.type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1120 | return NULL; |
| 1121 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1122 | /* OK cast succeeded */ |
| 1123 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1124 | if (!conv_expr->conv->process(conv_expr->arg_p, p, conv_expr->conv->private)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1125 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1126 | } |
| 1127 | return p; |
| 1128 | } |
| 1129 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1130 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1131 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1132 | * errors or 0 if everything is fine. If at least one error is met, it will |
| 1133 | * be appended to *err. If *err==NULL it will be allocated first. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1134 | */ |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1135 | int smp_resolve_args(struct proxy *p, char **err) |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1136 | { |
| 1137 | struct arg_list *cur, *bak; |
| 1138 | const char *ctx, *where; |
| 1139 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 1140 | struct userlist *ul; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1141 | struct my_regex *reg; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1142 | struct arg *arg; |
| 1143 | int cfgerr = 0; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1144 | int rflags; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1145 | |
| 1146 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 1147 | struct proxy *px; |
| 1148 | struct server *srv; |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1149 | struct stktable *t; |
| 1150 | char *pname, *sname, *stktname; |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1151 | char *err2; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1152 | |
| 1153 | arg = cur->arg; |
| 1154 | |
| 1155 | /* prepare output messages */ |
| 1156 | conv_pre = conv_pos = conv_ctx = ""; |
| 1157 | if (cur->conv) { |
| 1158 | conv_ctx = cur->conv; |
| 1159 | conv_pre = "conversion keyword '"; |
| 1160 | conv_pos = "' for "; |
| 1161 | } |
| 1162 | |
| 1163 | where = "in"; |
| 1164 | ctx = "sample fetch keyword"; |
| 1165 | switch (cur->ctx) { |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1166 | case ARGC_STK: where = "in stick rule in"; break; |
| 1167 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1168 | case ARGC_LOG: where = "in log-format string in"; break; |
| 1169 | case ARGC_LOGSD: where = "in log-format-sd string in"; break; |
Willy Tarreau | 57467b8 | 2021-09-02 19:43:20 +0200 | [diff] [blame] | 1170 | case ARGC_HRQ: where = "in http-request expression in"; break; |
| 1171 | case ARGC_HRS: where = "in http-response response in"; break; |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1172 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
| 1173 | case ARGC_RDR: where = "in redirect format string in"; break; |
| 1174 | case ARGC_CAP: where = "in capture rule in"; break; |
| 1175 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1176 | case ARGC_SRV: where = "in server directive in"; break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1177 | case ARGC_SPOE: where = "in spoe-message directive in"; break; |
Christopher Faulet | 3b967c1 | 2020-05-15 15:47:44 +0200 | [diff] [blame] | 1178 | case ARGC_HERR: where = "in http-error directive in"; break; |
Miroslav Zagorac | 7f8314c | 2020-12-09 16:31:48 +0100 | [diff] [blame] | 1179 | case ARGC_OT: where = "in ot-scope directive in"; break; |
Willy Tarreau | 57467b8 | 2021-09-02 19:43:20 +0200 | [diff] [blame] | 1180 | case ARGC_TCO: where = "in tcp-request connection expression in"; break; |
| 1181 | case ARGC_TSE: where = "in tcp-request session expression in"; break; |
| 1182 | case ARGC_TRQ: where = "in tcp-request content expression in"; break; |
| 1183 | case ARGC_TRS: where = "in tcp-response content expression in"; break; |
| 1184 | case ARGC_TCK: where = "in tcp-check expression in"; break; |
| 1185 | case ARGC_CFG: where = "in configuration expression in"; break; |
| 1186 | case ARGC_CLI: where = "in CLI expression in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | /* set a few default settings */ |
| 1190 | px = p; |
| 1191 | pname = p->id; |
| 1192 | |
| 1193 | switch (arg->type) { |
| 1194 | case ARGT_SRV: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1195 | if (!arg->data.str.data) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1196 | memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1197 | *err ? *err : "", cur->file, cur->line, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1198 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1199 | cfgerr++; |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | /* we support two formats : "bck/srv" and "srv" */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1204 | sname = strrchr(arg->data.str.area, '/'); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1205 | |
| 1206 | if (sname) { |
| 1207 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1208 | pname = arg->data.str.area; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1209 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1210 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1211 | if (!px) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1212 | memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1213 | *err ? *err : "", cur->file, cur->line, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1214 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1215 | cfgerr++; |
| 1216 | break; |
| 1217 | } |
| 1218 | } |
| 1219 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1220 | sname = arg->data.str.area; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1221 | |
| 1222 | srv = findserver(px, sname); |
| 1223 | if (!srv) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1224 | memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1225 | *err ? *err : "", cur->file, cur->line, sname, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1226 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1227 | cfgerr++; |
| 1228 | break; |
| 1229 | } |
| 1230 | |
Amaury Denoyelle | 0626961 | 2021-08-23 14:05:07 +0200 | [diff] [blame] | 1231 | srv->flags |= SRV_F_NON_PURGEABLE; |
| 1232 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1233 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1234 | arg->unresolved = 0; |
| 1235 | arg->data.srv = srv; |
| 1236 | break; |
| 1237 | |
| 1238 | case ARGT_FE: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1239 | if (arg->data.str.data) { |
| 1240 | pname = arg->data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1241 | px = proxy_fe_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | if (!px) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1245 | memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1246 | *err ? *err : "", cur->file, cur->line, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1247 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1248 | cfgerr++; |
| 1249 | break; |
| 1250 | } |
| 1251 | |
| 1252 | if (!(px->cap & PR_CAP_FE)) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1253 | memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1254 | *err ? *err : "", cur->file, cur->line, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1255 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1256 | cfgerr++; |
| 1257 | break; |
| 1258 | } |
| 1259 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1260 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1261 | arg->unresolved = 0; |
| 1262 | arg->data.prx = px; |
| 1263 | break; |
| 1264 | |
| 1265 | case ARGT_BE: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1266 | if (arg->data.str.data) { |
| 1267 | pname = arg->data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1268 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | if (!px) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1272 | memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1273 | *err ? *err : "", cur->file, cur->line, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1274 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1275 | cfgerr++; |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | if (!(px->cap & PR_CAP_BE)) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1280 | memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1281 | *err ? *err : "", cur->file, cur->line, pname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1282 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1283 | cfgerr++; |
| 1284 | break; |
| 1285 | } |
| 1286 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1287 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1288 | arg->unresolved = 0; |
| 1289 | arg->data.prx = px; |
| 1290 | break; |
| 1291 | |
| 1292 | case ARGT_TAB: |
Frédéric Lécaille | 9417f45 | 2019-06-20 09:31:04 +0200 | [diff] [blame] | 1293 | if (arg->data.str.data) |
| 1294 | stktname = arg->data.str.area; |
| 1295 | else |
| 1296 | stktname = px->id; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1297 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1298 | t = stktable_find_by_name(stktname); |
| 1299 | if (!t) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1300 | memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1301 | *err ? *err : "", cur->file, cur->line, stktname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1302 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1303 | cfgerr++; |
| 1304 | break; |
| 1305 | } |
| 1306 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1307 | if (!t->size) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1308 | memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1309 | *err ? *err : "", cur->file, cur->line, stktname, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1310 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1311 | cfgerr++; |
| 1312 | break; |
| 1313 | } |
| 1314 | |
Frédéric Lécaille | be36793 | 2019-08-07 09:28:39 +0200 | [diff] [blame] | 1315 | if (!in_proxies_list(t->proxies_list, p)) { |
Frédéric Lécaille | 015e4d7 | 2019-03-19 14:55:01 +0100 | [diff] [blame] | 1316 | p->next_stkt_ref = t->proxies_list; |
| 1317 | t->proxies_list = p; |
| 1318 | } |
| 1319 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1320 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1321 | arg->unresolved = 0; |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 1322 | arg->data.t = t; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1323 | break; |
| 1324 | |
| 1325 | case ARGT_USR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1326 | if (!arg->data.str.data) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1327 | memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1328 | *err ? *err : "", cur->file, cur->line, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1329 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1330 | cfgerr++; |
| 1331 | break; |
| 1332 | } |
| 1333 | |
| 1334 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1335 | strcmp(p->uri_auth->userlist->name, arg->data.str.area) == 0) |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1336 | ul = p->uri_auth->userlist; |
| 1337 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1338 | ul = auth_find_userlist(arg->data.str.area); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1339 | |
| 1340 | if (!ul) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1341 | memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1342 | *err ? *err : "", cur->file, cur->line, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1343 | arg->data.str.area, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1344 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1345 | cfgerr++; |
| 1346 | break; |
| 1347 | } |
| 1348 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1349 | chunk_destroy(&arg->data.str); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1350 | arg->unresolved = 0; |
| 1351 | arg->data.usr = ul; |
| 1352 | break; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1353 | |
| 1354 | case ARGT_REG: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1355 | if (!arg->data.str.data) { |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1356 | memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1357 | *err ? *err : "", cur->file, cur->line, |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1358 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1359 | cfgerr++; |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1363 | rflags = 0; |
| 1364 | rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0; |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1365 | err2 = NULL; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1366 | |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1367 | if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) { |
| 1368 | memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n", |
| 1369 | *err ? *err : "", cur->file, cur->line, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1370 | arg->data.str.area, |
Willy Tarreau | 77e6a4e | 2021-03-26 16:11:55 +0100 | [diff] [blame] | 1371 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1372 | cfgerr++; |
| 1373 | continue; |
| 1374 | } |
| 1375 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1376 | chunk_destroy(&arg->data.str); |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1377 | arg->unresolved = 0; |
| 1378 | arg->data.reg = reg; |
| 1379 | break; |
| 1380 | |
| 1381 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1384 | LIST_DELETE(&cur->list); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1385 | free(cur); |
| 1386 | } /* end of args processing */ |
| 1387 | |
| 1388 | return cfgerr; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1392 | * Process a fetch + format conversion as defined by the sample expression |
| 1393 | * <expr> on request or response considering the <opt> parameter. The output is |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1394 | * not explicitly set to <smp_type>, but shall be compatible with it as |
| 1395 | * specified by 'sample_casts' table. If a stable sample can be fetched, or an |
| 1396 | * unstable one when <opt> contains SMP_OPT_FINAL, the sample is converted and |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1397 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1398 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1399 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1400 | * take actions (eg: wait longer). If a sample could not be found or could not |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 1401 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1402 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1403 | * still hope to get it after waiting longer, and is not converted to string. |
| 1404 | * The possible output combinations are the following : |
| 1405 | * |
| 1406 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1407 | * NULL * * Not present and will never be (eg: header) |
| 1408 | * smp 0 * Final value converted (eg: header) |
| 1409 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1410 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1411 | */ |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1412 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1413 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1414 | struct sample_expr *expr, int smp_type) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1415 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1416 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1418 | memset(smp, 0, sizeof(*smp)); |
| 1419 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1420 | if (!sample_process(px, sess, strm, opt, expr, smp)) { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1421 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1422 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1423 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1424 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1425 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1426 | if (!sample_casts[smp->data.type][smp_type]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1427 | return NULL; |
| 1428 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1429 | if (!sample_casts[smp->data.type][smp_type](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1430 | return NULL; |
| 1431 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1432 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1433 | return smp; |
| 1434 | } |
| 1435 | |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1436 | static void release_sample_arg(struct arg *p) |
| 1437 | { |
| 1438 | struct arg *p_back = p; |
| 1439 | |
| 1440 | if (!p) |
| 1441 | return; |
| 1442 | |
| 1443 | while (p->type != ARGT_STOP) { |
| 1444 | if (p->type == ARGT_STR || p->unresolved) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1445 | chunk_destroy(&p->data.str); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1446 | p->unresolved = 0; |
| 1447 | } |
| 1448 | else if (p->type == ARGT_REG) { |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 1449 | regex_free(p->data.reg); |
| 1450 | p->data.reg = NULL; |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1451 | } |
| 1452 | p++; |
| 1453 | } |
| 1454 | |
| 1455 | if (p_back != empty_arg_list) |
| 1456 | free(p_back); |
| 1457 | } |
| 1458 | |
| 1459 | void release_sample_expr(struct sample_expr *expr) |
| 1460 | { |
| 1461 | struct sample_conv_expr *conv_expr, *conv_exprb; |
| 1462 | |
| 1463 | if (!expr) |
| 1464 | return; |
| 1465 | |
Tim Duesterhus | 867cd98 | 2020-07-04 11:49:39 +0200 | [diff] [blame] | 1466 | list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1467 | LIST_DELETE(&conv_expr->list); |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1468 | release_sample_arg(conv_expr->arg_p); |
Tim Duesterhus | 867cd98 | 2020-07-04 11:49:39 +0200 | [diff] [blame] | 1469 | free(conv_expr); |
| 1470 | } |
| 1471 | |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1472 | release_sample_arg(expr->arg_p); |
| 1473 | free(expr); |
| 1474 | } |
| 1475 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1476 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1477 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1478 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1479 | /*****************************************************************/ |
| 1480 | |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1481 | static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private) |
| 1482 | { |
| 1483 | int i; |
| 1484 | struct sample tmp; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1485 | struct buffer *buf; |
| 1486 | struct sink *sink; |
| 1487 | struct ist line; |
| 1488 | char *pfx; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1489 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1490 | buf = alloc_trash_chunk(); |
| 1491 | if (!buf) |
| 1492 | goto end; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1493 | |
Christopher Faulet | b45bf8e | 2020-08-07 14:00:23 +0200 | [diff] [blame] | 1494 | sink = (struct sink *)arg_p[1].data.ptr; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1495 | BUG_ON(!sink); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1496 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1497 | pfx = arg_p[0].data.str.area; |
| 1498 | BUG_ON(!pfx); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1499 | |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1500 | chunk_printf(buf, "[debug] %s: type=%s ", pfx, smp_to_type[smp->data.type]); |
| 1501 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 1502 | goto nocast; |
| 1503 | |
| 1504 | /* Copy sample fetch. This puts the sample as const, the |
| 1505 | * cast will copy data if a transformation is required. |
| 1506 | */ |
| 1507 | memcpy(&tmp, smp, sizeof(struct sample)); |
| 1508 | tmp.flags = SMP_F_CONST; |
| 1509 | |
| 1510 | if (!sample_casts[smp->data.type][SMP_T_STR](&tmp)) |
| 1511 | goto nocast; |
| 1512 | |
| 1513 | /* Display the displayable chars*. */ |
| 1514 | b_putchr(buf, '<'); |
| 1515 | for (i = 0; i < tmp.data.u.str.data; i++) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1516 | if (isprint((unsigned char)tmp.data.u.str.area[i])) |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1517 | b_putchr(buf, tmp.data.u.str.area[i]); |
| 1518 | else |
| 1519 | b_putchr(buf, '.'); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1520 | } |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1521 | b_putchr(buf, '>'); |
| 1522 | |
| 1523 | done: |
| 1524 | line = ist2(buf->area, buf->data); |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 1525 | sink_write(sink, &line, 1, 0, 0, NULL); |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1526 | end: |
| 1527 | free_trash_chunk(buf); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1528 | return 1; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1529 | nocast: |
| 1530 | chunk_appendf(buf, "(undisplayable)"); |
| 1531 | goto done; |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1532 | } |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1533 | |
| 1534 | // This function checks the "debug" converter's arguments. |
| 1535 | static int smp_check_debug(struct arg *args, struct sample_conv *conv, |
| 1536 | const char *file, int line, char **err) |
| 1537 | { |
| 1538 | const char *name = "buf0"; |
| 1539 | struct sink *sink = NULL; |
| 1540 | |
| 1541 | if (args[0].type != ARGT_STR) { |
| 1542 | /* optional prefix */ |
| 1543 | args[0].data.str.area = ""; |
| 1544 | args[0].data.str.data = 0; |
| 1545 | } |
| 1546 | |
| 1547 | if (args[1].type == ARGT_STR) |
| 1548 | name = args[1].data.str.area; |
| 1549 | |
| 1550 | sink = sink_find(name); |
| 1551 | if (!sink) { |
| 1552 | memprintf(err, "No such sink '%s'", name); |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1556 | chunk_destroy(&args[1].data.str); |
Christopher Faulet | b45bf8e | 2020-08-07 14:00:23 +0200 | [diff] [blame] | 1557 | args[1].type = ARGT_PTR; |
| 1558 | args[1].data.ptr = sink; |
Willy Tarreau | 0851fd5 | 2019-12-17 10:07:25 +0100 | [diff] [blame] | 1559 | return 1; |
| 1560 | } |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1561 | |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1562 | static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private) |
| 1563 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1564 | struct buffer *trash = get_trash_chunk(); |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1565 | int bin_len; |
| 1566 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1567 | trash->data = 0; |
| 1568 | bin_len = base64dec(smp->data.u.str.area, smp->data.u.str.data, |
| 1569 | trash->area, trash->size); |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1570 | if (bin_len < 0) |
| 1571 | return 0; |
| 1572 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1573 | trash->data = bin_len; |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1574 | smp->data.u.str = *trash; |
| 1575 | smp->data.type = SMP_T_BIN; |
| 1576 | smp->flags &= ~SMP_F_CONST; |
| 1577 | return 1; |
| 1578 | } |
| 1579 | |
Moemen MHEDHBI | 92f7d43 | 2021-04-01 20:53:59 +0200 | [diff] [blame] | 1580 | static int sample_conv_base64url2bin(const struct arg *arg_p, struct sample *smp, void *private) |
| 1581 | { |
| 1582 | struct buffer *trash = get_trash_chunk(); |
| 1583 | int bin_len; |
| 1584 | |
| 1585 | trash->data = 0; |
| 1586 | bin_len = base64urldec(smp->data.u.str.area, smp->data.u.str.data, |
| 1587 | trash->area, trash->size); |
| 1588 | if (bin_len < 0) |
| 1589 | return 0; |
| 1590 | |
| 1591 | trash->data = bin_len; |
| 1592 | smp->data.u.str = *trash; |
| 1593 | smp->data.type = SMP_T_BIN; |
| 1594 | smp->flags &= ~SMP_F_CONST; |
| 1595 | return 1; |
| 1596 | } |
| 1597 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1598 | static int sample_conv_bin2base64(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1599 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1600 | struct buffer *trash = get_trash_chunk(); |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1601 | int b64_len; |
| 1602 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1603 | trash->data = 0; |
| 1604 | b64_len = a2base64(smp->data.u.str.area, smp->data.u.str.data, |
| 1605 | trash->area, trash->size); |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1606 | if (b64_len < 0) |
| 1607 | return 0; |
| 1608 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1609 | trash->data = b64_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1610 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1611 | smp->data.type = SMP_T_STR; |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1612 | smp->flags &= ~SMP_F_CONST; |
| 1613 | return 1; |
| 1614 | } |
| 1615 | |
Moemen MHEDHBI | 92f7d43 | 2021-04-01 20:53:59 +0200 | [diff] [blame] | 1616 | static int sample_conv_bin2base64url(const struct arg *arg_p, struct sample *smp, void *private) |
| 1617 | { |
| 1618 | struct buffer *trash = get_trash_chunk(); |
| 1619 | int b64_len; |
| 1620 | |
| 1621 | trash->data = 0; |
| 1622 | b64_len = a2base64url(smp->data.u.str.area, smp->data.u.str.data, |
| 1623 | trash->area, trash->size); |
| 1624 | if (b64_len < 0) |
| 1625 | return 0; |
| 1626 | |
| 1627 | trash->data = b64_len; |
| 1628 | smp->data.u.str = *trash; |
| 1629 | smp->data.type = SMP_T_STR; |
| 1630 | smp->flags &= ~SMP_F_CONST; |
| 1631 | return 1; |
| 1632 | } |
| 1633 | |
Willy Tarreau | 168e8de | 2021-10-06 15:29:00 +0200 | [diff] [blame] | 1634 | /* This function returns a sample struct filled with the conversion of variable |
| 1635 | * <var> to sample type <type> (SMP_T_*), via a cast to the target type. If the |
| 1636 | * variable cannot be retrieved or casted, 0 is returned, otherwise 1. |
| 1637 | * |
| 1638 | * Keep in mind that the sample content may be written to a pre-allocated |
| 1639 | * trash chunk as returned by get_trash_chunk(). |
| 1640 | */ |
| 1641 | int sample_conv_var2smp(const struct var_desc *var, struct sample *smp, int type) |
| 1642 | { |
| 1643 | if (!vars_get_by_desc(var, smp, NULL)) |
| 1644 | return 0; |
| 1645 | if (!sample_casts[smp->data.type][type]) |
| 1646 | return 0; |
| 1647 | if (!sample_casts[smp->data.type][type](smp)) |
| 1648 | return 0; |
| 1649 | return 1; |
| 1650 | } |
| 1651 | |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1652 | static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private) |
| 1653 | { |
| 1654 | blk_SHA_CTX ctx; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1655 | struct buffer *trash = get_trash_chunk(); |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1656 | |
| 1657 | memset(&ctx, 0, sizeof(ctx)); |
| 1658 | |
| 1659 | blk_SHA1_Init(&ctx); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1660 | blk_SHA1_Update(&ctx, smp->data.u.str.area, smp->data.u.str.data); |
| 1661 | blk_SHA1_Final((unsigned char *) trash->area, &ctx); |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1662 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1663 | trash->data = 20; |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1664 | smp->data.u.str = *trash; |
| 1665 | smp->data.type = SMP_T_BIN; |
| 1666 | smp->flags &= ~SMP_F_CONST; |
| 1667 | return 1; |
| 1668 | } |
| 1669 | |
Dragan Dosen | 9e8db13 | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1670 | /* This function returns a sample struct filled with an <arg> content. |
| 1671 | * If the <arg> contains a string, it is returned in the sample flagged as |
| 1672 | * SMP_F_CONST. If the <arg> contains a variable descriptor, the sample is |
| 1673 | * filled with the content of the variable by using vars_get_by_desc(). |
| 1674 | * |
| 1675 | * Keep in mind that the sample content may be written to a pre-allocated |
| 1676 | * trash chunk as returned by get_trash_chunk(). |
| 1677 | * |
| 1678 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 1679 | */ |
Willy Tarreau | 4034e2c | 2021-10-06 15:20:18 +0200 | [diff] [blame] | 1680 | int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1681 | { |
| 1682 | switch (arg->type) { |
| 1683 | case ARGT_STR: |
| 1684 | smp->data.type = SMP_T_STR; |
| 1685 | smp->data.u.str = arg->data.str; |
Dragan Dosen | 9e8db13 | 2021-02-22 10:03:53 +0100 | [diff] [blame] | 1686 | smp->flags = SMP_F_CONST; |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1687 | return 1; |
| 1688 | case ARGT_VAR: |
Willy Tarreau | 2476ff1 | 2021-10-06 15:30:52 +0200 | [diff] [blame] | 1689 | return sample_conv_var2smp(&arg->data.var, smp, SMP_T_STR); |
Patrick Gansterer | b399bfb | 2018-06-17 11:21:11 +0200 | [diff] [blame] | 1690 | default: |
| 1691 | return 0; |
| 1692 | } |
| 1693 | } |
| 1694 | |
Marcin Deranek | 40ca09c | 2021-07-13 14:05:24 +0200 | [diff] [blame] | 1695 | static int sample_conv_be2dec_check(struct arg *args, struct sample_conv *conv, |
| 1696 | const char *file, int line, char **err) |
| 1697 | { |
| 1698 | if (args[1].data.sint <= 0 || args[1].data.sint > sizeof(unsigned long long)) { |
Willy Tarreau | c5d0fc9 | 2021-09-15 10:30:40 +0200 | [diff] [blame] | 1699 | memprintf(err, "chunk_size out of [1..%u] range (%lld)", (uint)sizeof(unsigned long long), args[1].data.sint); |
Marcin Deranek | 40ca09c | 2021-07-13 14:05:24 +0200 | [diff] [blame] | 1700 | return 0; |
| 1701 | } |
| 1702 | |
| 1703 | if (args[2].data.sint != 0 && args[2].data.sint != 1) { |
| 1704 | memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint); |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | return 1; |
| 1709 | } |
| 1710 | |
| 1711 | /* Converts big-endian binary input sample to a string containing an unsigned |
| 1712 | * integer number per <chunk_size> input bytes separated with <separator>. |
| 1713 | * Optional <truncate> flag indicates if input is truncated at <chunk_size> |
| 1714 | * boundaries. |
| 1715 | * Arguments: separator (string), chunk_size (integer), truncate (0,1) |
| 1716 | */ |
| 1717 | static int sample_conv_be2dec(const struct arg *args, struct sample *smp, void *private) |
| 1718 | { |
| 1719 | struct buffer *trash = get_trash_chunk(); |
| 1720 | const int last = args[2].data.sint ? smp->data.u.str.data - args[1].data.sint + 1 : smp->data.u.str.data; |
| 1721 | int max_size = trash->size - 2; |
| 1722 | int i; |
| 1723 | int start; |
| 1724 | int ptr = 0; |
| 1725 | unsigned long long number; |
| 1726 | char *pos; |
| 1727 | |
| 1728 | trash->data = 0; |
| 1729 | |
| 1730 | while (ptr < last && trash->data <= max_size) { |
| 1731 | start = trash->data; |
| 1732 | if (ptr) { |
| 1733 | /* Add separator */ |
| 1734 | memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data); |
| 1735 | trash->data += args[0].data.str.data; |
| 1736 | } |
| 1737 | else |
| 1738 | max_size -= args[0].data.str.data; |
| 1739 | |
| 1740 | /* Add integer */ |
| 1741 | for (number = 0, i = 0; i < args[1].data.sint && ptr < smp->data.u.str.data; i++) |
| 1742 | number = (number << 8) + (unsigned char)smp->data.u.str.area[ptr++]; |
| 1743 | |
| 1744 | pos = ulltoa(number, trash->area + trash->data, trash->size - trash->data); |
| 1745 | if (pos) |
| 1746 | trash->data = pos - trash->area; |
| 1747 | else { |
| 1748 | trash->data = start; |
| 1749 | break; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | smp->data.u.str = *trash; |
| 1754 | smp->data.type = SMP_T_STR; |
| 1755 | smp->flags &= ~SMP_F_CONST; |
| 1756 | return 1; |
| 1757 | } |
| 1758 | |
Marcin Deranek | da0264a | 2021-07-13 14:08:56 +0200 | [diff] [blame] | 1759 | static int sample_conv_be2hex_check(struct arg *args, struct sample_conv *conv, |
| 1760 | const char *file, int line, char **err) |
| 1761 | { |
| 1762 | if (args[1].data.sint <= 0 && (args[0].data.str.data > 0 || args[2].data.sint != 0)) { |
| 1763 | memprintf(err, "chunk_size needs to be positive (%lld)", args[1].data.sint); |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
| 1767 | if (args[2].data.sint != 0 && args[2].data.sint != 1) { |
| 1768 | memprintf(err, "Unsupported truncate value (%lld)", args[2].data.sint); |
| 1769 | return 0; |
| 1770 | } |
| 1771 | |
| 1772 | return 1; |
| 1773 | } |
| 1774 | |
| 1775 | /* Converts big-endian binary input sample to a hex string containing two hex |
| 1776 | * digits per input byte. <separator> is put every <chunk_size> binary input |
| 1777 | * bytes if specified. Optional <truncate> flag indicates if input is truncated |
| 1778 | * at <chunk_size> boundaries. |
| 1779 | * Arguments: separator (string), chunk_size (integer), truncate (0,1) |
| 1780 | */ |
| 1781 | static int sample_conv_be2hex(const struct arg *args, struct sample *smp, void *private) |
| 1782 | { |
| 1783 | struct buffer *trash = get_trash_chunk(); |
| 1784 | int chunk_size = args[1].data.sint; |
| 1785 | const int last = args[2].data.sint ? smp->data.u.str.data - chunk_size + 1 : smp->data.u.str.data; |
| 1786 | int i; |
| 1787 | int max_size; |
| 1788 | int ptr = 0; |
| 1789 | unsigned char c; |
| 1790 | |
| 1791 | trash->data = 0; |
| 1792 | if (args[0].data.str.data == 0 && args[2].data.sint == 0) |
| 1793 | chunk_size = smp->data.u.str.data; |
| 1794 | max_size = trash->size - 2 * chunk_size; |
| 1795 | |
| 1796 | while (ptr < last && trash->data <= max_size) { |
| 1797 | if (ptr) { |
| 1798 | /* Add separator */ |
| 1799 | memcpy(trash->area + trash->data, args[0].data.str.area, args[0].data.str.data); |
| 1800 | trash->data += args[0].data.str.data; |
| 1801 | } |
| 1802 | else |
| 1803 | max_size -= args[0].data.str.data; |
| 1804 | |
| 1805 | /* Add hex */ |
| 1806 | for (i = 0; i < chunk_size && ptr < smp->data.u.str.data; i++) { |
| 1807 | c = smp->data.u.str.area[ptr++]; |
| 1808 | trash->area[trash->data++] = hextab[(c >> 4) & 0xF]; |
| 1809 | trash->area[trash->data++] = hextab[c & 0xF]; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | smp->data.u.str = *trash; |
| 1814 | smp->data.type = SMP_T_STR; |
| 1815 | smp->flags &= ~SMP_F_CONST; |
| 1816 | return 1; |
| 1817 | } |
| 1818 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1819 | static int sample_conv_bin2hex(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1820 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1821 | struct buffer *trash = get_trash_chunk(); |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1822 | unsigned char c; |
| 1823 | int ptr = 0; |
| 1824 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1825 | trash->data = 0; |
| 1826 | while (ptr < smp->data.u.str.data && trash->data <= trash->size - 2) { |
| 1827 | c = smp->data.u.str.area[ptr++]; |
| 1828 | trash->area[trash->data++] = hextab[(c >> 4) & 0xF]; |
| 1829 | trash->area[trash->data++] = hextab[c & 0xF]; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1830 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1831 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1832 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1833 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1834 | return 1; |
| 1835 | } |
| 1836 | |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 1837 | static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private) |
| 1838 | { |
| 1839 | long long int n = 0; |
| 1840 | int i, c; |
| 1841 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1842 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 1843 | if ((c = hex2i(smp->data.u.str.area[i])) < 0) |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 1844 | return 0; |
| 1845 | n = (n << 4) + c; |
| 1846 | } |
| 1847 | |
| 1848 | smp->data.u.sint = n; |
| 1849 | smp->data.type = SMP_T_SINT; |
| 1850 | smp->flags &= ~SMP_F_CONST; |
| 1851 | return 1; |
| 1852 | } |
| 1853 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1854 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1855 | static int sample_conv_djb2(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1856 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1857 | smp->data.u.sint = hash_djb2(smp->data.u.str.area, |
| 1858 | smp->data.u.str.data); |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 1859 | if (arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1860 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1861 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1862 | return 1; |
| 1863 | } |
| 1864 | |
Willy Tarreau | 60a2ee7 | 2017-12-15 07:13:48 +0100 | [diff] [blame] | 1865 | static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void *private) |
Etienne Carriere | ed0d24e | 2017-12-13 13:41:34 +0100 | [diff] [blame] | 1866 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1867 | int i = smp->data.u.str.data; |
Etienne Carriere | ed0d24e | 2017-12-13 13:41:34 +0100 | [diff] [blame] | 1868 | smp->data.u.sint = i; |
| 1869 | smp->data.type = SMP_T_SINT; |
| 1870 | return 1; |
| 1871 | } |
| 1872 | |
| 1873 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1874 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1875 | { |
| 1876 | int i; |
| 1877 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 1878 | if (!smp_make_rw(smp)) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1879 | return 0; |
| 1880 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1881 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 1882 | if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z')) |
| 1883 | smp->data.u.str.area[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1884 | } |
| 1885 | return 1; |
| 1886 | } |
| 1887 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1888 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1889 | { |
| 1890 | int i; |
| 1891 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 1892 | if (!smp_make_rw(smp)) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1893 | return 0; |
| 1894 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1895 | for (i = 0; i < smp->data.u.str.data; i++) { |
| 1896 | if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z')) |
| 1897 | smp->data.u.str.area[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1898 | } |
| 1899 | return 1; |
| 1900 | } |
| 1901 | |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 1902 | /* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */ |
| 1903 | static int sample_conv_ipmask(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1904 | { |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 1905 | /* Attempt to convert to IPv4 to apply the correct mask. */ |
| 1906 | c_ipv62ip(smp); |
| 1907 | |
| 1908 | if (smp->data.type == SMP_T_IPV4) { |
| 1909 | smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr; |
| 1910 | smp->data.type = SMP_T_IPV4; |
| 1911 | } |
| 1912 | else if (smp->data.type == SMP_T_IPV6) { |
| 1913 | /* IPv6 cannot be converted without an IPv6 mask. */ |
| 1914 | if (args[1].type != ARGT_IPV6) |
| 1915 | return 0; |
| 1916 | |
Willy Tarreau | a8b7ecd | 2020-02-25 09:43:22 +0100 | [diff] [blame] | 1917 | write_u64(&smp->data.u.ipv6.s6_addr[0], |
| 1918 | read_u64(&smp->data.u.ipv6.s6_addr[0]) & read_u64(&args[1].data.ipv6.s6_addr[0])); |
| 1919 | write_u64(&smp->data.u.ipv6.s6_addr[8], |
| 1920 | read_u64(&smp->data.u.ipv6.s6_addr[8]) & read_u64(&args[1].data.ipv6.s6_addr[8])); |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 1921 | smp->data.type = SMP_T_IPV6; |
| 1922 | } |
| 1923 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1924 | return 1; |
| 1925 | } |
| 1926 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1927 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1928 | * adds an optional offset found in args[1] and emits a string representing |
| 1929 | * the local time in the format specified in args[1] using strftime(). |
| 1930 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1931 | static int sample_conv_ltime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1932 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1933 | struct buffer *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1934 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1935 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1936 | struct tm tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1937 | |
| 1938 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1939 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1940 | curr_date += args[1].data.sint; |
| 1941 | |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1942 | get_localtime(curr_date, &tm); |
| 1943 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1944 | temp = get_trash_chunk(); |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1945 | temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1946 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1947 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1948 | return 1; |
| 1949 | } |
| 1950 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1951 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1952 | static int sample_conv_sdbm(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1953 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1954 | smp->data.u.sint = hash_sdbm(smp->data.u.str.area, |
| 1955 | smp->data.u.str.data); |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 1956 | if (arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1957 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1958 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1959 | return 1; |
| 1960 | } |
| 1961 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1962 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1963 | * adds an optional offset found in args[1] and emits a string representing |
| 1964 | * the UTC date in the format specified in args[1] using strftime(). |
| 1965 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1966 | static int sample_conv_utime(const struct arg *args, struct sample *smp, void *private) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1967 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1968 | struct buffer *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1969 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1970 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1971 | struct tm tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1972 | |
| 1973 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1974 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1975 | curr_date += args[1].data.sint; |
| 1976 | |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1977 | get_gmtime(curr_date, &tm); |
| 1978 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1979 | temp = get_trash_chunk(); |
Tim Duesterhus | 1f269c1 | 2021-08-28 23:57:01 +0200 | [diff] [blame] | 1980 | temp->data = strftime(temp->area, temp->size, args[0].data.str.area, &tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1981 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1982 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1983 | return 1; |
| 1984 | } |
| 1985 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1986 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1987 | static int sample_conv_wt6(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1988 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1989 | smp->data.u.sint = hash_wt6(smp->data.u.str.area, |
| 1990 | smp->data.u.str.data); |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 1991 | if (arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1992 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1993 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1994 | return 1; |
| 1995 | } |
| 1996 | |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 1997 | /* hashes the binary input into a 32-bit unsigned int using xxh. |
| 1998 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 1999 | */ |
| 2000 | static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private) |
| 2001 | { |
| 2002 | unsigned int seed; |
| 2003 | |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2004 | if (arg_p->data.sint) |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2005 | seed = arg_p->data.sint; |
| 2006 | else |
| 2007 | seed = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2008 | smp->data.u.sint = XXH32(smp->data.u.str.area, smp->data.u.str.data, |
| 2009 | seed); |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2010 | smp->data.type = SMP_T_SINT; |
| 2011 | return 1; |
| 2012 | } |
| 2013 | |
| 2014 | /* hashes the binary input into a 64-bit unsigned int using xxh. |
| 2015 | * In fact, the function returns a 64 bit unsigned, but the sample |
| 2016 | * storage of haproxy only proposes 64-bits signed, so the value is |
| 2017 | * cast as signed. This cast doesn't impact the hash repartition. |
| 2018 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 2019 | */ |
| 2020 | static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private) |
| 2021 | { |
| 2022 | unsigned long long int seed; |
| 2023 | |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2024 | if (arg_p->data.sint) |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2025 | seed = (unsigned long long int)arg_p->data.sint; |
| 2026 | else |
| 2027 | seed = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2028 | smp->data.u.sint = (long long int)XXH64(smp->data.u.str.area, |
| 2029 | smp->data.u.str.data, seed); |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 2030 | smp->data.type = SMP_T_SINT; |
| 2031 | return 1; |
| 2032 | } |
| 2033 | |
Dragan Dosen | 04bf0cc | 2020-12-22 21:44:33 +0100 | [diff] [blame] | 2034 | static int sample_conv_xxh3(const struct arg *arg_p, struct sample *smp, void *private) |
| 2035 | { |
| 2036 | unsigned long long int seed; |
| 2037 | |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2038 | if (arg_p->data.sint) |
Dragan Dosen | 04bf0cc | 2020-12-22 21:44:33 +0100 | [diff] [blame] | 2039 | seed = (unsigned long long int)arg_p->data.sint; |
| 2040 | else |
| 2041 | seed = 0; |
| 2042 | smp->data.u.sint = (long long int)XXH3(smp->data.u.str.area, |
| 2043 | smp->data.u.str.data, seed); |
| 2044 | smp->data.type = SMP_T_SINT; |
| 2045 | return 1; |
| 2046 | } |
| 2047 | |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2048 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2049 | static int sample_conv_crc32(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2050 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2051 | smp->data.u.sint = hash_crc32(smp->data.u.str.area, |
| 2052 | smp->data.u.str.data); |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2053 | if (arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2054 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2055 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 2056 | return 1; |
| 2057 | } |
| 2058 | |
Emmanuel Hocdet | 50791a7 | 2018-03-21 11:19:01 +0100 | [diff] [blame] | 2059 | /* hashes the binary input into crc32c (RFC4960, Appendix B [8].) */ |
| 2060 | static int sample_conv_crc32c(const struct arg *arg_p, struct sample *smp, void *private) |
| 2061 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2062 | smp->data.u.sint = hash_crc32c(smp->data.u.str.area, |
| 2063 | smp->data.u.str.data); |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2064 | if (arg_p->data.sint) |
Emmanuel Hocdet | 50791a7 | 2018-03-21 11:19:01 +0100 | [diff] [blame] | 2065 | smp->data.u.sint = full_hash(smp->data.u.sint); |
| 2066 | smp->data.type = SMP_T_SINT; |
| 2067 | return 1; |
| 2068 | } |
| 2069 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2070 | /* This function escape special json characters. The returned string can be |
| 2071 | * safely set between two '"' and used as json string. The json string is |
| 2072 | * defined like this: |
| 2073 | * |
| 2074 | * any Unicode character except '"' or '\' or control character |
| 2075 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 2076 | * |
| 2077 | * The enum input_type contain all the allowed mode for decoding the input |
| 2078 | * string. |
| 2079 | */ |
| 2080 | enum input_type { |
| 2081 | IT_ASCII = 0, |
| 2082 | IT_UTF8, |
| 2083 | IT_UTF8S, |
| 2084 | IT_UTF8P, |
| 2085 | IT_UTF8PS, |
| 2086 | }; |
William Dauchy | 5417e89 | 2021-01-08 21:57:41 +0100 | [diff] [blame] | 2087 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2088 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 2089 | const char *file, int line, char **err) |
| 2090 | { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2091 | enum input_type type; |
| 2092 | |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2093 | if (strcmp(arg->data.str.area, "") == 0) |
| 2094 | type = IT_ASCII; |
| 2095 | else if (strcmp(arg->data.str.area, "ascii") == 0) |
| 2096 | type = IT_ASCII; |
| 2097 | else if (strcmp(arg->data.str.area, "utf8") == 0) |
| 2098 | type = IT_UTF8; |
| 2099 | else if (strcmp(arg->data.str.area, "utf8s") == 0) |
| 2100 | type = IT_UTF8S; |
| 2101 | else if (strcmp(arg->data.str.area, "utf8p") == 0) |
| 2102 | type = IT_UTF8P; |
| 2103 | else if (strcmp(arg->data.str.area, "utf8ps") == 0) |
| 2104 | type = IT_UTF8PS; |
| 2105 | else { |
| 2106 | memprintf(err, "Unexpected input code type. " |
| 2107 | "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'"); |
| 2108 | return 0; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2109 | } |
| 2110 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2111 | chunk_destroy(&arg->data.str); |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2112 | arg->type = ARGT_SINT; |
| 2113 | arg->data.sint = type; |
| 2114 | return 1; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2115 | } |
| 2116 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2117 | static int sample_conv_json(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2118 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2119 | struct buffer *temp; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2120 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 2121 | const char *str; |
| 2122 | int len; |
| 2123 | enum input_type input_type = IT_ASCII; |
| 2124 | unsigned int c; |
| 2125 | unsigned int ret; |
| 2126 | char *p; |
| 2127 | |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 2128 | input_type = arg_p->data.sint; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2129 | |
| 2130 | temp = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2131 | temp->data = 0; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2132 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2133 | p = smp->data.u.str.area; |
| 2134 | while (p < smp->data.u.str.area + smp->data.u.str.data) { |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2135 | |
| 2136 | if (input_type == IT_ASCII) { |
| 2137 | /* Read input as ASCII. */ |
| 2138 | c = *(unsigned char *)p; |
| 2139 | p++; |
| 2140 | } |
| 2141 | else { |
| 2142 | /* Read input as UTF8. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2143 | ret = utf8_next(p, |
| 2144 | smp->data.u.str.data - ( p - smp->data.u.str.area), |
| 2145 | &c); |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2146 | p += utf8_return_length(ret); |
| 2147 | |
| 2148 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 2149 | return 0; |
| 2150 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 2151 | continue; |
| 2152 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 2153 | return 0; |
| 2154 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 2155 | continue; |
| 2156 | |
| 2157 | /* Check too big values. */ |
| 2158 | if ((unsigned int)c > 0xffff) { |
| 2159 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 2160 | return 0; |
| 2161 | continue; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | /* Convert character. */ |
| 2166 | if (c == '"') { |
| 2167 | len = 2; |
| 2168 | str = "\\\""; |
| 2169 | } |
| 2170 | else if (c == '\\') { |
| 2171 | len = 2; |
| 2172 | str = "\\\\"; |
| 2173 | } |
| 2174 | else if (c == '/') { |
| 2175 | len = 2; |
| 2176 | str = "\\/"; |
| 2177 | } |
| 2178 | else if (c == '\b') { |
| 2179 | len = 2; |
| 2180 | str = "\\b"; |
| 2181 | } |
| 2182 | else if (c == '\f') { |
| 2183 | len = 2; |
| 2184 | str = "\\f"; |
| 2185 | } |
| 2186 | else if (c == '\r') { |
| 2187 | len = 2; |
| 2188 | str = "\\r"; |
| 2189 | } |
| 2190 | else if (c == '\n') { |
| 2191 | len = 2; |
| 2192 | str = "\\n"; |
| 2193 | } |
| 2194 | else if (c == '\t') { |
| 2195 | len = 2; |
| 2196 | str = "\\t"; |
| 2197 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 2198 | else if (c > 0xff || !isprint((unsigned char)c)) { |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2199 | /* isprint generate a segfault if c is too big. The man says that |
| 2200 | * c must have the value of an unsigned char or EOF. |
| 2201 | */ |
| 2202 | len = 6; |
| 2203 | _str[0] = '\\'; |
| 2204 | _str[1] = 'u'; |
| 2205 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 2206 | str = _str; |
| 2207 | } |
| 2208 | else { |
| 2209 | len = 1; |
Willy Tarreau | 5715da2 | 2020-02-25 08:37:37 +0100 | [diff] [blame] | 2210 | _str[0] = c; |
| 2211 | str = _str; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /* Check length */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2215 | if (temp->data + len > temp->size) |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2216 | return 0; |
| 2217 | |
| 2218 | /* Copy string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2219 | memcpy(temp->area + temp->data, str, len); |
| 2220 | temp->data += len; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2224 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2225 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 2226 | |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2230 | /* This sample function is designed to extract some bytes from an input buffer. |
| 2231 | * First arg is the offset. |
| 2232 | * Optional second arg is the length to truncate */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2233 | static int sample_conv_bytes(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2234 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2235 | if (smp->data.u.str.data <= arg_p[0].data.sint) { |
| 2236 | smp->data.u.str.data = 0; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2237 | return 1; |
| 2238 | } |
| 2239 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2240 | if (smp->data.u.str.size) |
| 2241 | smp->data.u.str.size -= arg_p[0].data.sint; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2242 | smp->data.u.str.data -= arg_p[0].data.sint; |
| 2243 | smp->data.u.str.area += arg_p[0].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2244 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2245 | if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.data)) |
| 2246 | smp->data.u.str.data = arg_p[1].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 2247 | |
| 2248 | return 1; |
| 2249 | } |
| 2250 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2251 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 2252 | const char *file, int line, char **err) |
| 2253 | { |
| 2254 | struct arg *arg = args; |
| 2255 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2256 | if (arg->type != ARGT_SINT) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2257 | memprintf(err, "Unexpected arg type"); |
| 2258 | return 0; |
| 2259 | } |
| 2260 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2261 | if (!arg->data.sint) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2262 | memprintf(err, "Unexpected value 0 for index"); |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | arg++; |
| 2267 | |
| 2268 | if (arg->type != ARGT_STR) { |
| 2269 | memprintf(err, "Unexpected arg type"); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2273 | if (!arg->data.str.data) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2274 | memprintf(err, "Empty separators list"); |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
| 2278 | return 1; |
| 2279 | } |
| 2280 | |
| 2281 | /* This sample function is designed to a return selected part of a string (field). |
| 2282 | * First arg is the index of the field (start at 1) |
| 2283 | * Second arg is a char list of separators (type string) |
| 2284 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2285 | static int sample_conv_field(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2286 | { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2287 | int field; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2288 | char *start, *end; |
| 2289 | int i; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2290 | int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2291 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2292 | if (!arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2293 | return 0; |
| 2294 | |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2295 | if (arg_p[0].data.sint < 0) { |
| 2296 | field = -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2297 | end = start = smp->data.u.str.area + smp->data.u.str.data; |
| 2298 | while (start > smp->data.u.str.area) { |
| 2299 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2300 | if (*(start-1) == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2301 | if (field == arg_p[0].data.sint) { |
| 2302 | if (count == 1) |
| 2303 | goto found; |
| 2304 | else if (count > 1) |
| 2305 | count--; |
| 2306 | } else { |
| 2307 | end = start-1; |
| 2308 | field--; |
| 2309 | } |
| 2310 | break; |
| 2311 | } |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2312 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2313 | start--; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2314 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2315 | } else { |
| 2316 | field = 1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2317 | end = start = smp->data.u.str.area; |
| 2318 | while (end - smp->data.u.str.area < smp->data.u.str.data) { |
| 2319 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2320 | if (*end == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2321 | if (field == arg_p[0].data.sint) { |
| 2322 | if (count == 1) |
| 2323 | goto found; |
| 2324 | else if (count > 1) |
| 2325 | count--; |
| 2326 | } else { |
| 2327 | start = end+1; |
| 2328 | field++; |
| 2329 | } |
| 2330 | break; |
| 2331 | } |
| 2332 | } |
| 2333 | end++; |
| 2334 | } |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2338 | if (field != arg_p[0].data.sint) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2339 | smp->data.u.str.data = 0; |
Tim Duesterhus | 4381d26 | 2019-10-16 15:11:15 +0200 | [diff] [blame] | 2340 | return 0; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2341 | } |
| 2342 | found: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2343 | smp->data.u.str.data = end - start; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2344 | /* If ret string is len 0, no need to |
| 2345 | change pointers or to update size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2346 | if (!smp->data.u.str.data) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2347 | return 1; |
| 2348 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2349 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2350 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2351 | if (smp->data.u.str.size) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2352 | smp->data.u.str.size -= start - smp->data.u.str.area; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2353 | |
Thayne McCombs | b284305 | 2021-04-11 23:26:59 -0600 | [diff] [blame] | 2354 | smp->data.u.str.area = start; |
| 2355 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2356 | return 1; |
| 2357 | } |
| 2358 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2359 | /* This sample function is designed to return a word from a string. |
| 2360 | * First arg is the index of the word (start at 1) |
| 2361 | * Second arg is a char list of words separators (type string) |
| 2362 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2363 | static int sample_conv_word(const struct arg *arg_p, struct sample *smp, void *private) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2364 | { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2365 | int word; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2366 | char *start, *end; |
| 2367 | int i, issep, inword; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2368 | int count = (arg_p[2].type == ARGT_SINT) ? arg_p[2].data.sint : 1; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2369 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2370 | if (!arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2371 | return 0; |
| 2372 | |
| 2373 | word = 0; |
| 2374 | inword = 0; |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2375 | if (arg_p[0].data.sint < 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2376 | end = start = smp->data.u.str.area + smp->data.u.str.data; |
| 2377 | while (start > smp->data.u.str.area) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2378 | issep = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2379 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2380 | if (*(start-1) == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2381 | issep = 1; |
| 2382 | break; |
| 2383 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2384 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2385 | if (!inword) { |
| 2386 | if (!issep) { |
| 2387 | if (word != arg_p[0].data.sint) { |
| 2388 | word--; |
| 2389 | end = start; |
| 2390 | } |
| 2391 | inword = 1; |
| 2392 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2393 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2394 | else if (issep) { |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2395 | if (word == arg_p[0].data.sint) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2396 | if (count == 1) |
| 2397 | goto found; |
| 2398 | else if (count > 1) |
| 2399 | count--; |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2400 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2401 | inword = 0; |
| 2402 | } |
| 2403 | start--; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2404 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2405 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2406 | end = start = smp->data.u.str.area; |
| 2407 | while (end - smp->data.u.str.area < smp->data.u.str.data) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2408 | issep = 0; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2409 | for (i = 0 ; i < arg_p[1].data.str.data; i++) { |
| 2410 | if (*end == arg_p[1].data.str.area[i]) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2411 | issep = 1; |
| 2412 | break; |
| 2413 | } |
| 2414 | } |
| 2415 | if (!inword) { |
| 2416 | if (!issep) { |
| 2417 | if (word != arg_p[0].data.sint) { |
| 2418 | word++; |
| 2419 | start = end; |
| 2420 | } |
| 2421 | inword = 1; |
| 2422 | } |
| 2423 | } |
| 2424 | else if (issep) { |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2425 | if (word == arg_p[0].data.sint) { |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2426 | if (count == 1) |
| 2427 | goto found; |
| 2428 | else if (count > 1) |
| 2429 | count--; |
Willy Tarreau | 9eb2a4a | 2018-04-19 10:33:28 +0200 | [diff] [blame] | 2430 | } |
Marcin Deranek | 9631a28 | 2018-04-16 14:30:46 +0200 | [diff] [blame] | 2431 | inword = 0; |
| 2432 | } |
| 2433 | end++; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2434 | } |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2438 | if (word != arg_p[0].data.sint) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2439 | smp->data.u.str.data = 0; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2440 | return 1; |
| 2441 | } |
| 2442 | found: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2443 | smp->data.u.str.data = end - start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2444 | /* If ret string is len 0, no need to |
| 2445 | change pointers or to update size */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2446 | if (!smp->data.u.str.data) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2447 | return 1; |
| 2448 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2449 | smp->data.u.str.area = start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2450 | |
| 2451 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2452 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2453 | if (smp->data.u.str.size) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2454 | smp->data.u.str.size -= start - smp->data.u.str.area; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2455 | |
| 2456 | return 1; |
| 2457 | } |
| 2458 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2459 | static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv, |
| 2460 | const char *file, int line, char **err) |
| 2461 | { |
| 2462 | struct arg *arg = args; |
| 2463 | char *p; |
| 2464 | int len; |
| 2465 | |
| 2466 | /* arg0 is a regex, it uses type_flag for ICASE and global match */ |
| 2467 | arg[0].type_flags = 0; |
| 2468 | |
| 2469 | if (arg[2].type != ARGT_STR) |
| 2470 | return 1; |
| 2471 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2472 | p = arg[2].data.str.area; |
| 2473 | len = arg[2].data.str.data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2474 | while (len) { |
| 2475 | if (*p == 'i') { |
| 2476 | arg[0].type_flags |= ARGF_REG_ICASE; |
| 2477 | } |
| 2478 | else if (*p == 'g') { |
| 2479 | arg[0].type_flags |= ARGF_REG_GLOB; |
| 2480 | } |
| 2481 | else { |
| 2482 | memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p); |
| 2483 | return 0; |
| 2484 | } |
| 2485 | p++; |
| 2486 | len--; |
| 2487 | } |
| 2488 | return 1; |
| 2489 | } |
| 2490 | |
| 2491 | /* This sample function is designed to do the equivalent of s/match/replace/ on |
| 2492 | * the input string. It applies a regex and restarts from the last matched |
| 2493 | * location until nothing matches anymore. First arg is the regex to apply to |
| 2494 | * the input string, second arg is the replacement expression. |
| 2495 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2496 | static int sample_conv_regsub(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2497 | { |
| 2498 | char *start, *end; |
| 2499 | struct my_regex *reg = arg_p[0].data.reg; |
| 2500 | regmatch_t pmatch[MAX_MATCH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2501 | struct buffer *trash = get_trash_chunk(); |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2502 | struct buffer *output; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2503 | int flag, max; |
| 2504 | int found; |
| 2505 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2506 | start = smp->data.u.str.area; |
| 2507 | end = start + smp->data.u.str.data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2508 | |
| 2509 | flag = 0; |
| 2510 | while (1) { |
| 2511 | /* check for last round which is used to copy remaining parts |
| 2512 | * when not running in global replacement mode. |
| 2513 | */ |
| 2514 | found = 0; |
| 2515 | if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) { |
| 2516 | /* Note: we can have start == end on empty strings or at the end */ |
| 2517 | found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag); |
| 2518 | } |
| 2519 | |
| 2520 | if (!found) |
| 2521 | pmatch[0].rm_so = end - start; |
| 2522 | |
| 2523 | /* copy the heading non-matching part (which may also be the tail if nothing matches) */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2524 | max = trash->size - trash->data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2525 | if (max && pmatch[0].rm_so > 0) { |
| 2526 | if (max > pmatch[0].rm_so) |
| 2527 | max = pmatch[0].rm_so; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2528 | memcpy(trash->area + trash->data, start, max); |
| 2529 | trash->data += max; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | if (!found) |
| 2533 | break; |
| 2534 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2535 | output = alloc_trash_chunk(); |
Willy Tarreau | 23997da | 2020-02-18 14:27:44 +0100 | [diff] [blame] | 2536 | if (!output) |
| 2537 | break; |
| 2538 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2539 | output->data = exp_replace(output->area, output->size, start, arg_p[1].data.str.area, pmatch); |
| 2540 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2541 | /* replace the matching part */ |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2542 | max = output->size - output->data; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2543 | if (max) { |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2544 | if (max > output->data) |
| 2545 | max = output->data; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2546 | memcpy(trash->area + trash->data, |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2547 | output->area, max); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2548 | trash->data += max; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2549 | } |
| 2550 | |
Jerome Magnin | 07e1e3c | 2020-02-16 19:20:19 +0100 | [diff] [blame] | 2551 | free_trash_chunk(output); |
| 2552 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2553 | /* stop here if we're done with this string */ |
| 2554 | if (start >= end) |
| 2555 | break; |
| 2556 | |
| 2557 | /* We have a special case for matches of length 0 (eg: "x*y*"). |
| 2558 | * These ones are considered to match in front of a character, |
| 2559 | * so we have to copy that character and skip to the next one. |
| 2560 | */ |
| 2561 | if (!pmatch[0].rm_eo) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2562 | if (trash->data < trash->size) |
| 2563 | trash->area[trash->data++] = start[pmatch[0].rm_eo]; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2564 | pmatch[0].rm_eo++; |
| 2565 | } |
| 2566 | |
| 2567 | start += pmatch[0].rm_eo; |
| 2568 | flag |= REG_NOTBOL; |
| 2569 | } |
| 2570 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2571 | smp->data.u.str = *trash; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2572 | return 1; |
| 2573 | } |
| 2574 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2575 | /* This function check an operator entry. It expects a string. |
| 2576 | * The string can be an integer or a variable name. |
| 2577 | */ |
| 2578 | static int check_operator(struct arg *args, struct sample_conv *conv, |
| 2579 | const char *file, int line, char **err) |
| 2580 | { |
| 2581 | const char *str; |
| 2582 | const char *end; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2583 | long long int i; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2584 | |
| 2585 | /* Try to decode a variable. */ |
| 2586 | if (vars_check_arg(&args[0], NULL)) |
| 2587 | return 1; |
| 2588 | |
| 2589 | /* Try to convert an integer */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2590 | str = args[0].data.str.area; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2591 | end = str + strlen(str); |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2592 | i = read_int64(&str, end); |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2593 | if (*str != '\0') { |
| 2594 | memprintf(err, "expects an integer or a variable name"); |
| 2595 | return 0; |
| 2596 | } |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2597 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 2598 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2599 | args[0].type = ARGT_SINT; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 2600 | args[0].data.sint = i; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2601 | return 1; |
| 2602 | } |
| 2603 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2604 | /* This function returns a sample struct filled with an arg content. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2605 | * If the arg contain an integer, the integer is returned in the |
| 2606 | * sample. If the arg contains a variable descriptor, it returns the |
| 2607 | * variable value. |
| 2608 | * |
| 2609 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 2610 | */ |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2611 | int sample_conv_var2smp_sint(const struct arg *arg, struct sample *smp) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2612 | { |
| 2613 | switch (arg->type) { |
| 2614 | case ARGT_SINT: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2615 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2616 | smp->data.u.sint = arg->data.sint; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2617 | return 1; |
| 2618 | case ARGT_VAR: |
Willy Tarreau | 2476ff1 | 2021-10-06 15:30:52 +0200 | [diff] [blame] | 2619 | return sample_conv_var2smp(&arg->data.var, smp, SMP_T_SINT); |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2620 | default: |
| 2621 | return 0; |
| 2622 | } |
| 2623 | } |
| 2624 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2625 | /* Takes a SINT on input, applies a binary twos complement and returns the SINT |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2626 | * result. |
| 2627 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2628 | static int sample_conv_binary_cpl(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2629 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2630 | smp->data.u.sint = ~smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2631 | return 1; |
| 2632 | } |
| 2633 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2634 | /* Takes a SINT on input, applies a binary "and" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2635 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2636 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2637 | static int sample_conv_binary_and(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2638 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2639 | struct sample tmp; |
| 2640 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2641 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2642 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2643 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2644 | smp->data.u.sint &= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2645 | return 1; |
| 2646 | } |
| 2647 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2648 | /* Takes a SINT on input, applies a binary "or" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2649 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2650 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2651 | static int sample_conv_binary_or(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2652 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2653 | struct sample tmp; |
| 2654 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2655 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2656 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2657 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2658 | smp->data.u.sint |= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2659 | return 1; |
| 2660 | } |
| 2661 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2662 | /* Takes a SINT on input, applies a binary "xor" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2663 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2664 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2665 | static int sample_conv_binary_xor(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2666 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2667 | struct sample tmp; |
| 2668 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2669 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2670 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2671 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2672 | smp->data.u.sint ^= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2673 | return 1; |
| 2674 | } |
| 2675 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2676 | static inline long long int arith_add(long long int a, long long int b) |
| 2677 | { |
| 2678 | /* Prevent overflow and makes capped calculus. |
| 2679 | * We must ensure that the check calculus doesn't |
| 2680 | * exceed the signed 64 bits limits. |
| 2681 | * |
| 2682 | * +----------+----------+ |
| 2683 | * | a<0 | a>=0 | |
| 2684 | * +------+----------+----------+ |
| 2685 | * | b<0 | MIN-a>b | no check | |
| 2686 | * +------+----------+----------+ |
| 2687 | * | b>=0 | no check | MAX-a<b | |
| 2688 | * +------+----------+----------+ |
| 2689 | */ |
| 2690 | if ((a ^ b) >= 0) { |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2691 | /* signs are different. */ |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2692 | if (a < 0) { |
| 2693 | if (LLONG_MIN - a > b) |
| 2694 | return LLONG_MIN; |
| 2695 | } |
| 2696 | if (LLONG_MAX - a < b) |
| 2697 | return LLONG_MAX; |
| 2698 | } |
| 2699 | return a + b; |
| 2700 | } |
| 2701 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2702 | /* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2703 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2704 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2705 | static int sample_conv_arith_add(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2706 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2707 | struct sample tmp; |
| 2708 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2709 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2710 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2711 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2712 | smp->data.u.sint = arith_add(smp->data.u.sint, tmp.data.u.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2713 | return 1; |
| 2714 | } |
| 2715 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2716 | /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2717 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2718 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2719 | static int sample_conv_arith_sub(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2720 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2721 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2722 | struct sample tmp; |
| 2723 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2724 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2725 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2726 | return 0; |
| 2727 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2728 | /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater |
| 2729 | * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place |
| 2730 | * of -LLONG_MIN and correct the result. |
| 2731 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2732 | if (tmp.data.u.sint == LLONG_MIN) { |
| 2733 | smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX); |
| 2734 | if (smp->data.u.sint < LLONG_MAX) |
| 2735 | smp->data.u.sint++; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2736 | return 1; |
| 2737 | } |
| 2738 | |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2739 | /* standard subtraction: we use the "add" function and negate |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2740 | * the second operand. |
| 2741 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2742 | smp->data.u.sint = arith_add(smp->data.u.sint, -tmp.data.u.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2743 | return 1; |
| 2744 | } |
| 2745 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2746 | /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2747 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2748 | * If the result makes an overflow, then the largest possible quantity is |
| 2749 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2750 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2751 | static int sample_conv_arith_mul(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2752 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2753 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2754 | struct sample tmp; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2755 | long long int c; |
| 2756 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2757 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2758 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2759 | return 0; |
| 2760 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2761 | /* prevent divide by 0 during the check */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2762 | if (!smp->data.u.sint || !tmp.data.u.sint) { |
| 2763 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2764 | return 1; |
| 2765 | } |
| 2766 | |
| 2767 | /* The multiply between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2768 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2769 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2770 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2771 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2772 | return 1; |
| 2773 | } |
| 2774 | |
| 2775 | /* execute standard multiplication. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2776 | c = smp->data.u.sint * tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2777 | |
| 2778 | /* check for overflow and makes capped multiply. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2779 | if (smp->data.u.sint != c / tmp.data.u.sint) { |
| 2780 | if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) { |
| 2781 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2782 | return 1; |
| 2783 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2784 | smp->data.u.sint = LLONG_MIN; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2785 | return 1; |
| 2786 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2787 | smp->data.u.sint = c; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2788 | return 1; |
| 2789 | } |
| 2790 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2791 | /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2792 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2793 | * If arg_p makes the result overflow, then the largest possible quantity is |
| 2794 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2795 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2796 | static int sample_conv_arith_div(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2797 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2798 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2799 | struct sample tmp; |
| 2800 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2801 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2802 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2803 | return 0; |
| 2804 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2805 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2806 | /* The divide between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2807 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2808 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2809 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2810 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2811 | return 1; |
| 2812 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2813 | smp->data.u.sint /= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2814 | return 1; |
| 2815 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2816 | smp->data.u.sint = LLONG_MAX; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2817 | return 1; |
| 2818 | } |
| 2819 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2820 | /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in |
Joseph Herlant | 757f5ad | 2018-11-15 12:14:56 -0800 | [diff] [blame] | 2821 | * arg_p or in the variable described in arg_p, and returns the SINT result. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2822 | * If arg_p makes the result overflow, then 0 is returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2823 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2824 | static int sample_conv_arith_mod(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2825 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2826 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2827 | struct sample tmp; |
| 2828 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2829 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | d9be599 | 2021-10-06 15:19:05 +0200 | [diff] [blame] | 2830 | if (!sample_conv_var2smp_sint(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2831 | return 0; |
| 2832 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2833 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2834 | /* The divide between LLONG_MIN and -1 returns a |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 2835 | * "floating point exception". |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2836 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2837 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2838 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2839 | return 1; |
| 2840 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2841 | smp->data.u.sint %= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2842 | return 1; |
| 2843 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2844 | smp->data.u.sint = 0; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2845 | return 1; |
| 2846 | } |
| 2847 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2848 | /* Takes an SINT on input, applies an arithmetic "neg" and returns the SINT |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2849 | * result. |
| 2850 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2851 | static int sample_conv_arith_neg(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2852 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2853 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2854 | if (smp->data.u.sint == LLONG_MIN) |
| 2855 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2856 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2857 | smp->data.u.sint = -smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2858 | return 1; |
| 2859 | } |
| 2860 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2861 | /* Takes a SINT on input, returns true is the value is non-null, otherwise |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2862 | * false. The output is a BOOL. |
| 2863 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2864 | static int sample_conv_arith_bool(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2865 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2866 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2867 | smp->data.u.sint = !!smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2868 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2869 | return 1; |
| 2870 | } |
| 2871 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2872 | /* Takes a SINT on input, returns false is the value is non-null, otherwise |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2873 | * truee. The output is a BOOL. |
| 2874 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2875 | static int sample_conv_arith_not(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2876 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2877 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2878 | smp->data.u.sint = !smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2879 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2880 | return 1; |
| 2881 | } |
| 2882 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2883 | /* Takes a SINT on input, returns true is the value is odd, otherwise false. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2884 | * The output is a BOOL. |
| 2885 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2886 | static int sample_conv_arith_odd(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2887 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2888 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2889 | smp->data.u.sint = smp->data.u.sint & 1; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2890 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2891 | return 1; |
| 2892 | } |
| 2893 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2894 | /* Takes a SINT on input, returns true is the value is even, otherwise false. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2895 | * The output is a BOOL. |
| 2896 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2897 | static int sample_conv_arith_even(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2898 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2899 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2900 | smp->data.u.sint = !(smp->data.u.sint & 1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2901 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
| 2904 | |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2905 | /* appends an optional const string, an optional variable contents and another |
| 2906 | * optional const string to an existing string. |
| 2907 | */ |
| 2908 | static int sample_conv_concat(const struct arg *arg_p, struct sample *smp, void *private) |
| 2909 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2910 | struct buffer *trash; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2911 | struct sample tmp; |
| 2912 | int max; |
| 2913 | |
Willy Tarreau | 591fc3a | 2021-01-08 16:08:43 +0100 | [diff] [blame] | 2914 | trash = alloc_trash_chunk(); |
William Dauchy | e997010 | 2021-01-11 11:05:58 +0100 | [diff] [blame] | 2915 | if (!trash) |
| 2916 | return 0; |
| 2917 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2918 | trash->data = smp->data.u.str.data; |
| 2919 | if (trash->data > trash->size - 1) |
| 2920 | trash->data = trash->size - 1; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2921 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2922 | memcpy(trash->area, smp->data.u.str.area, trash->data); |
| 2923 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2924 | |
| 2925 | /* append first string */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2926 | max = arg_p[0].data.str.data; |
| 2927 | if (max > trash->size - 1 - trash->data) |
| 2928 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2929 | |
| 2930 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2931 | memcpy(trash->area + trash->data, arg_p[0].data.str.area, max); |
| 2932 | trash->data += max; |
| 2933 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | /* append second string (variable) if it's found and we can turn it |
| 2937 | * into a string. |
| 2938 | */ |
| 2939 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 2940 | if (arg_p[1].type == ARGT_VAR && vars_get_by_desc(&arg_p[1].data.var, &tmp, NULL) && |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2941 | (sample_casts[tmp.data.type][SMP_T_STR] == c_none || |
| 2942 | sample_casts[tmp.data.type][SMP_T_STR](&tmp))) { |
| 2943 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2944 | max = tmp.data.u.str.data; |
| 2945 | if (max > trash->size - 1 - trash->data) |
| 2946 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2947 | |
| 2948 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2949 | memcpy(trash->area + trash->data, tmp.data.u.str.area, |
| 2950 | max); |
| 2951 | trash->data += max; |
| 2952 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2953 | } |
| 2954 | } |
| 2955 | |
| 2956 | /* append third string */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2957 | max = arg_p[2].data.str.data; |
| 2958 | if (max > trash->size - 1 - trash->data) |
| 2959 | max = trash->size - 1 - trash->data; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2960 | |
| 2961 | if (max) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2962 | memcpy(trash->area + trash->data, arg_p[2].data.str.area, max); |
| 2963 | trash->data += max; |
| 2964 | trash->area[trash->data] = 0; |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | smp->data.u.str = *trash; |
| 2968 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 591fc3a | 2021-01-08 16:08:43 +0100 | [diff] [blame] | 2969 | smp_dup(smp); |
| 2970 | free_trash_chunk(trash); |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2971 | return 1; |
| 2972 | } |
| 2973 | |
| 2974 | /* This function checks the "concat" converter's arguments and extracts the |
| 2975 | * variable name and its scope. |
| 2976 | */ |
| 2977 | static int smp_check_concat(struct arg *args, struct sample_conv *conv, |
| 2978 | const char *file, int line, char **err) |
| 2979 | { |
| 2980 | /* Try to decode a variable. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2981 | if (args[1].data.str.data > 0 && !vars_check_arg(&args[1], NULL)) { |
| 2982 | memprintf(err, "failed to register variable name '%s'", |
| 2983 | args[1].data.str.area); |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 2984 | return 0; |
| 2985 | } |
| 2986 | return 1; |
| 2987 | } |
| 2988 | |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 2989 | /* Compares string with a variable containing a string. Return value |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 2990 | * is compatible with strcmp(3)'s return value. |
| 2991 | */ |
| 2992 | static int sample_conv_strcmp(const struct arg *arg_p, struct sample *smp, void *private) |
| 2993 | { |
| 2994 | struct sample tmp; |
| 2995 | int max, result; |
| 2996 | |
| 2997 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
| 2998 | if (arg_p[0].type != ARGT_VAR) |
| 2999 | return 0; |
Willy Tarreau | 2476ff1 | 2021-10-06 15:30:52 +0200 | [diff] [blame] | 3000 | |
| 3001 | if (!sample_conv_var2smp(&arg_p[0].data.var, &tmp, SMP_T_STR)) |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3002 | return 0; |
| 3003 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3004 | max = MIN(smp->data.u.str.data, tmp.data.u.str.data); |
| 3005 | result = strncmp(smp->data.u.str.area, tmp.data.u.str.area, max); |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3006 | if (result == 0) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3007 | if (smp->data.u.str.data != tmp.data.u.str.data) { |
| 3008 | if (smp->data.u.str.data < tmp.data.u.str.data) { |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3009 | result = -1; |
| 3010 | } |
| 3011 | else { |
| 3012 | result = 1; |
| 3013 | } |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | smp->data.u.sint = result; |
| 3018 | smp->data.type = SMP_T_SINT; |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
Tim Duesterhus | 3943e4f | 2020-09-11 14:25:23 +0200 | [diff] [blame] | 3022 | /* Takes a boolean as input. Returns the first argument if that boolean is true and |
| 3023 | * the second argument otherwise. |
| 3024 | */ |
| 3025 | static int sample_conv_iif(const struct arg *arg_p, struct sample *smp, void *private) |
| 3026 | { |
| 3027 | smp->data.type = SMP_T_STR; |
| 3028 | smp->flags |= SMP_F_CONST; |
| 3029 | |
| 3030 | if (smp->data.u.sint) { |
| 3031 | smp->data.u.str.data = arg_p[0].data.str.data; |
| 3032 | smp->data.u.str.area = arg_p[0].data.str.area; |
| 3033 | } |
| 3034 | else { |
| 3035 | smp->data.u.str.data = arg_p[1].data.str.data; |
| 3036 | smp->data.u.str.area = arg_p[1].data.str.area; |
| 3037 | } |
| 3038 | |
| 3039 | return 1; |
| 3040 | } |
| 3041 | |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3042 | #define GRPC_MSG_COMPRESS_FLAG_SZ 1 /* 1 byte */ |
| 3043 | #define GRPC_MSG_LENGTH_SZ 4 /* 4 bytes */ |
| 3044 | #define GRPC_MSG_HEADER_SZ (GRPC_MSG_COMPRESS_FLAG_SZ + GRPC_MSG_LENGTH_SZ) |
| 3045 | |
| 3046 | /* |
| 3047 | * Extract the field value of an input binary sample. Takes a mandatory argument: |
| 3048 | * the protocol buffers field identifier (dotted notation) internally represented |
| 3049 | * as an array of unsigned integers and its size. |
| 3050 | * Return 1 if the field was found, 0 if not. |
| 3051 | */ |
| 3052 | static int sample_conv_ungrpc(const struct arg *arg_p, struct sample *smp, void *private) |
| 3053 | { |
| 3054 | unsigned char *pos; |
| 3055 | size_t grpc_left; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3056 | |
| 3057 | pos = (unsigned char *)smp->data.u.str.area; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3058 | grpc_left = smp->data.u.str.data; |
| 3059 | |
Frédéric Lécaille | 7c93e88 | 2019-03-04 07:33:41 +0100 | [diff] [blame] | 3060 | while (grpc_left > GRPC_MSG_HEADER_SZ) { |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3061 | size_t grpc_msg_len, left; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3062 | |
| 3063 | grpc_msg_len = left = ntohl(*(uint32_t *)(pos + GRPC_MSG_COMPRESS_FLAG_SZ)); |
| 3064 | |
| 3065 | pos += GRPC_MSG_HEADER_SZ; |
| 3066 | grpc_left -= GRPC_MSG_HEADER_SZ; |
| 3067 | |
| 3068 | if (grpc_left < left) |
| 3069 | return 0; |
| 3070 | |
Frédéric Lécaille | 5f33f85 | 2019-03-06 08:03:44 +0100 | [diff] [blame] | 3071 | if (protobuf_field_lookup(arg_p, smp, &pos, &left)) |
| 3072 | return 1; |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3073 | |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3074 | grpc_left -= grpc_msg_len; |
| 3075 | } |
| 3076 | |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
Frédéric Lécaille | bfe6138 | 2019-03-06 14:34:36 +0100 | [diff] [blame] | 3080 | static int sample_conv_protobuf(const struct arg *arg_p, struct sample *smp, void *private) |
| 3081 | { |
| 3082 | unsigned char *pos; |
| 3083 | size_t left; |
| 3084 | |
| 3085 | pos = (unsigned char *)smp->data.u.str.area; |
| 3086 | left = smp->data.u.str.data; |
| 3087 | |
| 3088 | return protobuf_field_lookup(arg_p, smp, &pos, &left); |
| 3089 | } |
| 3090 | |
| 3091 | static int sample_conv_protobuf_check(struct arg *args, struct sample_conv *conv, |
| 3092 | const char *file, int line, char **err) |
Frédéric Lécaille | 756d97f | 2019-03-04 19:03:48 +0100 | [diff] [blame] | 3093 | { |
| 3094 | if (!args[1].type) { |
| 3095 | args[1].type = ARGT_SINT; |
| 3096 | args[1].data.sint = PBUF_T_BINARY; |
| 3097 | } |
| 3098 | else { |
| 3099 | int pbuf_type; |
| 3100 | |
| 3101 | pbuf_type = protobuf_type(args[1].data.str.area); |
| 3102 | if (pbuf_type == -1) { |
| 3103 | memprintf(err, "Wrong protocol buffer type '%s'", args[1].data.str.area); |
| 3104 | return 0; |
| 3105 | } |
| 3106 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3107 | chunk_destroy(&args[1].data.str); |
Frédéric Lécaille | 756d97f | 2019-03-04 19:03:48 +0100 | [diff] [blame] | 3108 | args[1].type = ARGT_SINT; |
| 3109 | args[1].data.sint = pbuf_type; |
| 3110 | } |
| 3111 | |
| 3112 | return 1; |
| 3113 | } |
| 3114 | |
Baptiste Assmann | e138dda | 2020-10-22 15:39:03 +0200 | [diff] [blame] | 3115 | /* |
| 3116 | * Extract the tag value of an input binary sample. Takes a mandatory argument: |
| 3117 | * the FIX protocol tag identifier. |
| 3118 | * Return 1 if the tag was found, 0 if not. |
| 3119 | */ |
| 3120 | static int sample_conv_fix_tag_value(const struct arg *arg_p, struct sample *smp, void *private) |
| 3121 | { |
| 3122 | struct ist value; |
| 3123 | |
| 3124 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 3125 | value = fix_tag_value(ist2(smp->data.u.str.area, smp->data.u.str.data), |
| 3126 | arg_p[0].data.sint); |
| 3127 | if (!istlen(value)) { |
| 3128 | if (!isttest(value)) { |
| 3129 | /* value != IST_NULL, need more data */ |
| 3130 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3131 | } |
| 3132 | return 0; |
| 3133 | } |
| 3134 | |
| 3135 | smp->data.u.str = ist2buf(value); |
| 3136 | smp->flags |= SMP_F_CONST; |
| 3137 | |
| 3138 | return 1; |
| 3139 | } |
| 3140 | |
| 3141 | /* This function checks the "fix_tag_value" converter configuration. |
| 3142 | * It expects a "known" (by HAProxy) tag name or ID. |
| 3143 | * Tag string names are converted to their ID counterpart because this is the |
| 3144 | * format they are sent over the wire. |
| 3145 | */ |
| 3146 | static int sample_conv_fix_value_check(struct arg *args, struct sample_conv *conv, |
| 3147 | const char *file, int line, char **err) |
| 3148 | { |
| 3149 | struct ist str; |
| 3150 | unsigned int tag; |
| 3151 | |
| 3152 | str = ist2(args[0].data.str.area, args[0].data.str.data); |
| 3153 | tag = fix_tagid(str); |
| 3154 | if (!tag) { |
| 3155 | memprintf(err, "Unknown FIX tag name '%s'", args[0].data.str.area); |
| 3156 | return 0; |
| 3157 | } |
| 3158 | |
| 3159 | chunk_destroy(&args[0].data.str); |
| 3160 | args[0].type = ARGT_SINT; |
| 3161 | args[0].data.sint = tag; |
| 3162 | |
| 3163 | return 1; |
| 3164 | } |
| 3165 | |
| 3166 | /* |
| 3167 | * Checks that a buffer contains a valid FIX message |
| 3168 | * |
| 3169 | * Return 1 if the check could be run, 0 if not. |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 3170 | * The result of the analyse itself is stored in <smp> as a boolean |
Baptiste Assmann | e138dda | 2020-10-22 15:39:03 +0200 | [diff] [blame] | 3171 | */ |
| 3172 | static int sample_conv_fix_is_valid(const struct arg *arg_p, struct sample *smp, void *private) |
| 3173 | { |
| 3174 | struct ist msg; |
| 3175 | |
| 3176 | msg = ist2(smp->data.u.str.area, smp->data.u.str.data); |
| 3177 | |
| 3178 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 3179 | switch (fix_validate_message(msg)) { |
| 3180 | case FIX_VALID_MESSAGE: |
| 3181 | smp->data.type = SMP_T_BOOL; |
| 3182 | smp->data.u.sint = 1; |
| 3183 | return 1; |
| 3184 | case FIX_NEED_MORE_DATA: |
| 3185 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3186 | return 0; |
| 3187 | case FIX_INVALID_MESSAGE: |
| 3188 | smp->data.type = SMP_T_BOOL; |
| 3189 | smp->data.u.sint = 0; |
| 3190 | return 1; |
| 3191 | } |
| 3192 | return 0; |
| 3193 | } |
| 3194 | |
Baptiste Assmann | e279ca6 | 2020-10-27 18:10:06 +0100 | [diff] [blame] | 3195 | /* |
| 3196 | * Extract the field value of an input binary sample containing an MQTT packet. |
| 3197 | * Takes 2 mandatory arguments: |
| 3198 | * - packet type |
| 3199 | * - field name |
| 3200 | * |
| 3201 | * return 1 if the field was found, 0 if not. |
| 3202 | */ |
| 3203 | static int sample_conv_mqtt_field_value(const struct arg *arg_p, struct sample *smp, void *private) |
| 3204 | { |
| 3205 | struct ist pkt, value; |
| 3206 | int type, fieldname_id; |
| 3207 | |
| 3208 | pkt = ist2(smp->data.u.str.area, smp->data.u.str.data); |
| 3209 | type = arg_p[0].data.sint; |
| 3210 | fieldname_id = arg_p[1].data.sint; |
| 3211 | |
| 3212 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 3213 | value = mqtt_field_value(pkt, type, fieldname_id); |
| 3214 | if (!istlen(value)) { |
| 3215 | if (isttest(value)) { |
| 3216 | /* value != IST_NULL, need more data */ |
| 3217 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3218 | } |
| 3219 | return 0; |
| 3220 | } |
| 3221 | |
| 3222 | smp->data.u.str = ist2buf(value); |
| 3223 | smp->flags |= SMP_F_CONST; |
| 3224 | return 1; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * this function checks the "mqtt_field_value" converter configuration. |
| 3229 | * It expects a known packet type name or ID and a field name, in this order |
| 3230 | * |
Ilya Shipitsin | f38a018 | 2020-12-21 01:16:17 +0500 | [diff] [blame] | 3231 | * Args[0] will be turned into a MQTT_CPT_* value for direct matching when parsing |
Baptiste Assmann | e279ca6 | 2020-10-27 18:10:06 +0100 | [diff] [blame] | 3232 | * a packet. |
| 3233 | */ |
| 3234 | static int sample_conv_mqtt_field_value_check(struct arg *args, struct sample_conv *conv, |
| 3235 | const char *file, int line, char **err) |
| 3236 | { |
| 3237 | int type, fieldname_id; |
| 3238 | |
| 3239 | /* check the MQTT packet type is valid */ |
| 3240 | type = mqtt_typeid(ist2(args[0].data.str.area, args[0].data.str.data)); |
| 3241 | if (type == MQTT_CPT_INVALID) { |
| 3242 | memprintf(err, "Unknown MQTT type '%s'", args[0].data.str.area); |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | /* check the field name belongs to the MQTT packet type */ |
| 3247 | fieldname_id = mqtt_check_type_fieldname(type, ist2(args[1].data.str.area, args[1].data.str.data)); |
| 3248 | if (fieldname_id == MQTT_FN_INVALID) { |
| 3249 | memprintf(err, "Unknown MQTT field name '%s' for packet type '%s'", args[1].data.str.area, |
| 3250 | args[0].data.str.area); |
| 3251 | return 0; |
| 3252 | } |
| 3253 | |
| 3254 | /* save numeric counterparts of type and field name */ |
| 3255 | chunk_destroy(&args[0].data.str); |
| 3256 | chunk_destroy(&args[1].data.str); |
| 3257 | args[0].type = ARGT_SINT; |
| 3258 | args[0].data.sint = type; |
| 3259 | args[1].type = ARGT_SINT; |
| 3260 | args[1].data.sint = fieldname_id; |
| 3261 | |
| 3262 | return 1; |
| 3263 | } |
| 3264 | |
| 3265 | /* |
| 3266 | * Checks that <smp> contains a valid MQTT message |
| 3267 | * |
| 3268 | * The function returns 1 if the check was run to its end, 0 otherwise. |
| 3269 | * The result of the analyse itself is stored in <smp> as a boolean. |
| 3270 | */ |
| 3271 | static int sample_conv_mqtt_is_valid(const struct arg *arg_p, struct sample *smp, void *private) |
| 3272 | { |
| 3273 | struct ist msg; |
| 3274 | |
| 3275 | msg = ist2(smp->data.u.str.area, smp->data.u.str.data); |
| 3276 | |
| 3277 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 3278 | switch (mqtt_validate_message(msg, NULL)) { |
| 3279 | case FIX_VALID_MESSAGE: |
| 3280 | smp->data.type = SMP_T_BOOL; |
| 3281 | smp->data.u.sint = 1; |
| 3282 | return 1; |
| 3283 | case FIX_NEED_MORE_DATA: |
| 3284 | smp->flags |= SMP_F_MAY_CHANGE; |
| 3285 | return 0; |
| 3286 | case FIX_INVALID_MESSAGE: |
| 3287 | smp->data.type = SMP_T_BOOL; |
| 3288 | smp->data.u.sint = 0; |
| 3289 | return 1; |
| 3290 | } |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3294 | /* This function checks the "strcmp" converter's arguments and extracts the |
| 3295 | * variable name and its scope. |
| 3296 | */ |
| 3297 | static int smp_check_strcmp(struct arg *args, struct sample_conv *conv, |
| 3298 | const char *file, int line, char **err) |
| 3299 | { |
Willy Tarreau | a1169b6 | 2021-05-08 06:50:28 +0200 | [diff] [blame] | 3300 | if (!args[0].data.str.data) { |
| 3301 | memprintf(err, "missing variable name"); |
| 3302 | return 0; |
| 3303 | } |
| 3304 | |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3305 | /* Try to decode a variable. */ |
| 3306 | if (vars_check_arg(&args[0], NULL)) |
| 3307 | return 1; |
| 3308 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3309 | memprintf(err, "failed to register variable name '%s'", |
| 3310 | args[0].data.str.area); |
Tim Duesterhus | ca097c1 | 2018-04-27 21:18:45 +0200 | [diff] [blame] | 3311 | return 0; |
| 3312 | } |
Tim Duesterhus | f38175c | 2020-06-09 11:48:42 +0200 | [diff] [blame] | 3313 | |
Christopher Faulet | 4ccc12f | 2020-04-01 09:08:32 +0200 | [diff] [blame] | 3314 | /**/ |
| 3315 | static int sample_conv_htonl(const struct arg *arg_p, struct sample *smp, void *private) |
| 3316 | { |
| 3317 | struct buffer *tmp; |
| 3318 | uint32_t n; |
| 3319 | |
| 3320 | n = htonl((uint32_t)smp->data.u.sint); |
| 3321 | tmp = get_trash_chunk(); |
| 3322 | |
| 3323 | memcpy(b_head(tmp), &n, 4); |
| 3324 | b_add(tmp, 4); |
| 3325 | |
| 3326 | smp->data.u.str = *tmp; |
| 3327 | smp->data.type = SMP_T_BIN; |
| 3328 | return 1; |
| 3329 | } |
| 3330 | |
Christopher Faulet | ea159d6 | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 3331 | /**/ |
| 3332 | static int sample_conv_cut_crlf(const struct arg *arg_p, struct sample *smp, void *private) |
| 3333 | { |
| 3334 | char *p; |
| 3335 | size_t l; |
| 3336 | |
| 3337 | p = smp->data.u.str.area; |
| 3338 | for (l = 0; l < smp->data.u.str.data; l++) { |
| 3339 | if (*(p+l) == '\r' || *(p+l) == '\n') |
| 3340 | break; |
| 3341 | } |
| 3342 | smp->data.u.str.data = l; |
| 3343 | return 1; |
| 3344 | } |
| 3345 | |
Christopher Faulet | 51fc9d1 | 2020-04-01 17:24:41 +0200 | [diff] [blame] | 3346 | /**/ |
| 3347 | static int sample_conv_ltrim(const struct arg *arg_p, struct sample *smp, void *private) |
| 3348 | { |
| 3349 | char *delimiters, *p; |
| 3350 | size_t dlen, l; |
| 3351 | |
| 3352 | delimiters = arg_p[0].data.str.area; |
| 3353 | dlen = arg_p[0].data.str.data; |
| 3354 | |
| 3355 | l = smp->data.u.str.data; |
| 3356 | p = smp->data.u.str.area; |
| 3357 | while (l && memchr(delimiters, *p, dlen) != NULL) { |
| 3358 | p++; |
| 3359 | l--; |
| 3360 | } |
| 3361 | |
| 3362 | smp->data.u.str.area = p; |
| 3363 | smp->data.u.str.data = l; |
| 3364 | return 1; |
| 3365 | } |
| 3366 | |
Christopher Faulet | 568415a | 2020-04-01 17:24:47 +0200 | [diff] [blame] | 3367 | /**/ |
| 3368 | static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *private) |
| 3369 | { |
| 3370 | char *delimiters, *p; |
| 3371 | size_t dlen, l; |
| 3372 | |
| 3373 | delimiters = arg_p[0].data.str.area; |
| 3374 | dlen = arg_p[0].data.str.data; |
| 3375 | |
| 3376 | l = smp->data.u.str.data; |
| 3377 | p = smp->data.u.str.area + l - 1; |
| 3378 | while (l && memchr(delimiters, *p, dlen) != NULL) { |
| 3379 | p--; |
| 3380 | l--; |
| 3381 | } |
| 3382 | |
| 3383 | smp->data.u.str.data = l; |
| 3384 | return 1; |
| 3385 | } |
| 3386 | |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3387 | /* This function checks the "json_query" converter's arguments. */ |
| 3388 | static int sample_check_json_query(struct arg *arg, struct sample_conv *conv, |
| 3389 | const char *file, int line, char **err) |
| 3390 | { |
| 3391 | if (arg[0].data.str.data == 0) { |
| 3392 | memprintf(err, "json_path must not be empty"); |
| 3393 | return 0; |
| 3394 | } |
| 3395 | |
| 3396 | if (arg[1].data.str.data != 0) { |
| 3397 | if (strcmp(arg[1].data.str.area, "int") != 0) { |
| 3398 | memprintf(err, "output_type only supports \"int\" as argument"); |
| 3399 | return 0; |
| 3400 | } else { |
| 3401 | arg[1].type = ARGT_SINT; |
| 3402 | arg[1].data.sint = 0; |
| 3403 | } |
| 3404 | } |
| 3405 | return 1; |
| 3406 | } |
| 3407 | |
| 3408 | /* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per |
| 3409 | * the recommendation for interoperable integers in section 6 of RFC 7159. |
| 3410 | */ |
| 3411 | #define JSON_INT_MAX ((1LL << 53) - 1) |
| 3412 | #define JSON_INT_MIN (-JSON_INT_MAX) |
| 3413 | |
| 3414 | /* This sample function get the value from a given json string. |
| 3415 | * The mjson library is used to parse the JSON struct |
| 3416 | */ |
| 3417 | static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private) |
| 3418 | { |
| 3419 | struct buffer *trash = get_trash_chunk(); |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3420 | const char *token; /* holds the temporary string from mjson_find */ |
| 3421 | int token_size; /* holds the length of <token> */ |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3422 | |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3423 | enum mjson_tok token_type; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3424 | |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3425 | token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size); |
| 3426 | |
| 3427 | switch (token_type) { |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3428 | case MJSON_TOK_NUMBER: |
| 3429 | if (args[1].type == ARGT_SINT) { |
Willy Tarreau | fb60195 | 2021-05-14 08:51:53 +0200 | [diff] [blame] | 3430 | smp->data.u.sint = strtoll(token, NULL, 0); |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3431 | |
| 3432 | if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX) |
| 3433 | return 0; |
| 3434 | |
| 3435 | smp->data.type = SMP_T_SINT; |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3436 | |
| 3437 | return 1; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3438 | } else { |
| 3439 | double double_val; |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3440 | |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3441 | if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3442 | return 0; |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3443 | |
| 3444 | trash->data = snprintf(trash->area,trash->size,"%g",double_val); |
| 3445 | smp->data.u.str = *trash; |
| 3446 | smp->data.type = SMP_T_STR; |
| 3447 | |
| 3448 | return 1; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3449 | } |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3450 | case MJSON_TOK_TRUE: |
| 3451 | smp->data.type = SMP_T_BOOL; |
| 3452 | smp->data.u.sint = 1; |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3453 | |
| 3454 | return 1; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3455 | case MJSON_TOK_FALSE: |
| 3456 | smp->data.type = SMP_T_BOOL; |
| 3457 | smp->data.u.sint = 0; |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3458 | |
| 3459 | return 1; |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3460 | case MJSON_TOK_STRING: { |
| 3461 | int len; |
| 3462 | |
| 3463 | len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size); |
| 3464 | |
| 3465 | if (len == -1) { |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3466 | /* invalid string */ |
| 3467 | return 0; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3468 | } |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3469 | |
| 3470 | trash->data = len; |
| 3471 | smp->data.u.str = *trash; |
| 3472 | smp->data.type = SMP_T_STR; |
| 3473 | |
| 3474 | return 1; |
Tim Duesterhus | 4809c8c | 2021-04-15 18:08:48 +0200 | [diff] [blame] | 3475 | } |
Tim Duesterhus | 8f3bc8f | 2021-04-15 18:14:32 +0200 | [diff] [blame] | 3476 | case MJSON_TOK_NULL: |
| 3477 | case MJSON_TOK_ARRAY: |
| 3478 | case MJSON_TOK_OBJECT: |
| 3479 | /* We cannot handle these. */ |
| 3480 | return 0; |
| 3481 | case MJSON_TOK_INVALID: |
| 3482 | /* Nothing matches the query. */ |
| 3483 | return 0; |
| 3484 | case MJSON_TOK_KEY: |
| 3485 | /* This is not a valid return value according to the |
| 3486 | * mjson documentation, but we handle it to benefit |
| 3487 | * from '-Wswitch'. |
| 3488 | */ |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3489 | return 0; |
| 3490 | } |
Tim Duesterhus | 3b9cdf1 | 2021-04-15 18:40:06 +0200 | [diff] [blame] | 3491 | |
| 3492 | my_unreachable(); |
| 3493 | return 0; |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3497 | /************************************************************************/ |
| 3498 | /* All supported sample fetch functions must be declared here */ |
| 3499 | /************************************************************************/ |
| 3500 | |
| 3501 | /* force TRUE to be returned at the fetch level */ |
| 3502 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3503 | smp_fetch_true(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3504 | { |
Willy Tarreau | 280f42b | 2018-02-19 15:34:12 +0100 | [diff] [blame] | 3505 | if (!smp_make_rw(smp)) |
| 3506 | return 0; |
| 3507 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3508 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3509 | smp->data.u.sint = 1; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3510 | return 1; |
| 3511 | } |
| 3512 | |
| 3513 | /* force FALSE to be returned at the fetch level */ |
| 3514 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3515 | smp_fetch_false(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3516 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3517 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3518 | smp->data.u.sint = 0; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3519 | return 1; |
| 3520 | } |
| 3521 | |
| 3522 | /* retrieve environment variable $1 as a string */ |
| 3523 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3524 | smp_fetch_env(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3525 | { |
| 3526 | char *env; |
| 3527 | |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 3528 | if (args[0].type != ARGT_STR) |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3529 | return 0; |
| 3530 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3531 | env = getenv(args[0].data.str.area); |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3532 | if (!env) |
| 3533 | return 0; |
| 3534 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3535 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 3536 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3537 | smp->data.u.str.area = env; |
| 3538 | smp->data.u.str.data = strlen(env); |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3539 | return 1; |
| 3540 | } |
| 3541 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3542 | /* Validates the data unit argument passed to "date" fetch. Argument 1 support an |
| 3543 | * optional string representing the unit of the result: "s" for seconds, "ms" for |
| 3544 | * milliseconds and "us" for microseconds. |
| 3545 | * Returns 0 on error and non-zero if OK. |
| 3546 | */ |
| 3547 | int smp_check_date_unit(struct arg *args, char **err) |
| 3548 | { |
| 3549 | if (args[1].type == ARGT_STR) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3550 | long long int unit; |
| 3551 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3552 | if (strcmp(args[1].data.str.area, "s") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3553 | unit = TIME_UNIT_S; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3554 | } |
| 3555 | else if (strcmp(args[1].data.str.area, "ms") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3556 | unit = TIME_UNIT_MS; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3557 | } |
| 3558 | else if (strcmp(args[1].data.str.area, "us") == 0) { |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3559 | unit = TIME_UNIT_US; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3560 | } |
| 3561 | else { |
| 3562 | memprintf(err, "expects 's', 'ms' or 'us', got '%s'", |
| 3563 | args[1].data.str.area); |
| 3564 | return 0; |
| 3565 | } |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3566 | |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3567 | chunk_destroy(&args[1].data.str); |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3568 | args[1].type = ARGT_SINT; |
Christopher Faulet | 9591713 | 2020-08-05 23:07:07 +0200 | [diff] [blame] | 3569 | args[1].data.sint = unit; |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3570 | } |
| 3571 | else if (args[1].type != ARGT_STOP) { |
| 3572 | memprintf(err, "Unexpected arg type"); |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
| 3576 | return 1; |
| 3577 | } |
| 3578 | |
| 3579 | /* retrieve the current local date in epoch time, converts it to milliseconds |
| 3580 | * or microseconds if asked to in optional args[1] unit param, and applies an |
| 3581 | * optional args[0] offset. |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3582 | */ |
| 3583 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3584 | smp_fetch_date(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3585 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3586 | smp->data.u.sint = date.tv_sec; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3587 | |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3588 | /* report in milliseconds */ |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 3589 | if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_MS) { |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3590 | smp->data.u.sint *= 1000; |
| 3591 | smp->data.u.sint += date.tv_usec / 1000; |
| 3592 | } |
| 3593 | /* report in microseconds */ |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 3594 | else if (args[1].type == ARGT_SINT && args[1].data.sint == TIME_UNIT_US) { |
Damien Claisse | ae6f125 | 2019-10-30 15:57:28 +0000 | [diff] [blame] | 3595 | smp->data.u.sint *= 1000000; |
| 3596 | smp->data.u.sint += date.tv_usec; |
| 3597 | } |
| 3598 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3599 | /* add offset */ |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 3600 | if (args[0].type == ARGT_SINT) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3601 | smp->data.u.sint += args[0].data.sint; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3602 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3603 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 3604 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3605 | return 1; |
| 3606 | } |
| 3607 | |
Etienne Carriere | a792a0a | 2018-01-17 13:43:24 +0100 | [diff] [blame] | 3608 | /* retrieve the current microsecond part of the date */ |
| 3609 | static int |
| 3610 | smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3611 | { |
| 3612 | smp->data.u.sint = date.tv_usec; |
| 3613 | smp->data.type = SMP_T_SINT; |
| 3614 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3615 | return 1; |
| 3616 | } |
| 3617 | |
| 3618 | |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 3619 | /* returns the hostname */ |
| 3620 | static int |
| 3621 | smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3622 | { |
| 3623 | smp->data.type = SMP_T_STR; |
| 3624 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3625 | smp->data.u.str.area = hostname; |
| 3626 | smp->data.u.str.data = strlen(hostname); |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 3627 | return 1; |
| 3628 | } |
| 3629 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3630 | /* returns the number of processes */ |
| 3631 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3632 | smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3633 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3634 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 9135859 | 2021-06-15 08:08:04 +0200 | [diff] [blame] | 3635 | smp->data.u.sint = 1; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3636 | return 1; |
| 3637 | } |
| 3638 | |
| 3639 | /* returns the number of the current process (between 1 and nbproc */ |
| 3640 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3641 | smp_fetch_proc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3642 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3643 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | e8422bf | 2021-06-15 09:08:18 +0200 | [diff] [blame] | 3644 | smp->data.u.sint = 1; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3645 | return 1; |
| 3646 | } |
| 3647 | |
Christopher Faulet | 34adb2a | 2017-11-21 21:45:38 +0100 | [diff] [blame] | 3648 | /* returns the number of the current thread (between 1 and nbthread */ |
| 3649 | static int |
| 3650 | smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3651 | { |
| 3652 | smp->data.type = SMP_T_SINT; |
| 3653 | smp->data.u.sint = tid; |
| 3654 | return 1; |
| 3655 | } |
| 3656 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3657 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 3658 | * range specified in argument. |
| 3659 | */ |
| 3660 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3661 | smp_fetch_rand(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3662 | { |
Willy Tarreau | aa8bbc1 | 2020-03-08 18:01:10 +0100 | [diff] [blame] | 3663 | smp->data.u.sint = ha_random32(); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3664 | |
| 3665 | /* reduce if needed. Don't do a modulo, use all bits! */ |
Christopher Faulet | e6e7a58 | 2021-01-29 11:29:28 +0100 | [diff] [blame] | 3666 | if (args[0].type == ARGT_SINT) |
Willy Tarreau | aa8bbc1 | 2020-03-08 18:01:10 +0100 | [diff] [blame] | 3667 | smp->data.u.sint = ((u64)smp->data.u.sint * (u64)args[0].data.sint) >> 32; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3668 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3669 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 3670 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3671 | return 1; |
| 3672 | } |
| 3673 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3674 | /* returns true if the current process is stopping */ |
| 3675 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 3676 | smp_fetch_stopping(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3677 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3678 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3679 | smp->data.u.sint = stopping; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3680 | return 1; |
| 3681 | } |
| 3682 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3683 | /* returns the number of calls of the current stream's process_stream() */ |
| 3684 | static int |
| 3685 | smp_fetch_cpu_calls(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3686 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3687 | if (!smp->strm) |
| 3688 | return 0; |
| 3689 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3690 | smp->data.type = SMP_T_SINT; |
| 3691 | smp->data.u.sint = smp->strm->task->calls; |
| 3692 | return 1; |
| 3693 | } |
| 3694 | |
| 3695 | /* returns the average number of nanoseconds spent processing the stream per call */ |
| 3696 | static int |
| 3697 | smp_fetch_cpu_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3698 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3699 | if (!smp->strm) |
| 3700 | return 0; |
| 3701 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3702 | smp->data.type = SMP_T_SINT; |
| 3703 | smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->cpu_time / smp->strm->task->calls : 0; |
| 3704 | return 1; |
| 3705 | } |
| 3706 | |
| 3707 | /* returns the total number of nanoseconds spent processing the stream */ |
| 3708 | static int |
| 3709 | smp_fetch_cpu_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3710 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3711 | if (!smp->strm) |
| 3712 | return 0; |
| 3713 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3714 | smp->data.type = SMP_T_SINT; |
| 3715 | smp->data.u.sint = smp->strm->task->cpu_time; |
| 3716 | return 1; |
| 3717 | } |
| 3718 | |
| 3719 | /* returns the average number of nanoseconds per call spent waiting for other tasks to be processed */ |
| 3720 | static int |
| 3721 | smp_fetch_lat_ns_avg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3722 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3723 | if (!smp->strm) |
| 3724 | return 0; |
| 3725 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3726 | smp->data.type = SMP_T_SINT; |
| 3727 | smp->data.u.sint = smp->strm->task->calls ? smp->strm->task->lat_time / smp->strm->task->calls : 0; |
| 3728 | return 1; |
| 3729 | } |
| 3730 | |
| 3731 | /* returns the total number of nanoseconds per call spent waiting for other tasks to be processed */ |
| 3732 | static int |
| 3733 | smp_fetch_lat_ns_tot(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3734 | { |
Willy Tarreau | e0dd210 | 2020-04-29 11:59:02 +0200 | [diff] [blame] | 3735 | if (!smp->strm) |
| 3736 | return 0; |
| 3737 | |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3738 | smp->data.type = SMP_T_SINT; |
| 3739 | smp->data.u.sint = smp->strm->task->lat_time; |
| 3740 | return 1; |
| 3741 | } |
| 3742 | |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3743 | static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3744 | { |
| 3745 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3746 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3747 | smp->data.u.str.area = args[0].data.str.area; |
| 3748 | smp->data.u.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3749 | return 1; |
| 3750 | } |
| 3751 | |
| 3752 | static int smp_check_const_bool(struct arg *args, char **err) |
| 3753 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3754 | if (strcasecmp(args[0].data.str.area, "true") == 0 || |
| 3755 | strcasecmp(args[0].data.str.area, "1") == 0) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3756 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3757 | args[0].type = ARGT_SINT; |
| 3758 | args[0].data.sint = 1; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3759 | return 1; |
| 3760 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3761 | if (strcasecmp(args[0].data.str.area, "false") == 0 || |
| 3762 | strcasecmp(args[0].data.str.area, "0") == 0) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3763 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3764 | args[0].type = ARGT_SINT; |
| 3765 | args[0].data.sint = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3766 | return 1; |
| 3767 | } |
| 3768 | memprintf(err, "Expects 'true', 'false', '0' or '1'"); |
| 3769 | return 0; |
| 3770 | } |
| 3771 | |
| 3772 | static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3773 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3774 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3775 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3776 | return 1; |
| 3777 | } |
| 3778 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 3779 | static int smp_fetch_const_int(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3780 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3781 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3782 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3783 | return 1; |
| 3784 | } |
| 3785 | |
| 3786 | static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3787 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3788 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3789 | smp->data.u.ipv4 = args[0].data.ipv4; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3790 | return 1; |
| 3791 | } |
| 3792 | |
| 3793 | static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3794 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3795 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3796 | smp->data.u.ipv6 = args[0].data.ipv6; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3797 | return 1; |
| 3798 | } |
| 3799 | |
| 3800 | static int smp_check_const_bin(struct arg *args, char **err) |
| 3801 | { |
David Carlier | 64a16ab | 2016-04-08 10:37:02 +0100 | [diff] [blame] | 3802 | char *binstr = NULL; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3803 | int binstrlen; |
| 3804 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3805 | if (!parse_binary(args[0].data.str.area, &binstr, &binstrlen, err)) |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3806 | return 0; |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3807 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3808 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3809 | args[0].data.str.area = binstr; |
| 3810 | args[0].data.str.data = binstrlen; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3811 | return 1; |
| 3812 | } |
| 3813 | |
| 3814 | static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3815 | { |
| 3816 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3817 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3818 | smp->data.u.str.area = args[0].data.str.area; |
| 3819 | smp->data.u.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3820 | return 1; |
| 3821 | } |
| 3822 | |
| 3823 | static int smp_check_const_meth(struct arg *args, char **err) |
| 3824 | { |
| 3825 | enum http_meth_t meth; |
| 3826 | int i; |
| 3827 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3828 | meth = find_http_meth(args[0].data.str.area, args[0].data.str.data); |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3829 | if (meth != HTTP_METH_OTHER) { |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 3830 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3831 | args[0].type = ARGT_SINT; |
| 3832 | args[0].data.sint = meth; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3833 | } else { |
Ilya Shipitsin | 6b79f38 | 2020-07-23 00:32:55 +0500 | [diff] [blame] | 3834 | /* Check method avalaibility. A method is a token defined as : |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3835 | * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
| 3836 | * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 3837 | * token = 1*tchar |
| 3838 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3839 | for (i = 0; i < args[0].data.str.data; i++) { |
| 3840 | if (!HTTP_IS_TOKEN(args[0].data.str.area[i])) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3841 | memprintf(err, "expects valid method."); |
| 3842 | return 0; |
| 3843 | } |
| 3844 | } |
| 3845 | } |
| 3846 | return 1; |
| 3847 | } |
| 3848 | |
| 3849 | static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3850 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3851 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 3852 | if (args[0].type == ARGT_SINT) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3853 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3854 | smp->data.u.meth.meth = args[0].data.sint; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3855 | smp->data.u.meth.str.area = ""; |
| 3856 | smp->data.u.meth.str.data = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3857 | } else { |
| 3858 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 3859 | smp->data.u.meth.meth = HTTP_METH_OTHER; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3860 | smp->data.u.meth.str.area = args[0].data.str.area; |
| 3861 | smp->data.u.meth.str.data = args[0].data.str.data; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3862 | } |
| 3863 | return 1; |
| 3864 | } |
| 3865 | |
Luca Schimweg | 8a694b8 | 2019-09-10 15:42:52 +0200 | [diff] [blame] | 3866 | // This function checks the "uuid" sample's arguments. |
| 3867 | // Function won't get called when no parameter is specified (maybe a bug?) |
| 3868 | static int smp_check_uuid(struct arg *args, char **err) |
| 3869 | { |
| 3870 | if (!args[0].type) { |
| 3871 | args[0].type = ARGT_SINT; |
| 3872 | args[0].data.sint = 4; |
| 3873 | } |
| 3874 | else if (args[0].data.sint != 4) { |
| 3875 | memprintf(err, "Unsupported UUID version: '%lld'", args[0].data.sint); |
| 3876 | return 0; |
| 3877 | } |
| 3878 | |
| 3879 | return 1; |
| 3880 | } |
| 3881 | |
| 3882 | // Generate a RFC4122 UUID (default is v4 = fully random) |
| 3883 | static int smp_fetch_uuid(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 3884 | { |
| 3885 | if (args[0].data.sint == 4 || !args[0].type) { |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 3886 | ha_generate_uuid(&trash); |
Luca Schimweg | 8a694b8 | 2019-09-10 15:42:52 +0200 | [diff] [blame] | 3887 | smp->data.type = SMP_T_STR; |
| 3888 | smp->flags = SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 3889 | smp->data.u.str = trash; |
| 3890 | return 1; |
| 3891 | } |
| 3892 | |
| 3893 | // more implementations of other uuid formats possible here |
| 3894 | return 0; |
| 3895 | } |
| 3896 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3897 | /* Note: must not be declared <const> as its list will be overwritten. |
| 3898 | * Note: fetches that may return multiple types must be declared as the lowest |
| 3899 | * common denominator, the type that can be casted into all other ones. For |
| 3900 | * instance IPv4/IPv6 must be declared IPv4. |
| 3901 | */ |
| 3902 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
Willy Tarreau | 0209c97 | 2021-03-26 12:03:11 +0100 | [diff] [blame] | 3903 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_CONST }, |
| 3904 | { "always_true", smp_fetch_true, 0, NULL, SMP_T_BOOL, SMP_USE_CONST }, |
| 3905 | { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_CONST }, |
| 3906 | { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST }, |
| 3907 | { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, |
| 3908 | { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST }, |
| 3909 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST }, |
| 3910 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, |
| 3911 | { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, |
| 3912 | { "rand", smp_fetch_rand, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_CONST }, |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 3913 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Willy Tarreau | 0209c97 | 2021-03-26 12:03:11 +0100 | [diff] [blame] | 3914 | { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST }, |
Willy Tarreau | 70fe944 | 2018-11-22 16:07:39 +0100 | [diff] [blame] | 3915 | |
| 3916 | { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3917 | { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3918 | { "cpu_ns_tot", smp_fetch_cpu_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3919 | { "lat_ns_avg", smp_fetch_lat_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 3920 | { "lat_ns_tot", smp_fetch_lat_ns_tot, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3921 | |
Willy Tarreau | 0209c97 | 2021-03-26 12:03:11 +0100 | [diff] [blame] | 3922 | { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_CONST }, |
| 3923 | { "bool", smp_fetch_const_bool, ARG1(1,STR), smp_check_const_bool, SMP_T_BOOL, SMP_USE_CONST }, |
| 3924 | { "int", smp_fetch_const_int, ARG1(1,SINT), NULL , SMP_T_SINT, SMP_USE_CONST }, |
| 3925 | { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_CONST }, |
| 3926 | { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_CONST }, |
| 3927 | { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_CONST }, |
| 3928 | { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_CONST }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 3929 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 3930 | { /* END */ }, |
| 3931 | }}; |
| 3932 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 3933 | INITCALL1(STG_REGISTER, sample_register_fetches, &smp_kws); |
| 3934 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 3935 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 3936 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Moemen MHEDHBI | 848216f | 2021-04-02 01:05:07 +0200 | [diff] [blame] | 3937 | { "debug", sample_conv_debug, ARG2(0,STR,STR), smp_check_debug, SMP_T_ANY, SMP_T_ANY }, |
| 3938 | { "b64dec", sample_conv_base642bin, 0, NULL, SMP_T_STR, SMP_T_BIN }, |
| 3939 | { "base64", sample_conv_bin2base64, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
| 3940 | { "ub64enc", sample_conv_bin2base64url,0, NULL, SMP_T_BIN, SMP_T_STR }, |
| 3941 | { "ub64dec", sample_conv_base64url2bin,0, NULL, SMP_T_STR, SMP_T_BIN }, |
| 3942 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 3943 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 3944 | { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
Marcin Deranek | 40ca09c | 2021-07-13 14:05:24 +0200 | [diff] [blame] | 3945 | { "be2dec", sample_conv_be2dec, ARG3(1,STR,SINT,SINT), sample_conv_be2dec_check, SMP_T_BIN, SMP_T_STR }, |
Marcin Deranek | da0264a | 2021-07-13 14:08:56 +0200 | [diff] [blame] | 3946 | { "be2hex", sample_conv_be2hex, ARG3(1,STR,SINT,SINT), sample_conv_be2hex_check, SMP_T_BIN, SMP_T_STR }, |
Moemen MHEDHBI | 848216f | 2021-04-02 01:05:07 +0200 | [diff] [blame] | 3947 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
| 3948 | { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
| 3949 | { "ipmask", sample_conv_ipmask, ARG2(1,MSK4,MSK6), NULL, SMP_T_ADDR, SMP_T_IPV4 }, |
| 3950 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
| 3951 | { "utime", sample_conv_utime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
| 3952 | { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3953 | { "crc32c", sample_conv_crc32c, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3954 | { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3955 | { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3956 | { "wt6", sample_conv_wt6, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3957 | { "xxh3", sample_conv_xxh3, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3958 | { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3959 | { "xxh64", sample_conv_xxh64, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 3960 | { "json", sample_conv_json, ARG1(1,STR), sample_conv_json_check, SMP_T_STR, SMP_T_STR }, |
| 3961 | { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
| 3962 | { "field", sample_conv_field, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
| 3963 | { "word", sample_conv_word, ARG3(2,SINT,STR,SINT), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
| 3964 | { "regsub", sample_conv_regsub, ARG3(2,REG,STR,STR), sample_conv_regsub_check, SMP_T_STR, SMP_T_STR }, |
| 3965 | { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN }, |
Willy Tarreau | 99ea188 | 2021-10-06 15:37:17 +0200 | [diff] [blame] | 3966 | { "concat", sample_conv_concat, ARG3(1,STR,STR,STR), smp_check_concat, SMP_T_STR, SMP_T_STR }, |
| 3967 | { "strcmp", sample_conv_strcmp, ARG1(1,STR), smp_check_strcmp, SMP_T_STR, SMP_T_SINT }, |
Frédéric Lécaille | 50290fb | 2019-02-27 14:34:51 +0100 | [diff] [blame] | 3968 | |
| 3969 | /* gRPC converters. */ |
Frédéric Lécaille | bfe6138 | 2019-03-06 14:34:36 +0100 | [diff] [blame] | 3970 | { "ungrpc", sample_conv_ungrpc, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN }, |
| 3971 | { "protobuf", sample_conv_protobuf, ARG2(1,PBUF_FNUM,STR), sample_conv_protobuf_check, SMP_T_BIN, SMP_T_BIN }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3972 | |
Baptiste Assmann | e138dda | 2020-10-22 15:39:03 +0200 | [diff] [blame] | 3973 | /* FIX converters */ |
| 3974 | { "fix_is_valid", sample_conv_fix_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL }, |
| 3975 | { "fix_tag_value", sample_conv_fix_tag_value, ARG1(1,STR), sample_conv_fix_value_check, SMP_T_BIN, SMP_T_BIN }, |
| 3976 | |
Baptiste Assmann | e279ca6 | 2020-10-27 18:10:06 +0100 | [diff] [blame] | 3977 | /* MQTT converters */ |
| 3978 | { "mqtt_is_valid", sample_conv_mqtt_is_valid, 0, NULL, SMP_T_BIN, SMP_T_BOOL }, |
| 3979 | { "mqtt_field_value", sample_conv_mqtt_field_value, ARG2(2,STR,STR), sample_conv_mqtt_field_value_check, SMP_T_BIN, SMP_T_STR }, |
| 3980 | |
Tim Duesterhus | 3943e4f | 2020-09-11 14:25:23 +0200 | [diff] [blame] | 3981 | { "iif", sample_conv_iif, ARG2(2, STR, STR), NULL, SMP_T_BOOL, SMP_T_STR }, |
| 3982 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 3983 | { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3984 | { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3985 | { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3986 | { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
| 3987 | { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3988 | { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3989 | { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3990 | { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 3991 | { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3992 | { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3993 | { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3994 | { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3995 | { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 3996 | { "neg", sample_conv_arith_neg, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 3997 | |
Christopher Faulet | ea159d6 | 2020-04-01 16:21:44 +0200 | [diff] [blame] | 3998 | { "htonl", sample_conv_htonl, 0, NULL, SMP_T_SINT, SMP_T_BIN }, |
| 3999 | { "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Christopher Faulet | 51fc9d1 | 2020-04-01 17:24:41 +0200 | [diff] [blame] | 4000 | { "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Christopher Faulet | 568415a | 2020-04-01 17:24:47 +0200 | [diff] [blame] | 4001 | { "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR, SMP_T_STR }, |
Alex | 51c8ad4 | 2021-04-15 16:45:15 +0200 | [diff] [blame] | 4002 | { "json_query", sample_conv_json_query, ARG2(1,STR,STR), sample_check_json_query , SMP_T_STR, SMP_T_ANY }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 4003 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 4004 | }}; |
| 4005 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 4006 | INITCALL1(STG_REGISTER, sample_register_convs, &sample_conv_kws); |