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