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 | |
| 14 | #include <string.h> |
| 15 | #include <arpa/inet.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 16 | #include <stdio.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 17 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 18 | #include <types/global.h> |
| 19 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 20 | #include <common/chunk.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 21 | #include <common/standard.h> |
| 22 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 23 | #include <proto/arg.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 24 | #include <proto/sample.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 25 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 26 | /* static sample used in sample_process() when <p> is NULL */ |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 27 | static struct sample temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 28 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 29 | /* list head of all known sample fetch keywords */ |
| 30 | static struct sample_fetch_kw_list sample_fetches = { |
| 31 | .list = LIST_HEAD_INIT(sample_fetches.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 32 | }; |
| 33 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 34 | /* list head of all known sample format conversion keywords */ |
| 35 | static struct sample_conv_kw_list sample_convs = { |
| 36 | .list = LIST_HEAD_INIT(sample_convs.list) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 39 | const unsigned int fetch_cap[SMP_SRC_ENTRIES] = { |
| 40 | [SMP_SRC_INTRN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 41 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 42 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 43 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 44 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 45 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 46 | SMP_VAL_FE_LOG_END), |
| 47 | |
| 48 | [SMP_SRC_LISTN] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 49 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 50 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 51 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 52 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 53 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 54 | SMP_VAL_FE_LOG_END), |
| 55 | |
| 56 | [SMP_SRC_FTEND] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 57 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 58 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 59 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 60 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 61 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 62 | SMP_VAL_FE_LOG_END), |
| 63 | |
| 64 | [SMP_SRC_L4CLI] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 65 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 66 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 67 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 68 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 69 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 70 | SMP_VAL_FE_LOG_END), |
| 71 | |
| 72 | [SMP_SRC_L5CLI] = (SMP_VAL___________ | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 73 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 74 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 75 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 76 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 77 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 78 | SMP_VAL_FE_LOG_END), |
| 79 | |
| 80 | [SMP_SRC_TRACK] = (SMP_VAL_FE_CON_ACC | SMP_VAL_FE_SES_ACC | SMP_VAL_FE_REQ_CNT | |
| 81 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 82 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 83 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 84 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 85 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 86 | SMP_VAL_FE_LOG_END), |
| 87 | |
| 88 | [SMP_SRC_L6REQ] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 89 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 90 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 91 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 92 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 93 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 94 | SMP_VAL___________), |
| 95 | |
| 96 | [SMP_SRC_HRQHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 97 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 98 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 99 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 100 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 101 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 102 | SMP_VAL___________), |
| 103 | |
| 104 | [SMP_SRC_HRQHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_FE_REQ_CNT | |
| 105 | SMP_VAL_FE_HRQ_HDR | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 106 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 107 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 108 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 109 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 110 | SMP_VAL_FE_LOG_END), |
| 111 | |
| 112 | [SMP_SRC_HRQBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 113 | SMP_VAL___________ | SMP_VAL_FE_HRQ_BDY | SMP_VAL_FE_SET_BCK | |
| 114 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 115 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL___________ | |
| 116 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 117 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 118 | SMP_VAL___________), |
| 119 | |
| 120 | [SMP_SRC_BKEND] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 121 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 122 | SMP_VAL_BE_REQ_CNT | SMP_VAL_BE_HRQ_HDR | SMP_VAL_BE_HRQ_BDY | |
| 123 | SMP_VAL_BE_SET_SRV | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 124 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 125 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 126 | SMP_VAL_FE_LOG_END), |
| 127 | |
| 128 | [SMP_SRC_SERVR] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 129 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 130 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 131 | SMP_VAL___________ | SMP_VAL_BE_SRV_CON | SMP_VAL_BE_RES_CNT | |
| 132 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 133 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 134 | SMP_VAL_FE_LOG_END), |
| 135 | |
| 136 | [SMP_SRC_L4SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 137 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 138 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 139 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 140 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 141 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 142 | SMP_VAL_FE_LOG_END), |
| 143 | |
| 144 | [SMP_SRC_L5SRV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 145 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 146 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 147 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 148 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 149 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 150 | SMP_VAL_FE_LOG_END), |
| 151 | |
| 152 | [SMP_SRC_L6RES] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 153 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 154 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 155 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 156 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 157 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 158 | SMP_VAL___________), |
| 159 | |
| 160 | [SMP_SRC_HRSHV] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 161 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 162 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 163 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 164 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 165 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 166 | SMP_VAL___________), |
| 167 | |
| 168 | [SMP_SRC_HRSHP] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 169 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 170 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 171 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL_BE_RES_CNT | |
| 172 | SMP_VAL_BE_HRS_HDR | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 173 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 174 | SMP_VAL_FE_LOG_END), |
| 175 | |
| 176 | [SMP_SRC_HRSBO] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 177 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 178 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 179 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 180 | SMP_VAL___________ | SMP_VAL_BE_HRS_BDY | SMP_VAL_BE_STO_RUL | |
| 181 | SMP_VAL_FE_RES_CNT | SMP_VAL_FE_HRS_HDR | SMP_VAL_FE_HRS_BDY | |
| 182 | SMP_VAL___________), |
| 183 | |
| 184 | [SMP_SRC_RQFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 185 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 186 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 187 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 188 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 189 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 190 | SMP_VAL_FE_LOG_END), |
| 191 | |
| 192 | [SMP_SRC_RSFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 193 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 194 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 195 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 196 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 197 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 198 | SMP_VAL_FE_LOG_END), |
| 199 | |
| 200 | [SMP_SRC_TXFIN] = (SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 201 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 202 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 203 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 204 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 205 | SMP_VAL___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 206 | SMP_VAL_FE_LOG_END), |
| 207 | |
| 208 | [SMP_SRC_SSFIN] = (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___________ | SMP_VAL___________ | SMP_VAL___________ | |
| 214 | SMP_VAL_FE_LOG_END), |
| 215 | }; |
| 216 | |
| 217 | static const char *fetch_src_names[SMP_SRC_ENTRIES] = { |
| 218 | [SMP_SRC_INTRN] = "internal state", |
| 219 | [SMP_SRC_LISTN] = "listener", |
| 220 | [SMP_SRC_FTEND] = "frontend", |
| 221 | [SMP_SRC_L4CLI] = "client address", |
| 222 | [SMP_SRC_L5CLI] = "client-side connection", |
| 223 | [SMP_SRC_TRACK] = "track counters", |
| 224 | [SMP_SRC_L6REQ] = "request buffer", |
| 225 | [SMP_SRC_HRQHV] = "HTTP request headers", |
| 226 | [SMP_SRC_HRQHP] = "HTTP request", |
| 227 | [SMP_SRC_HRQBO] = "HTTP request body", |
| 228 | [SMP_SRC_BKEND] = "backend", |
| 229 | [SMP_SRC_SERVR] = "server", |
| 230 | [SMP_SRC_L4SRV] = "server address", |
| 231 | [SMP_SRC_L5SRV] = "server-side connection", |
| 232 | [SMP_SRC_L6RES] = "response buffer", |
| 233 | [SMP_SRC_HRSHV] = "HTTP response headers", |
| 234 | [SMP_SRC_HRSHP] = "HTTP response", |
| 235 | [SMP_SRC_HRSBO] = "HTTP response body", |
| 236 | [SMP_SRC_RQFIN] = "request buffer statistics", |
| 237 | [SMP_SRC_RSFIN] = "response buffer statistics", |
| 238 | [SMP_SRC_TXFIN] = "transaction statistics", |
| 239 | [SMP_SRC_SSFIN] = "session statistics", |
| 240 | }; |
| 241 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 242 | static const char *fetch_ckp_names[SMP_CKP_ENTRIES] = { |
| 243 | [SMP_CKP_FE_CON_ACC] = "frontend tcp-request connection rule", |
| 244 | [SMP_CKP_FE_SES_ACC] = "frontend tcp-request session rule", |
| 245 | [SMP_CKP_FE_REQ_CNT] = "frontend tcp-request content rule", |
| 246 | [SMP_CKP_FE_HRQ_HDR] = "frontend http-request header rule", |
| 247 | [SMP_CKP_FE_HRQ_BDY] = "frontend http-request body rule", |
| 248 | [SMP_CKP_FE_SET_BCK] = "frontend use-backend rule", |
| 249 | [SMP_CKP_BE_REQ_CNT] = "backend tcp-request content rule", |
| 250 | [SMP_CKP_BE_HRQ_HDR] = "backend http-request header rule", |
| 251 | [SMP_CKP_BE_HRQ_BDY] = "backend http-request body rule", |
| 252 | [SMP_CKP_BE_SET_SRV] = "backend use-server, balance or stick-match rule", |
| 253 | [SMP_CKP_BE_SRV_CON] = "server source selection", |
| 254 | [SMP_CKP_BE_RES_CNT] = "backend tcp-response content rule", |
| 255 | [SMP_CKP_BE_HRS_HDR] = "backend http-response header rule", |
| 256 | [SMP_CKP_BE_HRS_BDY] = "backend http-response body rule", |
| 257 | [SMP_CKP_BE_STO_RUL] = "backend stick-store rule", |
| 258 | [SMP_CKP_FE_RES_CNT] = "frontend tcp-response content rule", |
| 259 | [SMP_CKP_FE_HRS_HDR] = "frontend http-response header rule", |
| 260 | [SMP_CKP_FE_HRS_BDY] = "frontend http-response body rule", |
| 261 | [SMP_CKP_FE_LOG_END] = "logs", |
| 262 | }; |
| 263 | |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 264 | /* fill the trash with a comma-delimited list of source names for the <use> bit |
| 265 | * field which must be composed of a non-null set of SMP_USE_* flags. The return |
| 266 | * value is the pointer to the string in the trash buffer. |
| 267 | */ |
| 268 | const char *sample_src_names(unsigned int use) |
| 269 | { |
| 270 | int bit; |
| 271 | |
| 272 | trash.len = 0; |
| 273 | trash.str[0] = '\0'; |
| 274 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) { |
| 275 | if (!(use & ~((1 << bit) - 1))) |
| 276 | break; /* no more bits */ |
| 277 | |
| 278 | if (!(use & (1 << bit))) |
| 279 | continue; /* bit not set */ |
| 280 | |
| 281 | trash.len += snprintf(trash.str + trash.len, trash.size - trash.len, "%s%s", |
| 282 | (use & ((1 << bit) - 1)) ? "," : "", |
| 283 | fetch_src_names[bit]); |
| 284 | } |
| 285 | return trash.str; |
| 286 | } |
| 287 | |
Willy Tarreau | bf8e251 | 2013-03-25 14:52:41 +0100 | [diff] [blame] | 288 | /* return a pointer to the correct sample checkpoint name, or "unknown" when |
| 289 | * the flags are invalid. Only the lowest bit is used, higher bits are ignored |
| 290 | * if set. |
| 291 | */ |
| 292 | const char *sample_ckp_names(unsigned int use) |
| 293 | { |
| 294 | int bit; |
| 295 | |
| 296 | for (bit = 0; bit < SMP_CKP_ENTRIES; bit++) |
| 297 | if (use & (1 << bit)) |
| 298 | return fetch_ckp_names[bit]; |
| 299 | return "unknown sample check place, please report this bug"; |
| 300 | } |
| 301 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 302 | /* |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 303 | * Registers the sample fetch keyword list <kwl> as a list of valid keywords |
| 304 | * for next parsing sessions. The fetch keywords capabilities are also computed |
| 305 | * from their ->use field. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 306 | */ |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 307 | void sample_register_fetches(struct sample_fetch_kw_list *kwl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 308 | { |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 309 | struct sample_fetch *sf; |
| 310 | int bit; |
| 311 | |
| 312 | for (sf = kwl->kw; sf->kw != NULL; sf++) { |
| 313 | for (bit = 0; bit < SMP_SRC_ENTRIES; bit++) |
| 314 | if (sf->use & (1 << bit)) |
| 315 | sf->val |= fetch_cap[bit]; |
| 316 | } |
| 317 | LIST_ADDQ(&sample_fetches.list, &kwl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 321 | * 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] | 322 | * parsing sessions. |
| 323 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 324 | void sample_register_convs(struct sample_conv_kw_list *pckl) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 325 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 326 | LIST_ADDQ(&sample_convs.list, &pckl->list); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 330 | * Returns the pointer on sample fetch keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 331 | * string of <len> in buffer <kw>. |
| 332 | * |
| 333 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 334 | struct sample_fetch *find_sample_fetch(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 335 | { |
| 336 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 337 | struct sample_fetch_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 338 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 339 | list_for_each_entry(kwl, &sample_fetches.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 340 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 341 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 342 | kwl->kw[index].kw[len] == '\0') |
| 343 | return &kwl->kw[index]; |
| 344 | } |
| 345 | } |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 350 | * Returns the pointer on sample format conversion keyword structure identified by |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 351 | * string of <len> in buffer <kw>. |
| 352 | * |
| 353 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 354 | struct sample_conv *find_sample_conv(const char *kw, int len) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 355 | { |
| 356 | int index; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 357 | struct sample_conv_kw_list *kwl; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 358 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 359 | list_for_each_entry(kwl, &sample_convs.list, list) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 360 | for (index = 0; kwl->kw[index].kw != NULL; index++) { |
| 361 | if (strncmp(kwl->kw[index].kw, kw, len) == 0 && |
| 362 | kwl->kw[index].kw[len] == '\0') |
| 363 | return &kwl->kw[index]; |
| 364 | } |
| 365 | } |
| 366 | return NULL; |
| 367 | } |
| 368 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 369 | /******************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 370 | /* Sample casts functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 371 | /* Note: these functions do *NOT* set the output type on the */ |
| 372 | /* sample, the caller is responsible for doing this on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 373 | /******************************************************************/ |
| 374 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 375 | static int c_ip2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 376 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 377 | smp->data.uint = ntohl(smp->data.ipv4.s_addr); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 378 | return 1; |
| 379 | } |
| 380 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 381 | static int c_ip2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 382 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 383 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 384 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 385 | 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] | 386 | return 0; |
| 387 | |
| 388 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 389 | smp->data.str = *trash; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 390 | |
| 391 | return 1; |
| 392 | } |
| 393 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 394 | static int c_ip2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 395 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 396 | v4tov6(&smp->data.ipv6, &smp->data.ipv4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 400 | static int c_ipv62str(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 401 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 402 | struct chunk *trash = get_trash_chunk(); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 403 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 404 | 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] | 405 | return 0; |
| 406 | |
| 407 | trash->len = strlen(trash->str); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 408 | smp->data.str = *trash; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | /* |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 413 | static int c_ipv62ip(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 414 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 415 | return v6tov4(&smp->data.ipv4, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 416 | } |
| 417 | */ |
| 418 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 419 | static int c_int2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 420 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 421 | smp->data.ipv4.s_addr = htonl(smp->data.uint); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 422 | return 1; |
| 423 | } |
| 424 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 425 | static int c_str2ip(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 426 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 427 | 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] | 428 | return 0; |
| 429 | return 1; |
| 430 | } |
| 431 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 432 | static int c_str2ipv6(struct sample *smp) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 433 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 434 | return inet_pton(AF_INET6, smp->data.str.str, &smp->data.ipv6); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 437 | static int c_bin2str(struct sample *smp) |
| 438 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 439 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 440 | unsigned char c; |
| 441 | int ptr = 0; |
| 442 | |
| 443 | trash->len = 0; |
| 444 | while (ptr < smp->data.str.len && trash->len <= trash->size - 2) { |
| 445 | c = smp->data.str.str[ptr++]; |
| 446 | trash->str[trash->len++] = hextab[(c >> 4) & 0xF]; |
| 447 | trash->str[trash->len++] = hextab[c & 0xF]; |
| 448 | } |
| 449 | smp->data.str = *trash; |
| 450 | return 1; |
| 451 | } |
| 452 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 453 | static int c_int2str(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 454 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 455 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 456 | char *pos; |
| 457 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 458 | pos = ultoa_r(smp->data.uint, trash->str, trash->size); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 459 | |
| 460 | if (!pos) |
| 461 | return 0; |
| 462 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 463 | trash->size = trash->size - (pos - trash->str); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 464 | trash->str = pos; |
| 465 | trash->len = strlen(pos); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 466 | smp->data.str = *trash; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 467 | return 1; |
| 468 | } |
| 469 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 470 | static int c_datadup(struct sample *smp) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 471 | { |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 472 | struct chunk *trash = get_trash_chunk(); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 473 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 474 | trash->len = smp->data.str.len < trash->size ? smp->data.str.len : trash->size; |
| 475 | memcpy(trash->str, smp->data.str.str, trash->len); |
| 476 | smp->data.str = *trash; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 477 | return 1; |
| 478 | } |
| 479 | |
| 480 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 481 | static int c_none(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 482 | { |
| 483 | return 1; |
| 484 | } |
| 485 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 486 | static int c_str2int(struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 487 | { |
| 488 | int i; |
| 489 | uint32_t ret = 0; |
| 490 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 491 | for (i = 0; i < smp->data.str.len; i++) { |
| 492 | uint32_t val = smp->data.str.str[i] - '0'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 493 | |
| 494 | if (val > 9) |
| 495 | break; |
| 496 | |
| 497 | ret = ret * 10 + val; |
| 498 | } |
| 499 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 500 | smp->data.uint = ret; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 501 | return 1; |
| 502 | } |
| 503 | |
| 504 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 505 | /* Sample casts matrix: */ |
| 506 | /* sample_casts[from type][to type] */ |
| 507 | /* NULL pointer used for impossible sample casts */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 508 | /*****************************************************************/ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 509 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 510 | typedef int (*sample_cast_fct)(struct sample *smp); |
| 511 | static sample_cast_fct sample_casts[SMP_TYPES][SMP_TYPES] = { |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 512 | /* to: BOOL UINT SINT IPV4 IPV6 STR BIN CSTR CBIN */ |
Willy Tarreau | d167e6d | 2012-12-21 17:38:05 +0100 | [diff] [blame] | 513 | /* from: BOOL */ { c_none, c_none, c_none, NULL, NULL, c_int2str, NULL, c_int2str, NULL }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 514 | /* UINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL }, |
| 515 | /* SINT */ { c_none, c_none, c_none, c_int2ip, NULL, c_int2str, NULL, c_int2str, NULL }, |
| 516 | /* IPV4 */ { NULL, c_ip2int, c_ip2int, c_none, c_ip2ipv6, c_ip2str, NULL, c_ip2str, NULL }, |
| 517 | /* IPV6 */ { NULL, NULL, NULL, NULL, c_none, c_ipv62str, NULL, c_ipv62str, NULL }, |
| 518 | /* STR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_none, c_none, c_none, c_none }, |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 519 | /* BIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_none, c_bin2str, c_none }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 520 | /* CSTR */ { c_str2int, c_str2int, c_str2int, c_str2ip, c_str2ipv6, c_datadup, c_datadup, c_none, c_none }, |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 521 | /* CBIN */ { NULL, NULL, NULL, NULL, NULL, c_bin2str, c_datadup, c_bin2str, c_none }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 522 | }; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 523 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 524 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 525 | * Parse a sample expression configuration: |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 526 | * fetch keyword followed by format conversion keywords. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 527 | * Returns a pointer on allocated sample expression structure. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 528 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 529 | struct sample_expr *sample_parse_expr(char **str, int *idx, char *err, int err_size) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 530 | { |
| 531 | const char *endw; |
| 532 | const char *end; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 533 | struct sample_expr *expr; |
| 534 | struct sample_fetch *fetch; |
| 535 | struct sample_conv *conv; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 536 | unsigned long prev_type; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 537 | char *p; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 538 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 539 | snprintf(err, err_size, "memory error."); |
| 540 | if (!str[*idx]) { |
| 541 | |
| 542 | snprintf(err, err_size, "missing fetch method."); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 543 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 544 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 545 | |
| 546 | end = str[*idx] + strlen(str[*idx]); |
| 547 | endw = strchr(str[*idx], '('); |
| 548 | |
| 549 | if (!endw) |
| 550 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 551 | else if ((end-1)[0] != ')') { |
| 552 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 553 | if (p) { |
| 554 | snprintf(err, err_size, "syntax error: missing ')' after keyword '%s'.", p); |
| 555 | free(p); |
| 556 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 557 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 558 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 559 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 560 | fetch = find_sample_fetch(str[*idx], endw - str[*idx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 561 | if (!fetch) { |
| 562 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 563 | if (p) { |
| 564 | snprintf(err, err_size, "unknown fetch method '%s'.", p); |
| 565 | free(p); |
| 566 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 567 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 568 | } |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 569 | if (fetch->out_type >= SMP_TYPES) { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 570 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 571 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 572 | if (p) { |
| 573 | snprintf(err, err_size, "returns type of fetch method '%s' is unknown.", p); |
| 574 | free(p); |
| 575 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 576 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 577 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 578 | |
| 579 | prev_type = fetch->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 580 | expr = calloc(1, sizeof(struct sample_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 581 | if (!expr) |
| 582 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 583 | |
| 584 | LIST_INIT(&(expr->conv_exprs)); |
| 585 | expr->fetch = fetch; |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 586 | expr->arg_p = empty_arg_list; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 587 | |
| 588 | if (end != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 589 | char *err_msg = NULL; |
| 590 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 591 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 592 | if (!fetch->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 593 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 594 | if (p) { |
| 595 | snprintf(err, err_size, "fetch method '%s' does not support any args.", p); |
| 596 | free(p); |
| 597 | } |
| 598 | goto out_error; |
| 599 | } |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 600 | |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 601 | if (make_arg_list(endw + 1, end - endw - 2, fetch->arg_mask, &expr->arg_p, &err_msg, NULL, &err_arg) < 0) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 602 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 603 | if (p) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 604 | snprintf(err, err_size, "invalid arg %d in fetch method '%s' : %s.", err_arg+1, p, err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 605 | free(p); |
| 606 | } |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 607 | free(err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 608 | goto out_error; |
| 609 | } |
| 610 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 611 | if (!expr->arg_p) |
| 612 | expr->arg_p = empty_arg_list; |
| 613 | |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 614 | if (fetch->val_args && !fetch->val_args(expr->arg_p, &err_msg)) { |
| 615 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 616 | if (p) { |
| 617 | snprintf(err, err_size, "invalid args in fetch method '%s' : %s.", p, err_msg); |
| 618 | free(p); |
| 619 | } |
| 620 | free(err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 621 | goto out_error; |
| 622 | } |
| 623 | } |
Willy Tarreau | f22a508 | 2012-10-19 16:47:23 +0200 | [diff] [blame] | 624 | else if (ARGM(fetch->arg_mask)) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 625 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 626 | if (p) { |
| 627 | snprintf(err, err_size, "missing args for fetch method '%s'.", p); |
| 628 | free(p); |
| 629 | } |
| 630 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | for (*idx += 1; *(str[*idx]); (*idx)++) { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 634 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 635 | |
| 636 | end = str[*idx] + strlen(str[*idx]); |
| 637 | endw = strchr(str[*idx], '('); |
| 638 | |
| 639 | if (!endw) |
| 640 | endw = end; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 641 | else if ((end-1)[0] != ')') { |
| 642 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 643 | if (p) { |
| 644 | snprintf(err, err_size, "syntax error, missing ')' after keyword '%s'.", p); |
| 645 | free(p); |
| 646 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 647 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 648 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 649 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 650 | conv = find_sample_conv(str[*idx], endw - str[*idx]); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 651 | if (!conv) |
| 652 | break; |
| 653 | |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 654 | if (conv->in_type >= SMP_TYPES || |
| 655 | conv->out_type >= SMP_TYPES) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 656 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 657 | if (p) { |
| 658 | snprintf(err, err_size, "returns type of conv method '%s' is unknown.", p); |
| 659 | free(p); |
| 660 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 661 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 662 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 663 | |
| 664 | /* If impossible type conversion */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 665 | if (!sample_casts[prev_type][conv->in_type]) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 666 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 667 | if (p) { |
| 668 | snprintf(err, err_size, "conv method '%s' cannot be applied.", p); |
| 669 | free(p); |
| 670 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 671 | goto out_error; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 672 | } |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 673 | |
| 674 | prev_type = conv->out_type; |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 675 | conv_expr = calloc(1, sizeof(struct sample_conv_expr)); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 676 | if (!conv_expr) |
| 677 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 678 | |
| 679 | LIST_ADDQ(&(expr->conv_exprs), &(conv_expr->list)); |
| 680 | conv_expr->conv = conv; |
| 681 | |
| 682 | if (end != endw) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 683 | char *err_msg = NULL; |
| 684 | int err_arg; |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 685 | |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 686 | if (!conv->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 687 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 688 | |
| 689 | if (p) { |
| 690 | snprintf(err, err_size, "conv method '%s' does not support any args.", p); |
| 691 | free(p); |
| 692 | } |
| 693 | goto out_error; |
| 694 | } |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 695 | |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 696 | if (make_arg_list(endw + 1, end - endw - 2, conv->arg_mask, &conv_expr->arg_p, &err_msg, NULL, &err_arg) < 0) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 697 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 698 | if (p) { |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 699 | snprintf(err, err_size, "invalid arg %d in conv method '%s' : %s.", err_arg+1, p, err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 700 | free(p); |
| 701 | } |
Willy Tarreau | b27c0d3 | 2012-04-20 16:04:47 +0200 | [diff] [blame] | 702 | free(err_msg); |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 703 | goto out_error; |
| 704 | } |
| 705 | |
Willy Tarreau | 2e845be | 2012-10-19 19:49:09 +0200 | [diff] [blame] | 706 | if (!conv_expr->arg_p) |
| 707 | conv_expr->arg_p = empty_arg_list; |
| 708 | |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 709 | if (conv->val_args && !conv->val_args(conv_expr->arg_p, &err_msg)) { |
| 710 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 711 | if (p) { |
| 712 | snprintf(err, err_size, "invalid args in conv method '%s' : %s.", p, err_msg); |
| 713 | free(p); |
| 714 | } |
| 715 | free(err_msg); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 716 | goto out_error; |
| 717 | } |
| 718 | } |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 719 | else if (conv->arg_mask) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 720 | p = my_strndup(str[*idx], endw - str[*idx]); |
| 721 | if (p) { |
| 722 | snprintf(err, err_size, "missing args for conv method '%s'.", p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 723 | free(p); |
Willy Tarreau | 9e92d32 | 2010-01-26 17:58:06 +0100 | [diff] [blame] | 724 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 725 | goto out_error; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 726 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 727 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 728 | } |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 729 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 730 | return expr; |
| 731 | |
| 732 | out_error: |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 733 | /* TODO: prune_sample_expr(expr); */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 738 | * Process a fetch + format conversion of defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 739 | * on request or response considering the <opt> parameter. |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 740 | * Returns a pointer on a typed sample structure containing the result or NULL if |
| 741 | * sample is not found or when format conversion failed. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 742 | * 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] | 743 | * 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] | 744 | * |
| 745 | * Note: the fetch functions are required to properly set the return type. The |
| 746 | * conversion functions must do so too. However the cast functions do not need |
| 747 | * to since they're made to cast mutiple types according to what is required. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 748 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 749 | struct sample *sample_process(struct proxy *px, struct session *l4, void *l7, |
| 750 | unsigned int opt, |
| 751 | struct sample_expr *expr, struct sample *p) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 752 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 753 | struct sample_conv_expr *conv_expr; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 754 | |
| 755 | if (p == NULL) |
Willy Tarreau | b4a88f0 | 2012-04-23 21:35:11 +0200 | [diff] [blame] | 756 | p = &temp_smp; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 757 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 758 | p->flags = 0; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 759 | if (!expr->fetch->process(px, l4, l7, opt, expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 760 | return NULL; |
| 761 | |
Willy Tarreau | 40aebd9 | 2012-04-26 11:05:50 +0200 | [diff] [blame] | 762 | if (p->flags & SMP_F_MAY_CHANGE) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 763 | return NULL; /* we can only use stable samples */ |
Willy Tarreau | 40aebd9 | 2012-04-26 11:05:50 +0200 | [diff] [blame] | 764 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 765 | list_for_each_entry(conv_expr, &expr->conv_exprs, list) { |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 766 | /* we want to ensure that p->type can be casted into |
| 767 | * conv_expr->conv->in_type. We have 3 possibilities : |
| 768 | * - NULL => not castable. |
| 769 | * - c_none => nothing to do (let's optimize it) |
| 770 | * - other => apply cast and prepare to fail |
| 771 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 772 | if (!sample_casts[p->type][conv_expr->conv->in_type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 773 | return NULL; |
| 774 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 775 | if (sample_casts[p->type][conv_expr->conv->in_type] != c_none && |
| 776 | !sample_casts[p->type][conv_expr->conv->in_type](p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 777 | return NULL; |
| 778 | |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 779 | /* OK cast succeeded */ |
| 780 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 781 | /* force the output type after a cast */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 782 | p->type = conv_expr->conv->in_type; |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 783 | if (!conv_expr->conv->process(conv_expr->arg_p, p)) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 784 | return NULL; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 785 | } |
| 786 | return p; |
| 787 | } |
| 788 | |
Willy Tarreau | e7ad4bb | 2012-12-21 00:02:32 +0100 | [diff] [blame] | 789 | /* |
| 790 | * Process a fetch + format conversion as defined by the sample expression <expr> |
| 791 | * on request or response considering the <opt> parameter. The output is always of |
| 792 | * type string. Returns either NULL if no sample could be extracted, or a pointer |
| 793 | * to the converted result stored in static temp_smp in format string. |
| 794 | */ |
| 795 | struct sample *sample_fetch_string(struct proxy *px, struct session *l4, void *l7, |
| 796 | unsigned int opt, struct sample_expr *expr) |
| 797 | { |
| 798 | struct sample *smp; |
| 799 | |
| 800 | smp = sample_process(px, l4, l7, opt, expr, NULL); |
| 801 | if (!smp) |
| 802 | return NULL; |
| 803 | |
| 804 | if (!sample_casts[smp->type][SMP_T_CSTR]) |
| 805 | return NULL; |
| 806 | |
| 807 | if (!sample_casts[smp->type][SMP_T_CSTR](smp)) |
| 808 | return NULL; |
| 809 | |
| 810 | smp->type = SMP_T_CSTR; |
| 811 | return smp; |
| 812 | } |
| 813 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 814 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 815 | /* Sample format convert functions */ |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 816 | /* These functions set the data type on return. */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 817 | /*****************************************************************/ |
| 818 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 819 | static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 820 | { |
| 821 | int i; |
| 822 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 823 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 824 | return 0; |
| 825 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 826 | for (i = 0; i < smp->data.str.len; i++) { |
| 827 | if ((smp->data.str.str[i] >= 'A') && (smp->data.str.str[i] <= 'Z')) |
| 828 | smp->data.str.str[i] += 'a' - 'A'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 829 | } |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 830 | smp->type = SMP_T_STR; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 831 | return 1; |
| 832 | } |
| 833 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 834 | static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 835 | { |
| 836 | int i; |
| 837 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 838 | if (!smp->data.str.size) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 839 | return 0; |
| 840 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 841 | for (i = 0; i < smp->data.str.len; i++) { |
| 842 | if ((smp->data.str.str[i] >= 'a') && (smp->data.str.str[i] <= 'z')) |
| 843 | smp->data.str.str[i] += 'A' - 'a'; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 844 | } |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 845 | smp->type = SMP_T_STR; |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 846 | return 1; |
| 847 | } |
| 848 | |
Willy Tarreau | f995410 | 2012-04-20 14:03:29 +0200 | [diff] [blame] | 849 | /* takes the netmask in arg_p */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 850 | static int sample_conv_ipmask(const struct arg *arg_p, struct sample *smp) |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 851 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 852 | smp->data.ipv4.s_addr &= arg_p->data.ipv4.s_addr; |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 853 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d31d6eb | 2010-01-26 18:01:41 +0100 | [diff] [blame] | 854 | return 1; |
| 855 | } |
| 856 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 857 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 858 | static struct sample_conv_kw_list sample_conv_kws = {{ },{ |
| 859 | { "upper", sample_conv_str2upper, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 860 | { "lower", sample_conv_str2lower, 0, NULL, SMP_T_STR, SMP_T_STR }, |
| 861 | { "ipmask", sample_conv_ipmask, ARG1(1,MSK4), NULL, SMP_T_IPV4, SMP_T_IPV4 }, |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 862 | { NULL, NULL, 0, 0, 0 }, |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 863 | }}; |
| 864 | |
| 865 | __attribute__((constructor)) |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 866 | static void __sample_init(void) |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 867 | { |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 868 | /* register sample format convert keywords */ |
| 869 | sample_register_convs(&sample_conv_kws); |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 870 | } |