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 | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 502 | smp->data.sint = ntohl(smp->data.ipv4.s_addr); |
| 503 | smp->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 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 511 | if (!inet_ntop(AF_INET, (void *)&smp->data.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); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 515 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 516 | smp->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 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 524 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 525 | smp->type = SMP_T_IPV6; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 526 | return 1; |
| 527 | } |
| 528 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 529 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 530 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 531 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 532 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 533 | if (!inet_ntop(AF_INET6, (void *)&smp->data.ipv6, trash->str, trash->size)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 534 | return 0; |
| 535 | |
| 536 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 537 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 538 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 539 | smp->flags &= ~SMP_F_CONST; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 540 | return 1; |
| 541 | } |
| 542 | |
| 543 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 544 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 545 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 546 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 547 | } |
| 548 | */ |
| 549 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 550 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 551 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 552 | smp->data.ipv4.s_addr = htonl((unsigned int)smp->data.sint); |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 553 | smp->type = SMP_T_IPV4; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 554 | return 1; |
| 555 | } |
| 556 | |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 557 | static int c_str2addr(struct sample *smp) |
| 558 | { |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 559 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 560 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 561 | return 0; |
| 562 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 563 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 564 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 565 | } |
| 566 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 567 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 568 | return 1; |
| 569 | } |
| 570 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 571 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 572 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 573 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &smp->data.ipv4)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 574 | return 0; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 575 | smp->type = SMP_T_IPV4; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 576 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 577 | return 1; |
| 578 | } |
| 579 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 580 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 581 | { |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 582 | if (!buf2ip6(smp->data.str.str, smp->data.str.len, &smp->data.ipv6)) |
| 583 | return 0; |
| 584 | smp->type = SMP_T_IPV6; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 585 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | fd13990 | 2013-12-11 12:38:57 +0100 | [diff] [blame] | 586 | return 1; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 587 | } |
| 588 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 589 | /* |
| 590 | * The NULL char always enforces the end of string if it is met. |
| 591 | * Data is never changed, so we can ignore the CONST case |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 592 | */ |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 593 | static int c_bin2str(struct sample *smp) |
| 594 | { |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 595 | int i; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 596 | |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 597 | for (i = 0; i < smp->data.str.len; i++) { |
| 598 | if (!smp->data.str.str[i]) { |
| 599 | smp->data.str.len = i; |
Thierry FOURNIER | e87cac1 | 2014-03-12 15:07:59 +0100 | [diff] [blame] | 600 | break; |
Emeric Brun | 4b9e802 | 2014-11-03 18:17:10 +0100 | [diff] [blame] | 601 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 602 | } |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 603 | return 1; |
| 604 | } |
| 605 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 606 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 607 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 608 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 609 | char *pos; |
| 610 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 611 | pos = lltoa_r(smp->data.sint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 612 | if (!pos) |
| 613 | return 0; |
| 614 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 615 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 616 | trash->str = pos; |
| 617 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 618 | smp->data.str = *trash; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 619 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 620 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 621 | return 1; |
| 622 | } |
| 623 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 624 | /* This function duplicates data and removes the flag "const". */ |
| 625 | int smp_dup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 626 | { |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 627 | struct chunk *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 628 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 629 | /* If the const flag is not set, we don't need to duplicate the |
| 630 | * pattern as it can be modified in place. |
| 631 | */ |
| 632 | if (!(smp->flags & SMP_F_CONST)) |
| 633 | return 1; |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 634 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 635 | switch (smp->type) { |
| 636 | case SMP_T_BOOL: |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 637 | case SMP_T_SINT: |
| 638 | case SMP_T_ADDR: |
| 639 | case SMP_T_IPV4: |
| 640 | case SMP_T_IPV6: |
| 641 | /* These type are not const. */ |
| 642 | break; |
| 643 | case SMP_T_STR: |
| 644 | case SMP_T_BIN: |
| 645 | /* Duplicate data. */ |
| 646 | trash = get_trash_chunk(); |
| 647 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 648 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 649 | smp->data.str = *trash; |
| 650 | break; |
| 651 | default: |
| 652 | /* Other cases are unexpected. */ |
| 653 | return 0; |
| 654 | } |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 655 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 656 | /* remove const flag */ |
| 657 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 658 | return 1; |
| 659 | } |
| 660 | |
Thierry FOURNIER | 0e9af55 | 2013-12-14 14:55:04 +0100 | [diff] [blame] | 661 | int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 662 | { |
| 663 | return 1; |
| 664 | } |
| 665 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 666 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 667 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 668 | const char *str; |
| 669 | const char *end; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 670 | |
Thierry FOURNIER | 60bb020 | 2014-01-27 18:20:48 +0100 | [diff] [blame] | 671 | if (smp->data.str.len == 0) |
| 672 | return 0; |
| 673 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 674 | str = smp->data.str.str; |
| 675 | end = smp->data.str.str + smp->data.str.len; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 676 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 677 | smp->data.sint = read_int64(&str, end); |
| 678 | smp->type = SMP_T_SINT; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 679 | smp->flags &= ~SMP_F_CONST; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 680 | return 1; |
| 681 | } |
| 682 | |
Thierry FOURNIER | d437314 | 2013-12-17 01:10:10 +0100 | [diff] [blame] | 683 | static int c_str2meth(struct sample *smp) |
| 684 | { |
| 685 | enum http_meth_t meth; |
| 686 | int len; |
| 687 | |
| 688 | meth = find_http_meth(smp->data.str.str, smp->data.str.len); |
| 689 | if (meth == HTTP_METH_OTHER) { |
| 690 | len = smp->data.str.len; |
| 691 | smp->data.meth.str.str = smp->data.str.str; |
| 692 | smp->data.meth.str.len = len; |
| 693 | } |
| 694 | else |
| 695 | smp->flags &= ~SMP_F_CONST; |
| 696 | smp->data.meth.meth = meth; |
| 697 | smp->type = SMP_T_METH; |
| 698 | return 1; |
| 699 | } |
| 700 | |
| 701 | static int c_meth2str(struct sample *smp) |
| 702 | { |
| 703 | int len; |
| 704 | enum http_meth_t meth; |
| 705 | |
| 706 | if (smp->data.meth.meth == HTTP_METH_OTHER) { |
| 707 | /* The method is unknown. Copy the original pointer. */ |
| 708 | len = smp->data.meth.str.len; |
| 709 | smp->data.str.str = smp->data.meth.str.str; |
| 710 | smp->data.str.len = len; |
| 711 | smp->type = SMP_T_STR; |
| 712 | } |
| 713 | else if (smp->data.meth.meth < HTTP_METH_OTHER) { |
| 714 | /* The method is known, copy the pointer containing the string. */ |
| 715 | meth = smp->data.meth.meth; |
| 716 | smp->data.str.str = http_known_methods[meth].name; |
| 717 | smp->data.str.len = http_known_methods[meth].len; |
| 718 | smp->flags |= SMP_F_CONST; |
| 719 | smp->type = SMP_T_STR; |
| 720 | } |
| 721 | else { |
| 722 | /* Unknown method */ |
| 723 | return 0; |
| 724 | } |
| 725 | return 1; |
| 726 | } |
| 727 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 728 | static int c_addr2bin(struct sample *smp) |
| 729 | { |
| 730 | struct chunk *chk = get_trash_chunk(); |
| 731 | |
| 732 | if (smp->type == SMP_T_IPV4) { |
| 733 | chk->len = 4; |
| 734 | memcpy(chk->str, &smp->data.ipv4, chk->len); |
| 735 | } |
| 736 | else if (smp->type == SMP_T_IPV6) { |
| 737 | chk->len = 16; |
| 738 | memcpy(chk->str, &smp->data.ipv6, chk->len); |
| 739 | } |
| 740 | else |
| 741 | return 0; |
| 742 | |
| 743 | smp->data.str = *chk; |
| 744 | smp->type = SMP_T_BIN; |
| 745 | return 1; |
| 746 | } |
| 747 | |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 748 | static int c_int2bin(struct sample *smp) |
| 749 | { |
| 750 | struct chunk *chk = get_trash_chunk(); |
| 751 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 752 | *(unsigned long long int *)chk->str = htonll(smp->data.sint); |
| 753 | chk->len = 8; |
Willy Tarreau | bbfd1a2 | 2014-07-15 21:19:08 +0200 | [diff] [blame] | 754 | |
| 755 | smp->data.str = *chk; |
| 756 | smp->type = SMP_T_BIN; |
| 757 | return 1; |
| 758 | } |
| 759 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 760 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 761 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 762 | /* Sample casts matrix: */ |
| 763 | /* sample_casts[from type][to type] */ |
| 764 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 765 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 766 | |
Thierry FOURNIER | 8af6ff1 | 2013-11-21 10:53:12 +0100 | [diff] [blame] | 767 | sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 768 | /* to: ANY BOOL SINT ADDR IPV4 IPV6 STR BIN METH */ |
| 769 | /* from: ANY */ { c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, c_none, }, |
| 770 | /* BOOL */ { c_none, c_none, c_none, NULL, NULL, NULL, c_int2str, NULL, NULL, }, |
| 771 | /* SINT */ { c_none, c_none, c_none, c_int2ip, c_int2ip, NULL, c_int2str, c_int2bin, NULL, }, |
| 772 | /* ADDR */ { c_none, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }, |
| 773 | /* IPV4 */ { c_none, NULL, c_ip2int, c_none, c_none, c_ip2ipv6, c_ip2str, c_addr2bin, NULL, }, |
| 774 | /* IPV6 */ { c_none, NULL, NULL, c_none, NULL, c_none, c_ipv62str, c_addr2bin, NULL, }, |
| 775 | /* STR */ { c_none, c_str2int, c_str2int, c_str2addr, c_str2ip, c_str2ipv6, c_none, c_none, c_str2meth, }, |
| 776 | /* BIN */ { c_none, NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_str2meth, }, |
| 777 | /* 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] | 778 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 779 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 780 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 781 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 782 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 783 | * Returns a pointer on allocated sample expression structure. |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 784 | * The caller must have set al->ctx. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 785 | */ |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 786 | 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] | 787 | { |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 788 | const char *begw; /* beginning of word */ |
| 789 | const char *endw; /* end of word */ |
| 790 | const char *endt; /* end of term */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 791 | struct sample_expr *expr; |
| 792 | struct sample_fetch *fetch; |
| 793 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 794 | unsigned long prev_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 795 | char *fkw = NULL; |
| 796 | char *ckw = NULL; |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 797 | int err_arg; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 798 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 799 | begw = str[*idx]; |
| 800 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 801 | |
| 802 | if (endw == begw) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 803 | memprintf(err_msg, "missing fetch method"); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 804 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 805 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 806 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 807 | /* keep a copy of the current fetch keyword for error reporting */ |
| 808 | fkw = my_strndup(begw, endw - begw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 809 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 810 | fetch = find_sample_fetch(begw, endw - begw); |
| 811 | if (!fetch) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 812 | memprintf(err_msg, "unknown fetch method '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 813 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 814 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 815 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 816 | endt = endw; |
| 817 | if (*endt == '(') { |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 818 | /* look for the end of this term and skip the opening parenthesis */ |
| 819 | endt = ++endw; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 820 | while (*endt && *endt != ')') |
| 821 | endt++; |
| 822 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 823 | memprintf(err_msg, "missing closing ')' after arguments to fetch keyword '%s'", fkw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 824 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 825 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 826 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 827 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 828 | /* At this point, we have : |
| 829 | * - begw : beginning of the keyword |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 830 | * - endw : end of the keyword, first character not part of keyword |
| 831 | * nor the opening parenthesis (so first character of args |
| 832 | * if present). |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 833 | * - endt : end of the term (=endw or last parenthesis if args are present) |
| 834 | */ |
| 835 | |
| 836 | if (fetch->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 837 | memprintf(err_msg, "returns type of fetch method '%s' is unknown", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 838 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 839 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 840 | prev_type = fetch->out_type; |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 842 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 843 | if (!expr) |
| 844 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 845 | |
| 846 | LIST_INIT(&(expr->conv_exprs)); |
| 847 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 848 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 849 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 850 | /* Note that we call the argument parser even with an empty string, |
| 851 | * this allows it to automatically create entries for mandatory |
| 852 | * implicit arguments (eg: local proxy name). |
| 853 | */ |
| 854 | al->kw = expr->fetch->kw; |
| 855 | al->conv = NULL; |
| 856 | if (make_arg_list(endw, endt - endw, fetch->arg_mask, &expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 857 | memprintf(err_msg, "fetch method '%s' : %s", fkw, *err_msg); |
| 858 | goto out_error; |
| 859 | } |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 860 | |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 861 | if (!expr->arg_p) { |
| 862 | expr->arg_p = empty_arg_list; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 863 | } |
Willy Tarreau | 689a1df | 2013-12-13 00:40:11 +0100 | [diff] [blame] | 864 | else if (fetch->val_args && !fetch->val_args(expr->arg_p, err_msg)) { |
| 865 | 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] | 866 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 867 | } |
| 868 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 869 | /* Now process the converters if any. We have two supported syntaxes |
| 870 | * for the converters, which can be combined : |
| 871 | * - comma-delimited list of converters just after the keyword and args ; |
| 872 | * - one converter per keyword |
| 873 | * The combination allows to have each keyword being a comma-delimited |
| 874 | * series of converters. |
| 875 | * |
| 876 | * We want to process the former first, then the latter. For this we start |
| 877 | * from the beginning of the supposed place in the exiting conv chain, which |
| 878 | * starts at the last comma (endt). |
| 879 | */ |
| 880 | |
| 881 | while (1) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 882 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 883 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 884 | if (*endt == ')') /* skip last closing parenthesis */ |
| 885 | endt++; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 886 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 887 | if (*endt && *endt != ',') { |
| 888 | if (ckw) |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 889 | memprintf(err_msg, "missing comma after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 890 | else |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 891 | memprintf(err_msg, "missing comma after fetch keyword '%s'", fkw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 892 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 893 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 894 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 895 | while (*endt == ',') /* then trailing commas */ |
| 896 | endt++; |
| 897 | |
| 898 | begw = endt; /* start of conv keyword */ |
| 899 | |
| 900 | if (!*begw) { |
| 901 | /* none ? skip to next string */ |
| 902 | (*idx)++; |
| 903 | begw = str[*idx]; |
| 904 | if (!begw || !*begw) |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | for (endw = begw; *endw && *endw != '(' && *endw != ','; endw++); |
| 909 | |
| 910 | free(ckw); |
| 911 | ckw = my_strndup(begw, endw - begw); |
| 912 | |
| 913 | conv = find_sample_conv(begw, endw - begw); |
| 914 | if (!conv) { |
| 915 | /* we found an isolated keyword that we don't know, it's not ours */ |
| 916 | if (begw == str[*idx]) |
| 917 | break; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 918 | memprintf(err_msg, "unknown conv method '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 919 | goto out_error; |
| 920 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 921 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 922 | endt = endw; |
| 923 | if (*endt == '(') { |
| 924 | /* look for the end of this term */ |
| 925 | while (*endt && *endt != ')') |
| 926 | endt++; |
| 927 | if (*endt != ')') { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 928 | memprintf(err_msg, "syntax error: missing ')' after conv keyword '%s'", ckw); |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 929 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 930 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | if (conv->in_type >= SMP_TYPES || conv->out_type >= SMP_TYPES) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 934 | memprintf(err_msg, "returns type of conv method '%s' is unknown", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 935 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 936 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 937 | |
| 938 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 939 | if (!sample_casts[prev_type][conv->in_type]) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 940 | memprintf(err_msg, "conv method '%s' cannot be applied", ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 941 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 942 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 943 | |
| 944 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 945 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 946 | if (!conv_expr) |
| 947 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 948 | |
| 949 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 950 | conv_expr->conv = conv; |
| 951 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 952 | if (endt != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 953 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 954 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 955 | if (!conv->arg_mask) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 956 | memprintf(err_msg, "conv method '%s' does not support any args", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 957 | goto out_error; |
| 958 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 959 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 960 | al->kw = expr->fetch->kw; |
| 961 | al->conv = conv_expr->conv->kw; |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 962 | if (make_arg_list(endw + 1, endt - endw - 1, conv->arg_mask, &conv_expr->arg_p, err_msg, NULL, &err_arg, al) < 0) { |
| 963 | memprintf(err_msg, "invalid arg %d in conv method '%s' : %s", err_arg+1, ckw, *err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 964 | goto out_error; |
| 965 | } |
| 966 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 967 | if (!conv_expr->arg_p) |
| 968 | conv_expr->arg_p = empty_arg_list; |
| 969 | |
Thierry FOURNIER | eeaa951 | 2014-02-11 14:00:19 +0100 | [diff] [blame] | 970 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, conv, file, line, err_msg)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 971 | memprintf(err_msg, "invalid args in conv method '%s' : %s", ckw, *err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 972 | goto out_error; |
| 973 | } |
| 974 | } |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 975 | else if (ARGM(conv->arg_mask)) { |
Willy Tarreau | 975c178 | 2013-12-12 23:16:54 +0100 | [diff] [blame] | 976 | memprintf(err_msg, "missing args for conv method '%s'", ckw); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 977 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 978 | } |
| 979 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 980 | |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 981 | out: |
| 982 | free(fkw); |
| 983 | free(ckw); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 984 | return expr; |
| 985 | |
| 986 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 987 | /* TODO: prune_sample_expr(expr); */ |
Willy Tarreau | 833cc79 | 2013-07-24 15:34:19 +0200 | [diff] [blame] | 988 | expr = NULL; |
| 989 | goto out; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 993 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 994 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 995 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 996 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 997 | * 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] | 998 | * 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] | 999 | * |
| 1000 | * Note: the fetch functions are required to properly set the return type. The |
| 1001 | * conversion functions must do so too. However the cast functions do not need |
| 1002 | * 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] | 1003 | * |
| 1004 | * The caller may indicate in <opt> if it considers the result final or not. |
| 1005 | * The caller needs to check the SMP_F_MAY_CHANGE flag in p->flags to verify |
| 1006 | * if the result is stable or not, according to the following table : |
| 1007 | * |
| 1008 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1009 | * NULL 0 * Not present and will never be (eg: header) |
| 1010 | * NULL 1 0 Not present yet, could change (eg: POST param) |
| 1011 | * NULL 1 1 Not present yet, will not change anymore |
| 1012 | * smp 0 * Present and will not change (eg: header) |
| 1013 | * smp 1 0 Present, may change (eg: request length) |
| 1014 | * smp 1 1 Present, last known value (eg: request length) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1015 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1016 | struct sample *sample_process(struct proxy *px, struct session *sess, |
| 1017 | struct stream *strm, unsigned int opt, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1018 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1019 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1020 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1021 | |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1022 | if (p == NULL) { |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 1023 | p = &temp_smp; |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1024 | memset(p, 0, sizeof(*p)); |
Willy Tarreau | 18387e2 | 2013-07-25 12:02:38 +0200 | [diff] [blame] | 1025 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1026 | |
Thierry FOURNIER | 6879ad3 | 2015-05-11 11:54:58 +0200 | [diff] [blame] | 1027 | p->px = px; |
| 1028 | p->sess = sess; |
| 1029 | p->strm = strm; |
Thierry FOURNIER | 1d33b88 | 2015-05-11 15:25:29 +0200 | [diff] [blame] | 1030 | p->opt = opt; |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 1031 | 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] | 1032 | return NULL; |
| 1033 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1034 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1035 | /* we want to ensure that p->type can be casted into |
| 1036 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 1037 | * - NULL => not castable. |
| 1038 | * - c_none => nothing to do (let's optimize it) |
| 1039 | * - other => apply cast and prepare to fail |
| 1040 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1041 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1042 | return NULL; |
| 1043 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1044 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 1045 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1046 | return NULL; |
| 1047 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 1048 | /* OK cast succeeded */ |
| 1049 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1050 | 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] | 1051 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1052 | } |
| 1053 | return p; |
| 1054 | } |
| 1055 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1056 | /* |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1057 | * Resolve all remaining arguments in proxy <p>. Returns the number of |
| 1058 | * errors or 0 if everything is fine. |
| 1059 | */ |
| 1060 | int smp_resolve_args(struct proxy *p) |
| 1061 | { |
| 1062 | struct arg_list *cur, *bak; |
| 1063 | const char *ctx, *where; |
| 1064 | const char *conv_ctx, *conv_pre, *conv_pos; |
| 1065 | struct userlist *ul; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1066 | struct my_regex *reg; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1067 | struct arg *arg; |
| 1068 | int cfgerr = 0; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1069 | int rflags; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1070 | |
| 1071 | list_for_each_entry_safe(cur, bak, &p->conf.args.list, list) { |
| 1072 | struct proxy *px; |
| 1073 | struct server *srv; |
| 1074 | char *pname, *sname; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1075 | char *err; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1076 | |
| 1077 | arg = cur->arg; |
| 1078 | |
| 1079 | /* prepare output messages */ |
| 1080 | conv_pre = conv_pos = conv_ctx = ""; |
| 1081 | if (cur->conv) { |
| 1082 | conv_ctx = cur->conv; |
| 1083 | conv_pre = "conversion keyword '"; |
| 1084 | conv_pos = "' for "; |
| 1085 | } |
| 1086 | |
| 1087 | where = "in"; |
| 1088 | ctx = "sample fetch keyword"; |
| 1089 | switch (cur->ctx) { |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1090 | case ARGC_STK: where = "in stick rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1091 | case ARGC_TRK: where = "in tracking rule in"; break; |
| 1092 | case ARGC_LOG: where = "in log-format string in"; break; |
Thierry FOURNIER | 1c0054f | 2013-11-20 15:09:52 +0100 | [diff] [blame] | 1093 | case ARGC_HRQ: where = "in http-request header format string in"; break; |
| 1094 | case ARGC_HRS: where = "in http-response header format string in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1095 | case ARGC_UIF: where = "in unique-id-format string in"; break; |
Thierry FOURNIER | d18cd0f | 2013-11-29 12:15:45 +0100 | [diff] [blame] | 1096 | case ARGC_RDR: where = "in redirect format string in"; break; |
Willy Tarreau | 3a4ac42 | 2014-06-13 16:17:14 +0200 | [diff] [blame] | 1097 | case ARGC_CAP: where = "in capture rule in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1098 | case ARGC_ACL: ctx = "ACL keyword"; break; |
Willy Tarreau | 28d976d | 2015-07-09 11:39:33 +0200 | [diff] [blame] | 1099 | case ARGC_SRV: where = "in server directive in"; break; |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | /* set a few default settings */ |
| 1103 | px = p; |
| 1104 | pname = p->id; |
| 1105 | |
| 1106 | switch (arg->type) { |
| 1107 | case ARGT_SRV: |
| 1108 | if (!arg->data.str.len) { |
| 1109 | Alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1110 | cur->file, cur->line, |
| 1111 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1112 | cfgerr++; |
| 1113 | continue; |
| 1114 | } |
| 1115 | |
| 1116 | /* we support two formats : "bck/srv" and "srv" */ |
| 1117 | sname = strrchr(arg->data.str.str, '/'); |
| 1118 | |
| 1119 | if (sname) { |
| 1120 | *sname++ = '\0'; |
| 1121 | pname = arg->data.str.str; |
| 1122 | |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1123 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1124 | if (!px) { |
| 1125 | Alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1126 | cur->file, cur->line, pname, |
| 1127 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1128 | cfgerr++; |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | else |
| 1133 | sname = arg->data.str.str; |
| 1134 | |
| 1135 | srv = findserver(px, sname); |
| 1136 | if (!srv) { |
| 1137 | 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", |
| 1138 | cur->file, cur->line, sname, pname, |
| 1139 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1140 | cfgerr++; |
| 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | free(arg->data.str.str); |
| 1145 | arg->data.str.str = NULL; |
| 1146 | arg->unresolved = 0; |
| 1147 | arg->data.srv = srv; |
| 1148 | break; |
| 1149 | |
| 1150 | case ARGT_FE: |
| 1151 | if (arg->data.str.len) { |
| 1152 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1153 | px = proxy_fe_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | if (!px) { |
| 1157 | Alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1158 | cur->file, cur->line, pname, |
| 1159 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1160 | cfgerr++; |
| 1161 | break; |
| 1162 | } |
| 1163 | |
| 1164 | if (!(px->cap & PR_CAP_FE)) { |
| 1165 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n", |
| 1166 | cur->file, cur->line, pname, |
| 1167 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1168 | cfgerr++; |
| 1169 | break; |
| 1170 | } |
| 1171 | |
| 1172 | free(arg->data.str.str); |
| 1173 | arg->data.str.str = NULL; |
| 1174 | arg->unresolved = 0; |
| 1175 | arg->data.prx = px; |
| 1176 | break; |
| 1177 | |
| 1178 | case ARGT_BE: |
| 1179 | if (arg->data.str.len) { |
| 1180 | pname = arg->data.str.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1181 | px = proxy_be_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | if (!px) { |
| 1185 | Alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1186 | cur->file, cur->line, pname, |
| 1187 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1188 | cfgerr++; |
| 1189 | break; |
| 1190 | } |
| 1191 | |
| 1192 | if (!(px->cap & PR_CAP_BE)) { |
| 1193 | Alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n", |
| 1194 | cur->file, cur->line, pname, |
| 1195 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1196 | cfgerr++; |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | free(arg->data.str.str); |
| 1201 | arg->data.str.str = NULL; |
| 1202 | arg->unresolved = 0; |
| 1203 | arg->data.prx = px; |
| 1204 | break; |
| 1205 | |
| 1206 | case ARGT_TAB: |
| 1207 | if (arg->data.str.len) { |
| 1208 | pname = arg->data.str.str; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 1209 | px = proxy_tbl_by_name(pname); |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | if (!px) { |
| 1213 | Alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1214 | cur->file, cur->line, pname, |
| 1215 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1216 | cfgerr++; |
| 1217 | break; |
| 1218 | } |
| 1219 | |
| 1220 | if (!px->table.size) { |
| 1221 | Alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1222 | cur->file, cur->line, pname, |
| 1223 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1224 | cfgerr++; |
| 1225 | break; |
| 1226 | } |
| 1227 | |
| 1228 | free(arg->data.str.str); |
| 1229 | arg->data.str.str = NULL; |
| 1230 | arg->unresolved = 0; |
| 1231 | arg->data.prx = px; |
| 1232 | break; |
| 1233 | |
| 1234 | case ARGT_USR: |
| 1235 | if (!arg->data.str.len) { |
| 1236 | Alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1237 | cur->file, cur->line, |
| 1238 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1239 | cfgerr++; |
| 1240 | break; |
| 1241 | } |
| 1242 | |
| 1243 | if (p->uri_auth && p->uri_auth->userlist && |
| 1244 | !strcmp(p->uri_auth->userlist->name, arg->data.str.str)) |
| 1245 | ul = p->uri_auth->userlist; |
| 1246 | else |
| 1247 | ul = auth_find_userlist(arg->data.str.str); |
| 1248 | |
| 1249 | if (!ul) { |
| 1250 | Alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1251 | cur->file, cur->line, arg->data.str.str, |
| 1252 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1253 | cfgerr++; |
| 1254 | break; |
| 1255 | } |
| 1256 | |
| 1257 | free(arg->data.str.str); |
| 1258 | arg->data.str.str = NULL; |
| 1259 | arg->unresolved = 0; |
| 1260 | arg->data.usr = ul; |
| 1261 | break; |
Willy Tarreau | 4694778 | 2015-01-19 19:00:58 +0100 | [diff] [blame] | 1262 | |
| 1263 | case ARGT_REG: |
| 1264 | if (!arg->data.str.len) { |
| 1265 | Alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1266 | cur->file, cur->line, |
| 1267 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1268 | cfgerr++; |
| 1269 | continue; |
| 1270 | } |
| 1271 | |
| 1272 | reg = calloc(1, sizeof(*reg)); |
| 1273 | if (!reg) { |
| 1274 | Alert("parsing [%s:%d] : not enough memory to build regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n", |
| 1275 | cur->file, cur->line, |
| 1276 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id); |
| 1277 | cfgerr++; |
| 1278 | continue; |
| 1279 | } |
| 1280 | |
| 1281 | rflags = 0; |
| 1282 | rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0; |
| 1283 | err = NULL; |
| 1284 | |
Willy Tarreau | d817e46 | 2015-01-23 20:23:17 +0100 | [diff] [blame] | 1285 | 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] | 1286 | Alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n", |
| 1287 | cur->file, cur->line, |
| 1288 | arg->data.str.str, |
| 1289 | cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err); |
| 1290 | cfgerr++; |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
| 1294 | free(arg->data.str.str); |
| 1295 | arg->data.str.str = NULL; |
| 1296 | arg->unresolved = 0; |
| 1297 | arg->data.reg = reg; |
| 1298 | break; |
| 1299 | |
| 1300 | |
Willy Tarreau | a4312fa | 2013-04-02 16:34:32 +0200 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | LIST_DEL(&cur->list); |
| 1304 | free(cur); |
| 1305 | } /* end of args processing */ |
| 1306 | |
| 1307 | return cfgerr; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1311 | * Process a fetch + format conversion as defined by the sample expression |
| 1312 | * <expr> on request or response considering the <opt> parameter. The output is |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1313 | * not explicitly set to <smp_type>, but shall be compatible with it as |
| 1314 | * specified by 'sample_casts' table. If a stable sample can be fetched, or an |
| 1315 | * 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] | 1316 | * returned without the SMP_F_MAY_CHANGE flag. If an unstable sample is found |
| 1317 | * and <opt> does not contain SMP_OPT_FINAL, then the sample is returned as-is |
| 1318 | * with its SMP_F_MAY_CHANGE flag so that the caller can check it and decide to |
| 1319 | * 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] | 1320 | * be converted, NULL is returned. The caller MUST NOT use the sample if the |
| 1321 | * SMP_F_MAY_CHANGE flag is present, as it is used only as a hint that there is |
| 1322 | * still hope to get it after waiting longer, and is not converted to string. |
| 1323 | * The possible output combinations are the following : |
| 1324 | * |
| 1325 | * return MAY_CHANGE FINAL Meaning for the sample |
| 1326 | * NULL * * Not present and will never be (eg: header) |
| 1327 | * smp 0 * Final value converted (eg: header) |
| 1328 | * smp 1 0 Not present yet, may appear later (eg: header) |
| 1329 | * smp 1 1 never happens (either flag is cleared on output) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1330 | */ |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1331 | struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess, |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1332 | struct stream *strm, unsigned int opt, |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1333 | struct sample_expr *expr, int smp_type) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1334 | { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1335 | struct sample *smp = &temp_smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 6c616e0 | 2014-06-25 16:56:41 +0200 | [diff] [blame] | 1337 | memset(smp, 0, sizeof(*smp)); |
| 1338 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 1339 | if (!sample_process(px, sess, strm, opt, expr, smp)) { |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1340 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 1341 | return smp; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1342 | return NULL; |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1343 | } |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1344 | |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1345 | if (!sample_casts[smp->type][smp_type]) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1346 | return NULL; |
| 1347 | |
Adis Nezirovic | 79beb24 | 2015-07-06 15:41:02 +0200 | [diff] [blame] | 1348 | if (!sample_casts[smp->type][smp_type](smp)) |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1349 | return NULL; |
| 1350 | |
Willy Tarreau | 5b4bf70 | 2014-06-13 16:04:35 +0200 | [diff] [blame] | 1351 | smp->flags &= ~SMP_F_MAY_CHANGE; |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 1352 | return smp; |
| 1353 | } |
| 1354 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1355 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 1356 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1357 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1358 | /*****************************************************************/ |
| 1359 | |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 1360 | #ifdef DEBUG_EXPR |
| 1361 | static int sample_conv_debug(const struct arg *arg_p, struct sample *smp, void *private) |
| 1362 | { |
| 1363 | int i; |
| 1364 | struct sample tmp; |
| 1365 | |
| 1366 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 1367 | fprintf(stderr, "[debug converter] type: %s ", smp_to_type[smp->type]); |
| 1368 | if (!sample_casts[smp->type][SMP_T_STR]) { |
| 1369 | fprintf(stderr, "(undisplayable)"); |
| 1370 | } else { |
| 1371 | |
| 1372 | /* Copy sample fetch. This put the sample as const, the |
| 1373 | * cast will copy data if a transformation is required. |
| 1374 | */ |
| 1375 | memcpy(&tmp, smp, sizeof(struct sample)); |
| 1376 | tmp.flags = SMP_F_CONST; |
| 1377 | |
| 1378 | if (!sample_casts[smp->type][SMP_T_STR](&tmp)) |
| 1379 | fprintf(stderr, "(undisplayable)"); |
| 1380 | |
| 1381 | else { |
| 1382 | /* Display the displayable chars*. */ |
| 1383 | fprintf(stderr, "<"); |
| 1384 | for (i = 0; i < tmp.data.str.len; i++) { |
| 1385 | if (isprint(tmp.data.str.str[i])) |
| 1386 | fputc(tmp.data.str.str[i], stderr); |
| 1387 | else |
| 1388 | fputc('.', stderr); |
| 1389 | } |
| 1390 | } |
| 1391 | fprintf(stderr, ">\n"); |
| 1392 | } |
| 1393 | } |
| 1394 | return 1; |
| 1395 | } |
| 1396 | #endif |
| 1397 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1398 | 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] | 1399 | { |
| 1400 | struct chunk *trash = get_trash_chunk(); |
| 1401 | int b64_len; |
| 1402 | |
| 1403 | trash->len = 0; |
| 1404 | b64_len = a2base64(smp->data.str.str, smp->data.str.len, trash->str, trash->size); |
| 1405 | if (b64_len < 0) |
| 1406 | return 0; |
| 1407 | |
| 1408 | trash->len = b64_len; |
| 1409 | smp->data.str = *trash; |
| 1410 | smp->type = SMP_T_STR; |
| 1411 | smp->flags &= ~SMP_F_CONST; |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1415 | 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] | 1416 | { |
| 1417 | struct chunk *trash = get_trash_chunk(); |
| 1418 | unsigned char c; |
| 1419 | int ptr = 0; |
| 1420 | |
| 1421 | trash->len = 0; |
| 1422 | while (ptr < smp->data.str.len && trash->len <= trash->size - 2) { |
| 1423 | c = smp->data.str.str[ptr++]; |
| 1424 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 1425 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 1426 | } |
| 1427 | smp->data.str = *trash; |
| 1428 | smp->type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1429 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 1430 | return 1; |
| 1431 | } |
| 1432 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1433 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1434 | 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] | 1435 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1436 | smp->data.sint = hash_djb2(smp->data.str.str, smp->data.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1437 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1438 | smp->data.sint = full_hash(smp->data.sint); |
| 1439 | smp->type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1440 | return 1; |
| 1441 | } |
| 1442 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1443 | 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] | 1444 | { |
| 1445 | int i; |
| 1446 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1447 | if (!smp_dup(smp)) |
| 1448 | return 0; |
| 1449 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1450 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1451 | return 0; |
| 1452 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1453 | for (i = 0; i < smp->data.str.len; i++) { |
| 1454 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 1455 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1456 | } |
| 1457 | return 1; |
| 1458 | } |
| 1459 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1460 | 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] | 1461 | { |
| 1462 | int i; |
| 1463 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 1464 | if (!smp_dup(smp)) |
| 1465 | return 0; |
| 1466 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1467 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 1468 | return 0; |
| 1469 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1470 | for (i = 0; i < smp->data.str.len; i++) { |
| 1471 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 1472 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1473 | } |
| 1474 | return 1; |
| 1475 | } |
| 1476 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 1477 | /* takes the netmask in arg_p */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1478 | 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] | 1479 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 1480 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 1481 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 1482 | return 1; |
| 1483 | } |
| 1484 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1485 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1486 | * adds an optional offset found in args[1] and emits a string representing |
| 1487 | * the local time in the format specified in args[1] using strftime(). |
| 1488 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1489 | 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] | 1490 | { |
| 1491 | struct chunk *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1492 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
| 1493 | time_t curr_date = smp->data.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1494 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1495 | |
| 1496 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1497 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1498 | curr_date += args[1].data.sint; |
| 1499 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1500 | tm = localtime(&curr_date); |
| 1501 | if (!tm) |
| 1502 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1503 | temp = get_trash_chunk(); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1504 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm); |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1505 | smp->data.str = *temp; |
| 1506 | smp->type = SMP_T_STR; |
| 1507 | return 1; |
| 1508 | } |
| 1509 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1510 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1511 | 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] | 1512 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1513 | smp->data.sint = hash_sdbm(smp->data.str.str, smp->data.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1514 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1515 | smp->data.sint = full_hash(smp->data.sint); |
| 1516 | smp->type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1517 | return 1; |
| 1518 | } |
| 1519 | |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1520 | /* takes an UINT value on input supposed to represent the time since EPOCH, |
| 1521 | * adds an optional offset found in args[1] and emits a string representing |
| 1522 | * the UTC date in the format specified in args[1] using strftime(). |
| 1523 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1524 | 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] | 1525 | { |
| 1526 | struct chunk *temp; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1527 | /* With high numbers, the date returned can be negative, the 55 bits mask prevent this. */ |
| 1528 | time_t curr_date = smp->data.sint & 0x007fffffffffffffLL; |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1529 | struct tm *tm; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1530 | |
| 1531 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1532 | if (args[1].type == ARGT_SINT) |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1533 | curr_date += args[1].data.sint; |
| 1534 | |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1535 | tm = gmtime(&curr_date); |
| 1536 | if (!tm) |
| 1537 | return 0; |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1538 | temp = get_trash_chunk(); |
Thierry FOURNIER | fac9ccf | 2015-07-08 00:15:20 +0200 | [diff] [blame] | 1539 | temp->len = strftime(temp->str, temp->size, args[0].data.str.str, tm); |
Willy Tarreau | 0dbfdba | 2014-07-10 16:37:47 +0200 | [diff] [blame] | 1540 | smp->data.str = *temp; |
| 1541 | smp->type = SMP_T_STR; |
| 1542 | return 1; |
| 1543 | } |
| 1544 | |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1545 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1546 | 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] | 1547 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1548 | smp->data.sint = hash_wt6(smp->data.str.str, smp->data.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1549 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1550 | smp->data.sint = full_hash(smp->data.sint); |
| 1551 | smp->type = SMP_T_SINT; |
Willy Tarreau | 23ec4ca | 2014-07-15 20:15:37 +0200 | [diff] [blame] | 1552 | return 1; |
| 1553 | } |
| 1554 | |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1555 | /* hashes the binary input into a 32-bit unsigned int */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1556 | 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] | 1557 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1558 | smp->data.sint = hash_crc32(smp->data.str.str, smp->data.str.len); |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1559 | if (arg_p && arg_p->data.sint) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1560 | smp->data.sint = full_hash(smp->data.sint); |
| 1561 | smp->type = SMP_T_SINT; |
Willy Tarreau | 8059977 | 2015-01-20 19:35:24 +0100 | [diff] [blame] | 1562 | return 1; |
| 1563 | } |
| 1564 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1565 | /* This function escape special json characters. The returned string can be |
| 1566 | * safely set between two '"' and used as json string. The json string is |
| 1567 | * defined like this: |
| 1568 | * |
| 1569 | * any Unicode character except '"' or '\' or control character |
| 1570 | * \", \\, \/, \b, \f, \n, \r, \t, \u + four-hex-digits |
| 1571 | * |
| 1572 | * The enum input_type contain all the allowed mode for decoding the input |
| 1573 | * string. |
| 1574 | */ |
| 1575 | enum input_type { |
| 1576 | IT_ASCII = 0, |
| 1577 | IT_UTF8, |
| 1578 | IT_UTF8S, |
| 1579 | IT_UTF8P, |
| 1580 | IT_UTF8PS, |
| 1581 | }; |
| 1582 | static int sample_conv_json_check(struct arg *arg, struct sample_conv *conv, |
| 1583 | const char *file, int line, char **err) |
| 1584 | { |
| 1585 | if (!arg) { |
| 1586 | memprintf(err, "Unexpected empty arg list"); |
| 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | if (arg->type != ARGT_STR) { |
| 1591 | memprintf(err, "Unexpected arg type"); |
| 1592 | return 0; |
| 1593 | } |
| 1594 | |
| 1595 | if (strcmp(arg->data.str.str, "") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1596 | arg->type = ARGT_SINT; |
| 1597 | arg->data.sint = IT_ASCII; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1598 | return 1; |
| 1599 | } |
| 1600 | |
| 1601 | else if (strcmp(arg->data.str.str, "ascii") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1602 | arg->type = ARGT_SINT; |
| 1603 | arg->data.sint = IT_ASCII; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1604 | return 1; |
| 1605 | } |
| 1606 | |
| 1607 | else if (strcmp(arg->data.str.str, "utf8") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1608 | arg->type = ARGT_SINT; |
| 1609 | arg->data.sint = IT_UTF8; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1610 | return 1; |
| 1611 | } |
| 1612 | |
| 1613 | else if (strcmp(arg->data.str.str, "utf8s") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1614 | arg->type = ARGT_SINT; |
| 1615 | arg->data.sint = IT_UTF8S; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1616 | return 1; |
| 1617 | } |
| 1618 | |
| 1619 | else if (strcmp(arg->data.str.str, "utf8p") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1620 | arg->type = ARGT_SINT; |
| 1621 | arg->data.sint = IT_UTF8P; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1622 | return 1; |
| 1623 | } |
| 1624 | |
| 1625 | else if (strcmp(arg->data.str.str, "utf8ps") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1626 | arg->type = ARGT_SINT; |
| 1627 | arg->data.sint = IT_UTF8PS; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1628 | return 1; |
| 1629 | } |
| 1630 | |
| 1631 | memprintf(err, "Unexpected input code type at file '%s', line %d. " |
| 1632 | "Allowed value are 'ascii', 'utf8', 'utf8p' and 'utf8pp'", file, line); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1636 | 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] | 1637 | { |
| 1638 | struct chunk *temp; |
| 1639 | char _str[7]; /* \u + 4 hex digit + null char for sprintf. */ |
| 1640 | const char *str; |
| 1641 | int len; |
| 1642 | enum input_type input_type = IT_ASCII; |
| 1643 | unsigned int c; |
| 1644 | unsigned int ret; |
| 1645 | char *p; |
| 1646 | |
| 1647 | if (arg_p) |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1648 | input_type = arg_p->data.sint; |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 1649 | |
| 1650 | temp = get_trash_chunk(); |
| 1651 | temp->len = 0; |
| 1652 | |
| 1653 | p = smp->data.str.str; |
| 1654 | while (p < smp->data.str.str + smp->data.str.len) { |
| 1655 | |
| 1656 | if (input_type == IT_ASCII) { |
| 1657 | /* Read input as ASCII. */ |
| 1658 | c = *(unsigned char *)p; |
| 1659 | p++; |
| 1660 | } |
| 1661 | else { |
| 1662 | /* Read input as UTF8. */ |
| 1663 | ret = utf8_next(p, smp->data.str.len - ( p - smp->data.str.str ), &c); |
| 1664 | p += utf8_return_length(ret); |
| 1665 | |
| 1666 | if (input_type == IT_UTF8 && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1667 | return 0; |
| 1668 | if (input_type == IT_UTF8S && utf8_return_code(ret) != UTF8_CODE_OK) |
| 1669 | continue; |
| 1670 | if (input_type == IT_UTF8P && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1671 | return 0; |
| 1672 | if (input_type == IT_UTF8PS && utf8_return_code(ret) & (UTF8_CODE_INVRANGE|UTF8_CODE_BADSEQ)) |
| 1673 | continue; |
| 1674 | |
| 1675 | /* Check too big values. */ |
| 1676 | if ((unsigned int)c > 0xffff) { |
| 1677 | if (input_type == IT_UTF8 || input_type == IT_UTF8P) |
| 1678 | return 0; |
| 1679 | continue; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | /* Convert character. */ |
| 1684 | if (c == '"') { |
| 1685 | len = 2; |
| 1686 | str = "\\\""; |
| 1687 | } |
| 1688 | else if (c == '\\') { |
| 1689 | len = 2; |
| 1690 | str = "\\\\"; |
| 1691 | } |
| 1692 | else if (c == '/') { |
| 1693 | len = 2; |
| 1694 | str = "\\/"; |
| 1695 | } |
| 1696 | else if (c == '\b') { |
| 1697 | len = 2; |
| 1698 | str = "\\b"; |
| 1699 | } |
| 1700 | else if (c == '\f') { |
| 1701 | len = 2; |
| 1702 | str = "\\f"; |
| 1703 | } |
| 1704 | else if (c == '\r') { |
| 1705 | len = 2; |
| 1706 | str = "\\r"; |
| 1707 | } |
| 1708 | else if (c == '\n') { |
| 1709 | len = 2; |
| 1710 | str = "\\n"; |
| 1711 | } |
| 1712 | else if (c == '\t') { |
| 1713 | len = 2; |
| 1714 | str = "\\t"; |
| 1715 | } |
| 1716 | else if (c > 0xff || !isprint(c)) { |
| 1717 | /* isprint generate a segfault if c is too big. The man says that |
| 1718 | * c must have the value of an unsigned char or EOF. |
| 1719 | */ |
| 1720 | len = 6; |
| 1721 | _str[0] = '\\'; |
| 1722 | _str[1] = 'u'; |
| 1723 | snprintf(&_str[2], 5, "%04x", (unsigned short)c); |
| 1724 | str = _str; |
| 1725 | } |
| 1726 | else { |
| 1727 | len = 1; |
| 1728 | str = (char *)&c; |
| 1729 | } |
| 1730 | |
| 1731 | /* Check length */ |
| 1732 | if (temp->len + len > temp->size) |
| 1733 | return 0; |
| 1734 | |
| 1735 | /* Copy string. */ |
| 1736 | memcpy(temp->str + temp->len, str, len); |
| 1737 | temp->len += len; |
| 1738 | } |
| 1739 | |
| 1740 | smp->flags &= ~SMP_F_CONST; |
| 1741 | smp->data.str = *temp; |
| 1742 | smp->type = SMP_T_STR; |
| 1743 | |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1747 | /* This sample function is designed to extract some bytes from an input buffer. |
| 1748 | * First arg is the offset. |
| 1749 | * Optional second arg is the length to truncate */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1750 | 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] | 1751 | { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1752 | if (smp->data.str.len <= arg_p[0].data.sint) { |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1753 | smp->data.str.len = 0; |
| 1754 | return 1; |
| 1755 | } |
| 1756 | |
| 1757 | if (smp->data.str.size) |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1758 | smp->data.str.size -= arg_p[0].data.sint; |
| 1759 | smp->data.str.len -= arg_p[0].data.sint; |
| 1760 | smp->data.str.str += arg_p[0].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1761 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1762 | if ((arg_p[1].type == ARGT_SINT) && (arg_p[1].data.sint < smp->data.str.len)) |
| 1763 | smp->data.str.len = arg_p[1].data.sint; |
Emeric Brun | 54c4ac8 | 2014-11-03 15:32:43 +0100 | [diff] [blame] | 1764 | |
| 1765 | return 1; |
| 1766 | } |
| 1767 | |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1768 | static int sample_conv_field_check(struct arg *args, struct sample_conv *conv, |
| 1769 | const char *file, int line, char **err) |
| 1770 | { |
| 1771 | struct arg *arg = args; |
| 1772 | |
| 1773 | if (!arg) { |
| 1774 | memprintf(err, "Unexpected empty arg list"); |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1778 | if (arg->type != ARGT_SINT) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1779 | memprintf(err, "Unexpected arg type"); |
| 1780 | return 0; |
| 1781 | } |
| 1782 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1783 | if (!arg->data.sint) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1784 | memprintf(err, "Unexpected value 0 for index"); |
| 1785 | return 0; |
| 1786 | } |
| 1787 | |
| 1788 | arg++; |
| 1789 | |
| 1790 | if (arg->type != ARGT_STR) { |
| 1791 | memprintf(err, "Unexpected arg type"); |
| 1792 | return 0; |
| 1793 | } |
| 1794 | |
| 1795 | if (!arg->data.str.len) { |
| 1796 | memprintf(err, "Empty separators list"); |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
| 1803 | /* This sample function is designed to a return selected part of a string (field). |
| 1804 | * First arg is the index of the field (start at 1) |
| 1805 | * Second arg is a char list of separators (type string) |
| 1806 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1807 | 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] | 1808 | { |
| 1809 | unsigned int field; |
| 1810 | char *start, *end; |
| 1811 | int i; |
| 1812 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1813 | if (!arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1814 | return 0; |
| 1815 | |
| 1816 | field = 1; |
| 1817 | end = start = smp->data.str.str; |
| 1818 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1819 | |
| 1820 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1821 | if (*end == arg_p[1].data.str.str[i]) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1822 | if (field == arg_p[0].data.sint) |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1823 | goto found; |
| 1824 | start = end+1; |
| 1825 | field++; |
| 1826 | break; |
| 1827 | } |
| 1828 | } |
| 1829 | end++; |
| 1830 | } |
| 1831 | |
| 1832 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1833 | if (field != arg_p[0].data.sint) { |
Emeric Brun | f399b0d | 2014-11-03 17:07:03 +0100 | [diff] [blame] | 1834 | smp->data.str.len = 0; |
| 1835 | return 1; |
| 1836 | } |
| 1837 | found: |
| 1838 | smp->data.str.len = end - start; |
| 1839 | /* If ret string is len 0, no need to |
| 1840 | change pointers or to update size */ |
| 1841 | if (!smp->data.str.len) |
| 1842 | return 1; |
| 1843 | |
| 1844 | smp->data.str.str = start; |
| 1845 | |
| 1846 | /* Compute remaining size if needed |
| 1847 | Note: smp->data.str.size cannot be set to 0 */ |
| 1848 | if (smp->data.str.size) |
| 1849 | smp->data.str.size -= start - smp->data.str.str; |
| 1850 | |
| 1851 | return 1; |
| 1852 | } |
| 1853 | |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1854 | /* This sample function is designed to return a word from a string. |
| 1855 | * First arg is the index of the word (start at 1) |
| 1856 | * Second arg is a char list of words separators (type string) |
| 1857 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1858 | 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] | 1859 | { |
| 1860 | unsigned int word; |
| 1861 | char *start, *end; |
| 1862 | int i, issep, inword; |
| 1863 | |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1864 | if (!arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1865 | return 0; |
| 1866 | |
| 1867 | word = 0; |
| 1868 | inword = 0; |
| 1869 | end = start = smp->data.str.str; |
| 1870 | while (end - smp->data.str.str < smp->data.str.len) { |
| 1871 | issep = 0; |
| 1872 | for (i = 0 ; i < arg_p[1].data.str.len ; i++) { |
| 1873 | if (*end == arg_p[1].data.str.str[i]) { |
| 1874 | issep = 1; |
| 1875 | break; |
| 1876 | } |
| 1877 | } |
| 1878 | if (!inword) { |
| 1879 | if (!issep) { |
| 1880 | word++; |
| 1881 | start = end; |
| 1882 | inword = 1; |
| 1883 | } |
| 1884 | } |
| 1885 | else if (issep) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1886 | if (word == arg_p[0].data.sint) |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1887 | goto found; |
| 1888 | inword = 0; |
| 1889 | } |
| 1890 | end++; |
| 1891 | } |
| 1892 | |
| 1893 | /* Field not found */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 1894 | if (word != arg_p[0].data.sint) { |
Emeric Brun | c9a0f6d | 2014-11-25 14:09:01 +0100 | [diff] [blame] | 1895 | smp->data.str.len = 0; |
| 1896 | return 1; |
| 1897 | } |
| 1898 | found: |
| 1899 | smp->data.str.len = end - start; |
| 1900 | /* If ret string is len 0, no need to |
| 1901 | change pointers or to update size */ |
| 1902 | if (!smp->data.str.len) |
| 1903 | return 1; |
| 1904 | |
| 1905 | smp->data.str.str = start; |
| 1906 | |
| 1907 | /* Compute remaining size if needed |
| 1908 | Note: smp->data.str.size cannot be set to 0 */ |
| 1909 | if (smp->data.str.size) |
| 1910 | smp->data.str.size -= start - smp->data.str.str; |
| 1911 | |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
Willy Tarreau | 7eda849 | 2015-01-20 19:47:06 +0100 | [diff] [blame] | 1915 | static int sample_conv_regsub_check(struct arg *args, struct sample_conv *conv, |
| 1916 | const char *file, int line, char **err) |
| 1917 | { |
| 1918 | struct arg *arg = args; |
| 1919 | char *p; |
| 1920 | int len; |
| 1921 | |
| 1922 | /* arg0 is a regex, it uses type_flag for ICASE and global match */ |
| 1923 | arg[0].type_flags = 0; |
| 1924 | |
| 1925 | if (arg[2].type != ARGT_STR) |
| 1926 | return 1; |
| 1927 | |
| 1928 | p = arg[2].data.str.str; |
| 1929 | len = arg[2].data.str.len; |
| 1930 | while (len) { |
| 1931 | if (*p == 'i') { |
| 1932 | arg[0].type_flags |= ARGF_REG_ICASE; |
| 1933 | } |
| 1934 | else if (*p == 'g') { |
| 1935 | arg[0].type_flags |= ARGF_REG_GLOB; |
| 1936 | } |
| 1937 | else { |
| 1938 | memprintf(err, "invalid regex flag '%c', only 'i' and 'g' are supported", *p); |
| 1939 | return 0; |
| 1940 | } |
| 1941 | p++; |
| 1942 | len--; |
| 1943 | } |
| 1944 | return 1; |
| 1945 | } |
| 1946 | |
| 1947 | /* This sample function is designed to do the equivalent of s/match/replace/ on |
| 1948 | * the input string. It applies a regex and restarts from the last matched |
| 1949 | * location until nothing matches anymore. First arg is the regex to apply to |
| 1950 | * the input string, second arg is the replacement expression. |
| 1951 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1952 | 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] | 1953 | { |
| 1954 | char *start, *end; |
| 1955 | struct my_regex *reg = arg_p[0].data.reg; |
| 1956 | regmatch_t pmatch[MAX_MATCH]; |
| 1957 | struct chunk *trash = get_trash_chunk(); |
| 1958 | int flag, max; |
| 1959 | int found; |
| 1960 | |
| 1961 | start = smp->data.str.str; |
| 1962 | end = start + smp->data.str.len; |
| 1963 | |
| 1964 | flag = 0; |
| 1965 | while (1) { |
| 1966 | /* check for last round which is used to copy remaining parts |
| 1967 | * when not running in global replacement mode. |
| 1968 | */ |
| 1969 | found = 0; |
| 1970 | if ((arg_p[0].type_flags & ARGF_REG_GLOB) || !(flag & REG_NOTBOL)) { |
| 1971 | /* Note: we can have start == end on empty strings or at the end */ |
| 1972 | found = regex_exec_match2(reg, start, end - start, MAX_MATCH, pmatch, flag); |
| 1973 | } |
| 1974 | |
| 1975 | if (!found) |
| 1976 | pmatch[0].rm_so = end - start; |
| 1977 | |
| 1978 | /* copy the heading non-matching part (which may also be the tail if nothing matches) */ |
| 1979 | max = trash->size - trash->len; |
| 1980 | if (max && pmatch[0].rm_so > 0) { |
| 1981 | if (max > pmatch[0].rm_so) |
| 1982 | max = pmatch[0].rm_so; |
| 1983 | memcpy(trash->str + trash->len, start, max); |
| 1984 | trash->len += max; |
| 1985 | } |
| 1986 | |
| 1987 | if (!found) |
| 1988 | break; |
| 1989 | |
| 1990 | /* replace the matching part */ |
| 1991 | max = trash->size - trash->len; |
| 1992 | if (max) { |
| 1993 | if (max > arg_p[1].data.str.len) |
| 1994 | max = arg_p[1].data.str.len; |
| 1995 | memcpy(trash->str + trash->len, arg_p[1].data.str.str, max); |
| 1996 | trash->len += max; |
| 1997 | } |
| 1998 | |
| 1999 | /* stop here if we're done with this string */ |
| 2000 | if (start >= end) |
| 2001 | break; |
| 2002 | |
| 2003 | /* We have a special case for matches of length 0 (eg: "x*y*"). |
| 2004 | * These ones are considered to match in front of a character, |
| 2005 | * so we have to copy that character and skip to the next one. |
| 2006 | */ |
| 2007 | if (!pmatch[0].rm_eo) { |
| 2008 | if (trash->len < trash->size) |
| 2009 | trash->str[trash->len++] = start[pmatch[0].rm_eo]; |
| 2010 | pmatch[0].rm_eo++; |
| 2011 | } |
| 2012 | |
| 2013 | start += pmatch[0].rm_eo; |
| 2014 | flag |= REG_NOTBOL; |
| 2015 | } |
| 2016 | |
| 2017 | smp->data.str = *trash; |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2021 | /* This function check an operator entry. It expects a string. |
| 2022 | * The string can be an integer or a variable name. |
| 2023 | */ |
| 2024 | static int check_operator(struct arg *args, struct sample_conv *conv, |
| 2025 | const char *file, int line, char **err) |
| 2026 | { |
| 2027 | const char *str; |
| 2028 | const char *end; |
| 2029 | |
| 2030 | /* Try to decode a variable. */ |
| 2031 | if (vars_check_arg(&args[0], NULL)) |
| 2032 | return 1; |
| 2033 | |
| 2034 | /* Try to convert an integer */ |
| 2035 | str = args[0].data.str.str; |
| 2036 | end = str + strlen(str); |
| 2037 | args[0].data.sint = read_int64(&str, end); |
| 2038 | if (*str != '\0') { |
| 2039 | memprintf(err, "expects an integer or a variable name"); |
| 2040 | return 0; |
| 2041 | } |
| 2042 | args[0].type = ARGT_SINT; |
| 2043 | return 1; |
| 2044 | } |
| 2045 | |
| 2046 | /* This fucntion returns a sample struct filled with a arg content. |
| 2047 | * If the arg contain an integer, the integer is returned in the |
| 2048 | * sample. If the arg contains a variable descriptor, it returns the |
| 2049 | * variable value. |
| 2050 | * |
| 2051 | * This function returns 0 if an error occurs, otherwise it returns 1. |
| 2052 | */ |
| 2053 | static inline int sample_conv_var2smp(const struct arg *arg, struct stream *strm, struct sample *smp) |
| 2054 | { |
| 2055 | switch (arg->type) { |
| 2056 | case ARGT_SINT: |
| 2057 | smp->type = SMP_T_SINT; |
| 2058 | smp->data.sint = arg->data.sint; |
| 2059 | return 1; |
| 2060 | case ARGT_VAR: |
| 2061 | if (!vars_get_by_desc(&arg->data.var, strm, smp)) |
| 2062 | return 0; |
| 2063 | if (!sample_casts[smp->type][SMP_T_SINT]) |
| 2064 | return 0; |
| 2065 | if (!sample_casts[smp->type][SMP_T_SINT](smp)) |
| 2066 | return 0; |
| 2067 | return 1; |
| 2068 | default: |
| 2069 | return 0; |
| 2070 | } |
| 2071 | } |
| 2072 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2073 | /* 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] | 2074 | * result. |
| 2075 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2076 | 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] | 2077 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2078 | smp->data.sint = ~smp->data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2079 | return 1; |
| 2080 | } |
| 2081 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2082 | /* Takes a SINT on input, applies a binary "and" with the SINT directly in |
| 2083 | * 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] | 2084 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2085 | 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] | 2086 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2087 | struct sample tmp; |
| 2088 | |
| 2089 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2090 | return 0; |
| 2091 | smp->data.sint &= tmp.data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2092 | return 1; |
| 2093 | } |
| 2094 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2095 | /* Takes a SINT on input, applies a binary "or" with the SINT directly in |
| 2096 | * 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] | 2097 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2098 | 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] | 2099 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2100 | struct sample tmp; |
| 2101 | |
| 2102 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2103 | return 0; |
| 2104 | smp->data.sint |= tmp.data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2105 | return 1; |
| 2106 | } |
| 2107 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2108 | /* Takes a SINT on input, applies a binary "xor" with the SINT directly in |
| 2109 | * 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] | 2110 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2111 | 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] | 2112 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2113 | struct sample tmp; |
| 2114 | |
| 2115 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2116 | return 0; |
| 2117 | smp->data.sint ^= tmp.data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2118 | return 1; |
| 2119 | } |
| 2120 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2121 | static inline long long int arith_add(long long int a, long long int b) |
| 2122 | { |
| 2123 | /* Prevent overflow and makes capped calculus. |
| 2124 | * We must ensure that the check calculus doesn't |
| 2125 | * exceed the signed 64 bits limits. |
| 2126 | * |
| 2127 | * +----------+----------+ |
| 2128 | * | a<0 | a>=0 | |
| 2129 | * +------+----------+----------+ |
| 2130 | * | b<0 | MIN-a>b | no check | |
| 2131 | * +------+----------+----------+ |
| 2132 | * | b>=0 | no check | MAX-a<b | |
| 2133 | * +------+----------+----------+ |
| 2134 | */ |
| 2135 | if ((a ^ b) >= 0) { |
| 2136 | /* signs are differents. */ |
| 2137 | if (a < 0) { |
| 2138 | if (LLONG_MIN - a > b) |
| 2139 | return LLONG_MIN; |
| 2140 | } |
| 2141 | if (LLONG_MAX - a < b) |
| 2142 | return LLONG_MAX; |
| 2143 | } |
| 2144 | return a + b; |
| 2145 | } |
| 2146 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2147 | /* Takes a SINT on input, applies an arithmetic "add" 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_arith_add(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 | |
| 2154 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2155 | return 0; |
| 2156 | smp->data.sint = arith_add(smp->data.sint, tmp.data.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2157 | return 1; |
| 2158 | } |
| 2159 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2160 | /* Takes a SINT on input, applies an arithmetic "sub" with the SINT directly in |
| 2161 | * 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] | 2162 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2163 | static int sample_conv_arith_sub(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2164 | 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 | |
| 2168 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2169 | return 0; |
| 2170 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2171 | /* We cannot represent -LLONG_MIN because abs(LLONG_MIN) is greater |
| 2172 | * than abs(LLONG_MAX). So, the following code use LLONG_MAX in place |
| 2173 | * of -LLONG_MIN and correct the result. |
| 2174 | */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2175 | if (tmp.data.sint == LLONG_MIN) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2176 | smp->data.sint = arith_add(smp->data.sint, LLONG_MAX); |
| 2177 | if (smp->data.sint < LLONG_MAX) |
| 2178 | smp->data.sint++; |
| 2179 | return 1; |
| 2180 | } |
| 2181 | |
| 2182 | /* standard substraction: we use the "add" function and negate |
| 2183 | * the second operand. |
| 2184 | */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2185 | smp->data.sint = arith_add(smp->data.sint, -tmp.data.sint); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2186 | return 1; |
| 2187 | } |
| 2188 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2189 | /* Takes a SINT on input, applies an arithmetic "mul" with the SINT directly in |
| 2190 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2191 | * If the result makes an overflow, then the largest possible quantity is |
| 2192 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2193 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2194 | static int sample_conv_arith_mul(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2195 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2196 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2197 | struct sample tmp; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2198 | long long int c; |
| 2199 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2200 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2201 | return 0; |
| 2202 | |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2203 | /* prevent divide by 0 during the check */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2204 | if (!smp->data.sint || !tmp.data.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2205 | smp->data.sint = 0; |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | /* The multiply between LLONG_MIN and -1 returns a |
| 2210 | * "floting point exception". |
| 2211 | */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2212 | if (smp->data.sint == LLONG_MIN && tmp.data.sint == -1) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2213 | smp->data.sint = LLONG_MAX; |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
| 2217 | /* execute standard multiplication. */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2218 | c = smp->data.sint * tmp.data.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2219 | |
| 2220 | /* check for overflow and makes capped multiply. */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2221 | if (smp->data.sint != c / tmp.data.sint) { |
| 2222 | if ((smp->data.sint < 0) == (tmp.data.sint < 0)) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2223 | smp->data.sint = LLONG_MAX; |
| 2224 | return 1; |
| 2225 | } |
| 2226 | smp->data.sint = LLONG_MIN; |
| 2227 | return 1; |
| 2228 | } |
| 2229 | smp->data.sint = c; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2230 | return 1; |
| 2231 | } |
| 2232 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2233 | /* Takes a SINT on input, applies an arithmetic "div" with the SINT directly in |
| 2234 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2235 | * If arg_p makes the result overflow, then the largest possible quantity is |
| 2236 | * returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2237 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2238 | static int sample_conv_arith_div(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2239 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2240 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2241 | struct sample tmp; |
| 2242 | |
| 2243 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2244 | return 0; |
| 2245 | |
| 2246 | if (tmp.data.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2247 | /* The divide between LLONG_MIN and -1 returns a |
| 2248 | * "floting point exception". |
| 2249 | */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2250 | if (smp->data.sint == LLONG_MIN && tmp.data.sint == -1) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2251 | smp->data.sint = LLONG_MAX; |
| 2252 | return 1; |
| 2253 | } |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2254 | smp->data.sint /= tmp.data.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2255 | return 1; |
| 2256 | } |
| 2257 | smp->data.sint = LLONG_MAX; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2258 | return 1; |
| 2259 | } |
| 2260 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2261 | /* Takes a SINT on input, applies an arithmetic "mod" with the SINT directly in |
| 2262 | * arg_p or in the varaible described in arg_p, and returns the SINT result. |
| 2263 | * If arg_p makes the result overflow, then 0 is returned. |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2264 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2265 | static int sample_conv_arith_mod(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2266 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2267 | { |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2268 | struct sample tmp; |
| 2269 | |
| 2270 | if (!sample_conv_var2smp(arg_p, smp->strm, &tmp)) |
| 2271 | return 0; |
| 2272 | |
| 2273 | if (tmp.data.sint) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2274 | /* The divide between LLONG_MIN and -1 returns a |
| 2275 | * "floting point exception". |
| 2276 | */ |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2277 | if (smp->data.sint == LLONG_MIN && tmp.data.sint == -1) { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2278 | smp->data.sint = 0; |
| 2279 | return 1; |
| 2280 | } |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2281 | smp->data.sint %= tmp.data.sint; |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2282 | return 1; |
| 2283 | } |
| 2284 | smp->data.sint = 0; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2285 | return 1; |
| 2286 | } |
| 2287 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2288 | /* 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] | 2289 | * result. |
| 2290 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2291 | static int sample_conv_arith_neg(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2292 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2293 | { |
Thierry FOURNIER | 00c005c | 2015-07-08 01:10:21 +0200 | [diff] [blame] | 2294 | if (smp->data.sint == LLONG_MIN) |
| 2295 | smp->data.sint = LLONG_MAX; |
| 2296 | else |
| 2297 | smp->data.sint = -smp->data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2298 | return 1; |
| 2299 | } |
| 2300 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2301 | /* 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] | 2302 | * false. The output is a BOOL. |
| 2303 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2304 | static int sample_conv_arith_bool(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2305 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2306 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2307 | smp->data.sint = !!smp->data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2308 | smp->type = SMP_T_BOOL; |
| 2309 | return 1; |
| 2310 | } |
| 2311 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2312 | /* 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] | 2313 | * truee. The output is a BOOL. |
| 2314 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2315 | static int sample_conv_arith_not(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2316 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2317 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2318 | smp->data.sint = !smp->data.sint; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2319 | smp->type = SMP_T_BOOL; |
| 2320 | return 1; |
| 2321 | } |
| 2322 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2323 | /* 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] | 2324 | * The output is a BOOL. |
| 2325 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2326 | static int sample_conv_arith_odd(const struct arg *arg_p, |
Thierry FOURNIER | 68a556e | 2015-02-23 15:11:11 +0100 | [diff] [blame] | 2327 | struct sample *smp, void *private) |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2328 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2329 | smp->data.sint = smp->data.sint & 1; |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2330 | smp->type = SMP_T_BOOL; |
| 2331 | return 1; |
| 2332 | } |
| 2333 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2334 | /* 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] | 2335 | * The output is a BOOL. |
| 2336 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 2337 | static int sample_conv_arith_even(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 | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2340 | smp->data.sint = !(smp->data.sint & 1); |
Willy Tarreau | 9770787 | 2015-01-27 15:12:13 +0100 | [diff] [blame] | 2341 | smp->type = SMP_T_BOOL; |
| 2342 | return 1; |
| 2343 | } |
| 2344 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2345 | /************************************************************************/ |
| 2346 | /* All supported sample fetch functions must be declared here */ |
| 2347 | /************************************************************************/ |
| 2348 | |
| 2349 | /* force TRUE to be returned at the fetch level */ |
| 2350 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2351 | 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] | 2352 | { |
| 2353 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2354 | smp->data.sint = 1; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2355 | return 1; |
| 2356 | } |
| 2357 | |
| 2358 | /* force FALSE to be returned at the fetch level */ |
| 2359 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2360 | 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] | 2361 | { |
| 2362 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2363 | smp->data.sint = 0; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2364 | return 1; |
| 2365 | } |
| 2366 | |
| 2367 | /* retrieve environment variable $1 as a string */ |
| 2368 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2369 | 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] | 2370 | { |
| 2371 | char *env; |
| 2372 | |
| 2373 | if (!args || args[0].type != ARGT_STR) |
| 2374 | return 0; |
| 2375 | |
| 2376 | env = getenv(args[0].data.str.str); |
| 2377 | if (!env) |
| 2378 | return 0; |
| 2379 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 2380 | smp->type = SMP_T_STR; |
| 2381 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2382 | smp->data.str.str = env; |
| 2383 | smp->data.str.len = strlen(env); |
| 2384 | return 1; |
| 2385 | } |
| 2386 | |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2387 | /* retrieve the current local date in epoch time, and applies an optional offset |
| 2388 | * of args[0] seconds. |
| 2389 | */ |
| 2390 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2391 | 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] | 2392 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2393 | smp->data.sint = date.tv_sec; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2394 | |
| 2395 | /* add offset */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2396 | if (args && args[0].type == ARGT_SINT) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2397 | smp->data.sint += args[0].data.sint; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2398 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2399 | smp->type = SMP_T_SINT; |
Willy Tarreau | 6236d3a | 2013-07-25 14:28:25 +0200 | [diff] [blame] | 2400 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2401 | return 1; |
| 2402 | } |
| 2403 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2404 | /* returns the number of processes */ |
| 2405 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2406 | 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] | 2407 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2408 | smp->type = SMP_T_SINT; |
| 2409 | smp->data.sint = global.nbproc; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2410 | return 1; |
| 2411 | } |
| 2412 | |
| 2413 | /* returns the number of the current process (between 1 and nbproc */ |
| 2414 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2415 | 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] | 2416 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2417 | smp->type = SMP_T_SINT; |
| 2418 | smp->data.sint = relative_pid; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2419 | return 1; |
| 2420 | } |
| 2421 | |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2422 | /* generate a random 32-bit integer for whatever purpose, with an optional |
| 2423 | * range specified in argument. |
| 2424 | */ |
| 2425 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2426 | 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] | 2427 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2428 | smp->data.sint = random(); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2429 | |
| 2430 | /* reduce if needed. Don't do a modulo, use all bits! */ |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2431 | if (args && args[0].type == ARGT_SINT) |
| 2432 | smp->data.sint = (smp->data.sint * args[0].data.sint) / ((u64)RAND_MAX+1); |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2433 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2434 | smp->type = SMP_T_SINT; |
Willy Tarreau | 84310e2 | 2014-02-14 11:59:04 +0100 | [diff] [blame] | 2435 | smp->flags |= SMP_F_VOL_TEST | SMP_F_MAY_CHANGE; |
| 2436 | return 1; |
| 2437 | } |
| 2438 | |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2439 | /* returns true if the current process is stopping */ |
| 2440 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 2441 | 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] | 2442 | { |
| 2443 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2444 | smp->data.sint = stopping; |
Willy Tarreau | 0f30d26 | 2014-11-24 16:02:05 +0100 | [diff] [blame] | 2445 | return 1; |
| 2446 | } |
| 2447 | |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2448 | static int smp_fetch_const_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2449 | { |
| 2450 | smp->flags |= SMP_F_CONST; |
| 2451 | smp->type = SMP_T_STR; |
| 2452 | smp->data.str.str = args[0].data.str.str; |
| 2453 | smp->data.str.len = args[0].data.str.len; |
| 2454 | return 1; |
| 2455 | } |
| 2456 | |
| 2457 | static int smp_check_const_bool(struct arg *args, char **err) |
| 2458 | { |
| 2459 | if (strcasecmp(args[0].data.str.str, "true") == 0 || |
| 2460 | strcasecmp(args[0].data.str.str, "1") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2461 | args[0].type = ARGT_SINT; |
| 2462 | args[0].data.sint = 1; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2463 | return 1; |
| 2464 | } |
| 2465 | if (strcasecmp(args[0].data.str.str, "false") == 0 || |
| 2466 | strcasecmp(args[0].data.str.str, "0") == 0) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2467 | args[0].type = ARGT_SINT; |
| 2468 | args[0].data.sint = 0; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2469 | return 1; |
| 2470 | } |
| 2471 | memprintf(err, "Expects 'true', 'false', '0' or '1'"); |
| 2472 | return 0; |
| 2473 | } |
| 2474 | |
| 2475 | static int smp_fetch_const_bool(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2476 | { |
| 2477 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2478 | smp->data.sint = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2479 | return 1; |
| 2480 | } |
| 2481 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2482 | 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] | 2483 | { |
| 2484 | smp->type = SMP_T_SINT; |
| 2485 | smp->data.sint = args[0].data.sint; |
| 2486 | return 1; |
| 2487 | } |
| 2488 | |
| 2489 | static int smp_fetch_const_ipv4(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2490 | { |
| 2491 | smp->type = SMP_T_IPV4; |
| 2492 | smp->data.ipv4 = args[0].data.ipv4; |
| 2493 | return 1; |
| 2494 | } |
| 2495 | |
| 2496 | static int smp_fetch_const_ipv6(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2497 | { |
| 2498 | smp->type = SMP_T_IPV6; |
| 2499 | smp->data.ipv6 = args[0].data.ipv6; |
| 2500 | return 1; |
| 2501 | } |
| 2502 | |
| 2503 | static int smp_check_const_bin(struct arg *args, char **err) |
| 2504 | { |
| 2505 | char *binstr; |
| 2506 | int binstrlen; |
| 2507 | |
| 2508 | if (!parse_binary(args[0].data.str.str, &binstr, &binstrlen, err)) |
| 2509 | return 0; |
| 2510 | args[0].type = ARGT_STR; |
| 2511 | args[0].data.str.str = binstr; |
| 2512 | args[0].data.str.len = binstrlen; |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
| 2516 | static int smp_fetch_const_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2517 | { |
| 2518 | smp->flags |= SMP_F_CONST; |
| 2519 | smp->type = SMP_T_BIN; |
| 2520 | smp->data.str.str = args[0].data.str.str; |
| 2521 | smp->data.str.len = args[0].data.str.len; |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
| 2525 | static int smp_check_const_meth(struct arg *args, char **err) |
| 2526 | { |
| 2527 | enum http_meth_t meth; |
| 2528 | int i; |
| 2529 | |
| 2530 | meth = find_http_meth(args[0].data.str.str, args[0].data.str.len); |
| 2531 | if (meth != HTTP_METH_OTHER) { |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2532 | args[0].type = ARGT_SINT; |
| 2533 | args[0].data.sint = meth; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2534 | } else { |
| 2535 | /* Check method avalaibility. A methos is a token defined as : |
| 2536 | * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "." / |
| 2537 | * "^" / "_" / "`" / "|" / "~" / DIGIT / ALPHA |
| 2538 | * token = 1*tchar |
| 2539 | */ |
| 2540 | for (i = 0; i < args[0].data.str.len; i++) { |
| 2541 | if (!http_is_token[(unsigned char)args[0].data.str.str[i]]) { |
| 2542 | memprintf(err, "expects valid method."); |
| 2543 | return 0; |
| 2544 | } |
| 2545 | } |
| 2546 | } |
| 2547 | return 1; |
| 2548 | } |
| 2549 | |
| 2550 | static int smp_fetch_const_meth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2551 | { |
| 2552 | smp->type = SMP_T_METH; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2553 | if (args[0].type == ARGT_SINT) { |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2554 | smp->flags &= ~SMP_F_CONST; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2555 | smp->data.meth.meth = args[0].data.sint; |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2556 | smp->data.meth.str.str = ""; |
| 2557 | smp->data.meth.str.len = 0; |
| 2558 | } else { |
| 2559 | smp->flags |= SMP_F_CONST; |
| 2560 | smp->data.meth.meth = HTTP_METH_OTHER; |
| 2561 | smp->data.meth.str.str = args[0].data.str.str; |
| 2562 | smp->data.meth.str.len = args[0].data.str.len; |
| 2563 | } |
| 2564 | return 1; |
| 2565 | } |
| 2566 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2567 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2568 | * Note: fetches that may return multiple types must be declared as the lowest |
| 2569 | * common denominator, the type that can be casted into all other ones. For |
| 2570 | * instance IPv4/IPv6 must be declared IPv4. |
| 2571 | */ |
| 2572 | static struct sample_fetch_kw_list smp_kws = {ILH, { |
| 2573 | { "always_false", smp_fetch_false, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
| 2574 | { "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] | 2575 | { "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] | 2576 | { "date", smp_fetch_date, ARG1(0,SINT), NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 2577 | { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
| 2578 | { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 2579 | { "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] | 2580 | { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, |
Thierry FOURNIER | cc10329 | 2015-06-06 19:30:17 +0200 | [diff] [blame] | 2581 | |
| 2582 | { "str", smp_fetch_const_str, ARG1(1,STR), NULL , SMP_T_STR, SMP_USE_INTRN }, |
| 2583 | { "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] | 2584 | { "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] | 2585 | { "ipv4", smp_fetch_const_ipv4, ARG1(1,IPV4), NULL , SMP_T_IPV4, SMP_USE_INTRN }, |
| 2586 | { "ipv6", smp_fetch_const_ipv6, ARG1(1,IPV6), NULL , SMP_T_IPV6, SMP_USE_INTRN }, |
| 2587 | { "bin", smp_fetch_const_bin, ARG1(1,STR), smp_check_const_bin , SMP_T_BIN, SMP_USE_INTRN }, |
| 2588 | { "meth", smp_fetch_const_meth, ARG1(1,STR), smp_check_const_meth, SMP_T_METH, SMP_USE_INTRN }, |
| 2589 | |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2590 | { /* END */ }, |
| 2591 | }}; |
| 2592 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2593 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 2594 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Thierry FOURNIER | 9687c77 | 2015-05-07 15:46:29 +0200 | [diff] [blame] | 2595 | #ifdef DEBUG_EXPR |
| 2596 | { "debug", sample_conv_debug, 0, NULL, SMP_T_ANY, SMP_T_ANY }, |
| 2597 | #endif |
| 2598 | |
Emeric Brun | 53d1a98 | 2014-04-30 18:21:37 +0200 | [diff] [blame] | 2599 | { "base64", sample_conv_bin2base64,0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2600 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 2601 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
Thierry FOURNIER | 2f49d6d | 2014-03-12 15:01:52 +0100 | [diff] [blame] | 2602 | { "hex", sample_conv_bin2hex, 0, NULL, SMP_T_BIN, SMP_T_STR }, |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2603 | { "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] | 2604 | { "ltime", sample_conv_ltime, ARG2(1,STR,SINT), NULL, SMP_T_SINT, SMP_T_STR }, |
| 2605 | { "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] | 2606 | { "crc32", sample_conv_crc32, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2607 | { "djb2", sample_conv_djb2, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2608 | { "sdbm", sample_conv_sdbm, ARG1(0,SINT), NULL, SMP_T_BIN, SMP_T_SINT }, |
| 2609 | { "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] | 2610 | { "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] | 2611 | { "bytes", sample_conv_bytes, ARG2(1,SINT,SINT), NULL, SMP_T_BIN, SMP_T_BIN }, |
| 2612 | { "field", sample_conv_field, ARG2(2,SINT,STR), sample_conv_field_check, SMP_T_STR, SMP_T_STR }, |
| 2613 | { "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] | 2614 | { "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] | 2615 | |
Thierry FOURNIER | 5d86fae | 2015-07-07 21:10:16 +0200 | [diff] [blame] | 2616 | { "and", sample_conv_binary_and, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2617 | { "or", sample_conv_binary_or, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2618 | { "xor", sample_conv_binary_xor, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2619 | { "cpl", sample_conv_binary_cpl, 0, NULL, SMP_T_SINT, SMP_T_SINT }, |
| 2620 | { "bool", sample_conv_arith_bool, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2621 | { "not", sample_conv_arith_not, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2622 | { "odd", sample_conv_arith_odd, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2623 | { "even", sample_conv_arith_even, 0, NULL, SMP_T_SINT, SMP_T_BOOL }, |
| 2624 | { "add", sample_conv_arith_add, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2625 | { "sub", sample_conv_arith_sub, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2626 | { "mul", sample_conv_arith_mul, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2627 | { "div", sample_conv_arith_div, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2628 | { "mod", sample_conv_arith_mod, ARG1(1,STR), check_operator, SMP_T_SINT, SMP_T_SINT }, |
| 2629 | { "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] | 2630 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 2631 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2632 | }}; |
| 2633 | |
| 2634 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2635 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2636 | { |
Willy Tarreau | 5b8ad22 | 2013-07-25 12:17:57 +0200 | [diff] [blame] | 2637 | /* register sample fetch and format conversion keywords */ |
| 2638 | sample_register_fetches(&smp_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 2639 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 2640 | } |