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