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