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