Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/spoe.h |
| 3 | * Encoding/Decoding functions for the SPOE filters (and other helpers). |
| 4 | * |
| 5 | * Copyright (C) 2017 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_SPOE_H |
| 23 | #define _PROTO_SPOE_H |
| 24 | |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 25 | #include <common/standard.h> |
| 26 | |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 27 | #include <types/spoe.h> |
| 28 | |
| 29 | #include <proto/sample.h> |
| 30 | |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 31 | |
| 32 | /* Encode a buffer. Its length <len> is encoded as a varint, followed by a copy |
| 33 | * of <str>. It must have enough space in <*buf> to encode the buffer, else an |
| 34 | * error is triggered. |
| 35 | * On success, it returns <len> and <*buf> is moved after the encoded value. If |
| 36 | * an error occurred, it returns -1. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 37 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 38 | spoe_encode_buffer(const char *str, size_t len, char **buf, char *end) |
| 39 | { |
| 40 | char *p = *buf; |
| 41 | int ret; |
| 42 | |
| 43 | if (p >= end) |
| 44 | return -1; |
| 45 | |
| 46 | if (!len) { |
| 47 | *p++ = 0; |
| 48 | *buf = p; |
| 49 | return 0; |
| 50 | } |
| 51 | |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 52 | ret = encode_varint(len, &p, end); |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 53 | if (ret == -1 || p + len > end) |
| 54 | return -1; |
| 55 | |
| 56 | memcpy(p, str, len); |
| 57 | *buf = p + len; |
| 58 | return len; |
| 59 | } |
| 60 | |
| 61 | /* Encode a buffer, possibly partially. It does the same thing than |
| 62 | * 'spoe_encode_buffer', but if there is not enough space, it does not fail. |
| 63 | * On success, it returns the number of copied bytes and <*buf> is moved after |
| 64 | * the encoded value. If an error occured, it returns -1. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 65 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 66 | spoe_encode_frag_buffer(const char *str, size_t len, char **buf, char *end) |
| 67 | { |
| 68 | char *p = *buf; |
| 69 | int ret; |
| 70 | |
| 71 | if (p >= end) |
| 72 | return -1; |
| 73 | |
| 74 | if (!len) { |
| 75 | *p++ = 0; |
| 76 | *buf = p; |
| 77 | return 0; |
| 78 | } |
| 79 | |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 80 | ret = encode_varint(len, &p, end); |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 81 | if (ret == -1 || p >= end) |
| 82 | return -1; |
| 83 | |
| 84 | ret = (p+len < end) ? len : (end - p); |
| 85 | memcpy(p, str, ret); |
| 86 | *buf = p + ret; |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | /* Decode a buffer. The buffer length is decoded and saved in <*len>. <*str> |
| 91 | * points on the first byte of the buffer. |
| 92 | * On success, it returns the buffer length and <*buf> is moved after the |
| 93 | * encoded buffer. Otherwise, it returns -1. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 94 | static inline int |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 95 | spoe_decode_buffer(char **buf, char *end, char **str, uint64_t *len) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 96 | { |
| 97 | char *p = *buf; |
| 98 | uint64_t sz; |
| 99 | int ret; |
| 100 | |
| 101 | *str = NULL; |
| 102 | *len = 0; |
| 103 | |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 104 | ret = decode_varint(&p, end, &sz); |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 105 | if (ret == -1 || p + sz > end) |
| 106 | return -1; |
| 107 | |
| 108 | *str = p; |
| 109 | *len = sz; |
| 110 | *buf = p + sz; |
| 111 | return sz; |
| 112 | } |
| 113 | |
| 114 | /* Encode a typed data using value in <smp>. On success, it returns the number |
| 115 | * of copied bytes and <*buf> is moved after the encoded value. If an error |
| 116 | * occured, it returns -1. |
| 117 | * |
| 118 | * If the value is too big to be encoded, depending on its type, then encoding |
| 119 | * failed or the value is partially encoded. Only strings and binaries can be |
| 120 | * partially encoded. In this case, the offset <*off> is updated to known how |
| 121 | * many bytes has been encoded. If <*off> is zero at the end, it means that all |
| 122 | * data has been encoded. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 123 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 124 | spoe_encode_data(struct sample *smp, unsigned int *off, char **buf, char *end) |
| 125 | { |
| 126 | char *p = *buf; |
| 127 | int ret; |
| 128 | |
| 129 | if (p >= end) |
| 130 | return -1; |
| 131 | |
| 132 | if (smp == NULL) { |
| 133 | *p++ = SPOE_DATA_T_NULL; |
| 134 | goto end; |
| 135 | } |
| 136 | |
| 137 | switch (smp->data.type) { |
| 138 | case SMP_T_BOOL: |
| 139 | *p = SPOE_DATA_T_BOOL; |
| 140 | *p++ |= ((!smp->data.u.sint) ? SPOE_DATA_FL_FALSE : SPOE_DATA_FL_TRUE); |
| 141 | break; |
| 142 | |
| 143 | case SMP_T_SINT: |
| 144 | *p++ = SPOE_DATA_T_INT64; |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 145 | if (encode_varint(smp->data.u.sint, &p, end) == -1) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 146 | return -1; |
| 147 | break; |
| 148 | |
| 149 | case SMP_T_IPV4: |
| 150 | if (p + 5 > end) |
| 151 | return -1; |
| 152 | *p++ = SPOE_DATA_T_IPV4; |
| 153 | memcpy(p, &smp->data.u.ipv4, 4); |
| 154 | p += 4; |
| 155 | break; |
| 156 | |
| 157 | case SMP_T_IPV6: |
| 158 | if (p + 17 > end) |
| 159 | return -1; |
| 160 | *p++ = SPOE_DATA_T_IPV6; |
| 161 | memcpy(p, &smp->data.u.ipv6, 16); |
| 162 | p += 16; |
| 163 | break; |
| 164 | |
| 165 | case SMP_T_STR: |
| 166 | case SMP_T_BIN: { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 167 | struct buffer *chk = &smp->data.u.str; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 168 | |
| 169 | /* Here, we need to know if the sample has already been |
| 170 | * partially encoded. If yes, we only need to encode the |
| 171 | * remaining, <*off> reprensenting the number of bytes |
| 172 | * already encoded. */ |
| 173 | if (!*off) { |
| 174 | /* First evaluation of the sample : encode the |
| 175 | * type (string or binary), the buffer length |
| 176 | * (as a varint) and at least 1 byte of the |
| 177 | * buffer. */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 178 | struct buffer *chk = &smp->data.u.str; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 179 | |
| 180 | *p++ = (smp->data.type == SMP_T_STR) |
| 181 | ? SPOE_DATA_T_STR |
| 182 | : SPOE_DATA_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 183 | ret = spoe_encode_frag_buffer(chk->area, |
| 184 | chk->data, &p, |
| 185 | end); |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 186 | if (ret == -1) |
| 187 | return -1; |
| 188 | } |
| 189 | else { |
| 190 | /* The sample has been fragmented, encode remaining data */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 191 | ret = MIN(chk->data - *off, end - p); |
| 192 | memcpy(p, chk->area + *off, ret); |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 193 | p += ret; |
| 194 | } |
| 195 | /* Now update <*off> */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 196 | if (ret + *off != chk->data) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 197 | *off += ret; |
| 198 | else |
| 199 | *off = 0; |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | case SMP_T_METH: { |
| 204 | char *m; |
| 205 | size_t len; |
| 206 | |
| 207 | *p++ = SPOE_DATA_T_STR; |
| 208 | switch (smp->data.u.meth.meth) { |
| 209 | case HTTP_METH_OPTIONS: m = "OPTIONS"; len = 7; break; |
| 210 | case HTTP_METH_GET : m = "GET"; len = 3; break; |
| 211 | case HTTP_METH_HEAD : m = "HEAD"; len = 4; break; |
| 212 | case HTTP_METH_POST : m = "POST"; len = 4; break; |
| 213 | case HTTP_METH_PUT : m = "PUT"; len = 3; break; |
| 214 | case HTTP_METH_DELETE : m = "DELETE"; len = 6; break; |
| 215 | case HTTP_METH_TRACE : m = "TRACE"; len = 5; break; |
| 216 | case HTTP_METH_CONNECT: m = "CONNECT"; len = 7; break; |
| 217 | |
| 218 | default : |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 219 | m = smp->data.u.meth.str.area; |
| 220 | len = smp->data.u.meth.str.data; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 221 | } |
| 222 | if (spoe_encode_buffer(m, len, &p, end) == -1) |
| 223 | return -1; |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | default: |
| 228 | *p++ = SPOE_DATA_T_NULL; |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | end: |
| 233 | ret = (p - *buf); |
| 234 | *buf = p; |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | /* Skip a typed data. If an error occurred, -1 is returned, otherwise the number |
| 239 | * of skipped bytes is returned and the <*buf> is moved after skipped data. |
| 240 | * |
| 241 | * A types data is composed of a type (1 byte) and corresponding data: |
| 242 | * - boolean: non additional data (0 bytes) |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 243 | * - integers: a variable-length integer (see decode_varint) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 244 | * - ipv4: 4 bytes |
| 245 | * - ipv6: 16 bytes |
| 246 | * - binary and string: a buffer prefixed by its size, a variable-length |
| 247 | * integer (see spoe_decode_buffer) */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 248 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 249 | spoe_skip_data(char **buf, char *end) |
| 250 | { |
| 251 | char *str, *p = *buf; |
| 252 | int type, ret; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 253 | uint64_t v, sz; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 254 | |
| 255 | if (p >= end) |
| 256 | return -1; |
| 257 | |
| 258 | type = *p++; |
| 259 | switch (type & SPOE_DATA_T_MASK) { |
| 260 | case SPOE_DATA_T_BOOL: |
| 261 | break; |
| 262 | case SPOE_DATA_T_INT32: |
| 263 | case SPOE_DATA_T_INT64: |
| 264 | case SPOE_DATA_T_UINT32: |
| 265 | case SPOE_DATA_T_UINT64: |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 266 | if (decode_varint(&p, end, &v) == -1) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 267 | return -1; |
| 268 | break; |
| 269 | case SPOE_DATA_T_IPV4: |
| 270 | if (p+4 > end) |
| 271 | return -1; |
| 272 | p += 4; |
| 273 | break; |
| 274 | case SPOE_DATA_T_IPV6: |
| 275 | if (p+16 > end) |
| 276 | return -1; |
| 277 | p += 16; |
| 278 | break; |
| 279 | case SPOE_DATA_T_STR: |
| 280 | case SPOE_DATA_T_BIN: |
| 281 | /* All the buffer must be skipped */ |
| 282 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 283 | return -1; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | ret = (p - *buf); |
| 288 | *buf = p; |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | /* Decode a typed data and fill <smp>. If an error occurred, -1 is returned, |
| 293 | * otherwise the number of read bytes is returned and <*buf> is moved after the |
| 294 | * decoded data. See spoe_skip_data for details. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 295 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 296 | spoe_decode_data(char **buf, char *end, struct sample *smp) |
| 297 | { |
| 298 | char *str, *p = *buf; |
| 299 | int type, r = 0; |
Frédéric Lécaille | 6ca71a9 | 2017-08-22 10:33:14 +0200 | [diff] [blame] | 300 | uint64_t sz; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 301 | |
| 302 | if (p >= end) |
| 303 | return -1; |
| 304 | |
| 305 | type = *p++; |
| 306 | switch (type & SPOE_DATA_T_MASK) { |
| 307 | case SPOE_DATA_T_BOOL: |
| 308 | smp->data.u.sint = ((type & SPOE_DATA_FL_MASK) == SPOE_DATA_FL_TRUE); |
| 309 | smp->data.type = SMP_T_BOOL; |
| 310 | break; |
| 311 | case SPOE_DATA_T_INT32: |
| 312 | case SPOE_DATA_T_INT64: |
| 313 | case SPOE_DATA_T_UINT32: |
| 314 | case SPOE_DATA_T_UINT64: |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 315 | if (decode_varint(&p, end, (uint64_t *)&smp->data.u.sint) == -1) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 316 | return -1; |
| 317 | smp->data.type = SMP_T_SINT; |
| 318 | break; |
| 319 | case SPOE_DATA_T_IPV4: |
| 320 | if (p+4 > end) |
| 321 | return -1; |
| 322 | smp->data.type = SMP_T_IPV4; |
| 323 | memcpy(&smp->data.u.ipv4, p, 4); |
| 324 | p += 4; |
| 325 | break; |
| 326 | case SPOE_DATA_T_IPV6: |
| 327 | if (p+16 > end) |
| 328 | return -1; |
| 329 | memcpy(&smp->data.u.ipv6, p, 16); |
| 330 | smp->data.type = SMP_T_IPV6; |
| 331 | p += 16; |
| 332 | break; |
| 333 | case SPOE_DATA_T_STR: |
| 334 | case SPOE_DATA_T_BIN: |
| 335 | /* All the buffer must be decoded */ |
| 336 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 337 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 338 | smp->data.u.str.area = str; |
| 339 | smp->data.u.str.data = sz; |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 340 | smp->data.type = (type == SPOE_DATA_T_STR) ? SMP_T_STR : SMP_T_BIN; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | r = (p - *buf); |
| 345 | *buf = p; |
| 346 | return r; |
| 347 | } |
| 348 | |
| 349 | #endif /* _PROTO_SPOE_H */ |