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