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 |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 95 | spoe_decode_buffer(char **buf, char *end, char **str, size_t *len) |
| 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: { |
| 167 | struct chunk *chk = &smp->data.u.str; |
| 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. */ |
| 178 | struct chunk *chk = &smp->data.u.str; |
| 179 | |
| 180 | *p++ = (smp->data.type == SMP_T_STR) |
| 181 | ? SPOE_DATA_T_STR |
| 182 | : SPOE_DATA_T_BIN; |
| 183 | ret = spoe_encode_frag_buffer(chk->str, chk->len, &p, end); |
| 184 | if (ret == -1) |
| 185 | return -1; |
| 186 | } |
| 187 | else { |
| 188 | /* The sample has been fragmented, encode remaining data */ |
| 189 | ret = MIN(chk->len - *off, end - p); |
| 190 | memcpy(p, chk->str + *off, ret); |
| 191 | p += ret; |
| 192 | } |
| 193 | /* Now update <*off> */ |
| 194 | if (ret + *off != chk->len) |
| 195 | *off += ret; |
| 196 | else |
| 197 | *off = 0; |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | case SMP_T_METH: { |
| 202 | char *m; |
| 203 | size_t len; |
| 204 | |
| 205 | *p++ = SPOE_DATA_T_STR; |
| 206 | switch (smp->data.u.meth.meth) { |
| 207 | case HTTP_METH_OPTIONS: m = "OPTIONS"; len = 7; break; |
| 208 | case HTTP_METH_GET : m = "GET"; len = 3; break; |
| 209 | case HTTP_METH_HEAD : m = "HEAD"; len = 4; break; |
| 210 | case HTTP_METH_POST : m = "POST"; len = 4; break; |
| 211 | case HTTP_METH_PUT : m = "PUT"; len = 3; break; |
| 212 | case HTTP_METH_DELETE : m = "DELETE"; len = 6; break; |
| 213 | case HTTP_METH_TRACE : m = "TRACE"; len = 5; break; |
| 214 | case HTTP_METH_CONNECT: m = "CONNECT"; len = 7; break; |
| 215 | |
| 216 | default : |
| 217 | m = smp->data.u.meth.str.str; |
| 218 | len = smp->data.u.meth.str.len; |
| 219 | } |
| 220 | if (spoe_encode_buffer(m, len, &p, end) == -1) |
| 221 | return -1; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | default: |
| 226 | *p++ = SPOE_DATA_T_NULL; |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | end: |
| 231 | ret = (p - *buf); |
| 232 | *buf = p; |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | /* Skip a typed data. If an error occurred, -1 is returned, otherwise the number |
| 237 | * of skipped bytes is returned and the <*buf> is moved after skipped data. |
| 238 | * |
| 239 | * A types data is composed of a type (1 byte) and corresponding data: |
| 240 | * - boolean: non additional data (0 bytes) |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 241 | * - integers: a variable-length integer (see decode_varint) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 242 | * - ipv4: 4 bytes |
| 243 | * - ipv6: 16 bytes |
| 244 | * - binary and string: a buffer prefixed by its size, a variable-length |
| 245 | * integer (see spoe_decode_buffer) */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 246 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 247 | spoe_skip_data(char **buf, char *end) |
| 248 | { |
| 249 | char *str, *p = *buf; |
| 250 | int type, ret; |
| 251 | size_t sz; |
| 252 | uint64_t v; |
| 253 | |
| 254 | if (p >= end) |
| 255 | return -1; |
| 256 | |
| 257 | type = *p++; |
| 258 | switch (type & SPOE_DATA_T_MASK) { |
| 259 | case SPOE_DATA_T_BOOL: |
| 260 | break; |
| 261 | case SPOE_DATA_T_INT32: |
| 262 | case SPOE_DATA_T_INT64: |
| 263 | case SPOE_DATA_T_UINT32: |
| 264 | case SPOE_DATA_T_UINT64: |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 265 | if (decode_varint(&p, end, &v) == -1) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 266 | return -1; |
| 267 | break; |
| 268 | case SPOE_DATA_T_IPV4: |
| 269 | if (p+4 > end) |
| 270 | return -1; |
| 271 | p += 4; |
| 272 | break; |
| 273 | case SPOE_DATA_T_IPV6: |
| 274 | if (p+16 > end) |
| 275 | return -1; |
| 276 | p += 16; |
| 277 | break; |
| 278 | case SPOE_DATA_T_STR: |
| 279 | case SPOE_DATA_T_BIN: |
| 280 | /* All the buffer must be skipped */ |
| 281 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 282 | return -1; |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | ret = (p - *buf); |
| 287 | *buf = p; |
| 288 | return ret; |
| 289 | } |
| 290 | |
| 291 | /* Decode a typed data and fill <smp>. If an error occurred, -1 is returned, |
| 292 | * otherwise the number of read bytes is returned and <*buf> is moved after the |
| 293 | * decoded data. See spoe_skip_data for details. */ |
Thierry FOURNIER | f4128a9 | 2017-04-09 05:41:27 +0200 | [diff] [blame] | 294 | static inline int |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 295 | spoe_decode_data(char **buf, char *end, struct sample *smp) |
| 296 | { |
| 297 | char *str, *p = *buf; |
| 298 | int type, r = 0; |
| 299 | size_t sz; |
| 300 | |
| 301 | if (p >= end) |
| 302 | return -1; |
| 303 | |
| 304 | type = *p++; |
| 305 | switch (type & SPOE_DATA_T_MASK) { |
| 306 | case SPOE_DATA_T_BOOL: |
| 307 | smp->data.u.sint = ((type & SPOE_DATA_FL_MASK) == SPOE_DATA_FL_TRUE); |
| 308 | smp->data.type = SMP_T_BOOL; |
| 309 | break; |
| 310 | case SPOE_DATA_T_INT32: |
| 311 | case SPOE_DATA_T_INT64: |
| 312 | case SPOE_DATA_T_UINT32: |
| 313 | case SPOE_DATA_T_UINT64: |
Thierry FOURNIER | 6ab2bae | 2017-04-19 11:49:44 +0200 | [diff] [blame] | 314 | if (decode_varint(&p, end, (uint64_t *)&smp->data.u.sint) == -1) |
Christopher Faulet | 4ff3e57 | 2017-02-24 14:31:11 +0100 | [diff] [blame] | 315 | return -1; |
| 316 | smp->data.type = SMP_T_SINT; |
| 317 | break; |
| 318 | case SPOE_DATA_T_IPV4: |
| 319 | if (p+4 > end) |
| 320 | return -1; |
| 321 | smp->data.type = SMP_T_IPV4; |
| 322 | memcpy(&smp->data.u.ipv4, p, 4); |
| 323 | p += 4; |
| 324 | break; |
| 325 | case SPOE_DATA_T_IPV6: |
| 326 | if (p+16 > end) |
| 327 | return -1; |
| 328 | memcpy(&smp->data.u.ipv6, p, 16); |
| 329 | smp->data.type = SMP_T_IPV6; |
| 330 | p += 16; |
| 331 | break; |
| 332 | case SPOE_DATA_T_STR: |
| 333 | case SPOE_DATA_T_BIN: |
| 334 | /* All the buffer must be decoded */ |
| 335 | if (spoe_decode_buffer(&p, end, &str, &sz) == -1) |
| 336 | return -1; |
| 337 | smp->data.u.str.str = str; |
| 338 | smp->data.u.str.len = sz; |
| 339 | smp->data.type = (type == SPOE_DATA_T_STR) ? SMP_T_STR : SMP_T_BIN; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | r = (p - *buf); |
| 344 | *buf = p; |
| 345 | return r; |
| 346 | } |
| 347 | |
| 348 | #endif /* _PROTO_SPOE_H */ |