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