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 | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 19 | #include <types/global.h> |
| 20 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 21 | #include <common/chunk.h> |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 22 | #include <common/hash.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 23 | #include <common/standard.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 24 | #include <common/uri_auth.h> |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 25 | #include <common/base64.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 27 | #include <proto/arg.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 28 | #include <proto/auth.h> |
| 29 | #include <proto/log.h> |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 30 | #include <proto/proto_http.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 31 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 32 | #include <proto/sample.h> |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 33 | #include <proto/stick_table.h> |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 34 | #include <proto/vars.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 35 | |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 36 | #include <import/sha1.h> |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 37 | #include <import/xxhash.h> |
| 38 | |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 39 | /* sample type names */ |
| 40 | const char *smp_to_type[SMP_TYPES] = { |
Thierry FOURNIER | 9c627e8 | 2015-06-03 20:12:35 +0200 | [diff] [blame] | 41 | [SMP_T_ANY] = "any", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 42 | [SMP_T_BOOL] = "bool", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 43 | [SMP_T_SINT] = "sint", |
| 44 | [SMP_T_ADDR] = "addr", |
| 45 | [SMP_T_IPV4] = "ipv4", |
| 46 | [SMP_T_IPV6] = "ipv6", |
| 47 | [SMP_T_STR] = "str", |
| 48 | [SMP_T_BIN] = "bin", |
Thierry FOURNIER | 4c2479e | 2015-06-03 20:12:04 +0200 | [diff] [blame] | 49 | [SMP_T_METH] = "meth", |
Willy Tarreau | 1cf8f08 | 2014-02-07 12:14:54 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 52 | /* static sample used in sample_process() when <p> is NULL */ |
Emeric Brun | e5c918b | 2017-06-14 14:15:36 +0200 | [diff] [blame] | 53 | static THREAD_LOCAL struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 54 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 55 | /* list head of all known sample fetch keywords */ |
| 56 | static struct sample_fetch_kw_list sample_fetches = { |
| 57 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 58 | }; |
| 59 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 60 | /* list head of all known sample format conversion keywords */ |
| 61 | static struct sample_conv_kw_list sample_convs = { |
| 62 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 65 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
| 66 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 67 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 68 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 69 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 70 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 71 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 72 | SMP_VAL_FE_LOG_END), |
| 73 | |
| 74 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 75 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 76 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 77 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 78 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 79 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 80 | SMP_VAL_FE_LOG_END), |
| 81 | |
| 82 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 83 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 84 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 85 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 86 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 87 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 88 | SMP_VAL_FE_LOG_END), |
| 89 | |
| 90 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 91 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 92 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 93 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 94 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 95 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 96 | SMP_VAL_FE_LOG_END), |
| 97 | |
| 98 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 99 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 100 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 101 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 102 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 103 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 104 | SMP_VAL_FE_LOG_END), |
| 105 | |
| 106 | [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 107 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 108 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 109 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 110 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 111 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 112 | SMP_VAL_FE_LOG_END), |
| 113 | |
| 114 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 115 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 116 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 117 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 118 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 119 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 120 | SMP_VAL___________), |
| 121 | |
| 122 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 123 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 124 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 125 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 126 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 127 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 128 | SMP_VAL___________), |
| 129 | |
| 130 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 131 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 132 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 133 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 134 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 135 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 136 | SMP_VAL_FE_LOG_END), |
| 137 | |
| 138 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 139 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 140 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 141 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 142 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 143 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 144 | SMP_VAL___________), |
| 145 | |
| 146 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 147 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 148 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 149 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 150 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 151 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 152 | SMP_VAL_FE_LOG_END), |
| 153 | |
| 154 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 155 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 156 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 157 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 158 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 159 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 160 | SMP_VAL_FE_LOG_END), |
| 161 | |
| 162 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 163 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 164 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 165 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 166 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 167 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 168 | SMP_VAL_FE_LOG_END), |
| 169 | |
| 170 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 171 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 172 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 173 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 174 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 175 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 176 | SMP_VAL_FE_LOG_END), |
| 177 | |
| 178 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 179 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 180 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 181 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 182 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 183 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 184 | SMP_VAL___________), |
| 185 | |
| 186 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 187 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 188 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 189 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 190 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 191 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 192 | SMP_VAL___________), |
| 193 | |
| 194 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 195 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 196 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 197 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 198 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 199 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 200 | SMP_VAL_FE_LOG_END), |
| 201 | |
| 202 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 203 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 204 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 205 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 206 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 207 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 208 | SMP_VAL___________), |
| 209 | |
| 210 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 211 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 212 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 213 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 214 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 215 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 216 | SMP_VAL_FE_LOG_END), |
| 217 | |
| 218 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 219 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 220 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 221 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 222 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 223 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 224 | SMP_VAL_FE_LOG_END), |
| 225 | |
| 226 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 227 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 228 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 229 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 230 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 231 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 232 | SMP_VAL_FE_LOG_END), |
| 233 | |
| 234 | [SMP_SRC_SSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 235 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 236 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 237 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 238 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 239 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 240 | SMP_VAL_FE_LOG_END), |
| 241 | }; |
| 242 | |
| 243 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 244 | [SMP_SRC_INTRN] = "internal state", |
| 245 | [SMP_SRC_LISTN] = "listener", |
| 246 | [SMP_SRC_FTEND] = "frontend", |
| 247 | [SMP_SRC_L4CLI] = "client address", |
| 248 | [SMP_SRC_L5CLI] = "client-side connection", |
| 249 | [SMP_SRC_TRACK] = "track counters", |
| 250 | [SMP_SRC_L6REQ] = "request buffer", |
| 251 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 252 | [SMP_SRC_HRQHP] = "HTTP request", |
| 253 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 254 | [SMP_SRC_BKEND] = "backend", |
| 255 | [SMP_SRC_SERVR] = "server", |
| 256 | [SMP_SRC_L4SRV] = "server address", |
| 257 | [SMP_SRC_L5SRV] = "server-side connection", |
| 258 | [SMP_SRC_L6RES] = "response buffer", |
| 259 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 260 | [SMP_SRC_HRSHP] = "HTTP response", |
| 261 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 262 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 263 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 264 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 265 | [SMP_SRC_SSFIN] = "session statistics", |
| 266 | }; |
| 267 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 268 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 269 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 270 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 271 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 272 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 273 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 274 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 275 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 276 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 277 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 278 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 279 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 280 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 281 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 282 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 283 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 284 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 285 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 286 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 287 | [SMP_CKP_FE_LOG_END] = "logs", |
| 288 | }; |
| 289 | |
Thierry FOURNIER | 348971e | 2013-11-21 10:50:10 +0100 | [diff] [blame] | 290 | /* This function returns the type of the data returned by the sample_expr. |
| 291 | * It assumes that the <expr> and all of its converters are properly |
| 292 | * initialized. |
| 293 | */ |
| 294 | inline |
| 295 | int smp_expr_output_type(struct sample_expr *expr) |
| 296 | { |
| 297 | struct sample_conv_expr *smp_expr; |
| 298 | |
| 299 | if (!LIST_ISEMPTY(&expr->conv_exprs)) { |
| 300 | smp_expr = LIST_PREV(&expr->conv_exprs, struct sample_conv_expr *, list); |
| 301 | return smp_expr->conv->out_type; |
| 302 | } |
| 303 | return expr->fetch->out_type; |
| 304 | } |
| 305 | |
| 306 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 307 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 308 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 309 | * value is the pointer to the string in the trash buffer. |
| 310 | */ |
| 311 | const char *sample_src_names(unsigned int use) |
| 312 | { |
| 313 | int bit; |
| 314 | |
| 315 | trash.len = 0; |
| 316 | trash.str[0] = '\0'; |
| 317 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 318 | if (!(use & ~((1 << bit) - 1))) |
| 319 | break; /* no more bits */ |
| 320 | |
| 321 | if (!(use & (1 << bit))) |
| 322 | continue; /* bit not set */ |
| 323 | |
| 324 | trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s", |
| 325 | (use & ((1 << bit) - 1)) ? "," : "", |
| 326 | fetch_src_names[bit]); |
| 327 | } |
| 328 | return trash.str; |
| 329 | } |
| 330 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 331 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 332 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 333 | * if set. |
| 334 | */ |
| 335 | const char *sample_ckp_names(unsigned int use) |
| 336 | { |
| 337 | int bit; |
| 338 | |
| 339 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 340 | if (use & (1 << bit)) |
| 341 | return fetch_ckp_names[bit]; |
| 342 | return "unknown sample check place, please report this bug"; |
| 343 | } |
| 344 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 345 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 346 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 347 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 348 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 349 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 350 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 351 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 352 | struct sample_fetch *sf; |
| 353 | int bit; |
| 354 | |
| 355 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 356 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 357 | if (sf->use & (1 << bit)) |
| 358 | sf->val |= fetch_cap[bit]; |
| 359 | } |
| 360 | LIST_ADDQ(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 364 | * 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] | 365 | * parsing sessions. |
| 366 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 367 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 368 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 369 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 373 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 374 | * string of <len> in buffer <kw>. |
| 375 | * |
| 376 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 377 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 378 | { |
| 379 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 380 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 381 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 382 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 383 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 384 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 385 | kwl->kw[index].kw[len] == '\0') |
| 386 | return &kwl->kw[index]; |
| 387 | } |
| 388 | } |
| 389 | return NULL; |
| 390 | } |
| 391 | |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 392 | /* This function browses the list of available sample fetches. <current> is |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 393 | * 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] | 394 | * <idx> is the index of the next sample fetch entry. It is used as private |
| 395 | * value. It is useless to initiate it. |
Thierry FOURNIER | 4d9a1d1 | 2014-12-08 14:49:19 +0100 | [diff] [blame] | 396 | * |
Tim Duesterhus | c555ee0 | 2018-01-25 16:24:44 +0100 | [diff] [blame] | 397 | * 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] | 398 | * the list is reached. |
| 399 | */ |
| 400 | struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx) |
| 401 | { |
| 402 | struct sample_fetch_kw_list *kwl; |
| 403 | struct sample_fetch *base; |
| 404 | |
| 405 | if (!current) { |
| 406 | /* Get first kwl entry. */ |
| 407 | kwl = LIST_NEXT(&sample_fetches.list, struct sample_fetch_kw_list *, list); |
| 408 | (*idx) = 0; |
| 409 | } else { |
| 410 | /* Get kwl corresponding to the curret entry. */ |
| 411 | base = current + 1 - (*idx); |
| 412 | kwl = container_of(base, struct sample_fetch_kw_list, kw); |
| 413 | } |
| 414 | |
| 415 | while (1) { |
| 416 | |
| 417 | /* Check if kwl is the last entry. */ |
| 418 | if (&kwl->list == &sample_fetches.list) |
| 419 | return NULL; |
| 420 | |
| 421 | /* idx contain the next keyword. If it is available, return it. */ |
| 422 | if (kwl->kw[*idx].kw) { |
| 423 | (*idx)++; |
| 424 | return &kwl->kw[(*idx)-1]; |
| 425 | } |
| 426 | |
| 427 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 428 | kwl = LIST_NEXT(&kwl->list, struct sample_fetch_kw_list *, list); |
| 429 | |
| 430 | /* Set index to 0, ans do one other loop. */ |
| 431 | (*idx) = 0; |
| 432 | } |
| 433 | } |
| 434 | |
Thierry FOURNIER | 8fd1376 | 2015-03-10 23:56:48 +0100 | [diff] [blame] | 435 | /* This function browses the list of available converters. <current> is |
| 436 | * the last used converter. If it is the first call, it must set to NULL. |
| 437 | * <idx> is the index of the next converter entry. It is used as private |
| 438 | * value. It is useless to initiate it. |
| 439 | * |
| 440 | * It returns always the next sample_conv entry, and NULL when the end of |
| 441 | * the list is reached. |
| 442 | */ |
| 443 | struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx) |
| 444 | { |
| 445 | struct sample_conv_kw_list *kwl; |
| 446 | struct sample_conv *base; |
| 447 | |
| 448 | if (!current) { |
| 449 | /* Get first kwl entry. */ |
| 450 | kwl = LIST_NEXT(&sample_convs.list, struct sample_conv_kw_list *, list); |
| 451 | (*idx) = 0; |
| 452 | } else { |
| 453 | /* Get kwl corresponding to the curret entry. */ |
| 454 | base = current + 1 - (*idx); |
| 455 | kwl = container_of(base, struct sample_conv_kw_list, kw); |
| 456 | } |
| 457 | |
| 458 | while (1) { |
| 459 | /* Check if kwl is the last entry. */ |
| 460 | if (&kwl->list == &sample_convs.list) |
| 461 | return NULL; |
| 462 | |
| 463 | /* idx contain the next keyword. If it is available, return it. */ |
| 464 | if (kwl->kw[*idx].kw) { |
| 465 | (*idx)++; |
| 466 | return &kwl->kw[(*idx)-1]; |
| 467 | } |
| 468 | |
| 469 | /* get next entry in the main list, and return NULL if the end is reached. */ |
| 470 | kwl = LIST_NEXT(&kwl->list, struct sample_conv_kw_list *, list); |
| 471 | |
| 472 | /* Set index to 0, ans do one other loop. */ |
| 473 | (*idx) = 0; |
| 474 | } |
| 475 | } |
| 476 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 477 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 478 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 479 | * string of <len> in buffer <kw>. |
| 480 | * |
| 481 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 482 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 483 | { |
| 484 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 485 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 486 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 487 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 488 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 489 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 490 | kwl->kw[index].kw[len] == '\0') |
| 491 | return &kwl->kw[index]; |
| 492 | } |
| 493 | } |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 497 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 498 | /* Sample casts functions */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 499 | /******************************************************************/ |
| 500 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 501 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 502 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 503 | smp->data.u.sint = ntohl(smp->data.u.ipv4.s_addr); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 504 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 505 | return 1; |
| 506 | } |
| 507 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 508 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 509 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 510 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 511 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 512 | if (!inet_ntop(AF_INET, (void *)&smp->data.u.ipv4, trash->str, trash->size)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 513 | return 0; |
| 514 | |
| 515 | trash->len = strlen(trash->str); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 516 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 517 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 518 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 519 | |
| 520 | return 1; |
| 521 | } |
| 522 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 523 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 524 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 525 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 526 | smp->data.type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 527 | return 1; |
| 528 | } |
| 529 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 530 | static int c_ipv62ip(struct sample *smp) |
| 531 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 532 | if (!v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6)) |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 533 | return 0; |
Tim Duesterhus | bf5ce02 | 2018-01-25 16:24:46 +0100 | [diff] [blame] | 534 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 535 | return 1; |
| 536 | } |
| 537 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 538 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 539 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 540 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 541 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 542 | if (!inet_ntop(AF_INET6, (void *)&smp->data.u.ipv6, trash->str, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | trash->len = strlen(trash->str); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 546 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 547 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 548 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 553 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 554 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 555 | return v6tov4(&smp->data.u.ipv4, &smp->data.u.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 556 | } |
| 557 | */ |
| 558 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 559 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 560 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 561 | 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] | 562 | smp->data.type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 563 | return 1; |
| 564 | } |
| 565 | |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 566 | static int c_int2ipv6(struct sample *smp) |
| 567 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 568 | smp->data.u.ipv4.s_addr = htonl((unsigned int)smp->data.u.sint); |
| 569 | v4tov6(&smp->data.u.ipv6, &smp->data.u.ipv4); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 570 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | cc4d171 | 2015-07-24 09:04:56 +0200 | [diff] [blame] | 571 | return 1; |
| 572 | } |
| 573 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 574 | static int c_str2addr(struct sample *smp) |
| 575 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 576 | if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) { |
| 577 | if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 578 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 579 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 580 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 581 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 582 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 583 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 584 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 585 | return 1; |
| 586 | } |
| 587 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 588 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 589 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 590 | if (!buf2ip(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 591 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 592 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 593 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 594 | return 1; |
| 595 | } |
| 596 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 597 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 598 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 599 | if (!buf2ip6(smp->data.u.str.str, smp->data.u.str.len, &smp->data.u.ipv6)) |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 600 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 601 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 602 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 603 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 604 | } |
| 605 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 606 | /* |
| 607 | * The NULL char always enforces the end of string if it is met. |
| 608 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 609 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 610 | static int c_bin2str(struct sample *smp) |
| 611 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 612 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 613 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 614 | for (i = 0; i < smp->data.u.str.len; i++) { |
| 615 | if (!smp->data.u.str.str[i]) { |
| 616 | smp->data.u.str.len = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 617 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 618 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 619 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 620 | return 1; |
| 621 | } |
| 622 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 623 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 624 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 625 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 626 | char *pos; |
| 627 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 628 | pos = lltoa_r(smp->data.u.sint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 629 | if (!pos) |
| 630 | return 0; |
| 631 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 632 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 633 | trash->str = pos; |
| 634 | trash->len = strlen(pos); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 635 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 636 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 637 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 638 | return 1; |
| 639 | } |
| 640 | |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 641 | /* This function inconditionally duplicates data and removes the "const" flag. |
| 642 | * For strings and binary blocks, it also provides a known allocated size with |
| 643 | * a length that is capped to the size, and ensures a trailing zero is always |
| 644 | * appended for strings. This is necessary for some operations which may |
| 645 | * require to extend the length. It returns 0 if it fails, 1 on success. |
| 646 | */ |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 647 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 648 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 649 | struct chunk *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 650 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 651 | switch (smp->data.type) { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 652 | case SMP_T_BOOL: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 653 | case SMP_T_SINT: |
| 654 | case SMP_T_ADDR: |
| 655 | case SMP_T_IPV4: |
| 656 | case SMP_T_IPV6: |
| 657 | /* These type are not const. */ |
| 658 | break; |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 659 | |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 660 | case SMP_T_METH: |
| 661 | if (smp->data.u.meth.meth != HTTP_METH_OTHER) |
| 662 | break; |
| 663 | /* Fall through */ |
| 664 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 665 | case SMP_T_STR: |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 666 | trash = get_trash_chunk(); |
| 667 | trash->len = smp->data.u.str.len; |
| 668 | if (trash->len > trash->size - 1) |
| 669 | trash->len = trash->size - 1; |
| 670 | |
| 671 | memcpy(trash->str, smp->data.u.str.str, trash->len); |
| 672 | trash->str[trash->len] = 0; |
| 673 | smp->data.u.str = *trash; |
| 674 | break; |
| 675 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 676 | case SMP_T_BIN: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 677 | trash = get_trash_chunk(); |
Willy Tarreau | ad63582 | 2016-08-08 19:21:09 +0200 | [diff] [blame] | 678 | trash->len = smp->data.u.str.len; |
| 679 | if (trash->len > trash->size) |
| 680 | trash->len = trash->size; |
| 681 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 682 | memcpy(trash->str, smp->data.u.str.str, trash->len); |
| 683 | smp->data.u.str = *trash; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 684 | break; |
Christopher Faulet | ec10051 | 2017-07-24 15:38:41 +0200 | [diff] [blame] | 685 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 686 | default: |
| 687 | /* Other cases are unexpected. */ |
| 688 | return 0; |
| 689 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 690 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 691 | /* remove const flag */ |
| 692 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 693 | return 1; |
| 694 | } |
| 695 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 696 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 697 | { |
| 698 | return 1; |
| 699 | } |
| 700 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 701 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 702 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 703 | const char *str; |
| 704 | const char *end; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 705 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 706 | if (smp->data.u.str.len == 0) |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 707 | return 0; |
| 708 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 709 | str = smp->data.u.str.str; |
| 710 | end = smp->data.u.str.str + smp->data.u.str.len; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 711 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 712 | smp->data.u.sint = read_int64(&str, end); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 713 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 714 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 715 | return 1; |
| 716 | } |
| 717 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 718 | static int c_str2meth(struct sample *smp) |
| 719 | { |
| 720 | enum http_meth_t meth; |
| 721 | int len; |
| 722 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | meth = find_http_meth(smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 724 | if (meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 725 | len = smp->data.u.str.len; |
| 726 | smp->data.u.meth.str.str = smp->data.u.str.str; |
| 727 | smp->data.u.meth.str.len = len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 728 | } |
| 729 | else |
| 730 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 731 | smp->data.u.meth.meth = meth; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 732 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 733 | return 1; |
| 734 | } |
| 735 | |
| 736 | static int c_meth2str(struct sample *smp) |
| 737 | { |
| 738 | int len; |
| 739 | enum http_meth_t meth; |
| 740 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 741 | if (smp->data.u.meth.meth == HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 742 | /* The method is unknown. Copy the original pointer. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 743 | len = smp->data.u.meth.str.len; |
| 744 | smp->data.u.str.str = smp->data.u.meth.str.str; |
| 745 | smp->data.u.str.len = len; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 746 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 747 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 748 | else if (smp->data.u.meth.meth < HTTP_METH_OTHER) { |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 749 | /* The method is known, copy the pointer containing the string. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 750 | meth = smp->data.u.meth.meth; |
| 751 | smp->data.u.str.str = http_known_methods[meth].name; |
| 752 | smp->data.u.str.len = http_known_methods[meth].len; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 753 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 754 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 755 | } |
| 756 | else { |
| 757 | /* Unknown method */ |
| 758 | return 0; |
| 759 | } |
| 760 | return 1; |
| 761 | } |
| 762 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 763 | static int c_addr2bin(struct sample *smp) |
| 764 | { |
| 765 | struct chunk *chk = get_trash_chunk(); |
| 766 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 767 | if (smp->data.type == SMP_T_IPV4) { |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 768 | chk->len = 4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 769 | memcpy(chk->str, &smp->data.u.ipv4, chk->len); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 770 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 771 | else if (smp->data.type == SMP_T_IPV6) { |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 772 | chk->len = 16; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 773 | memcpy(chk->str, &smp->data.u.ipv6, chk->len); |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 774 | } |
| 775 | else |
| 776 | return 0; |
| 777 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 778 | smp->data.u.str = *chk; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 779 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 780 | return 1; |
| 781 | } |
| 782 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 783 | static int c_int2bin(struct sample *smp) |
| 784 | { |
| 785 | struct chunk *chk = get_trash_chunk(); |
| 786 | |
Willy Tarreau | 5f6e905 | 2016-05-20 06:29:59 +0200 | [diff] [blame] | 787 | *(unsigned long long int *)chk->str = my_htonll(smp->data.u.sint); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 788 | chk->len = 8; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 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 | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 792 | return 1; |
| 793 | } |
| 794 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 795 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 796 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 797 | /* Sample casts matrix: */ |
| 798 | /* sample_casts[from type][to type] */ |
| 799 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 800 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 801 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 802 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 803 | /* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 804 | /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, }, |
| 805 | /* 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] | 806 | /* 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] | 807 | /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
| 808 | /* 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] | 809 | /* 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] | 810 | /* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 811 | /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 812 | /* 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] | 813 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 814 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 815 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 816 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 817 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 818 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 819 | * The caller must have set al->ctx. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 820 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 821 | struct sample_expr *sample_parse_expr(char **str, int *idx, const char *file, int line, char **err_msg, struct arg_list *al) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 822 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 823 | const char *begw; /* beginning of word */ |
| 824 | const char *endw; /* end of word */ |
| 825 | const char *endt; /* end of term */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 826 | struct sample_expr *expr; |
| 827 | struct sample_fetch *fetch; |
| 828 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 829 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 830 | char *fkw = NULL; |
| 831 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 832 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 833 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 834 | begw = str[*idx]; |
| 835 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 836 | |
| 837 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 838 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 839 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 840 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 841 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 842 | /* keep a copy of the current fetch keyword for error reporting */ |
| 843 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 844 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 845 | fetch = find_sample_fetch(begw, endw - begw); |
| 846 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 847 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 848 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 849 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 850 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 851 | endt = endw; |
| 852 | if (*endt == '(') { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 853 | /* look for the end of this term and skip the opening parenthesis */ |
| 854 | endt = ++endw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 855 | while (*endt && *endt != ')') |
| 856 | endt++; |
| 857 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 858 | memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 859 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 860 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 861 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 862 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 863 | /* At this point, we have : |
| 864 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 865 | * - endw : end of the keyword, first character not part of keyword |
| 866 | * nor the opening parenthesis (so first character of args |
| 867 | * if present). |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 868 | * - endt : end of the term (=endw or last parenthesis if args are present) |
| 869 | */ |
| 870 | |
| 871 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 872 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 873 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 874 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 875 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 876 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 877 | expr = calloc(1, sizeof(*expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 878 | if (!expr) |
| 879 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 880 | |
| 881 | LIST_INIT(&(expr->conv_exprs)); |
| 882 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 883 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 884 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 885 | /* Note that we call the argument parser even with an empty string, |
| 886 | * this allows it to automatically create entries for mandatory |
| 887 | * implicit arguments (eg: local proxy name). |
| 888 | */ |
| 889 | al->kw = expr->fetch->kw; |
| 890 | al->conv = NULL; |
| 891 | if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 892 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 893 | goto out_error; |
| 894 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 895 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 896 | if (!expr->arg_p) { |
| 897 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 898 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 899 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 900 | 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] | 901 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 904 | /* Now process the converters if any. We have two supported syntaxes |
| 905 | * for the converters, which can be combined : |
| 906 | * - comma-delimited list of converters just after the keyword and args ; |
| 907 | * - one converter per keyword |
| 908 | * The combination allows to have each keyword being a comma-delimited |
| 909 | * series of converters. |
| 910 | * |
| 911 | * We want to process the former first, then the latter. For this we start |
| 912 | * from the beginning of the supposed place in the exiting conv chain, which |
| 913 | * starts at the last comma (endt). |
| 914 | */ |
| 915 | |
| 916 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 917 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 918 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 919 | if (*endt == ')') /* skip last closing parenthesis */ |
| 920 | endt++; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 921 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 922 | if (*endt && *endt != ',') { |
| 923 | if (ckw) |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 924 | memprintf(err_msg, "missing comma after converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 925 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 926 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 927 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 928 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 929 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 930 | while (*endt == ',') /* then trailing commas */ |
| 931 | endt++; |
| 932 | |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 933 | begw = endt; /* start of converter */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 934 | |
| 935 | if (!*begw) { |
| 936 | /* none ? skip to next string */ |
| 937 | (*idx)++; |
| 938 | begw = str[*idx]; |
| 939 | if (!begw || !*begw) |
| 940 | break; |
| 941 | } |
| 942 | |
| 943 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 944 | |
| 945 | free(ckw); |
| 946 | ckw = my_strndup(begw, endw - begw); |
| 947 | |
| 948 | conv = find_sample_conv(begw, endw - begw); |
| 949 | if (!conv) { |
| 950 | /* we found an isolated keyword that we don't know, it's not ours */ |
| 951 | if (begw == str[*idx]) |
| 952 | break; |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 953 | memprintf(err_msg, "unknown converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 954 | goto out_error; |
| 955 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 956 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 957 | endt = endw; |
| 958 | if (*endt == '(') { |
| 959 | /* look for the end of this term */ |
| 960 | while (*endt && *endt != ')') |
| 961 | endt++; |
| 962 | if (*endt != ')') { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 963 | memprintf(err_msg, "syntax error: missing ')' after converter '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 964 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 965 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 969 | memprintf(err_msg, "returns type of converter '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 970 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 971 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 972 | |
| 973 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 974 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 975 | memprintf(err_msg, "converter '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 976 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 977 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 978 | |
| 979 | prev_type = conv->out_type; |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 980 | conv_expr = calloc(1, sizeof(*conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 981 | if (!conv_expr) |
| 982 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 983 | |
| 984 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 985 | conv_expr->conv = conv; |
| 986 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 987 | if (endt != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 988 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 989 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 990 | if (!conv->arg_mask) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 991 | memprintf(err_msg, "converter '%s' does not support any args", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 992 | goto out_error; |
| 993 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 994 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 995 | al->kw = expr->fetch->kw; |
| 996 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 997 | if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 998 | memprintf(err_msg, "invalid arg %d in converter '%s' : %s", err_arg+1, ckw, *err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 999 | goto out_error; |
| 1000 | } |
| 1001 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 1002 | if (!conv_expr->arg_p) |
| 1003 | conv_expr->arg_p = empty_arg_list; |
| 1004 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 1005 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 1006 | memprintf(err_msg, "invalid args in converter '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1007 | goto out_error; |
| 1008 | } |
| 1009 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1010 | else if (ARGM(conv->arg_mask)) { |
Willy Tarreau | 97108e0 | 2016-11-25 07:33:24 +0100 | [diff] [blame] | 1011 | memprintf(err_msg, "missing args for converter '%s'", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1012 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1013 | } |
| 1014 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1015 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1016 | out: |
| 1017 | free(fkw); |
| 1018 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1019 | return expr; |
| 1020 | |
| 1021 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1022 | /* TODO: prune_sample_expr(expr); */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 1023 | expr = NULL; |
| 1024 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1028 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 1029 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1030 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 1031 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1032 | * 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] | 1033 | * 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] | 1034 | * |
| 1035 | * Note: the fetch functions are required to properly set the return type. The |
| 1036 | * conversion functions must do so too. However the cast functions do not need |
| 1037 | * 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] | 1038 | * |
| 1039 | * The caller may indicate in <opt> if it considers the result final or not. |
| 1040 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 1041 | * if the result is stable or not, according to the following table : |
| 1042 | * |
| 1043 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1044 | * NULL 0 * Not present and will never be (eg: header) |
| 1045 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 1046 | * NULL 1 1 Not present yet, will not change anymore |
| 1047 | * smp 0 * Present and will not change (eg: header) |
| 1048 | * smp 1 0 Present, may change (eg: request length) |
| 1049 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1050 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1051 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 1052 | struct stream *strm, unsigned int opt, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1053 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1054 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1055 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1056 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1057 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 1058 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1059 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1060 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1061 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 1062 | smp_set_owner(p, px, sess, strm, opt); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1063 | 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] | 1064 | return NULL; |
| 1065 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1066 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1067 | /* we want to ensure that p->type can be casted into |
| 1068 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 1069 | * - NULL => not castable. |
| 1070 | * - c_none => nothing to do (let's optimize it) |
| 1071 | * - other => apply cast and prepare to fail |
| 1072 | */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1073 | if (!sample_casts[p->data.type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1074 | return NULL; |
| 1075 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1076 | if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none && |
| 1077 | !sample_casts[p->data.type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1078 | return NULL; |
| 1079 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1080 | /* OK cast succeeded */ |
| 1081 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1082 | 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] | 1083 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1084 | } |
| 1085 | return p; |
| 1086 | } |
| 1087 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1088 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1089 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 1090 | * errors or 0 if everything is fine. |
| 1091 | */ |
| 1092 | int smp_resolve_args(struct proxy *p) |
| 1093 | { |
| 1094 | struct arg_list *cur, *bak; |
| 1095 | const char *ctx, *where; |
| 1096 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 1097 | struct userlist *ul; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1098 | struct my_regex *reg; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1099 | struct arg *arg; |
| 1100 | int cfgerr = 0; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1101 | int rflags; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1102 | |
| 1103 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 1104 | struct proxy *px; |
| 1105 | struct server *srv; |
| 1106 | char *pname, *sname; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1107 | char *err; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1108 | |
| 1109 | arg = cur->arg; |
| 1110 | |
| 1111 | /* prepare output messages */ |
| 1112 | conv_pre = conv_pos = conv_ctx = ""; |
| 1113 | if (cur->conv) { |
| 1114 | conv_ctx = cur->conv; |
| 1115 | conv_pre = "conversion keyword '"; |
| 1116 | conv_pos = "' for "; |
| 1117 | } |
| 1118 | |
| 1119 | where = "in"; |
| 1120 | ctx = "sample fetch keyword"; |
| 1121 | switch (cur->ctx) { |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 1122 | case ARGC_STK: where = "in stick rule in"; break; |
| 1123 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1124 | case ARGC_LOG: where = "in log-format string in"; break; |
| 1125 | case ARGC_LOGSD: where = "in log-format-sd string in"; break; |
| 1126 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1127 | case ARGC_HRS: where = "in http-response header format string in"; break; |
| 1128 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
| 1129 | case ARGC_RDR: where = "in redirect format string in"; break; |
| 1130 | case ARGC_CAP: where = "in capture rule in"; break; |
| 1131 | case ARGC_ACL: ctx = "ACL keyword"; break; |
| 1132 | case ARGC_SRV: where = "in server directive in"; break; |
Christopher Faulet | f7e4e7e | 2016-10-27 22:29:49 +0200 | [diff] [blame] | 1133 | case ARGC_SPOE: where = "in spoe-message directive in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* set a few default settings */ |
| 1137 | px = p; |
| 1138 | pname = p->id; |
| 1139 | |
| 1140 | switch (arg->type) { |
| 1141 | case ARGT_SRV: |
| 1142 | if (!arg->data.str.len) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1143 | ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1144 | cur->file, cur->line, |
| 1145 | 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] | 1146 | cfgerr++; |
| 1147 | continue; |
| 1148 | } |
| 1149 | |
| 1150 | /* we support two formats : "bck/srv" and "srv" */ |
| 1151 | sname = strrchr(arg->data.str.str, '/'); |
| 1152 | |
| 1153 | if (sname) { |
| 1154 | *sname++ = '\0'; |
| 1155 | pname = arg->data.str.str; |
| 1156 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1157 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1158 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1159 | ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1160 | cur->file, cur->line, pname, |
| 1161 | 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] | 1162 | cfgerr++; |
| 1163 | break; |
| 1164 | } |
| 1165 | } |
| 1166 | else |
| 1167 | sname = arg->data.str.str; |
| 1168 | |
| 1169 | srv = findserver(px, sname); |
| 1170 | if (!srv) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1171 | 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", |
| 1172 | cur->file, cur->line, sname, pname, |
| 1173 | 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] | 1174 | cfgerr++; |
| 1175 | break; |
| 1176 | } |
| 1177 | |
| 1178 | free(arg->data.str.str); |
| 1179 | arg->data.str.str = NULL; |
| 1180 | arg->unresolved = 0; |
| 1181 | arg->data.srv = srv; |
| 1182 | break; |
| 1183 | |
| 1184 | case ARGT_FE: |
| 1185 | if (arg->data.str.len) { |
| 1186 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1187 | px = proxy_fe_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1191 | ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1192 | cur->file, cur->line, pname, |
| 1193 | 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] | 1194 | cfgerr++; |
| 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | if (!(px->cap & PR_CAP_FE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1199 | 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", |
| 1200 | cur->file, cur->line, pname, |
| 1201 | 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] | 1202 | cfgerr++; |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | free(arg->data.str.str); |
| 1207 | arg->data.str.str = NULL; |
| 1208 | arg->unresolved = 0; |
| 1209 | arg->data.prx = px; |
| 1210 | break; |
| 1211 | |
| 1212 | case ARGT_BE: |
| 1213 | if (arg->data.str.len) { |
| 1214 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1215 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1219 | ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1220 | cur->file, cur->line, pname, |
| 1221 | 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] | 1222 | cfgerr++; |
| 1223 | break; |
| 1224 | } |
| 1225 | |
| 1226 | if (!(px->cap & PR_CAP_BE)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1227 | 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", |
| 1228 | cur->file, cur->line, pname, |
| 1229 | 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] | 1230 | cfgerr++; |
| 1231 | break; |
| 1232 | } |
| 1233 | |
| 1234 | free(arg->data.str.str); |
| 1235 | arg->data.str.str = NULL; |
| 1236 | arg->unresolved = 0; |
| 1237 | arg->data.prx = px; |
| 1238 | break; |
| 1239 | |
| 1240 | case ARGT_TAB: |
| 1241 | if (arg->data.str.len) { |
| 1242 | pname = arg->data.str.str; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 1243 | px = proxy_tbl_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | if (!px) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1247 | ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1248 | cur->file, cur->line, pname, |
| 1249 | 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] | 1250 | cfgerr++; |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | if (!px->table.size) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1255 | ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1256 | cur->file, cur->line, pname, |
| 1257 | 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] | 1258 | cfgerr++; |
| 1259 | break; |
| 1260 | } |
| 1261 | |
| 1262 | free(arg->data.str.str); |
| 1263 | arg->data.str.str = NULL; |
| 1264 | arg->unresolved = 0; |
| 1265 | arg->data.prx = px; |
| 1266 | break; |
| 1267 | |
| 1268 | case ARGT_USR: |
| 1269 | if (!arg->data.str.len) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1270 | ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1271 | cur->file, cur->line, |
| 1272 | 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] | 1273 | cfgerr++; |
| 1274 | break; |
| 1275 | } |
| 1276 | |
| 1277 | if (p->uri_auth && p->uri_auth->userlist && |
| 1278 | !strcmp(p->uri_auth->userlist->name, arg->data.str.str)) |
| 1279 | ul = p->uri_auth->userlist; |
| 1280 | else |
| 1281 | ul = auth_find_userlist(arg->data.str.str); |
| 1282 | |
| 1283 | if (!ul) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1284 | ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1285 | cur->file, cur->line, arg->data.str.str, |
| 1286 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1287 | cfgerr++; |
| 1288 | break; |
| 1289 | } |
| 1290 | |
| 1291 | free(arg->data.str.str); |
| 1292 | arg->data.str.str = NULL; |
| 1293 | arg->unresolved = 0; |
| 1294 | arg->data.usr = ul; |
| 1295 | break; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1296 | |
| 1297 | case ARGT_REG: |
| 1298 | if (!arg->data.str.len) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1299 | ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1300 | cur->file, cur->line, |
| 1301 | 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] | 1302 | cfgerr++; |
| 1303 | continue; |
| 1304 | } |
| 1305 | |
| 1306 | reg = calloc(1, sizeof(*reg)); |
| 1307 | if (!reg) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1308 | ha_alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1309 | cur->file, cur->line, |
| 1310 | 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] | 1311 | cfgerr++; |
| 1312 | continue; |
| 1313 | } |
| 1314 | |
| 1315 | rflags = 0; |
| 1316 | rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0; |
| 1317 | err = NULL; |
| 1318 | |
Willy Tarreau | d817e46 | 2015-01-23 20:23:17 +0100 | [diff] [blame] | 1319 | if (!regex_comp(arg->data.str.str, reg, !(rflags & REG_ICASE), 1 /* capture substr */, &err)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1320 | ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n", |
| 1321 | cur->file, cur->line, |
| 1322 | arg->data.str.str, |
| 1323 | 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] | 1324 | cfgerr++; |
| 1325 | continue; |
| 1326 | } |
| 1327 | |
| 1328 | free(arg->data.str.str); |
| 1329 | arg->data.str.str = NULL; |
| 1330 | arg->unresolved = 0; |
| 1331 | arg->data.reg = reg; |
| 1332 | break; |
| 1333 | |
| 1334 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | LIST_DEL(&cur->list); |
| 1338 | free(cur); |
| 1339 | } /* end of args processing */ |
| 1340 | |
| 1341 | return cfgerr; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1345 | * Process a fetch + format conversion as defined by the sample expression |
| 1346 | * <expr> on request or response considering the <opt> parameter. The output is |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1347 | * not explicitly set to <smp_type>, but shall be compatible with it as |
| 1348 | * specified by 'sample_casts' table. If a stable sample can be fetched, or an |
| 1349 | * 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] | 1350 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1351 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1352 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1353 | * 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] | 1354 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1355 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1356 | * still hope to get it after waiting longer, and is not converted to string. |
| 1357 | * The possible output combinations are the following : |
| 1358 | * |
| 1359 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1360 | * NULL * * Not present and will never be (eg: header) |
| 1361 | * smp 0 * Final value converted (eg: header) |
| 1362 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1363 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1364 | */ |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1365 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1366 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1367 | struct sample_expr *expr, int smp_type) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1368 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1369 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1370 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1371 | memset(smp, 0, sizeof(*smp)); |
| 1372 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1373 | if (!sample_process(px, sess, strm, opt, expr, smp)) { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1374 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1375 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1376 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1377 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1378 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1379 | if (!sample_casts[smp->data.type][smp_type]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1380 | return NULL; |
| 1381 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1382 | if (!sample_casts[smp->data.type][smp_type](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1383 | return NULL; |
| 1384 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1385 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1386 | return smp; |
| 1387 | } |
| 1388 | |
Christopher Faulet | 476e5d0 | 2016-10-26 11:34:47 +0200 | [diff] [blame] | 1389 | static void release_sample_arg(struct arg *p) |
| 1390 | { |
| 1391 | struct arg *p_back = p; |
| 1392 | |
| 1393 | if (!p) |
| 1394 | return; |
| 1395 | |
| 1396 | while (p->type != ARGT_STOP) { |
| 1397 | if (p->type == ARGT_STR || p->unresolved) { |
| 1398 | free(p->data.str.str); |
| 1399 | p->data.str.str = NULL; |
| 1400 | p->unresolved = 0; |
| 1401 | } |
| 1402 | else if (p->type == ARGT_REG) { |
| 1403 | if (p->data.reg) { |
| 1404 | regex_free(p->data.reg); |
| 1405 | free(p->data.reg); |
| 1406 | p->data.reg = NULL; |
| 1407 | } |
| 1408 | } |
| 1409 | p++; |
| 1410 | } |
| 1411 | |
| 1412 | if (p_back != empty_arg_list) |
| 1413 | free(p_back); |
| 1414 | } |
| 1415 | |
| 1416 | void release_sample_expr(struct sample_expr *expr) |
| 1417 | { |
| 1418 | struct sample_conv_expr *conv_expr, *conv_exprb; |
| 1419 | |
| 1420 | if (!expr) |
| 1421 | return; |
| 1422 | |
| 1423 | list_for_each_entry_safe(conv_expr, conv_exprb, &expr->conv_exprs, list) |
| 1424 | release_sample_arg(conv_expr->arg_p); |
| 1425 | release_sample_arg(expr->arg_p); |
| 1426 | free(expr); |
| 1427 | } |
| 1428 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1429 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1430 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1431 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1432 | /*****************************************************************/ |
| 1433 | |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1434 | #ifdef DEBUG_EXPR |
| 1435 | static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private) |
| 1436 | { |
| 1437 | int i; |
| 1438 | struct sample tmp; |
| 1439 | |
| 1440 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1441 | fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->data.type]); |
| 1442 | if (!sample_casts[smp->data.type][SMP_T_STR]) { |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1443 | fprintf(stderr, "(undisplayable)"); |
| 1444 | } else { |
| 1445 | |
| 1446 | /* Copy sample fetch. This put the sample as const, the |
| 1447 | * cast will copy data if a transformation is required. |
| 1448 | */ |
| 1449 | memcpy(&tmp, smp, sizeof(struct sample)); |
| 1450 | tmp.flags = SMP_F_CONST; |
| 1451 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1452 | if (!sample_casts[smp->data.type][SMP_T_STR](&tmp)) |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1453 | fprintf(stderr, "(undisplayable)"); |
| 1454 | |
| 1455 | else { |
| 1456 | /* Display the displayable chars*. */ |
| 1457 | fprintf(stderr, "<"); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1458 | for (i = 0; i < tmp.data.u.str.len; i++) { |
| 1459 | if (isprint(tmp.data.u.str.str[i])) |
| 1460 | fputc(tmp.data.u.str.str[i], stderr); |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1461 | else |
| 1462 | fputc('.', stderr); |
| 1463 | } |
| 1464 | } |
| 1465 | fprintf(stderr, ">\n"); |
| 1466 | } |
| 1467 | } |
| 1468 | return 1; |
| 1469 | } |
| 1470 | #endif |
| 1471 | |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 1472 | static int sample_conv_base642bin(const struct arg *arg_p, struct sample *smp, void *private) |
| 1473 | { |
| 1474 | struct chunk *trash = get_trash_chunk(); |
| 1475 | int bin_len; |
| 1476 | |
| 1477 | trash->len = 0; |
| 1478 | bin_len = base64dec(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size); |
| 1479 | if (bin_len < 0) |
| 1480 | return 0; |
| 1481 | |
| 1482 | trash->len = bin_len; |
| 1483 | smp->data.u.str = *trash; |
| 1484 | smp->data.type = SMP_T_BIN; |
| 1485 | smp->flags &= ~SMP_F_CONST; |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1489 | 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] | 1490 | { |
| 1491 | struct chunk *trash = get_trash_chunk(); |
| 1492 | int b64_len; |
| 1493 | |
| 1494 | trash->len = 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1495 | b64_len = a2base64(smp->data.u.str.str, smp->data.u.str.len, trash->str, trash->size); |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1496 | if (b64_len < 0) |
| 1497 | return 0; |
| 1498 | |
| 1499 | trash->len = b64_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1500 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1501 | smp->data.type = SMP_T_STR; |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 1502 | smp->flags &= ~SMP_F_CONST; |
| 1503 | return 1; |
| 1504 | } |
| 1505 | |
Dragan Dosen | 6e5a9ca | 2017-10-24 09:18:23 +0200 | [diff] [blame] | 1506 | static int sample_conv_sha1(const struct arg *arg_p, struct sample *smp, void *private) |
| 1507 | { |
| 1508 | blk_SHA_CTX ctx; |
| 1509 | struct chunk *trash = get_trash_chunk(); |
| 1510 | |
| 1511 | memset(&ctx, 0, sizeof(ctx)); |
| 1512 | |
| 1513 | blk_SHA1_Init(&ctx); |
| 1514 | blk_SHA1_Update(&ctx, smp->data.u.str.str, smp->data.u.str.len); |
| 1515 | blk_SHA1_Final((unsigned char *)trash->str, &ctx); |
| 1516 | |
| 1517 | trash->len = 20; |
| 1518 | smp->data.u.str = *trash; |
| 1519 | smp->data.type = SMP_T_BIN; |
| 1520 | smp->flags &= ~SMP_F_CONST; |
| 1521 | return 1; |
| 1522 | } |
| 1523 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1524 | 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] | 1525 | { |
| 1526 | struct chunk *trash = get_trash_chunk(); |
| 1527 | unsigned char c; |
| 1528 | int ptr = 0; |
| 1529 | |
| 1530 | trash->len = 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1531 | while (ptr < smp->data.u.str.len && trash->len <= trash->size - 2) { |
| 1532 | c = smp->data.u.str.str[ptr++]; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1533 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 1534 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 1535 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1536 | smp->data.u.str = *trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1537 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1538 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1539 | return 1; |
| 1540 | } |
| 1541 | |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 1542 | static int sample_conv_hex2int(const struct arg *arg_p, struct sample *smp, void *private) |
| 1543 | { |
| 1544 | long long int n = 0; |
| 1545 | int i, c; |
| 1546 | |
| 1547 | for (i = 0; i < smp->data.u.str.len; i++) { |
| 1548 | if ((c = hex2i(smp->data.u.str.str[i])) < 0) |
| 1549 | return 0; |
| 1550 | n = (n << 4) + c; |
| 1551 | } |
| 1552 | |
| 1553 | smp->data.u.sint = n; |
| 1554 | smp->data.type = SMP_T_SINT; |
| 1555 | smp->flags &= ~SMP_F_CONST; |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1559 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1560 | 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] | 1561 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1562 | smp->data.u.sint = hash_djb2(smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1563 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1564 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1565 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1566 | return 1; |
| 1567 | } |
| 1568 | |
Willy Tarreau | 60a2ee7 | 2017-12-15 07:13:48 +0100 | [diff] [blame] | 1569 | 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] | 1570 | { |
| 1571 | int i = smp->data.u.str.len; |
| 1572 | smp->data.u.sint = i; |
| 1573 | smp->data.type = SMP_T_SINT; |
| 1574 | return 1; |
| 1575 | } |
| 1576 | |
| 1577 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1578 | 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] | 1579 | { |
| 1580 | int i; |
| 1581 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 1582 | if (!smp_make_rw(smp)) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1583 | return 0; |
| 1584 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1585 | for (i = 0; i < smp->data.u.str.len; i++) { |
| 1586 | if ((smp->data.u.str.str[i] >= 'A') && (smp->data.u.str.str[i] <= 'Z')) |
| 1587 | smp->data.u.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1588 | } |
| 1589 | return 1; |
| 1590 | } |
| 1591 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1592 | 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] | 1593 | { |
| 1594 | int i; |
| 1595 | |
Willy Tarreau | f0645dc | 2016-08-09 14:29:38 +0200 | [diff] [blame] | 1596 | if (!smp_make_rw(smp)) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1597 | return 0; |
| 1598 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1599 | for (i = 0; i < smp->data.u.str.len; i++) { |
| 1600 | if ((smp->data.u.str.str[i] >= 'a') && (smp->data.u.str.str[i] <= 'z')) |
| 1601 | smp->data.u.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1602 | } |
| 1603 | return 1; |
| 1604 | } |
| 1605 | |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 1606 | /* takes the IPv4 mask in args[0] and an optional IPv6 mask in args[1] */ |
| 1607 | 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] | 1608 | { |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 1609 | /* Attempt to convert to IPv4 to apply the correct mask. */ |
| 1610 | c_ipv62ip(smp); |
| 1611 | |
| 1612 | if (smp->data.type == SMP_T_IPV4) { |
| 1613 | smp->data.u.ipv4.s_addr &= args[0].data.ipv4.s_addr; |
| 1614 | smp->data.type = SMP_T_IPV4; |
| 1615 | } |
| 1616 | else if (smp->data.type == SMP_T_IPV6) { |
| 1617 | /* IPv6 cannot be converted without an IPv6 mask. */ |
| 1618 | if (args[1].type != ARGT_IPV6) |
| 1619 | return 0; |
| 1620 | |
| 1621 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[0] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[0]; |
| 1622 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[4] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[4]; |
| 1623 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[8] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[8]; |
| 1624 | *(uint32_t*)&smp->data.u.ipv6.s6_addr[12] &= *(uint32_t*)&args[1].data.ipv6.s6_addr[12]; |
| 1625 | smp->data.type = SMP_T_IPV6; |
| 1626 | } |
| 1627 | |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
| 1630 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1631 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1632 | * adds an optional offset found in args[1] and emits a string representing |
| 1633 | * the local time in the format specified in args[1] using strftime(). |
| 1634 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1635 | 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] | 1636 | { |
| 1637 | struct chunk *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1638 | /* 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] | 1639 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1640 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1641 | |
| 1642 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1643 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1644 | curr_date += args[1].data.sint; |
| 1645 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1646 | tm = localtime(&curr_date); |
| 1647 | if (!tm) |
| 1648 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1649 | temp = get_trash_chunk(); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1650 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1651 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1652 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1653 | return 1; |
| 1654 | } |
| 1655 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1656 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1657 | 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] | 1658 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1659 | smp->data.u.sint = hash_sdbm(smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1660 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1661 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1662 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1663 | return 1; |
| 1664 | } |
| 1665 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1666 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1667 | * adds an optional offset found in args[1] and emits a string representing |
| 1668 | * the UTC date in the format specified in args[1] using strftime(). |
| 1669 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1670 | 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] | 1671 | { |
| 1672 | struct chunk *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1673 | /* 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] | 1674 | time_t curr_date = smp->data.u.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1675 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1676 | |
| 1677 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1678 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1679 | curr_date += args[1].data.sint; |
| 1680 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1681 | tm = gmtime(&curr_date); |
| 1682 | if (!tm) |
| 1683 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1684 | temp = get_trash_chunk(); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1685 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1686 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1687 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1688 | return 1; |
| 1689 | } |
| 1690 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1691 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1692 | 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] | 1693 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1694 | smp->data.u.sint = hash_wt6(smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1695 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1696 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1697 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1698 | return 1; |
| 1699 | } |
| 1700 | |
Thierry FOURNIER | 01e0974 | 2016-12-26 11:46:11 +0100 | [diff] [blame] | 1701 | /* hashes the binary input into a 32-bit unsigned int using xxh. |
| 1702 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 1703 | */ |
| 1704 | static int sample_conv_xxh32(const struct arg *arg_p, struct sample *smp, void *private) |
| 1705 | { |
| 1706 | unsigned int seed; |
| 1707 | |
| 1708 | if (arg_p && arg_p->data.sint) |
| 1709 | seed = arg_p->data.sint; |
| 1710 | else |
| 1711 | seed = 0; |
| 1712 | smp->data.u.sint = XXH32(smp->data.u.str.str, smp->data.u.str.len, seed); |
| 1713 | smp->data.type = SMP_T_SINT; |
| 1714 | return 1; |
| 1715 | } |
| 1716 | |
| 1717 | /* hashes the binary input into a 64-bit unsigned int using xxh. |
| 1718 | * In fact, the function returns a 64 bit unsigned, but the sample |
| 1719 | * storage of haproxy only proposes 64-bits signed, so the value is |
| 1720 | * cast as signed. This cast doesn't impact the hash repartition. |
| 1721 | * The seed of the hash defaults to 0 but can be changd in argument 1. |
| 1722 | */ |
| 1723 | static int sample_conv_xxh64(const struct arg *arg_p, struct sample *smp, void *private) |
| 1724 | { |
| 1725 | unsigned long long int seed; |
| 1726 | |
| 1727 | if (arg_p && arg_p->data.sint) |
| 1728 | seed = (unsigned long long int)arg_p->data.sint; |
| 1729 | else |
| 1730 | seed = 0; |
| 1731 | smp->data.u.sint = (long long int)XXH64(smp->data.u.str.str, smp->data.u.str.len, seed); |
| 1732 | smp->data.type = SMP_T_SINT; |
| 1733 | return 1; |
| 1734 | } |
| 1735 | |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1736 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1737 | 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] | 1738 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1739 | smp->data.u.sint = hash_crc32(smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1740 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1741 | smp->data.u.sint = full_hash(smp->data.u.sint); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1742 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1743 | return 1; |
| 1744 | } |
| 1745 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1746 | /* This function escape special json characters. The returned string can be |
| 1747 | * safely set between two '"' and used as json string. The json string is |
| 1748 | * defined like this: |
| 1749 | * |
| 1750 | * any Unicode character except '"' or '\' or control character |
| 1751 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 1752 | * |
| 1753 | * The enum input_type contain all the allowed mode for decoding the input |
| 1754 | * string. |
| 1755 | */ |
| 1756 | enum input_type { |
| 1757 | IT_ASCII = 0, |
| 1758 | IT_UTF8, |
| 1759 | IT_UTF8S, |
| 1760 | IT_UTF8P, |
| 1761 | IT_UTF8PS, |
| 1762 | }; |
| 1763 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 1764 | const char *file, int line, char **err) |
| 1765 | { |
| 1766 | if (!arg) { |
| 1767 | memprintf(err, "Unexpected empty arg list"); |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | if (arg->type != ARGT_STR) { |
| 1772 | memprintf(err, "Unexpected arg type"); |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | if (strcmp(arg->data.str.str, "") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1777 | arg->type = ARGT_SINT; |
| 1778 | arg->data.sint = IT_ASCII; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1779 | return 1; |
| 1780 | } |
| 1781 | |
| 1782 | else if (strcmp(arg->data.str.str, "ascii") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1783 | arg->type = ARGT_SINT; |
| 1784 | arg->data.sint = IT_ASCII; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1785 | return 1; |
| 1786 | } |
| 1787 | |
| 1788 | else if (strcmp(arg->data.str.str, "utf8") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1789 | arg->type = ARGT_SINT; |
| 1790 | arg->data.sint = IT_UTF8; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1791 | return 1; |
| 1792 | } |
| 1793 | |
| 1794 | else if (strcmp(arg->data.str.str, "utf8s") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1795 | arg->type = ARGT_SINT; |
| 1796 | arg->data.sint = IT_UTF8S; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1797 | return 1; |
| 1798 | } |
| 1799 | |
| 1800 | else if (strcmp(arg->data.str.str, "utf8p") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1801 | arg->type = ARGT_SINT; |
| 1802 | arg->data.sint = IT_UTF8P; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | else if (strcmp(arg->data.str.str, "utf8ps") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1807 | arg->type = ARGT_SINT; |
| 1808 | arg->data.sint = IT_UTF8PS; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1809 | return 1; |
| 1810 | } |
| 1811 | |
Thierry FOURNIER / OZON.IO | a69c912 | 2016-11-23 01:13:57 +0100 | [diff] [blame] | 1812 | memprintf(err, "Unexpected input code type. " |
| 1813 | "Allowed value are 'ascii', 'utf8', 'utf8s', 'utf8p' and 'utf8ps'"); |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1814 | return 0; |
| 1815 | } |
| 1816 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1817 | 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] | 1818 | { |
| 1819 | struct chunk *temp; |
| 1820 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 1821 | const char *str; |
| 1822 | int len; |
| 1823 | enum input_type input_type = IT_ASCII; |
| 1824 | unsigned int c; |
| 1825 | unsigned int ret; |
| 1826 | char *p; |
| 1827 | |
| 1828 | if (arg_p) |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1829 | input_type = arg_p->data.sint; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1830 | |
| 1831 | temp = get_trash_chunk(); |
| 1832 | temp->len = 0; |
| 1833 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1834 | p = smp->data.u.str.str; |
| 1835 | while (p < smp->data.u.str.str + smp->data.u.str.len) { |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1836 | |
| 1837 | if (input_type == IT_ASCII) { |
| 1838 | /* Read input as ASCII. */ |
| 1839 | c = *(unsigned char *)p; |
| 1840 | p++; |
| 1841 | } |
| 1842 | else { |
| 1843 | /* Read input as UTF8. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1844 | ret = utf8_next(p, smp->data.u.str.len - ( p - smp->data.u.str.str ), &c); |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1845 | p += utf8_return_length(ret); |
| 1846 | |
| 1847 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1848 | return 0; |
| 1849 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1850 | continue; |
| 1851 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1852 | return 0; |
| 1853 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1854 | continue; |
| 1855 | |
| 1856 | /* Check too big values. */ |
| 1857 | if ((unsigned int)c > 0xffff) { |
| 1858 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 1859 | return 0; |
| 1860 | continue; |
| 1861 | } |
| 1862 | } |
| 1863 | |
| 1864 | /* Convert character. */ |
| 1865 | if (c == '"') { |
| 1866 | len = 2; |
| 1867 | str = "\\\""; |
| 1868 | } |
| 1869 | else if (c == '\\') { |
| 1870 | len = 2; |
| 1871 | str = "\\\\"; |
| 1872 | } |
| 1873 | else if (c == '/') { |
| 1874 | len = 2; |
| 1875 | str = "\\/"; |
| 1876 | } |
| 1877 | else if (c == '\b') { |
| 1878 | len = 2; |
| 1879 | str = "\\b"; |
| 1880 | } |
| 1881 | else if (c == '\f') { |
| 1882 | len = 2; |
| 1883 | str = "\\f"; |
| 1884 | } |
| 1885 | else if (c == '\r') { |
| 1886 | len = 2; |
| 1887 | str = "\\r"; |
| 1888 | } |
| 1889 | else if (c == '\n') { |
| 1890 | len = 2; |
| 1891 | str = "\\n"; |
| 1892 | } |
| 1893 | else if (c == '\t') { |
| 1894 | len = 2; |
| 1895 | str = "\\t"; |
| 1896 | } |
| 1897 | else if (c > 0xff || !isprint(c)) { |
| 1898 | /* isprint generate a segfault if c is too big. The man says that |
| 1899 | * c must have the value of an unsigned char or EOF. |
| 1900 | */ |
| 1901 | len = 6; |
| 1902 | _str[0] = '\\'; |
| 1903 | _str[1] = 'u'; |
| 1904 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 1905 | str = _str; |
| 1906 | } |
| 1907 | else { |
| 1908 | len = 1; |
| 1909 | str = (char *)&c; |
| 1910 | } |
| 1911 | |
| 1912 | /* Check length */ |
| 1913 | if (temp->len + len > temp->size) |
| 1914 | return 0; |
| 1915 | |
| 1916 | /* Copy string. */ |
| 1917 | memcpy(temp->str + temp->len, str, len); |
| 1918 | temp->len += len; |
| 1919 | } |
| 1920 | |
| 1921 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1922 | smp->data.u.str = *temp; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1923 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1924 | |
| 1925 | return 1; |
| 1926 | } |
| 1927 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1928 | /* This sample function is designed to extract some bytes from an input buffer. |
| 1929 | * First arg is the offset. |
| 1930 | * Optional second arg is the length to truncate */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1931 | 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] | 1932 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1933 | if (smp->data.u.str.len <= arg_p[0].data.sint) { |
| 1934 | smp->data.u.str.len = 0; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1935 | return 1; |
| 1936 | } |
| 1937 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1938 | if (smp->data.u.str.size) |
| 1939 | smp->data.u.str.size -= arg_p[0].data.sint; |
| 1940 | smp->data.u.str.len -= arg_p[0].data.sint; |
| 1941 | smp->data.u.str.str += arg_p[0].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1942 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1943 | if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.u.str.len)) |
| 1944 | smp->data.u.str.len = arg_p[1].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1945 | |
| 1946 | return 1; |
| 1947 | } |
| 1948 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1949 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 1950 | const char *file, int line, char **err) |
| 1951 | { |
| 1952 | struct arg *arg = args; |
| 1953 | |
| 1954 | if (!arg) { |
| 1955 | memprintf(err, "Unexpected empty arg list"); |
| 1956 | return 0; |
| 1957 | } |
| 1958 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1959 | if (arg->type != ARGT_SINT) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1960 | memprintf(err, "Unexpected arg type"); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1964 | if (!arg->data.sint) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1965 | memprintf(err, "Unexpected value 0 for index"); |
| 1966 | return 0; |
| 1967 | } |
| 1968 | |
| 1969 | arg++; |
| 1970 | |
| 1971 | if (arg->type != ARGT_STR) { |
| 1972 | memprintf(err, "Unexpected arg type"); |
| 1973 | return 0; |
| 1974 | } |
| 1975 | |
| 1976 | if (!arg->data.str.len) { |
| 1977 | memprintf(err, "Empty separators list"); |
| 1978 | return 0; |
| 1979 | } |
| 1980 | |
| 1981 | return 1; |
| 1982 | } |
| 1983 | |
| 1984 | /* This sample function is designed to a return selected part of a string (field). |
| 1985 | * First arg is the index of the field (start at 1) |
| 1986 | * Second arg is a char list of separators (type string) |
| 1987 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1988 | 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] | 1989 | { |
| 1990 | unsigned int field; |
| 1991 | char *start, *end; |
| 1992 | int i; |
| 1993 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1994 | if (!arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1995 | return 0; |
| 1996 | |
| 1997 | field = 1; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1998 | end = start = smp->data.u.str.str; |
| 1999 | while (end - smp->data.u.str.str < smp->data.u.str.len) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2000 | |
| 2001 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 2002 | if (*end == arg_p[1].data.str.str[i]) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2003 | if (field == arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2004 | goto found; |
| 2005 | start = end+1; |
| 2006 | field++; |
| 2007 | break; |
| 2008 | } |
| 2009 | } |
| 2010 | end++; |
| 2011 | } |
| 2012 | |
| 2013 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2014 | if (field != arg_p[0].data.sint) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2015 | smp->data.u.str.len = 0; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2016 | return 1; |
| 2017 | } |
| 2018 | found: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2019 | smp->data.u.str.len = end - start; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2020 | /* If ret string is len 0, no need to |
| 2021 | change pointers or to update size */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2022 | if (!smp->data.u.str.len) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2023 | return 1; |
| 2024 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2025 | smp->data.u.str.str = start; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2026 | |
| 2027 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2028 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2029 | if (smp->data.u.str.size) |
| 2030 | smp->data.u.str.size -= start - smp->data.u.str.str; |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 2031 | |
| 2032 | return 1; |
| 2033 | } |
| 2034 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2035 | /* This sample function is designed to return a word from a string. |
| 2036 | * First arg is the index of the word (start at 1) |
| 2037 | * Second arg is a char list of words separators (type string) |
| 2038 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2039 | 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] | 2040 | { |
| 2041 | unsigned int word; |
| 2042 | char *start, *end; |
| 2043 | int i, issep, inword; |
| 2044 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2045 | if (!arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2046 | return 0; |
| 2047 | |
| 2048 | word = 0; |
| 2049 | inword = 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2050 | end = start = smp->data.u.str.str; |
| 2051 | while (end - smp->data.u.str.str < smp->data.u.str.len) { |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2052 | issep = 0; |
| 2053 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 2054 | if (*end == arg_p[1].data.str.str[i]) { |
| 2055 | issep = 1; |
| 2056 | break; |
| 2057 | } |
| 2058 | } |
| 2059 | if (!inword) { |
| 2060 | if (!issep) { |
| 2061 | word++; |
| 2062 | start = end; |
| 2063 | inword = 1; |
| 2064 | } |
| 2065 | } |
| 2066 | else if (issep) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2067 | if (word == arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2068 | goto found; |
| 2069 | inword = 0; |
| 2070 | } |
| 2071 | end++; |
| 2072 | } |
| 2073 | |
| 2074 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2075 | if (word != arg_p[0].data.sint) { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2076 | smp->data.u.str.len = 0; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2077 | return 1; |
| 2078 | } |
| 2079 | found: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2080 | smp->data.u.str.len = end - start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2081 | /* If ret string is len 0, no need to |
| 2082 | change pointers or to update size */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2083 | if (!smp->data.u.str.len) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2084 | return 1; |
| 2085 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2086 | smp->data.u.str.str = start; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2087 | |
| 2088 | /* Compute remaining size if needed |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2089 | Note: smp->data.u.str.size cannot be set to 0 */ |
| 2090 | if (smp->data.u.str.size) |
| 2091 | smp->data.u.str.size -= start - smp->data.u.str.str; |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 2092 | |
| 2093 | return 1; |
| 2094 | } |
| 2095 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2096 | static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv, |
| 2097 | const char *file, int line, char **err) |
| 2098 | { |
| 2099 | struct arg *arg = args; |
| 2100 | char *p; |
| 2101 | int len; |
| 2102 | |
| 2103 | /* arg0 is a regex, it uses type_flag for ICASE and global match */ |
| 2104 | arg[0].type_flags = 0; |
| 2105 | |
| 2106 | if (arg[2].type != ARGT_STR) |
| 2107 | return 1; |
| 2108 | |
| 2109 | p = arg[2].data.str.str; |
| 2110 | len = arg[2].data.str.len; |
| 2111 | while (len) { |
| 2112 | if (*p == 'i') { |
| 2113 | arg[0].type_flags |= ARGF_REG_ICASE; |
| 2114 | } |
| 2115 | else if (*p == 'g') { |
| 2116 | arg[0].type_flags |= ARGF_REG_GLOB; |
| 2117 | } |
| 2118 | else { |
| 2119 | memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p); |
| 2120 | return 0; |
| 2121 | } |
| 2122 | p++; |
| 2123 | len--; |
| 2124 | } |
| 2125 | return 1; |
| 2126 | } |
| 2127 | |
| 2128 | /* This sample function is designed to do the equivalent of s/match/replace/ on |
| 2129 | * the input string. It applies a regex and restarts from the last matched |
| 2130 | * location until nothing matches anymore. First arg is the regex to apply to |
| 2131 | * the input string, second arg is the replacement expression. |
| 2132 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2133 | 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] | 2134 | { |
| 2135 | char *start, *end; |
| 2136 | struct my_regex *reg = arg_p[0].data.reg; |
| 2137 | regmatch_t pmatch[MAX_MATCH]; |
| 2138 | struct chunk *trash = get_trash_chunk(); |
| 2139 | int flag, max; |
| 2140 | int found; |
| 2141 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2142 | start = smp->data.u.str.str; |
| 2143 | end = start + smp->data.u.str.len; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2144 | |
| 2145 | flag = 0; |
| 2146 | while (1) { |
| 2147 | /* check for last round which is used to copy remaining parts |
| 2148 | * when not running in global replacement mode. |
| 2149 | */ |
| 2150 | found = 0; |
| 2151 | if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) { |
| 2152 | /* Note: we can have start == end on empty strings or at the end */ |
| 2153 | found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag); |
| 2154 | } |
| 2155 | |
| 2156 | if (!found) |
| 2157 | pmatch[0].rm_so = end - start; |
| 2158 | |
| 2159 | /* copy the heading non-matching part (which may also be the tail if nothing matches) */ |
| 2160 | max = trash->size - trash->len; |
| 2161 | if (max && pmatch[0].rm_so > 0) { |
| 2162 | if (max > pmatch[0].rm_so) |
| 2163 | max = pmatch[0].rm_so; |
| 2164 | memcpy(trash->str + trash->len, start, max); |
| 2165 | trash->len += max; |
| 2166 | } |
| 2167 | |
| 2168 | if (!found) |
| 2169 | break; |
| 2170 | |
| 2171 | /* replace the matching part */ |
| 2172 | max = trash->size - trash->len; |
| 2173 | if (max) { |
| 2174 | if (max > arg_p[1].data.str.len) |
| 2175 | max = arg_p[1].data.str.len; |
| 2176 | memcpy(trash->str + trash->len, arg_p[1].data.str.str, max); |
| 2177 | trash->len += max; |
| 2178 | } |
| 2179 | |
| 2180 | /* stop here if we're done with this string */ |
| 2181 | if (start >= end) |
| 2182 | break; |
| 2183 | |
| 2184 | /* We have a special case for matches of length 0 (eg: "x*y*"). |
| 2185 | * These ones are considered to match in front of a character, |
| 2186 | * so we have to copy that character and skip to the next one. |
| 2187 | */ |
| 2188 | if (!pmatch[0].rm_eo) { |
| 2189 | if (trash->len < trash->size) |
| 2190 | trash->str[trash->len++] = start[pmatch[0].rm_eo]; |
| 2191 | pmatch[0].rm_eo++; |
| 2192 | } |
| 2193 | |
| 2194 | start += pmatch[0].rm_eo; |
| 2195 | flag |= REG_NOTBOL; |
| 2196 | } |
| 2197 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2198 | smp->data.u.str = *trash; |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2199 | return 1; |
| 2200 | } |
| 2201 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2202 | /* This function check an operator entry. It expects a string. |
| 2203 | * The string can be an integer or a variable name. |
| 2204 | */ |
| 2205 | static int check_operator(struct arg *args, struct sample_conv *conv, |
| 2206 | const char *file, int line, char **err) |
| 2207 | { |
| 2208 | const char *str; |
| 2209 | const char *end; |
| 2210 | |
| 2211 | /* Try to decode a variable. */ |
| 2212 | if (vars_check_arg(&args[0], NULL)) |
| 2213 | return 1; |
| 2214 | |
| 2215 | /* Try to convert an integer */ |
| 2216 | str = args[0].data.str.str; |
| 2217 | end = str + strlen(str); |
| 2218 | args[0].data.sint = read_int64(&str, end); |
| 2219 | if (*str != '\0') { |
| 2220 | memprintf(err, "expects an integer or a variable name"); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | args[0].type = ARGT_SINT; |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2227 | /* This function returns a sample struct filled with an arg content. |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2228 | * If the arg contain an integer, the integer is returned in the |
| 2229 | * sample. If the arg contains a variable descriptor, it returns the |
| 2230 | * variable value. |
| 2231 | * |
| 2232 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 2233 | */ |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2234 | 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] | 2235 | { |
| 2236 | switch (arg->type) { |
| 2237 | case ARGT_SINT: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2238 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2239 | smp->data.u.sint = arg->data.sint; |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2240 | return 1; |
| 2241 | case ARGT_VAR: |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2242 | if (!vars_get_by_desc(&arg->data.var, smp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2243 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2244 | if (!sample_casts[smp->data.type][SMP_T_SINT]) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2245 | return 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2246 | if (!sample_casts[smp->data.type][SMP_T_SINT](smp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2247 | return 0; |
| 2248 | return 1; |
| 2249 | default: |
| 2250 | return 0; |
| 2251 | } |
| 2252 | } |
| 2253 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2254 | /* 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] | 2255 | * result. |
| 2256 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2257 | 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] | 2258 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2259 | smp->data.u.sint = ~smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2260 | return 1; |
| 2261 | } |
| 2262 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2263 | /* Takes a SINT on input, applies a binary "and" with the SINT directly in |
| 2264 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2265 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2266 | 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] | 2267 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2268 | struct sample tmp; |
| 2269 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2270 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2271 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2272 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2273 | smp->data.u.sint &= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2274 | return 1; |
| 2275 | } |
| 2276 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2277 | /* Takes a SINT on input, applies a binary "or" with the SINT directly in |
| 2278 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2279 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2280 | 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] | 2281 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2282 | struct sample tmp; |
| 2283 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2284 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2285 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2286 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2287 | smp->data.u.sint |= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2288 | return 1; |
| 2289 | } |
| 2290 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2291 | /* Takes a SINT on input, applies a binary "xor" with the SINT directly in |
| 2292 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2293 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2294 | 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] | 2295 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2296 | struct sample tmp; |
| 2297 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2298 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2299 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2300 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2301 | smp->data.u.sint ^= tmp.data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2302 | return 1; |
| 2303 | } |
| 2304 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2305 | static inline long long int arith_add(long long int a, long long int b) |
| 2306 | { |
| 2307 | /* Prevent overflow and makes capped calculus. |
| 2308 | * We must ensure that the check calculus doesn't |
| 2309 | * exceed the signed 64 bits limits. |
| 2310 | * |
| 2311 | * +----------+----------+ |
| 2312 | * | a<0 | a>=0 | |
| 2313 | * +------+----------+----------+ |
| 2314 | * | b<0 | MIN-a>b | no check | |
| 2315 | * +------+----------+----------+ |
| 2316 | * | b>=0 | no check | MAX-a<b | |
| 2317 | * +------+----------+----------+ |
| 2318 | */ |
| 2319 | if ((a ^ b) >= 0) { |
| 2320 | /* signs are differents. */ |
| 2321 | if (a < 0) { |
| 2322 | if (LLONG_MIN - a > b) |
| 2323 | return LLONG_MIN; |
| 2324 | } |
| 2325 | if (LLONG_MAX - a < b) |
| 2326 | return LLONG_MAX; |
| 2327 | } |
| 2328 | return a + b; |
| 2329 | } |
| 2330 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2331 | /* Takes a SINT on input, applies an arithmetic "add" with the SINT directly in |
| 2332 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2333 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2334 | 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] | 2335 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2336 | struct sample tmp; |
| 2337 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2338 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2339 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2340 | return 0; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2341 | 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] | 2342 | return 1; |
| 2343 | } |
| 2344 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2345 | /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in |
| 2346 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2347 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2348 | static int sample_conv_arith_sub(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2349 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2350 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2351 | struct sample tmp; |
| 2352 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2353 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2354 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2355 | return 0; |
| 2356 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2357 | /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater |
| 2358 | * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place |
| 2359 | * of -LLONG_MIN and correct the result. |
| 2360 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2361 | if (tmp.data.u.sint == LLONG_MIN) { |
| 2362 | smp->data.u.sint = arith_add(smp->data.u.sint, LLONG_MAX); |
| 2363 | if (smp->data.u.sint < LLONG_MAX) |
| 2364 | smp->data.u.sint++; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2365 | return 1; |
| 2366 | } |
| 2367 | |
| 2368 | /* standard substraction: we use the "add" function and negate |
| 2369 | * the second operand. |
| 2370 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2371 | 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] | 2372 | return 1; |
| 2373 | } |
| 2374 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2375 | /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in |
| 2376 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2377 | * If the result makes an overflow, then the largest possible quantity is |
| 2378 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2379 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2380 | static int sample_conv_arith_mul(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2381 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2382 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2383 | struct sample tmp; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2384 | long long int c; |
| 2385 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2386 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2387 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2388 | return 0; |
| 2389 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2390 | /* prevent divide by 0 during the check */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2391 | if (!smp->data.u.sint || !tmp.data.u.sint) { |
| 2392 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2393 | return 1; |
| 2394 | } |
| 2395 | |
| 2396 | /* The multiply between LLONG_MIN and -1 returns a |
| 2397 | * "floting point exception". |
| 2398 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2399 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2400 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2401 | return 1; |
| 2402 | } |
| 2403 | |
| 2404 | /* execute standard multiplication. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2405 | c = smp->data.u.sint * tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2406 | |
| 2407 | /* check for overflow and makes capped multiply. */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2408 | if (smp->data.u.sint != c / tmp.data.u.sint) { |
| 2409 | if ((smp->data.u.sint < 0) == (tmp.data.u.sint < 0)) { |
| 2410 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2411 | return 1; |
| 2412 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2413 | smp->data.u.sint = LLONG_MIN; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2414 | return 1; |
| 2415 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2416 | smp->data.u.sint = c; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2417 | return 1; |
| 2418 | } |
| 2419 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2420 | /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in |
| 2421 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2422 | * If arg_p makes the result overflow, then the largest possible quantity is |
| 2423 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2424 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2425 | static int sample_conv_arith_div(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2426 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2427 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2428 | struct sample tmp; |
| 2429 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2430 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2431 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2432 | return 0; |
| 2433 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2434 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2435 | /* The divide between LLONG_MIN and -1 returns a |
| 2436 | * "floting point exception". |
| 2437 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2438 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2439 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2440 | return 1; |
| 2441 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2442 | smp->data.u.sint /= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2443 | return 1; |
| 2444 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2445 | smp->data.u.sint = LLONG_MAX; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2446 | return 1; |
| 2447 | } |
| 2448 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2449 | /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in |
| 2450 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2451 | * If arg_p makes the result overflow, then 0 is returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2452 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2453 | static int sample_conv_arith_mod(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2454 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2455 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2456 | struct sample tmp; |
| 2457 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 2458 | smp_set_owner(&tmp, smp->px, smp->sess, smp->strm, smp->opt); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 2459 | if (!sample_conv_var2smp(arg_p, &tmp)) |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2460 | return 0; |
| 2461 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2462 | if (tmp.data.u.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2463 | /* The divide between LLONG_MIN and -1 returns a |
| 2464 | * "floting point exception". |
| 2465 | */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2466 | if (smp->data.u.sint == LLONG_MIN && tmp.data.u.sint == -1) { |
| 2467 | smp->data.u.sint = 0; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2468 | return 1; |
| 2469 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2470 | smp->data.u.sint %= tmp.data.u.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2471 | return 1; |
| 2472 | } |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2473 | smp->data.u.sint = 0; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2474 | return 1; |
| 2475 | } |
| 2476 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2477 | /* 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] | 2478 | * result. |
| 2479 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2480 | static int sample_conv_arith_neg(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2481 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2482 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2483 | if (smp->data.u.sint == LLONG_MIN) |
| 2484 | smp->data.u.sint = LLONG_MAX; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2485 | else |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2486 | smp->data.u.sint = -smp->data.u.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2487 | return 1; |
| 2488 | } |
| 2489 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2490 | /* 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] | 2491 | * false. The output is a BOOL. |
| 2492 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2493 | static int sample_conv_arith_bool(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2494 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2495 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2496 | smp->data.u.sint = !!smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2497 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2498 | return 1; |
| 2499 | } |
| 2500 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2501 | /* 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] | 2502 | * truee. The output is a BOOL. |
| 2503 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2504 | static int sample_conv_arith_not(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2505 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2506 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2507 | smp->data.u.sint = !smp->data.u.sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2508 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2509 | return 1; |
| 2510 | } |
| 2511 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2512 | /* 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] | 2513 | * The output is a BOOL. |
| 2514 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2515 | static int sample_conv_arith_odd(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2516 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2517 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2518 | smp->data.u.sint = smp->data.u.sint & 1; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2519 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2520 | return 1; |
| 2521 | } |
| 2522 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2523 | /* 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] | 2524 | * The output is a BOOL. |
| 2525 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2526 | static int sample_conv_arith_even(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2527 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2528 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2529 | smp->data.u.sint = !(smp->data.u.sint & 1); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2530 | smp->data.type = SMP_T_BOOL; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2531 | return 1; |
| 2532 | } |
| 2533 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2534 | /************************************************************************/ |
| 2535 | /* All supported sample fetch functions must be declared here */ |
| 2536 | /************************************************************************/ |
| 2537 | |
| 2538 | /* force TRUE to be returned at the fetch level */ |
| 2539 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2540 | 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] | 2541 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2542 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2543 | smp->data.u.sint = 1; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2544 | return 1; |
| 2545 | } |
| 2546 | |
| 2547 | /* force FALSE to be returned at the fetch level */ |
| 2548 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2549 | 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] | 2550 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2551 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2552 | smp->data.u.sint = 0; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2553 | return 1; |
| 2554 | } |
| 2555 | |
| 2556 | /* retrieve environment variable $1 as a string */ |
| 2557 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2558 | 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] | 2559 | { |
| 2560 | char *env; |
| 2561 | |
| 2562 | if (!args || args[0].type != ARGT_STR) |
| 2563 | return 0; |
| 2564 | |
| 2565 | env = getenv(args[0].data.str.str); |
| 2566 | if (!env) |
| 2567 | return 0; |
| 2568 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2569 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2570 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2571 | smp->data.u.str.str = env; |
| 2572 | smp->data.u.str.len = strlen(env); |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2573 | return 1; |
| 2574 | } |
| 2575 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2576 | /* retrieve the current local date in epoch time, and applies an optional offset |
| 2577 | * of args[0] seconds. |
| 2578 | */ |
| 2579 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2580 | 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] | 2581 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2582 | smp->data.u.sint = date.tv_sec; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2585 | if (args && args[0].type == ARGT_SINT) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2586 | smp->data.u.sint += args[0].data.sint; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2587 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2588 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2589 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2590 | return 1; |
| 2591 | } |
| 2592 | |
Etienne Carriere | a792a0a | 2018-01-17 13:43:24 +0100 | [diff] [blame] | 2593 | /* retrieve the current microsecond part of the date */ |
| 2594 | static int |
| 2595 | smp_fetch_date_us(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2596 | { |
| 2597 | smp->data.u.sint = date.tv_usec; |
| 2598 | smp->data.type = SMP_T_SINT; |
| 2599 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2600 | return 1; |
| 2601 | } |
| 2602 | |
| 2603 | |
Nenad Merdanovic | 807a6e7 | 2017-03-12 22:00:00 +0100 | [diff] [blame] | 2604 | /* returns the hostname */ |
| 2605 | static int |
| 2606 | smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2607 | { |
| 2608 | smp->data.type = SMP_T_STR; |
| 2609 | smp->flags = SMP_F_CONST; |
| 2610 | smp->data.u.str.str = hostname; |
| 2611 | smp->data.u.str.len = strlen(hostname); |
| 2612 | return 1; |
| 2613 | } |
| 2614 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2615 | /* returns the number of processes */ |
| 2616 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2617 | 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] | 2618 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2619 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2620 | smp->data.u.sint = global.nbproc; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2621 | return 1; |
| 2622 | } |
| 2623 | |
| 2624 | /* returns the number of the current process (between 1 and nbproc */ |
| 2625 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2626 | 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] | 2627 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2628 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2629 | smp->data.u.sint = relative_pid; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2630 | return 1; |
| 2631 | } |
| 2632 | |
Christopher Faulet | 34adb2a | 2017-11-21 21:45:38 +0100 | [diff] [blame] | 2633 | /* returns the number of the current thread (between 1 and nbthread */ |
| 2634 | static int |
| 2635 | smp_fetch_thread(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2636 | { |
| 2637 | smp->data.type = SMP_T_SINT; |
| 2638 | smp->data.u.sint = tid; |
| 2639 | return 1; |
| 2640 | } |
| 2641 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2642 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 2643 | * range specified in argument. |
| 2644 | */ |
| 2645 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2646 | 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] | 2647 | { |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2648 | smp->data.u.sint = random(); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2649 | |
| 2650 | /* reduce if needed. Don't do a modulo, use all bits! */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2651 | if (args && args[0].type == ARGT_SINT) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2652 | smp->data.u.sint = (smp->data.u.sint * args[0].data.sint) / ((u64)RAND_MAX+1); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2653 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2654 | smp->data.type = SMP_T_SINT; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2655 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2656 | return 1; |
| 2657 | } |
| 2658 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2659 | /* returns true if the current process is stopping */ |
| 2660 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2661 | 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] | 2662 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2663 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2664 | smp->data.u.sint = stopping; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2665 | return 1; |
| 2666 | } |
| 2667 | |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2668 | static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2669 | { |
| 2670 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2671 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2672 | smp->data.u.str.str = args[0].data.str.str; |
| 2673 | smp->data.u.str.len = args[0].data.str.len; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2674 | return 1; |
| 2675 | } |
| 2676 | |
| 2677 | static int smp_check_const_bool(struct arg *args, char **err) |
| 2678 | { |
| 2679 | if (strcasecmp(args[0].data.str.str, "true") == 0 || |
| 2680 | strcasecmp(args[0].data.str.str, "1") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2681 | args[0].type = ARGT_SINT; |
| 2682 | args[0].data.sint = 1; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2683 | return 1; |
| 2684 | } |
| 2685 | if (strcasecmp(args[0].data.str.str, "false") == 0 || |
| 2686 | strcasecmp(args[0].data.str.str, "0") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2687 | args[0].type = ARGT_SINT; |
| 2688 | args[0].data.sint = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2689 | return 1; |
| 2690 | } |
| 2691 | memprintf(err, "Expects 'true', 'false', '0' or '1'"); |
| 2692 | return 0; |
| 2693 | } |
| 2694 | |
| 2695 | static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2696 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2697 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2698 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2699 | return 1; |
| 2700 | } |
| 2701 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2702 | 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] | 2703 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2704 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2705 | smp->data.u.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2706 | return 1; |
| 2707 | } |
| 2708 | |
| 2709 | static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2710 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2711 | smp->data.type = SMP_T_IPV4; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2712 | smp->data.u.ipv4 = args[0].data.ipv4; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2713 | return 1; |
| 2714 | } |
| 2715 | |
| 2716 | static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2717 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2718 | smp->data.type = SMP_T_IPV6; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2719 | smp->data.u.ipv6 = args[0].data.ipv6; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2720 | return 1; |
| 2721 | } |
| 2722 | |
| 2723 | static int smp_check_const_bin(struct arg *args, char **err) |
| 2724 | { |
David Carlier | 64a16ab | 2016-04-08 10:37:02 +0100 | [diff] [blame] | 2725 | char *binstr = NULL; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2726 | int binstrlen; |
| 2727 | |
| 2728 | if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err)) |
| 2729 | return 0; |
| 2730 | args[0].type = ARGT_STR; |
| 2731 | args[0].data.str.str = binstr; |
| 2732 | args[0].data.str.len = binstrlen; |
| 2733 | return 1; |
| 2734 | } |
| 2735 | |
| 2736 | static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2737 | { |
| 2738 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2739 | smp->data.type = SMP_T_BIN; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2740 | smp->data.u.str.str = args[0].data.str.str; |
| 2741 | smp->data.u.str.len = args[0].data.str.len; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2742 | return 1; |
| 2743 | } |
| 2744 | |
| 2745 | static int smp_check_const_meth(struct arg *args, char **err) |
| 2746 | { |
| 2747 | enum http_meth_t meth; |
| 2748 | int i; |
| 2749 | |
| 2750 | meth = find_http_meth(args[0].data.str.str, args[0].data.str.len); |
| 2751 | if (meth != HTTP_METH_OTHER) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2752 | args[0].type = ARGT_SINT; |
| 2753 | args[0].data.sint = meth; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2754 | } else { |
| 2755 | /* Check method avalaibility. A methos is a token defined as : |
| 2756 | * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
| 2757 | * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 2758 | * token = 1*tchar |
| 2759 | */ |
| 2760 | for (i = 0; i < args[0].data.str.len; i++) { |
Willy Tarreau | 2235b26 | 2016-11-05 15:50:20 +0100 | [diff] [blame] | 2761 | if (!HTTP_IS_TOKEN(args[0].data.str.str[i])) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2762 | memprintf(err, "expects valid method."); |
| 2763 | return 0; |
| 2764 | } |
| 2765 | } |
| 2766 | } |
| 2767 | return 1; |
| 2768 | } |
| 2769 | |
| 2770 | static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2771 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2772 | smp->data.type = SMP_T_METH; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2773 | if (args[0].type == ARGT_SINT) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2774 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2775 | smp->data.u.meth.meth = args[0].data.sint; |
| 2776 | smp->data.u.meth.str.str = ""; |
| 2777 | smp->data.u.meth.str.len = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2778 | } else { |
| 2779 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2780 | smp->data.u.meth.meth = HTTP_METH_OTHER; |
| 2781 | smp->data.u.meth.str.str = args[0].data.str.str; |
| 2782 | smp->data.u.meth.str.len = args[0].data.str.len; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2783 | } |
| 2784 | return 1; |
| 2785 | } |
| 2786 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2787 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2788 | * Note: fetches that may return multiple types must be declared as the lowest |
| 2789 | * common denominator, the type that can be casted into all other ones. For |
| 2790 | * instance IPv4/IPv6 must be declared IPv4. |
| 2791 | */ |
| 2792 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 2793 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 2794 | { "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] | 2795 | { "env", smp_fetch_env, ARG1(1,STR), NULL, SMP_T_STR, SMP_USE_INTRN }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2796 | { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Etienne Carriere | a792a0a | 2018-01-17 13:43:24 +0100 | [diff] [blame] | 2797 | { "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] | 2798 | { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_INTRN }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2799 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 2800 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Christopher Faulet | 34adb2a | 2017-11-21 21:45:38 +0100 | [diff] [blame] | 2801 | { "thread", smp_fetch_thread, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2802 | { "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] | 2803 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2804 | |
| 2805 | { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN }, |
| 2806 | { "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] | 2807 | { "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] | 2808 | { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN }, |
| 2809 | { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN }, |
| 2810 | { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN }, |
| 2811 | { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN }, |
| 2812 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2813 | { /* END */ }, |
| 2814 | }}; |
| 2815 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2816 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2817 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 2818 | #ifdef DEBUG_EXPR |
| 2819 | { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY }, |
| 2820 | #endif |
| 2821 | |
Holger Just | 1bfc24b | 2017-05-06 00:56:53 +0200 | [diff] [blame] | 2822 | { "b64dec", sample_conv_base642bin,0, NULL, SMP_T_STR, SMP_T_BIN }, |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 2823 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2824 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 2825 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 60a2ee7 | 2017-12-15 07:13:48 +0100 | [diff] [blame] | 2826 | { "length", sample_conv_length, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 2827 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Dragan Dosen | 3f957b2 | 2017-10-24 09:27:34 +0200 | [diff] [blame] | 2828 | { "hex2i", sample_conv_hex2int, 0, NULL, SMP_T_STR, SMP_T_SINT }, |
Tim Duesterhus | 1478aa7 | 2018-01-25 16:24:51 +0100 | [diff] [blame] | 2829 | { "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] | 2830 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
| 2831 | { "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] | 2832 | { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2833 | { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2834 | { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2835 | { "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] | 2836 | { "xxh32", sample_conv_xxh32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2837 | { "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] | 2838 | { "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] | 2839 | { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
| 2840 | { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
| 2841 | { "word", sample_conv_word, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 2842 | { "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] | 2843 | { "sha1", sample_conv_sha1, 0, NULL, SMP_T_BIN, SMP_T_BIN }, |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2844 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2845 | { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2846 | { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2847 | { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2848 | { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
| 2849 | { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2850 | { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2851 | { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2852 | { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2853 | { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2854 | { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2855 | { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2856 | { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2857 | { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2858 | { "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] | 2859 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 2860 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2861 | }}; |
| 2862 | |
| 2863 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2864 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2865 | { |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2866 | /* register sample fetch and format conversion keywords */ |
| 2867 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2868 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2869 | } |