Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General purpose functions. |
| 3 | * |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 13 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | #include <dlfcn.h> |
| 16 | #include <link.h> |
| 17 | #endif |
| 18 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 19 | #include <ctype.h> |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 20 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <netdb.h> |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 22 | #include <stdarg.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 23 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 26 | #include <time.h> |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 28 | #include <sys/socket.h> |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 31 | #include <sys/un.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | |
Willy Tarreau | 3005306 | 2020-08-20 16:39:14 +0200 | [diff] [blame] | 35 | #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
| 36 | #include <sys/auxv.h> |
| 37 | #endif |
| 38 | |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 39 | #include <import/eb32sctree.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 40 | #include <import/eb32tree.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 42 | #include <haproxy/api.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 43 | #include <haproxy/chunk.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 44 | #include <haproxy/dgram.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 45 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 46 | #include <haproxy/hlua.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 47 | #include <haproxy/listener.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 48 | #include <haproxy/namespace.h> |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 49 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 50 | #include <haproxy/protocol.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 51 | #include <haproxy/resolvers.h> |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 52 | #include <haproxy/sock.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 53 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 54 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 55 | #include <haproxy/task.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 56 | #include <haproxy/tools.h> |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 57 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 58 | /* This macro returns false if the test __x is false. Many |
| 59 | * of the following parsing function must be abort the processing |
| 60 | * if it returns 0, so this macro is useful for writing light code. |
| 61 | */ |
| 62 | #define RET0_UNLESS(__x) do { if (!(__x)) return 0; } while (0) |
| 63 | |
Willy Tarreau | 56adcf2 | 2012-12-23 18:00:29 +0100 | [diff] [blame] | 64 | /* enough to store NB_ITOA_STR integers of : |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 65 | * 2^64-1 = 18446744073709551615 or |
| 66 | * -2^63 = -9223372036854775808 |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 67 | * |
| 68 | * The HTML version needs room for adding the 25 characters |
| 69 | * '<span class="rls"></span>' around digits at positions 3N+1 in order |
| 70 | * to add spacing at up to 6 positions : 18 446 744 073 709 551 615 |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 71 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 72 | THREAD_LOCAL char itoa_str[NB_ITOA_STR][171]; |
| 73 | THREAD_LOCAL int itoa_idx = 0; /* index of next itoa_str to use */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 75 | /* sometimes we'll need to quote strings (eg: in stats), and we don't expect |
| 76 | * to quote strings larger than a max configuration line. |
| 77 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 78 | THREAD_LOCAL char quoted_str[NB_QSTR][QSTR_SIZE + 1]; |
| 79 | THREAD_LOCAL int quoted_idx = 0; |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 80 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 81 | /* thread-local PRNG state. It's modified to start from a different sequence |
| 82 | * on all threads upon startup. It must not be used or anything beyond getting |
| 83 | * statistical values as it's 100% predictable. |
| 84 | */ |
| 85 | THREAD_LOCAL unsigned int statistical_prng_state = 2463534242U; |
| 86 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | /* |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 88 | * unsigned long long ASCII representation |
| 89 | * |
| 90 | * return the last char '\0' or NULL if no enough |
| 91 | * space in dst |
| 92 | */ |
| 93 | char *ulltoa(unsigned long long n, char *dst, size_t size) |
| 94 | { |
| 95 | int i = 0; |
| 96 | char *res; |
| 97 | |
| 98 | switch(n) { |
| 99 | case 1ULL ... 9ULL: |
| 100 | i = 0; |
| 101 | break; |
| 102 | |
| 103 | case 10ULL ... 99ULL: |
| 104 | i = 1; |
| 105 | break; |
| 106 | |
| 107 | case 100ULL ... 999ULL: |
| 108 | i = 2; |
| 109 | break; |
| 110 | |
| 111 | case 1000ULL ... 9999ULL: |
| 112 | i = 3; |
| 113 | break; |
| 114 | |
| 115 | case 10000ULL ... 99999ULL: |
| 116 | i = 4; |
| 117 | break; |
| 118 | |
| 119 | case 100000ULL ... 999999ULL: |
| 120 | i = 5; |
| 121 | break; |
| 122 | |
| 123 | case 1000000ULL ... 9999999ULL: |
| 124 | i = 6; |
| 125 | break; |
| 126 | |
| 127 | case 10000000ULL ... 99999999ULL: |
| 128 | i = 7; |
| 129 | break; |
| 130 | |
| 131 | case 100000000ULL ... 999999999ULL: |
| 132 | i = 8; |
| 133 | break; |
| 134 | |
| 135 | case 1000000000ULL ... 9999999999ULL: |
| 136 | i = 9; |
| 137 | break; |
| 138 | |
| 139 | case 10000000000ULL ... 99999999999ULL: |
| 140 | i = 10; |
| 141 | break; |
| 142 | |
| 143 | case 100000000000ULL ... 999999999999ULL: |
| 144 | i = 11; |
| 145 | break; |
| 146 | |
| 147 | case 1000000000000ULL ... 9999999999999ULL: |
| 148 | i = 12; |
| 149 | break; |
| 150 | |
| 151 | case 10000000000000ULL ... 99999999999999ULL: |
| 152 | i = 13; |
| 153 | break; |
| 154 | |
| 155 | case 100000000000000ULL ... 999999999999999ULL: |
| 156 | i = 14; |
| 157 | break; |
| 158 | |
| 159 | case 1000000000000000ULL ... 9999999999999999ULL: |
| 160 | i = 15; |
| 161 | break; |
| 162 | |
| 163 | case 10000000000000000ULL ... 99999999999999999ULL: |
| 164 | i = 16; |
| 165 | break; |
| 166 | |
| 167 | case 100000000000000000ULL ... 999999999999999999ULL: |
| 168 | i = 17; |
| 169 | break; |
| 170 | |
| 171 | case 1000000000000000000ULL ... 9999999999999999999ULL: |
| 172 | i = 18; |
| 173 | break; |
| 174 | |
| 175 | case 10000000000000000000ULL ... ULLONG_MAX: |
| 176 | i = 19; |
| 177 | break; |
| 178 | } |
| 179 | if (i + 2 > size) // (i + 1) + '\0' |
| 180 | return NULL; // too long |
| 181 | res = dst + i + 1; |
| 182 | *res = '\0'; |
| 183 | for (; i >= 0; i--) { |
| 184 | dst[i] = n % 10ULL + '0'; |
| 185 | n /= 10ULL; |
| 186 | } |
| 187 | return res; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * unsigned long ASCII representation |
| 192 | * |
| 193 | * return the last char '\0' or NULL if no enough |
| 194 | * space in dst |
| 195 | */ |
| 196 | char *ultoa_o(unsigned long n, char *dst, size_t size) |
| 197 | { |
| 198 | int i = 0; |
| 199 | char *res; |
| 200 | |
| 201 | switch (n) { |
| 202 | case 0U ... 9UL: |
| 203 | i = 0; |
| 204 | break; |
| 205 | |
| 206 | case 10U ... 99UL: |
| 207 | i = 1; |
| 208 | break; |
| 209 | |
| 210 | case 100U ... 999UL: |
| 211 | i = 2; |
| 212 | break; |
| 213 | |
| 214 | case 1000U ... 9999UL: |
| 215 | i = 3; |
| 216 | break; |
| 217 | |
| 218 | case 10000U ... 99999UL: |
| 219 | i = 4; |
| 220 | break; |
| 221 | |
| 222 | case 100000U ... 999999UL: |
| 223 | i = 5; |
| 224 | break; |
| 225 | |
| 226 | case 1000000U ... 9999999UL: |
| 227 | i = 6; |
| 228 | break; |
| 229 | |
| 230 | case 10000000U ... 99999999UL: |
| 231 | i = 7; |
| 232 | break; |
| 233 | |
| 234 | case 100000000U ... 999999999UL: |
| 235 | i = 8; |
| 236 | break; |
| 237 | #if __WORDSIZE == 32 |
| 238 | |
| 239 | case 1000000000ULL ... ULONG_MAX: |
| 240 | i = 9; |
| 241 | break; |
| 242 | |
| 243 | #elif __WORDSIZE == 64 |
| 244 | |
| 245 | case 1000000000ULL ... 9999999999UL: |
| 246 | i = 9; |
| 247 | break; |
| 248 | |
| 249 | case 10000000000ULL ... 99999999999UL: |
| 250 | i = 10; |
| 251 | break; |
| 252 | |
| 253 | case 100000000000ULL ... 999999999999UL: |
| 254 | i = 11; |
| 255 | break; |
| 256 | |
| 257 | case 1000000000000ULL ... 9999999999999UL: |
| 258 | i = 12; |
| 259 | break; |
| 260 | |
| 261 | case 10000000000000ULL ... 99999999999999UL: |
| 262 | i = 13; |
| 263 | break; |
| 264 | |
| 265 | case 100000000000000ULL ... 999999999999999UL: |
| 266 | i = 14; |
| 267 | break; |
| 268 | |
| 269 | case 1000000000000000ULL ... 9999999999999999UL: |
| 270 | i = 15; |
| 271 | break; |
| 272 | |
| 273 | case 10000000000000000ULL ... 99999999999999999UL: |
| 274 | i = 16; |
| 275 | break; |
| 276 | |
| 277 | case 100000000000000000ULL ... 999999999999999999UL: |
| 278 | i = 17; |
| 279 | break; |
| 280 | |
| 281 | case 1000000000000000000ULL ... 9999999999999999999UL: |
| 282 | i = 18; |
| 283 | break; |
| 284 | |
| 285 | case 10000000000000000000ULL ... ULONG_MAX: |
| 286 | i = 19; |
| 287 | break; |
| 288 | |
| 289 | #endif |
| 290 | } |
| 291 | if (i + 2 > size) // (i + 1) + '\0' |
| 292 | return NULL; // too long |
| 293 | res = dst + i + 1; |
| 294 | *res = '\0'; |
| 295 | for (; i >= 0; i--) { |
| 296 | dst[i] = n % 10U + '0'; |
| 297 | n /= 10U; |
| 298 | } |
| 299 | return res; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * signed long ASCII representation |
| 304 | * |
| 305 | * return the last char '\0' or NULL if no enough |
| 306 | * space in dst |
| 307 | */ |
| 308 | char *ltoa_o(long int n, char *dst, size_t size) |
| 309 | { |
| 310 | char *pos = dst; |
| 311 | |
| 312 | if (n < 0) { |
| 313 | if (size < 3) |
| 314 | return NULL; // min size is '-' + digit + '\0' but another test in ultoa |
| 315 | *pos = '-'; |
| 316 | pos++; |
| 317 | dst = ultoa_o(-n, pos, size - 1); |
| 318 | } else { |
| 319 | dst = ultoa_o(n, dst, size); |
| 320 | } |
| 321 | return dst; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * signed long long ASCII representation |
| 326 | * |
| 327 | * return the last char '\0' or NULL if no enough |
| 328 | * space in dst |
| 329 | */ |
| 330 | char *lltoa(long long n, char *dst, size_t size) |
| 331 | { |
| 332 | char *pos = dst; |
| 333 | |
| 334 | if (n < 0) { |
| 335 | if (size < 3) |
| 336 | return NULL; // min size is '-' + digit + '\0' but another test in ulltoa |
| 337 | *pos = '-'; |
| 338 | pos++; |
| 339 | dst = ulltoa(-n, pos, size - 1); |
| 340 | } else { |
| 341 | dst = ulltoa(n, dst, size); |
| 342 | } |
| 343 | return dst; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * write a ascii representation of a unsigned into dst, |
| 348 | * return a pointer to the last character |
| 349 | * Pad the ascii representation with '0', using size. |
| 350 | */ |
| 351 | char *utoa_pad(unsigned int n, char *dst, size_t size) |
| 352 | { |
| 353 | int i = 0; |
| 354 | char *ret; |
| 355 | |
| 356 | switch(n) { |
| 357 | case 0U ... 9U: |
| 358 | i = 0; |
| 359 | break; |
| 360 | |
| 361 | case 10U ... 99U: |
| 362 | i = 1; |
| 363 | break; |
| 364 | |
| 365 | case 100U ... 999U: |
| 366 | i = 2; |
| 367 | break; |
| 368 | |
| 369 | case 1000U ... 9999U: |
| 370 | i = 3; |
| 371 | break; |
| 372 | |
| 373 | case 10000U ... 99999U: |
| 374 | i = 4; |
| 375 | break; |
| 376 | |
| 377 | case 100000U ... 999999U: |
| 378 | i = 5; |
| 379 | break; |
| 380 | |
| 381 | case 1000000U ... 9999999U: |
| 382 | i = 6; |
| 383 | break; |
| 384 | |
| 385 | case 10000000U ... 99999999U: |
| 386 | i = 7; |
| 387 | break; |
| 388 | |
| 389 | case 100000000U ... 999999999U: |
| 390 | i = 8; |
| 391 | break; |
| 392 | |
| 393 | case 1000000000U ... 4294967295U: |
| 394 | i = 9; |
| 395 | break; |
| 396 | } |
| 397 | if (i + 2 > size) // (i + 1) + '\0' |
| 398 | return NULL; // too long |
| 399 | if (i < size) |
| 400 | i = size - 2; // padding - '\0' |
| 401 | |
| 402 | ret = dst + i + 1; |
| 403 | *ret = '\0'; |
| 404 | for (; i >= 0; i--) { |
| 405 | dst[i] = n % 10U + '0'; |
| 406 | n /= 10U; |
| 407 | } |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 413 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 414 | * (excluding the terminating zero). |
| 415 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 416 | * long, uses only registers, and consumes only 4 cycles per char. |
| 417 | */ |
| 418 | int strlcpy2(char *dst, const char *src, int size) |
| 419 | { |
| 420 | char *orig = dst; |
| 421 | if (size) { |
| 422 | while (--size && (*dst = *src)) { |
| 423 | src++; dst++; |
| 424 | } |
| 425 | *dst = 0; |
| 426 | } |
| 427 | return dst - orig; |
| 428 | } |
| 429 | |
| 430 | /* |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 431 | * This function simply returns a locally allocated string containing |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 432 | * the ascii representation for number 'n' in decimal. |
| 433 | */ |
Emeric Brun | 3a7fce5 | 2010-01-04 14:54:38 +0100 | [diff] [blame] | 434 | char *ultoa_r(unsigned long n, char *buffer, int size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 435 | { |
| 436 | char *pos; |
| 437 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 438 | pos = buffer + size - 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 439 | *pos-- = '\0'; |
| 440 | |
| 441 | do { |
| 442 | *pos-- = '0' + n % 10; |
| 443 | n /= 10; |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 444 | } while (n && pos >= buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 445 | return pos + 1; |
| 446 | } |
| 447 | |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 448 | /* |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 449 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 450 | * the ascii representation for number 'n' in decimal. |
| 451 | */ |
| 452 | char *lltoa_r(long long int in, char *buffer, int size) |
| 453 | { |
| 454 | char *pos; |
| 455 | int neg = 0; |
| 456 | unsigned long long int n; |
| 457 | |
| 458 | pos = buffer + size - 1; |
| 459 | *pos-- = '\0'; |
| 460 | |
| 461 | if (in < 0) { |
| 462 | neg = 1; |
| 463 | n = -in; |
| 464 | } |
| 465 | else |
| 466 | n = in; |
| 467 | |
| 468 | do { |
| 469 | *pos-- = '0' + n % 10; |
| 470 | n /= 10; |
| 471 | } while (n && pos >= buffer); |
| 472 | if (neg && pos > buffer) |
| 473 | *pos-- = '-'; |
| 474 | return pos + 1; |
| 475 | } |
| 476 | |
| 477 | /* |
| 478 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 1480bd8 | 2015-06-06 19:14:59 +0200 | [diff] [blame] | 479 | * the ascii representation for signed number 'n' in decimal. |
| 480 | */ |
| 481 | char *sltoa_r(long n, char *buffer, int size) |
| 482 | { |
| 483 | char *pos; |
| 484 | |
| 485 | if (n >= 0) |
| 486 | return ultoa_r(n, buffer, size); |
| 487 | |
| 488 | pos = ultoa_r(-n, buffer + 1, size - 1) - 1; |
| 489 | *pos = '-'; |
| 490 | return pos; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * This function simply returns a locally allocated string containing |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 495 | * the ascii representation for number 'n' in decimal, formatted for |
| 496 | * HTML output with tags to create visual grouping by 3 digits. The |
| 497 | * output needs to support at least 171 characters. |
| 498 | */ |
| 499 | const char *ulltoh_r(unsigned long long n, char *buffer, int size) |
| 500 | { |
| 501 | char *start; |
| 502 | int digit = 0; |
| 503 | |
| 504 | start = buffer + size; |
| 505 | *--start = '\0'; |
| 506 | |
| 507 | do { |
| 508 | if (digit == 3 && start >= buffer + 7) |
| 509 | memcpy(start -= 7, "</span>", 7); |
| 510 | |
| 511 | if (start >= buffer + 1) { |
| 512 | *--start = '0' + n % 10; |
| 513 | n /= 10; |
| 514 | } |
| 515 | |
| 516 | if (digit == 3 && start >= buffer + 18) |
| 517 | memcpy(start -= 18, "<span class=\"rls\">", 18); |
| 518 | |
| 519 | if (digit++ == 3) |
| 520 | digit = 1; |
| 521 | } while (n && start > buffer); |
| 522 | return start; |
| 523 | } |
| 524 | |
| 525 | /* |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 526 | * This function simply returns a locally allocated string containing the ascii |
| 527 | * representation for number 'n' in decimal, unless n is 0 in which case it |
| 528 | * returns the alternate string (or an empty string if the alternate string is |
| 529 | * NULL). It use is intended for limits reported in reports, where it's |
| 530 | * desirable not to display anything if there is no limit. Warning! it shares |
| 531 | * the same vector as ultoa_r(). |
| 532 | */ |
| 533 | const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) |
| 534 | { |
| 535 | return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); |
| 536 | } |
| 537 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 538 | /* returns a locally allocated string containing the quoted encoding of the |
| 539 | * input string. The output may be truncated to QSTR_SIZE chars, but it is |
| 540 | * guaranteed that the string will always be properly terminated. Quotes are |
| 541 | * encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must |
| 542 | * always be at least 4 chars. |
| 543 | */ |
| 544 | const char *qstr(const char *str) |
| 545 | { |
| 546 | char *ret = quoted_str[quoted_idx]; |
| 547 | char *p, *end; |
| 548 | |
| 549 | if (++quoted_idx >= NB_QSTR) |
| 550 | quoted_idx = 0; |
| 551 | |
| 552 | p = ret; |
| 553 | end = ret + QSTR_SIZE; |
| 554 | |
| 555 | *p++ = '"'; |
| 556 | |
| 557 | /* always keep 3 chars to support passing "" and the ending " */ |
| 558 | while (*str && p < end - 3) { |
| 559 | if (*str == '"') { |
| 560 | *p++ = '"'; |
| 561 | *p++ = '"'; |
| 562 | } |
| 563 | else |
| 564 | *p++ = *str; |
| 565 | str++; |
| 566 | } |
| 567 | *p++ = '"'; |
| 568 | return ret; |
| 569 | } |
| 570 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 571 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 572 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 573 | * |
| 574 | * It looks like this one would be a good candidate for inlining, but this is |
| 575 | * not interesting because it around 35 bytes long and often called multiple |
| 576 | * times within the same function. |
| 577 | */ |
| 578 | int ishex(char s) |
| 579 | { |
| 580 | s -= '0'; |
| 581 | if ((unsigned char)s <= 9) |
| 582 | return 1; |
| 583 | s -= 'A' - '0'; |
| 584 | if ((unsigned char)s <= 5) |
| 585 | return 1; |
| 586 | s -= 'a' - 'A'; |
| 587 | if ((unsigned char)s <= 5) |
| 588 | return 1; |
| 589 | return 0; |
| 590 | } |
| 591 | |
Willy Tarreau | 3ca1a88 | 2015-01-15 18:43:49 +0100 | [diff] [blame] | 592 | /* rounds <i> down to the closest value having max 2 digits */ |
| 593 | unsigned int round_2dig(unsigned int i) |
| 594 | { |
| 595 | unsigned int mul = 1; |
| 596 | |
| 597 | while (i >= 100) { |
| 598 | i /= 10; |
| 599 | mul *= 10; |
| 600 | } |
| 601 | return i * mul; |
| 602 | } |
| 603 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 604 | /* |
| 605 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 606 | * invalid character is found, a pointer to it is returned. If everything is |
| 607 | * fine, NULL is returned. |
| 608 | */ |
| 609 | const char *invalid_char(const char *name) |
| 610 | { |
| 611 | if (!*name) |
| 612 | return name; |
| 613 | |
| 614 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 615 | if (!isalnum((unsigned char)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 616 | *name != '_' && *name != '-') |
| 617 | return name; |
| 618 | name++; |
| 619 | } |
| 620 | return NULL; |
| 621 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 622 | |
| 623 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 624 | * Checks <name> for invalid characters. Valid chars are [_.-] and those |
| 625 | * accepted by <f> function. |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 626 | * If an invalid character is found, a pointer to it is returned. |
| 627 | * If everything is fine, NULL is returned. |
| 628 | */ |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 629 | static inline const char *__invalid_char(const char *name, int (*f)(int)) { |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 630 | |
| 631 | if (!*name) |
| 632 | return name; |
| 633 | |
| 634 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 635 | if (!f((unsigned char)*name) && *name != '.' && |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 636 | *name != '_' && *name != '-') |
| 637 | return name; |
| 638 | |
| 639 | name++; |
| 640 | } |
| 641 | |
| 642 | return NULL; |
| 643 | } |
| 644 | |
| 645 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 646 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 647 | * If an invalid character is found, a pointer to it is returned. |
| 648 | * If everything is fine, NULL is returned. |
| 649 | */ |
| 650 | const char *invalid_domainchar(const char *name) { |
| 651 | return __invalid_char(name, isalnum); |
| 652 | } |
| 653 | |
| 654 | /* |
| 655 | * Checks <name> for invalid characters. Valid chars are [A-Za-z_.-]. |
| 656 | * If an invalid character is found, a pointer to it is returned. |
| 657 | * If everything is fine, NULL is returned. |
| 658 | */ |
| 659 | const char *invalid_prefix_char(const char *name) { |
Thierry Fournier | f7b7c3e | 2018-03-26 11:54:39 +0200 | [diff] [blame] | 660 | return __invalid_char(name, isalnum); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /* |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 664 | * converts <str> to a struct sockaddr_storage* provided by the caller. The |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 665 | * caller must have zeroed <sa> first, and may have set sa->ss_family to force |
| 666 | * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then |
| 667 | * the function tries to guess the address family from the syntax. If the |
| 668 | * family is forced and the format doesn't match, an error is returned. The |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 669 | * string is assumed to contain only an address, no port. The address can be a |
| 670 | * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to |
| 671 | * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. |
| 672 | * The return address will only have the address family and the address set, |
| 673 | * all other fields remain zero. The string is not supposed to be modified. |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 674 | * The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname |
| 675 | * is resolved, otherwise only IP addresses are resolved, and anything else |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 676 | * returns NULL. If the address contains a port, this one is preserved. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 677 | */ |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 678 | struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 679 | { |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 680 | struct hostent *he; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 681 | /* max IPv6 length, including brackets and terminating NULL */ |
| 682 | char tmpip[48]; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 683 | int port = get_host_port(sa); |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 684 | |
| 685 | /* check IPv6 with square brackets */ |
| 686 | if (str[0] == '[') { |
| 687 | size_t iplength = strlen(str); |
| 688 | |
| 689 | if (iplength < 4) { |
| 690 | /* minimal size is 4 when using brackets "[::]" */ |
| 691 | goto fail; |
| 692 | } |
| 693 | else if (iplength >= sizeof(tmpip)) { |
| 694 | /* IPv6 literal can not be larger than tmpip */ |
| 695 | goto fail; |
| 696 | } |
| 697 | else { |
| 698 | if (str[iplength - 1] != ']') { |
| 699 | /* if address started with bracket, it should end with bracket */ |
| 700 | goto fail; |
| 701 | } |
| 702 | else { |
| 703 | memcpy(tmpip, str + 1, iplength - 2); |
| 704 | tmpip[iplength - 2] = '\0'; |
| 705 | str = tmpip; |
| 706 | } |
| 707 | } |
| 708 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 709 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 710 | /* Any IPv6 address */ |
| 711 | if (str[0] == ':' && str[1] == ':' && !str[2]) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 712 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 713 | sa->ss_family = AF_INET6; |
| 714 | else if (sa->ss_family != AF_INET6) |
| 715 | goto fail; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 716 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 717 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 720 | /* Any address for the family, defaults to IPv4 */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 721 | if (!str[0] || (str[0] == '*' && !str[1])) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 722 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 723 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 724 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 725 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /* check for IPv6 first */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 729 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) && |
| 730 | inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 731 | sa->ss_family = AF_INET6; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 732 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 733 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* then check for IPv4 */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 737 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) && |
| 738 | inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 739 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 740 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 741 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 744 | if (!resolve) |
| 745 | return NULL; |
| 746 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 747 | if (!resolv_hostname_validation(str, NULL)) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 748 | return NULL; |
| 749 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 750 | #ifdef USE_GETADDRINFO |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 751 | if (global.tune.options & GTUNE_USE_GAI) { |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 752 | struct addrinfo hints, *result; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 753 | int success = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 754 | |
| 755 | memset(&result, 0, sizeof(result)); |
| 756 | memset(&hints, 0, sizeof(hints)); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 757 | hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 758 | hints.ai_socktype = SOCK_DGRAM; |
Dmitry Sivachenko | eab7f39 | 2015-10-02 01:01:58 +0200 | [diff] [blame] | 759 | hints.ai_flags = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 760 | hints.ai_protocol = 0; |
| 761 | |
| 762 | if (getaddrinfo(str, NULL, &hints, &result) == 0) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 763 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 764 | sa->ss_family = result->ai_family; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 765 | else if (sa->ss_family != result->ai_family) { |
| 766 | freeaddrinfo(result); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 767 | goto fail; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 768 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 769 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 770 | switch (result->ai_family) { |
| 771 | case AF_INET: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 772 | memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 773 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 774 | success = 1; |
| 775 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 776 | case AF_INET6: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 777 | memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 778 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 779 | success = 1; |
| 780 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
Sean Carey | 58ea039 | 2013-02-15 23:39:18 +0100 | [diff] [blame] | 784 | if (result) |
| 785 | freeaddrinfo(result); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 786 | |
| 787 | if (success) |
| 788 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 789 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 790 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 791 | /* try to resolve an IPv4/IPv6 hostname */ |
| 792 | he = gethostbyname(str); |
| 793 | if (he) { |
| 794 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 795 | sa->ss_family = he->h_addrtype; |
| 796 | else if (sa->ss_family != he->h_addrtype) |
| 797 | goto fail; |
| 798 | |
| 799 | switch (sa->ss_family) { |
| 800 | case AF_INET: |
| 801 | ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 802 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 803 | return sa; |
| 804 | case AF_INET6: |
| 805 | ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 806 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 807 | return sa; |
| 808 | } |
| 809 | } |
| 810 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 811 | /* unsupported address family */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 812 | fail: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 813 | return NULL; |
| 814 | } |
| 815 | |
| 816 | /* |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 817 | * Converts <str> to a locally allocated struct sockaddr_storage *, and a port |
| 818 | * range or offset consisting in two integers that the caller will have to |
| 819 | * check to find the relevant input format. The following format are supported : |
| 820 | * |
| 821 | * String format | address | port | low | high |
| 822 | * addr | <addr> | 0 | 0 | 0 |
| 823 | * addr: | <addr> | 0 | 0 | 0 |
| 824 | * addr:port | <addr> | <port> | <port> | <port> |
| 825 | * addr:pl-ph | <addr> | <pl> | <pl> | <ph> |
| 826 | * addr:+port | <addr> | <port> | 0 | <port> |
| 827 | * addr:-port | <addr> |-<port> | <port> | 0 |
| 828 | * |
| 829 | * The detection of a port range or increment by the caller is made by |
| 830 | * comparing <low> and <high>. If both are equal, then port 0 means no port |
| 831 | * was specified. The caller may pass NULL for <low> and <high> if it is not |
| 832 | * interested in retrieving port ranges. |
| 833 | * |
| 834 | * Note that <addr> above may also be : |
| 835 | * - empty ("") => family will be AF_INET and address will be INADDR_ANY |
| 836 | * - "*" => family will be AF_INET and address will be INADDR_ANY |
| 837 | * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY |
| 838 | * - a host name => family and address will depend on host name resolving. |
| 839 | * |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 840 | * A prefix may be passed in before the address above to force the family : |
| 841 | * - "ipv4@" => force address to resolve as IPv4 and fail if not possible. |
| 842 | * - "ipv6@" => force address to resolve as IPv6 and fail if not possible. |
| 843 | * - "unix@" => force address to be a path to a UNIX socket even if the |
| 844 | * path does not start with a '/' |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 845 | * - 'abns@' -> force address to belong to the abstract namespace (Linux |
| 846 | * only). These sockets are just like Unix sockets but without |
| 847 | * the need for an underlying file system. The address is a |
| 848 | * string. Technically it's like a Unix socket with a zero in |
| 849 | * the first byte of the address. |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 850 | * - "fd@" => an integer must follow, and is a file descriptor number. |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 851 | * |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 852 | * IPv6 addresses can be declared with or without square brackets. When using |
| 853 | * square brackets for IPv6 addresses, the port separator (colon) is optional. |
| 854 | * If not using square brackets, and in order to avoid any ambiguity with |
| 855 | * IPv6 addresses, the last colon ':' is mandatory even when no port is specified. |
| 856 | * NULL is returned if the address cannot be parsed. The <low> and <high> ports |
| 857 | * are always initialized if non-null, even for non-IP families. |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 858 | * |
| 859 | * If <pfx> is non-null, it is used as a string prefix before any path-based |
| 860 | * address (typically the path to a unix socket). |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 861 | * |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 862 | * if <fqdn> is non-null, it will be filled with : |
| 863 | * - a pointer to the FQDN of the server name to resolve if there's one, and |
| 864 | * that the caller will have to free(), |
| 865 | * - NULL if there was an explicit address that doesn't require resolution. |
| 866 | * |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 867 | * Hostnames are only resolved if <opts> has PA_O_RESOLVE. Otherwise <fqdn> is |
| 868 | * still honored so it is possible for the caller to know whether a resolution |
| 869 | * failed by clearing this flag and checking if <fqdn> was filled, indicating |
| 870 | * the need for a resolution. |
Thierry FOURNIER | 7fe3be7 | 2015-09-26 20:03:36 +0200 | [diff] [blame] | 871 | * |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 872 | * When a file descriptor is passed, its value is put into the s_addr part of |
Willy Tarreau | a5b325f | 2020-09-04 16:44:20 +0200 | [diff] [blame] | 873 | * the address when cast to sockaddr_in and the address family is |
| 874 | * AF_CUST_EXISTING_FD. |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 875 | * |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 876 | * The matching protocol will be set into <proto> if non-null. |
| 877 | * |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 878 | * Any known file descriptor is also assigned to <fd> if non-null, otherwise it |
| 879 | * is forced to -1. |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 880 | */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 881 | struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd, |
| 882 | struct protocol **proto, char **err, |
| 883 | const char *pfx, char **fqdn, unsigned int opts) |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 884 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 885 | static THREAD_LOCAL struct sockaddr_storage ss; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 886 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 887 | struct protocol *new_proto = NULL; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 888 | char *back, *str2; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 889 | char *port1, *port2; |
| 890 | int portl, porth, porta; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 891 | int abstract = 0; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 892 | int new_fd = -1; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 893 | int sock_type, ctrl_type; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 894 | |
| 895 | portl = porth = porta = 0; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 896 | if (fqdn) |
| 897 | *fqdn = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 898 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 899 | str2 = back = env_expand(strdup(str)); |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 900 | if (str2 == NULL) { |
| 901 | memprintf(err, "out of memory in '%s'\n", __FUNCTION__); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 902 | goto out; |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 903 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 904 | |
Willy Tarreau | 9f69f46 | 2015-09-08 16:01:25 +0200 | [diff] [blame] | 905 | if (!*str2) { |
| 906 | memprintf(err, "'%s' resolves to an empty address (environment variable missing?)\n", str); |
| 907 | goto out; |
| 908 | } |
| 909 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 910 | memset(&ss, 0, sizeof(ss)); |
| 911 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 912 | /* prepare the default socket types */ |
Willy Tarreau | f23b1bc | 2021-03-23 18:36:37 +0100 | [diff] [blame] | 913 | if ((opts & (PA_O_STREAM|PA_O_DGRAM)) == PA_O_DGRAM || |
| 914 | ((opts & (PA_O_STREAM|PA_O_DGRAM)) == (PA_O_DGRAM|PA_O_STREAM) && (opts & PA_O_DEFAULT_DGRAM))) |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 915 | sock_type = ctrl_type = SOCK_DGRAM; |
| 916 | else |
| 917 | sock_type = ctrl_type = SOCK_STREAM; |
| 918 | |
| 919 | if (strncmp(str2, "stream+", 7) == 0) { |
| 920 | str2 += 7; |
| 921 | sock_type = ctrl_type = SOCK_STREAM; |
| 922 | } |
| 923 | else if (strncmp(str2, "dgram+", 6) == 0) { |
| 924 | str2 += 6; |
| 925 | sock_type = ctrl_type = SOCK_DGRAM; |
| 926 | } |
| 927 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 928 | if (strncmp(str2, "unix@", 5) == 0) { |
| 929 | str2 += 5; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 930 | abstract = 0; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 931 | ss.ss_family = AF_UNIX; |
| 932 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 933 | else if (strncmp(str2, "abns@", 5) == 0) { |
| 934 | str2 += 5; |
| 935 | abstract = 1; |
| 936 | ss.ss_family = AF_UNIX; |
| 937 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 938 | else if (strncmp(str2, "ipv4@", 5) == 0) { |
| 939 | str2 += 5; |
| 940 | ss.ss_family = AF_INET; |
| 941 | } |
| 942 | else if (strncmp(str2, "ipv6@", 5) == 0) { |
| 943 | str2 += 5; |
| 944 | ss.ss_family = AF_INET6; |
| 945 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 946 | else if (strncmp(str2, "udp4@", 5) == 0) { |
| 947 | str2 += 5; |
| 948 | ss.ss_family = AF_INET; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 949 | sock_type = ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 950 | } |
| 951 | else if (strncmp(str2, "udp6@", 5) == 0) { |
| 952 | str2 += 5; |
| 953 | ss.ss_family = AF_INET6; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 954 | sock_type = ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 955 | } |
| 956 | else if (strncmp(str2, "udp@", 4) == 0) { |
| 957 | str2 += 4; |
| 958 | ss.ss_family = AF_UNSPEC; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 959 | sock_type = ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 960 | } |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 961 | else if (strncmp(str2, "quic4@", 6) == 0) { |
| 962 | str2 += 6; |
| 963 | ss.ss_family = AF_INET; |
| 964 | sock_type = SOCK_DGRAM; |
| 965 | ctrl_type = SOCK_STREAM; |
| 966 | } |
| 967 | else if (strncmp(str2, "quic6@", 6) == 0) { |
| 968 | str2 += 6; |
| 969 | ss.ss_family = AF_INET6; |
| 970 | sock_type = SOCK_DGRAM; |
| 971 | ctrl_type = SOCK_STREAM; |
| 972 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 973 | else if (strncmp(str2, "fd@", 3) == 0) { |
| 974 | str2 += 3; |
| 975 | ss.ss_family = AF_CUST_EXISTING_FD; |
| 976 | } |
| 977 | else if (strncmp(str2, "sockpair@", 9) == 0) { |
| 978 | str2 += 9; |
| 979 | ss.ss_family = AF_CUST_SOCKPAIR; |
| 980 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 981 | else if (*str2 == '/') { |
| 982 | ss.ss_family = AF_UNIX; |
| 983 | } |
| 984 | else |
| 985 | ss.ss_family = AF_UNSPEC; |
| 986 | |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 987 | if (ss.ss_family == AF_CUST_SOCKPAIR) { |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 988 | struct sockaddr_storage ss2; |
| 989 | socklen_t addr_len; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 990 | char *endptr; |
| 991 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 992 | new_fd = strtol(str2, &endptr, 10); |
| 993 | if (!*str2 || new_fd < 0 || *endptr) { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 994 | memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str); |
| 995 | goto out; |
| 996 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 997 | |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 998 | /* just verify that it's a socket */ |
| 999 | addr_len = sizeof(ss2); |
| 1000 | if (getsockname(new_fd, (struct sockaddr *)&ss2, &addr_len) == -1) { |
| 1001 | memprintf(err, "cannot use file descriptor '%d' : %s.\n", new_fd, strerror(errno)); |
| 1002 | goto out; |
| 1003 | } |
| 1004 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1005 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1006 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1007 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1008 | else if (ss.ss_family == AF_CUST_EXISTING_FD) { |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1009 | char *endptr; |
| 1010 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1011 | new_fd = strtol(str2, &endptr, 10); |
| 1012 | if (!*str2 || new_fd < 0 || *endptr) { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 1013 | memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str); |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1014 | goto out; |
| 1015 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1016 | |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1017 | if (opts & PA_O_SOCKET_FD) { |
| 1018 | socklen_t addr_len; |
| 1019 | int type; |
| 1020 | |
| 1021 | addr_len = sizeof(ss); |
| 1022 | if (getsockname(new_fd, (struct sockaddr *)&ss, &addr_len) == -1) { |
| 1023 | memprintf(err, "cannot use file descriptor '%d' : %s.\n", new_fd, strerror(errno)); |
| 1024 | goto out; |
| 1025 | } |
| 1026 | |
| 1027 | addr_len = sizeof(type); |
| 1028 | if (getsockopt(new_fd, SOL_SOCKET, SO_TYPE, &type, &addr_len) != 0 || |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1029 | (type == SOCK_STREAM) != (sock_type == SOCK_STREAM)) { |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1030 | memprintf(err, "socket on file descriptor '%d' is of the wrong type.\n", new_fd); |
| 1031 | goto out; |
| 1032 | } |
| 1033 | |
| 1034 | porta = portl = porth = get_host_port(&ss); |
| 1035 | } else if (opts & PA_O_RAW_FD) { |
| 1036 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1037 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
| 1038 | } else { |
| 1039 | memprintf(err, "a file descriptor is not acceptable here in '%s'\n", str); |
| 1040 | goto out; |
| 1041 | } |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1042 | } |
| 1043 | else if (ss.ss_family == AF_UNIX) { |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1044 | struct sockaddr_un *un = (struct sockaddr_un *)&ss; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1045 | int prefix_path_len; |
| 1046 | int max_path_len; |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1047 | int adr_len; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1048 | |
| 1049 | /* complete unix socket path name during startup or soft-restart is |
| 1050 | * <unix_bind_prefix><path>.<pid>.<bak|tmp> |
| 1051 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1052 | prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0; |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1053 | max_path_len = (sizeof(un->sun_path) - 1) - |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 1054 | (abstract ? 0 : prefix_path_len + 1 + 5 + 1 + 3); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1055 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1056 | adr_len = strlen(str2); |
| 1057 | if (adr_len > max_path_len) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1058 | memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len); |
| 1059 | goto out; |
| 1060 | } |
| 1061 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1062 | /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */ |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1063 | memset(un->sun_path, 0, sizeof(un->sun_path)); |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1064 | if (prefix_path_len) |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1065 | memcpy(un->sun_path, pfx, prefix_path_len); |
| 1066 | memcpy(un->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1067 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1068 | else { /* IPv4 and IPv6 */ |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1069 | char *end = str2 + strlen(str2); |
| 1070 | char *chr; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1071 | |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1072 | /* search for : or ] whatever comes first */ |
| 1073 | for (chr = end-1; chr > str2; chr--) { |
| 1074 | if (*chr == ']' || *chr == ':') |
| 1075 | break; |
| 1076 | } |
| 1077 | |
| 1078 | if (*chr == ':') { |
| 1079 | /* Found a colon before a closing-bracket, must be a port separator. |
| 1080 | * This guarantee backward compatibility. |
| 1081 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1082 | if (!(opts & PA_O_PORT_OK)) { |
| 1083 | memprintf(err, "port specification not permitted here in '%s'", str); |
| 1084 | goto out; |
| 1085 | } |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1086 | *chr++ = '\0'; |
| 1087 | port1 = chr; |
| 1088 | } |
| 1089 | else { |
| 1090 | /* Either no colon and no closing-bracket |
| 1091 | * or directly ending with a closing-bracket. |
| 1092 | * However, no port. |
| 1093 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1094 | if (opts & PA_O_PORT_MAND) { |
| 1095 | memprintf(err, "missing port specification in '%s'", str); |
| 1096 | goto out; |
| 1097 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1098 | port1 = ""; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1099 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1100 | |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1101 | if (isdigit((unsigned char)*port1)) { /* single port or range */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1102 | port2 = strchr(port1, '-'); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1103 | if (port2) { |
| 1104 | if (!(opts & PA_O_PORT_RANGE)) { |
| 1105 | memprintf(err, "port range not permitted here in '%s'", str); |
| 1106 | goto out; |
| 1107 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1108 | *port2++ = '\0'; |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1109 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1110 | else |
| 1111 | port2 = port1; |
| 1112 | portl = atoi(port1); |
| 1113 | porth = atoi(port2); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1114 | |
| 1115 | if (portl < !!(opts & PA_O_PORT_MAND) || portl > 65535) { |
| 1116 | memprintf(err, "invalid port '%s'", port1); |
| 1117 | goto out; |
| 1118 | } |
| 1119 | |
| 1120 | if (porth < !!(opts & PA_O_PORT_MAND) || porth > 65535) { |
| 1121 | memprintf(err, "invalid port '%s'", port2); |
| 1122 | goto out; |
| 1123 | } |
| 1124 | |
| 1125 | if (portl > porth) { |
| 1126 | memprintf(err, "invalid port range '%d-%d'", portl, porth); |
| 1127 | goto out; |
| 1128 | } |
| 1129 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1130 | porta = portl; |
| 1131 | } |
| 1132 | else if (*port1 == '-') { /* negative offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1133 | if (!(opts & PA_O_PORT_OFS)) { |
| 1134 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1135 | goto out; |
| 1136 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1137 | portl = atoi(port1 + 1); |
| 1138 | porta = -portl; |
| 1139 | } |
| 1140 | else if (*port1 == '+') { /* positive offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1141 | if (!(opts & PA_O_PORT_OFS)) { |
| 1142 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1143 | goto out; |
| 1144 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1145 | porth = atoi(port1 + 1); |
| 1146 | porta = porth; |
| 1147 | } |
| 1148 | else if (*port1) { /* other any unexpected char */ |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 1149 | memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1150 | goto out; |
| 1151 | } |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1152 | else if (opts & PA_O_PORT_MAND) { |
| 1153 | memprintf(err, "missing port specification in '%s'", str); |
| 1154 | goto out; |
| 1155 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1156 | |
| 1157 | /* first try to parse the IP without resolving. If it fails, it |
| 1158 | * tells us we need to keep a copy of the FQDN to resolve later |
| 1159 | * and to enable DNS. In this case we can proceed if <fqdn> is |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 1160 | * set or if PA_O_RESOLVE is set, otherwise it's an error. |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1161 | */ |
| 1162 | if (str2ip2(str2, &ss, 0) == NULL) { |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 1163 | if ((!(opts & PA_O_RESOLVE) && !fqdn) || |
| 1164 | ((opts & PA_O_RESOLVE) && str2ip2(str2, &ss, 1) == NULL)) { |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1165 | memprintf(err, "invalid address: '%s' in '%s'\n", str2, str); |
| 1166 | goto out; |
| 1167 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1168 | |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1169 | if (fqdn) { |
| 1170 | if (str2 != back) |
| 1171 | memmove(back, str2, strlen(str2) + 1); |
| 1172 | *fqdn = back; |
| 1173 | back = NULL; |
| 1174 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1175 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1176 | set_host_port(&ss, porta); |
Willy Tarreau | e4c58c8 | 2013-03-06 15:28:17 +0100 | [diff] [blame] | 1177 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 1178 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1179 | if (ctrl_type == SOCK_STREAM && !(opts & PA_O_STREAM)) { |
| 1180 | memprintf(err, "stream-type socket not acceptable in '%s'\n", str); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | else if (ctrl_type == SOCK_DGRAM && !(opts & PA_O_DGRAM)) { |
| 1184 | memprintf(err, "dgram-type socket not acceptable in '%s'\n", str); |
| 1185 | goto out; |
| 1186 | } |
| 1187 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1188 | if (proto || (opts & PA_O_CONNECT)) { |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1189 | /* Note: if the caller asks for a proto, we must find one, |
| 1190 | * except if we return with an fqdn that will resolve later, |
| 1191 | * in which case the address is not known yet (this is only |
| 1192 | * for servers actually). |
| 1193 | */ |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1194 | new_proto = protocol_lookup(ss.ss_family, |
Willy Tarreau | af9609b | 2020-09-16 22:04:46 +0200 | [diff] [blame] | 1195 | sock_type == SOCK_DGRAM, |
| 1196 | ctrl_type == SOCK_DGRAM); |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1197 | |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1198 | if (!new_proto && (!fqdn || !*fqdn)) { |
| 1199 | memprintf(err, "unsupported protocol family %d for address '%s'", ss.ss_family, str); |
| 1200 | goto out; |
| 1201 | } |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1202 | |
| 1203 | if ((opts & PA_O_CONNECT) && new_proto && !new_proto->connect) { |
| 1204 | memprintf(err, "connect() not supported for this protocol family %d used by address '%s'", ss.ss_family, str); |
| 1205 | goto out; |
| 1206 | } |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1207 | } |
| 1208 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1209 | ret = &ss; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1210 | out: |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 1211 | if (port) |
| 1212 | *port = porta; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 1213 | if (low) |
| 1214 | *low = portl; |
| 1215 | if (high) |
| 1216 | *high = porth; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1217 | if (fd) |
| 1218 | *fd = new_fd; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1219 | if (proto) |
| 1220 | *proto = new_proto; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1221 | free(back); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1222 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 1225 | /* converts <addr> and <port> into a string representation of the address and port. This is sort |
| 1226 | * of an inverse of str2sa_range, with some restrictions. The supported families are AF_INET, |
| 1227 | * AF_INET6, AF_UNIX, and AF_CUST_SOCKPAIR. If the family is unsopported NULL is returned. |
| 1228 | * If map_ports is true, then the sign of the port is included in the output, to indicate it is |
| 1229 | * relative to the incoming port. AF_INET and AF_INET6 will be in the form "<addr>:<port>". |
| 1230 | * AF_UNIX will either be just the path (if using a pathname) or "abns@<path>" if it is abstract. |
| 1231 | * AF_CUST_SOCKPAIR will be of the form "sockpair@<fd>". |
| 1232 | * |
| 1233 | * The returned char* is allocated, and it is the responsibility of the caller to free it. |
| 1234 | */ |
| 1235 | char * sa2str(const struct sockaddr_storage *addr, int port, int map_ports) |
| 1236 | { |
| 1237 | char buffer[INET6_ADDRSTRLEN]; |
| 1238 | char *out = NULL; |
| 1239 | const void *ptr; |
| 1240 | const char *path; |
| 1241 | |
| 1242 | switch (addr->ss_family) { |
| 1243 | case AF_INET: |
| 1244 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1245 | break; |
| 1246 | case AF_INET6: |
| 1247 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1248 | break; |
| 1249 | case AF_UNIX: |
| 1250 | path = ((struct sockaddr_un *)addr)->sun_path; |
| 1251 | if (path[0] == '\0') { |
| 1252 | const int max_length = sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1; |
| 1253 | return memprintf(&out, "abns@%.*s", max_length, path+1); |
| 1254 | } else { |
| 1255 | return strdup(path); |
| 1256 | } |
| 1257 | case AF_CUST_SOCKPAIR: |
| 1258 | return memprintf(&out, "sockpair@%d", ((struct sockaddr_in *)addr)->sin_addr.s_addr); |
| 1259 | default: |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | inet_ntop(addr->ss_family, ptr, buffer, get_addr_len(addr)); |
| 1263 | if (map_ports) |
| 1264 | return memprintf(&out, "%s:%+d", buffer, port); |
| 1265 | else |
| 1266 | return memprintf(&out, "%s:%d", buffer, port); |
| 1267 | } |
| 1268 | |
| 1269 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1270 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 1271 | * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1 |
Jarno Huuskonen | 577d5ac | 2017-05-21 17:32:21 +0300 | [diff] [blame] | 1272 | * if the conversion succeeds otherwise zero. |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1273 | */ |
| 1274 | int str2mask(const char *str, struct in_addr *mask) |
| 1275 | { |
| 1276 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 1277 | if (!inet_pton(AF_INET, str, mask)) |
| 1278 | return 0; |
| 1279 | } |
| 1280 | else { /* mask length */ |
| 1281 | char *err; |
| 1282 | unsigned long len = strtol(str, &err, 10); |
| 1283 | |
| 1284 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 1285 | return 0; |
Tim Duesterhus | 8575f72 | 2018-01-25 16:24:48 +0100 | [diff] [blame] | 1286 | |
| 1287 | len2mask4(len, mask); |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1288 | } |
| 1289 | return 1; |
| 1290 | } |
| 1291 | |
Tim Duesterhus | 4718517 | 2018-01-25 16:24:49 +0100 | [diff] [blame] | 1292 | /* converts <str> to a struct in6_addr containing a network mask. It can be |
Tim Duesterhus | 5e64286 | 2018-02-20 17:02:18 +0100 | [diff] [blame] | 1293 | * passed in quadruplet form (ffff:ffff::) or in CIDR form (64). It returns 1 |
Tim Duesterhus | 4718517 | 2018-01-25 16:24:49 +0100 | [diff] [blame] | 1294 | * if the conversion succeeds otherwise zero. |
| 1295 | */ |
| 1296 | int str2mask6(const char *str, struct in6_addr *mask) |
| 1297 | { |
| 1298 | if (strchr(str, ':') != NULL) { /* quadruplet notation */ |
| 1299 | if (!inet_pton(AF_INET6, str, mask)) |
| 1300 | return 0; |
| 1301 | } |
| 1302 | else { /* mask length */ |
| 1303 | char *err; |
| 1304 | unsigned long len = strtol(str, &err, 10); |
| 1305 | |
| 1306 | if (!*str || (err && *err) || (unsigned)len > 128) |
| 1307 | return 0; |
| 1308 | |
| 1309 | len2mask6(len, mask); |
| 1310 | } |
| 1311 | return 1; |
| 1312 | } |
| 1313 | |
Thierry FOURNIER | b050463 | 2013-12-14 15:39:02 +0100 | [diff] [blame] | 1314 | /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion |
| 1315 | * succeeds otherwise zero. |
| 1316 | */ |
| 1317 | int cidr2dotted(int cidr, struct in_addr *mask) { |
| 1318 | |
| 1319 | if (cidr < 0 || cidr > 32) |
| 1320 | return 0; |
| 1321 | |
| 1322 | mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0; |
| 1323 | return 1; |
| 1324 | } |
| 1325 | |
Thierry Fournier | 70473a5 | 2016-02-17 17:12:14 +0100 | [diff] [blame] | 1326 | /* Convert mask from bit length form to in_addr form. |
| 1327 | * This function never fails. |
| 1328 | */ |
| 1329 | void len2mask4(int len, struct in_addr *addr) |
| 1330 | { |
| 1331 | if (len >= 32) { |
| 1332 | addr->s_addr = 0xffffffff; |
| 1333 | return; |
| 1334 | } |
| 1335 | if (len <= 0) { |
| 1336 | addr->s_addr = 0x00000000; |
| 1337 | return; |
| 1338 | } |
| 1339 | addr->s_addr = 0xffffffff << (32 - len); |
| 1340 | addr->s_addr = htonl(addr->s_addr); |
| 1341 | } |
| 1342 | |
| 1343 | /* Convert mask from bit length form to in6_addr form. |
| 1344 | * This function never fails. |
| 1345 | */ |
| 1346 | void len2mask6(int len, struct in6_addr *addr) |
| 1347 | { |
| 1348 | len2mask4(len, (struct in_addr *)&addr->s6_addr[0]); /* msb */ |
| 1349 | len -= 32; |
| 1350 | len2mask4(len, (struct in_addr *)&addr->s6_addr[4]); |
| 1351 | len -= 32; |
| 1352 | len2mask4(len, (struct in_addr *)&addr->s6_addr[8]); |
| 1353 | len -= 32; |
| 1354 | len2mask4(len, (struct in_addr *)&addr->s6_addr[12]); /* lsb */ |
| 1355 | } |
| 1356 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1357 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 1358 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1359 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1360 | * is optional and either in the dotted or CIDR notation. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1361 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 1362 | */ |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1363 | int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1364 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1365 | __label__ out_free, out_err; |
| 1366 | char *c, *s; |
| 1367 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1368 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1369 | s = strdup(str); |
| 1370 | if (!s) |
| 1371 | return 0; |
| 1372 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1373 | memset(mask, 0, sizeof(*mask)); |
| 1374 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1375 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1376 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1377 | *c++ = '\0'; |
| 1378 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1379 | if (!str2mask(c, mask)) |
| 1380 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1381 | } |
| 1382 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 1383 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1384 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1385 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1386 | struct hostent *he; |
| 1387 | |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1388 | if (!resolve) |
| 1389 | goto out_err; |
| 1390 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1391 | if ((he = gethostbyname(s)) == NULL) { |
| 1392 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1393 | } |
| 1394 | else |
| 1395 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1396 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1397 | |
| 1398 | ret_val = 1; |
| 1399 | out_free: |
| 1400 | free(s); |
| 1401 | return ret_val; |
| 1402 | out_err: |
| 1403 | ret_val = 0; |
| 1404 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1405 | } |
| 1406 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1407 | |
| 1408 | /* |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1409 | * converts <str> to two struct in6_addr* which must be pre-allocated. |
| 1410 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1411 | * is an optional number of bits (128 being the default). |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1412 | * Returns 1 if OK, 0 if error. |
| 1413 | */ |
| 1414 | int str62net(const char *str, struct in6_addr *addr, unsigned char *mask) |
| 1415 | { |
| 1416 | char *c, *s; |
| 1417 | int ret_val = 0; |
| 1418 | char *err; |
| 1419 | unsigned long len = 128; |
| 1420 | |
| 1421 | s = strdup(str); |
| 1422 | if (!s) |
| 1423 | return 0; |
| 1424 | |
| 1425 | memset(mask, 0, sizeof(*mask)); |
| 1426 | memset(addr, 0, sizeof(*addr)); |
| 1427 | |
| 1428 | if ((c = strrchr(s, '/')) != NULL) { |
| 1429 | *c++ = '\0'; /* c points to the mask */ |
| 1430 | if (!*c) |
| 1431 | goto out_free; |
| 1432 | |
| 1433 | len = strtoul(c, &err, 10); |
| 1434 | if ((err && *err) || (unsigned)len > 128) |
| 1435 | goto out_free; |
| 1436 | } |
| 1437 | *mask = len; /* OK we have a valid mask in <len> */ |
| 1438 | |
| 1439 | if (!inet_pton(AF_INET6, s, addr)) |
| 1440 | goto out_free; |
| 1441 | |
| 1442 | ret_val = 1; |
| 1443 | out_free: |
| 1444 | free(s); |
| 1445 | return ret_val; |
| 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /* |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1450 | * Parse IPv4 address found in url. Return the number of bytes parsed. It |
| 1451 | * expects exactly 4 numbers between 0 and 255 delimited by dots, and returns |
| 1452 | * zero in case of mismatch. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1453 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1454 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1455 | { |
| 1456 | int saw_digit, octets, ch; |
| 1457 | u_char tmp[4], *tp; |
| 1458 | const char *cp = addr; |
| 1459 | |
| 1460 | saw_digit = 0; |
| 1461 | octets = 0; |
| 1462 | *(tp = tmp) = 0; |
| 1463 | |
| 1464 | while (*addr) { |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1465 | unsigned char digit = (ch = *addr) - '0'; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1466 | if (digit > 9 && ch != '.') |
| 1467 | break; |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1468 | addr++; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1469 | if (digit <= 9) { |
| 1470 | u_int new = *tp * 10 + digit; |
| 1471 | if (new > 255) |
| 1472 | return 0; |
| 1473 | *tp = new; |
| 1474 | if (!saw_digit) { |
| 1475 | if (++octets > 4) |
| 1476 | return 0; |
| 1477 | saw_digit = 1; |
| 1478 | } |
| 1479 | } else if (ch == '.' && saw_digit) { |
| 1480 | if (octets == 4) |
| 1481 | return 0; |
| 1482 | *++tp = 0; |
| 1483 | saw_digit = 0; |
| 1484 | } else |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | if (octets < 4) |
| 1489 | return 0; |
| 1490 | |
| 1491 | memcpy(&dst->s_addr, tmp, 4); |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1492 | return addr - cp; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /* |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1496 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1497 | * <out> contain the code of the detected scheme, the start and length of |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1498 | * the hostname. Actually only http and https are supported. <out> can be NULL. |
| 1499 | * This function returns the consumed length. It is useful if you parse complete |
| 1500 | * url like http://host:port/path, because the consumed length corresponds to |
| 1501 | * the first character of the path. If the conversion fails, it returns -1. |
| 1502 | * |
| 1503 | * This function tries to resolve the DNS name if haproxy is in starting mode. |
| 1504 | * So, this function may be used during the configuration parsing. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1505 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1506 | int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1507 | { |
| 1508 | const char *curr = url, *cp = url; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1509 | const char *end; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1510 | int ret, url_code = 0; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1511 | unsigned long long int http_code = 0; |
| 1512 | int default_port; |
| 1513 | struct hostent *he; |
| 1514 | char *p; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1515 | |
| 1516 | /* Firstly, try to find :// pattern */ |
| 1517 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 1518 | url_code = ((url_code & 0xffff) << 8); |
| 1519 | url_code += (unsigned char)*curr++; |
| 1520 | } |
| 1521 | |
| 1522 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 1523 | * before pattern is matching our http pattern. |
| 1524 | * If so parse ip address and port in uri. |
| 1525 | * |
| 1526 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 1527 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1528 | if (url_code != 0x3a2f2f) |
| 1529 | return -1; |
| 1530 | |
| 1531 | /* Copy scheme, and utrn to lower case. */ |
| 1532 | while (cp < curr - 3) |
| 1533 | http_code = (http_code << 8) + *cp++; |
| 1534 | http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */ |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1535 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1536 | /* HTTP or HTTPS url matching */ |
| 1537 | if (http_code == 0x2020202068747470ULL) { |
| 1538 | default_port = 80; |
| 1539 | if (out) |
| 1540 | out->scheme = SCH_HTTP; |
| 1541 | } |
| 1542 | else if (http_code == 0x2020206874747073ULL) { |
| 1543 | default_port = 443; |
| 1544 | if (out) |
| 1545 | out->scheme = SCH_HTTPS; |
| 1546 | } |
| 1547 | else |
| 1548 | return -1; |
| 1549 | |
| 1550 | /* If the next char is '[', the host address is IPv6. */ |
| 1551 | if (*curr == '[') { |
| 1552 | curr++; |
| 1553 | |
| 1554 | /* Check trash size */ |
| 1555 | if (trash.size < ulen) |
| 1556 | return -1; |
| 1557 | |
| 1558 | /* Look for ']' and copy the address in a trash buffer. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1559 | p = trash.area; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1560 | for (end = curr; |
| 1561 | end < url + ulen && *end != ']'; |
| 1562 | end++, p++) |
| 1563 | *p = *end; |
| 1564 | if (*end != ']') |
| 1565 | return -1; |
| 1566 | *p = '\0'; |
| 1567 | |
| 1568 | /* Update out. */ |
| 1569 | if (out) { |
| 1570 | out->host = curr; |
| 1571 | out->host_len = end - curr; |
| 1572 | } |
| 1573 | |
| 1574 | /* Try IPv6 decoding. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1575 | if (!inet_pton(AF_INET6, trash.area, &((struct sockaddr_in6 *)addr)->sin6_addr)) |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1576 | return -1; |
| 1577 | end++; |
| 1578 | |
| 1579 | /* Decode port. */ |
| 1580 | if (*end == ':') { |
| 1581 | end++; |
| 1582 | default_port = read_uint(&end, url + ulen); |
| 1583 | } |
| 1584 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1585 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1586 | return end - url; |
| 1587 | } |
| 1588 | else { |
| 1589 | /* We are looking for IP address. If you want to parse and |
| 1590 | * resolve hostname found in url, you can use str2sa_range(), but |
| 1591 | * be warned this can slow down global daemon performances |
| 1592 | * while handling lagging dns responses. |
| 1593 | */ |
| 1594 | ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr); |
| 1595 | if (ret) { |
| 1596 | /* Update out. */ |
| 1597 | if (out) { |
| 1598 | out->host = curr; |
| 1599 | out->host_len = ret; |
| 1600 | } |
| 1601 | |
| 1602 | curr += ret; |
| 1603 | |
| 1604 | /* Decode port. */ |
| 1605 | if (*curr == ':') { |
| 1606 | curr++; |
| 1607 | default_port = read_uint(&curr, url + ulen); |
| 1608 | } |
| 1609 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1610 | |
| 1611 | /* Set family. */ |
| 1612 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1613 | return curr - url; |
| 1614 | } |
| 1615 | else if (global.mode & MODE_STARTING) { |
| 1616 | /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute |
| 1617 | * synchronous DNS request only if HAProxy is in the start state. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1618 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1619 | |
| 1620 | /* look for : or / or end */ |
| 1621 | for (end = curr; |
| 1622 | end < url + ulen && *end != '/' && *end != ':'; |
| 1623 | end++); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1624 | memcpy(trash.area, curr, end - curr); |
| 1625 | trash.area[end - curr] = '\0'; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* try to resolve an IPv4/IPv6 hostname */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1628 | he = gethostbyname(trash.area); |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1629 | if (!he) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1630 | return -1; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* Update out. */ |
| 1633 | if (out) { |
| 1634 | out->host = curr; |
| 1635 | out->host_len = end - curr; |
| 1636 | } |
| 1637 | |
| 1638 | /* Decode port. */ |
| 1639 | if (*end == ':') { |
| 1640 | end++; |
| 1641 | default_port = read_uint(&end, url + ulen); |
| 1642 | } |
| 1643 | |
| 1644 | /* Copy IP address, set port and family. */ |
| 1645 | switch (he->h_addrtype) { |
| 1646 | case AF_INET: |
| 1647 | ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1648 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1649 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1650 | return end - url; |
| 1651 | |
| 1652 | case AF_INET6: |
| 1653 | ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 1654 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1655 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1656 | return end - url; |
| 1657 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1658 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1659 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1660 | return -1; |
| 1661 | } |
| 1662 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1663 | /* Tries to convert a sockaddr_storage address to text form. Upon success, the |
| 1664 | * address family is returned so that it's easy for the caller to adapt to the |
| 1665 | * output format. Zero is returned if the address family is not supported. -1 |
| 1666 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1667 | * supported. |
| 1668 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1669 | int addr_to_str(const struct sockaddr_storage *addr, char *str, int size) |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1670 | { |
| 1671 | |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1672 | const void *ptr; |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1673 | |
| 1674 | if (size < 5) |
| 1675 | return 0; |
| 1676 | *str = '\0'; |
| 1677 | |
| 1678 | switch (addr->ss_family) { |
| 1679 | case AF_INET: |
| 1680 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1681 | break; |
| 1682 | case AF_INET6: |
| 1683 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1684 | break; |
| 1685 | case AF_UNIX: |
| 1686 | memcpy(str, "unix", 5); |
| 1687 | return addr->ss_family; |
| 1688 | default: |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | if (inet_ntop(addr->ss_family, ptr, str, size)) |
| 1693 | return addr->ss_family; |
| 1694 | |
| 1695 | /* failed */ |
| 1696 | return -1; |
| 1697 | } |
| 1698 | |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1699 | /* Tries to convert a sockaddr_storage port to text form. Upon success, the |
| 1700 | * address family is returned so that it's easy for the caller to adapt to the |
| 1701 | * output format. Zero is returned if the address family is not supported. -1 |
| 1702 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1703 | * supported. |
| 1704 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1705 | int port_to_str(const struct sockaddr_storage *addr, char *str, int size) |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1706 | { |
| 1707 | |
| 1708 | uint16_t port; |
| 1709 | |
| 1710 | |
Willy Tarreau | d7dad1b | 2017-01-06 16:46:22 +0100 | [diff] [blame] | 1711 | if (size < 6) |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1712 | return 0; |
| 1713 | *str = '\0'; |
| 1714 | |
| 1715 | switch (addr->ss_family) { |
| 1716 | case AF_INET: |
| 1717 | port = ((struct sockaddr_in *)addr)->sin_port; |
| 1718 | break; |
| 1719 | case AF_INET6: |
| 1720 | port = ((struct sockaddr_in6 *)addr)->sin6_port; |
| 1721 | break; |
| 1722 | case AF_UNIX: |
| 1723 | memcpy(str, "unix", 5); |
| 1724 | return addr->ss_family; |
| 1725 | default: |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | snprintf(str, size, "%u", ntohs(port)); |
| 1730 | return addr->ss_family; |
| 1731 | } |
| 1732 | |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1733 | /* check if the given address is local to the system or not. It will return |
| 1734 | * -1 when it's not possible to know, 0 when the address is not local, 1 when |
| 1735 | * it is. We don't want to iterate over all interfaces for this (and it is not |
| 1736 | * portable). So instead we try to bind in UDP to this address on a free non |
| 1737 | * privileged port and to connect to the same address, port 0 (connect doesn't |
| 1738 | * care). If it succeeds, we own the address. Note that non-inet addresses are |
| 1739 | * considered local since they're most likely AF_UNIX. |
| 1740 | */ |
| 1741 | int addr_is_local(const struct netns_entry *ns, |
| 1742 | const struct sockaddr_storage *orig) |
| 1743 | { |
| 1744 | struct sockaddr_storage addr; |
| 1745 | int result; |
| 1746 | int fd; |
| 1747 | |
| 1748 | if (!is_inet_addr(orig)) |
| 1749 | return 1; |
| 1750 | |
| 1751 | memcpy(&addr, orig, sizeof(addr)); |
| 1752 | set_host_port(&addr, 0); |
| 1753 | |
| 1754 | fd = my_socketat(ns, addr.ss_family, SOCK_DGRAM, IPPROTO_UDP); |
| 1755 | if (fd < 0) |
| 1756 | return -1; |
| 1757 | |
| 1758 | result = -1; |
| 1759 | if (bind(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == 0) { |
| 1760 | if (connect(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == -1) |
| 1761 | result = 0; // fail, non-local address |
| 1762 | else |
| 1763 | result = 1; // success, local address |
| 1764 | } |
| 1765 | else { |
| 1766 | if (errno == EADDRNOTAVAIL) |
| 1767 | result = 0; // definitely not local :-) |
| 1768 | } |
| 1769 | close(fd); |
| 1770 | |
| 1771 | return result; |
| 1772 | } |
| 1773 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1774 | /* will try to encode the string <string> replacing all characters tagged in |
| 1775 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1776 | * prefixed by <escape>, and will store the result between <start> (included) |
| 1777 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 1778 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1779 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1780 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1781 | * cannot even be stored so we return <start> without writing the 0. |
| 1782 | * The input string must also be zero-terminated. |
| 1783 | */ |
| 1784 | const char hextab[16] = "0123456789ABCDEF"; |
| 1785 | char *encode_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1786 | const char escape, const long *map, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1787 | const char *string) |
| 1788 | { |
| 1789 | if (start < stop) { |
| 1790 | stop--; /* reserve one byte for the final '\0' */ |
| 1791 | while (start < stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1792 | if (!ha_bit_test((unsigned char)(*string), map)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1793 | *start++ = *string; |
| 1794 | else { |
| 1795 | if (start + 3 >= stop) |
| 1796 | break; |
| 1797 | *start++ = escape; |
| 1798 | *start++ = hextab[(*string >> 4) & 15]; |
| 1799 | *start++ = hextab[*string & 15]; |
| 1800 | } |
| 1801 | string++; |
| 1802 | } |
| 1803 | *start = '\0'; |
| 1804 | } |
| 1805 | return start; |
| 1806 | } |
| 1807 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1808 | /* |
| 1809 | * Same behavior as encode_string() above, except that it encodes chunk |
| 1810 | * <chunk> instead of a string. |
| 1811 | */ |
| 1812 | char *encode_chunk(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1813 | const char escape, const long *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1814 | const struct buffer *chunk) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1815 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1816 | char *str = chunk->area; |
| 1817 | char *end = chunk->area + chunk->data; |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1818 | |
| 1819 | if (start < stop) { |
| 1820 | stop--; /* reserve one byte for the final '\0' */ |
| 1821 | while (start < stop && str < end) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1822 | if (!ha_bit_test((unsigned char)(*str), map)) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1823 | *start++ = *str; |
| 1824 | else { |
| 1825 | if (start + 3 >= stop) |
| 1826 | break; |
| 1827 | *start++ = escape; |
| 1828 | *start++ = hextab[(*str >> 4) & 15]; |
| 1829 | *start++ = hextab[*str & 15]; |
| 1830 | } |
| 1831 | str++; |
| 1832 | } |
| 1833 | *start = '\0'; |
| 1834 | } |
| 1835 | return start; |
| 1836 | } |
| 1837 | |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1838 | /* |
| 1839 | * Tries to prefix characters tagged in the <map> with the <escape> |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1840 | * character. The input <string> must be zero-terminated. The result will |
| 1841 | * be stored between <start> (included) and <stop> (excluded). This |
| 1842 | * function will always try to terminate the resulting string with a '\0' |
| 1843 | * before <stop>, and will return its position if the conversion |
| 1844 | * completes. |
| 1845 | */ |
| 1846 | char *escape_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1847 | const char escape, const long *map, |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1848 | const char *string) |
| 1849 | { |
| 1850 | if (start < stop) { |
| 1851 | stop--; /* reserve one byte for the final '\0' */ |
| 1852 | while (start < stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1853 | if (!ha_bit_test((unsigned char)(*string), map)) |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1854 | *start++ = *string; |
| 1855 | else { |
| 1856 | if (start + 2 >= stop) |
| 1857 | break; |
| 1858 | *start++ = escape; |
| 1859 | *start++ = *string; |
| 1860 | } |
| 1861 | string++; |
| 1862 | } |
| 1863 | *start = '\0'; |
| 1864 | } |
| 1865 | return start; |
| 1866 | } |
| 1867 | |
| 1868 | /* |
| 1869 | * Tries to prefix characters tagged in the <map> with the <escape> |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1870 | * character. <chunk> contains the input to be escaped. The result will be |
| 1871 | * stored between <start> (included) and <stop> (excluded). The function |
| 1872 | * will always try to terminate the resulting string with a '\0' before |
| 1873 | * <stop>, and will return its position if the conversion completes. |
| 1874 | */ |
| 1875 | char *escape_chunk(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1876 | const char escape, const long *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1877 | const struct buffer *chunk) |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1878 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1879 | char *str = chunk->area; |
| 1880 | char *end = chunk->area + chunk->data; |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1881 | |
| 1882 | if (start < stop) { |
| 1883 | stop--; /* reserve one byte for the final '\0' */ |
| 1884 | while (start < stop && str < end) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1885 | if (!ha_bit_test((unsigned char)(*str), map)) |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1886 | *start++ = *str; |
| 1887 | else { |
| 1888 | if (start + 2 >= stop) |
| 1889 | break; |
| 1890 | *start++ = escape; |
| 1891 | *start++ = *str; |
| 1892 | } |
| 1893 | str++; |
| 1894 | } |
| 1895 | *start = '\0'; |
| 1896 | } |
| 1897 | return start; |
| 1898 | } |
| 1899 | |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1900 | /* Check a string for using it in a CSV output format. If the string contains |
| 1901 | * one of the following four char <">, <,>, CR or LF, the string is |
| 1902 | * encapsulated between <"> and the <"> are escaped by a <""> sequence. |
| 1903 | * <str> is the input string to be escaped. The function assumes that |
| 1904 | * the input string is null-terminated. |
| 1905 | * |
| 1906 | * If <quote> is 0, the result is returned escaped but without double quote. |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1907 | * It is useful if the escaped string is used between double quotes in the |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1908 | * format. |
| 1909 | * |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1910 | * printf("..., \"%s\", ...\r\n", csv_enc(str, 0, &trash)); |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1911 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 1912 | * If <quote> is 1, the converter puts the quotes only if any reserved character |
| 1913 | * is present. If <quote> is 2, the converter always puts the quotes. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1914 | * |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1915 | * <output> is a struct buffer used for storing the output string. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1916 | * |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1917 | * The function returns the converted string on its output. If an error |
| 1918 | * occurs, the function returns an empty string. This type of output is useful |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1919 | * for using the function directly as printf() argument. |
| 1920 | * |
| 1921 | * If the output buffer is too short to contain the input string, the result |
| 1922 | * is truncated. |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1923 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 1924 | * This function appends the encoding to the existing output chunk, and it |
| 1925 | * guarantees that it starts immediately at the first available character of |
| 1926 | * the chunk. Please use csv_enc() instead if you want to replace the output |
| 1927 | * chunk. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1928 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1929 | const char *csv_enc_append(const char *str, int quote, struct buffer *output) |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1930 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1931 | char *end = output->area + output->size; |
| 1932 | char *out = output->area + output->data; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1933 | char *ptr = out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1934 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 1935 | if (quote == 1) { |
| 1936 | /* automatic quoting: first verify if we'll have to quote the string */ |
| 1937 | if (!strpbrk(str, "\n\r,\"")) |
| 1938 | quote = 0; |
| 1939 | } |
| 1940 | |
| 1941 | if (quote) |
| 1942 | *ptr++ = '"'; |
| 1943 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1944 | while (*str && ptr < end - 2) { /* -2 for reserving space for <"> and \0. */ |
| 1945 | *ptr = *str; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1946 | if (*str == '"') { |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1947 | ptr++; |
| 1948 | if (ptr >= end - 2) { |
| 1949 | ptr--; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1950 | break; |
| 1951 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1952 | *ptr = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1953 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1954 | ptr++; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1955 | str++; |
| 1956 | } |
| 1957 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 1958 | if (quote) |
| 1959 | *ptr++ = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1960 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1961 | *ptr = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1962 | output->data = ptr - output->area; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 1963 | return out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 1964 | } |
| 1965 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1966 | /* Decode an URL-encoded string in-place. The resulting string might |
| 1967 | * be shorter. If some forbidden characters are found, the conversion is |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1968 | * aborted, the string is truncated before the issue and a negative value is |
| 1969 | * returned, otherwise the operation returns the length of the decoded string. |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1970 | * If the 'in_form' argument is non-nul the string is assumed to be part of |
| 1971 | * an "application/x-www-form-urlencoded" encoded string, and the '+' will be |
| 1972 | * turned to a space. If it's zero, this will only be done after a question |
| 1973 | * mark ('?'). |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1974 | */ |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1975 | int url_decode(char *string, int in_form) |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1976 | { |
| 1977 | char *in, *out; |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1978 | int ret = -1; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1979 | |
| 1980 | in = string; |
| 1981 | out = string; |
| 1982 | while (*in) { |
| 1983 | switch (*in) { |
| 1984 | case '+' : |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1985 | *out++ = in_form ? ' ' : *in; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1986 | break; |
| 1987 | case '%' : |
| 1988 | if (!ishex(in[1]) || !ishex(in[2])) |
| 1989 | goto end; |
| 1990 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 1991 | in += 2; |
| 1992 | break; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1993 | case '?': |
| 1994 | in_form = 1; |
| 1995 | /* fall through */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1996 | default: |
| 1997 | *out++ = *in; |
| 1998 | break; |
| 1999 | } |
| 2000 | in++; |
| 2001 | } |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2002 | ret = out - string; /* success */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2003 | end: |
| 2004 | *out = 0; |
| 2005 | return ret; |
| 2006 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2007 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2008 | unsigned int str2ui(const char *s) |
| 2009 | { |
| 2010 | return __str2ui(s); |
| 2011 | } |
| 2012 | |
| 2013 | unsigned int str2uic(const char *s) |
| 2014 | { |
| 2015 | return __str2uic(s); |
| 2016 | } |
| 2017 | |
| 2018 | unsigned int strl2ui(const char *s, int len) |
| 2019 | { |
| 2020 | return __strl2ui(s, len); |
| 2021 | } |
| 2022 | |
| 2023 | unsigned int strl2uic(const char *s, int len) |
| 2024 | { |
| 2025 | return __strl2uic(s, len); |
| 2026 | } |
| 2027 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 2028 | unsigned int read_uint(const char **s, const char *end) |
| 2029 | { |
| 2030 | return __read_uint(s, end); |
| 2031 | } |
| 2032 | |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 2033 | /* This function reads an unsigned integer from the string pointed to by <s> and |
| 2034 | * returns it. The <s> pointer is adjusted to point to the first unread char. The |
| 2035 | * function automatically stops at <end>. If the number overflows, the 2^64-1 |
| 2036 | * value is returned. |
| 2037 | */ |
| 2038 | unsigned long long int read_uint64(const char **s, const char *end) |
| 2039 | { |
| 2040 | const char *ptr = *s; |
| 2041 | unsigned long long int i = 0, tmp; |
| 2042 | unsigned int j; |
| 2043 | |
| 2044 | while (ptr < end) { |
| 2045 | |
| 2046 | /* read next char */ |
| 2047 | j = *ptr - '0'; |
| 2048 | if (j > 9) |
| 2049 | goto read_uint64_end; |
| 2050 | |
| 2051 | /* add char to the number and check overflow. */ |
| 2052 | tmp = i * 10; |
| 2053 | if (tmp / 10 != i) { |
| 2054 | i = ULLONG_MAX; |
| 2055 | goto read_uint64_eat; |
| 2056 | } |
| 2057 | if (ULLONG_MAX - tmp < j) { |
| 2058 | i = ULLONG_MAX; |
| 2059 | goto read_uint64_eat; |
| 2060 | } |
| 2061 | i = tmp + j; |
| 2062 | ptr++; |
| 2063 | } |
| 2064 | read_uint64_eat: |
| 2065 | /* eat each numeric char */ |
| 2066 | while (ptr < end) { |
| 2067 | if ((unsigned int)(*ptr - '0') > 9) |
| 2068 | break; |
| 2069 | ptr++; |
| 2070 | } |
| 2071 | read_uint64_end: |
| 2072 | *s = ptr; |
| 2073 | return i; |
| 2074 | } |
| 2075 | |
| 2076 | /* This function reads an integer from the string pointed to by <s> and returns |
| 2077 | * it. The <s> pointer is adjusted to point to the first unread char. The function |
| 2078 | * automatically stops at <end>. Il the number is bigger than 2^63-2, the 2^63-1 |
| 2079 | * value is returned. If the number is lowest than -2^63-1, the -2^63 value is |
| 2080 | * returned. |
| 2081 | */ |
| 2082 | long long int read_int64(const char **s, const char *end) |
| 2083 | { |
| 2084 | unsigned long long int i = 0; |
| 2085 | int neg = 0; |
| 2086 | |
| 2087 | /* Look for minus char. */ |
| 2088 | if (**s == '-') { |
| 2089 | neg = 1; |
| 2090 | (*s)++; |
| 2091 | } |
| 2092 | else if (**s == '+') |
| 2093 | (*s)++; |
| 2094 | |
| 2095 | /* convert as positive number. */ |
| 2096 | i = read_uint64(s, end); |
| 2097 | |
| 2098 | if (neg) { |
| 2099 | if (i > 0x8000000000000000ULL) |
| 2100 | return LLONG_MIN; |
| 2101 | return -i; |
| 2102 | } |
| 2103 | if (i > 0x7fffffffffffffffULL) |
| 2104 | return LLONG_MAX; |
| 2105 | return i; |
| 2106 | } |
| 2107 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2108 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 2109 | * It returns the value of the number composed of all valid digits read, |
| 2110 | * and can process negative numbers too. |
| 2111 | */ |
| 2112 | int strl2ic(const char *s, int len) |
| 2113 | { |
| 2114 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2115 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2116 | |
| 2117 | if (len > 0) { |
| 2118 | if (*s != '-') { |
| 2119 | /* positive number */ |
| 2120 | while (len-- > 0) { |
| 2121 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2122 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2123 | if (j > 9) |
| 2124 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2125 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2126 | } |
| 2127 | } else { |
| 2128 | /* negative number */ |
| 2129 | s++; |
| 2130 | while (--len > 0) { |
| 2131 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2132 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2133 | if (j > 9) |
| 2134 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2135 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | } |
| 2139 | return i; |
| 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2144 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2145 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2146 | * applications designed for hostile environments. It returns zero when the |
| 2147 | * number has successfully been converted, non-zero otherwise. When an error |
| 2148 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 2149 | * faster than strtol(). |
| 2150 | */ |
| 2151 | int strl2irc(const char *s, int len, int *ret) |
| 2152 | { |
| 2153 | int i = 0; |
| 2154 | int j; |
| 2155 | |
| 2156 | if (!len) |
| 2157 | return 1; |
| 2158 | |
| 2159 | if (*s != '-') { |
| 2160 | /* positive number */ |
| 2161 | while (len-- > 0) { |
| 2162 | j = (*s++) - '0'; |
| 2163 | if (j > 9) return 1; /* invalid char */ |
| 2164 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 2165 | i = i * 10; |
| 2166 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2167 | i = i + j; |
| 2168 | } |
| 2169 | } else { |
| 2170 | /* negative number */ |
| 2171 | s++; |
| 2172 | while (--len > 0) { |
| 2173 | j = (*s++) - '0'; |
| 2174 | if (j > 9) return 1; /* invalid char */ |
| 2175 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 2176 | i = i * 10; |
| 2177 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2178 | i = i - j; |
| 2179 | } |
| 2180 | } |
| 2181 | *ret = i; |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | |
| 2186 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2187 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2188 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2189 | * applications designed for hostile environments. It returns zero when the |
| 2190 | * number has successfully been converted, non-zero otherwise. When an error |
| 2191 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
William Dauchy | 060ffc8 | 2021-02-06 20:47:51 +0100 | [diff] [blame] | 2192 | * than strl2irc(). |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2193 | */ |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2194 | |
| 2195 | int strl2llrc(const char *s, int len, long long *ret) |
| 2196 | { |
| 2197 | long long i = 0; |
| 2198 | int j; |
| 2199 | |
| 2200 | if (!len) |
| 2201 | return 1; |
| 2202 | |
| 2203 | if (*s != '-') { |
| 2204 | /* positive number */ |
| 2205 | while (len-- > 0) { |
| 2206 | j = (*s++) - '0'; |
| 2207 | if (j > 9) return 1; /* invalid char */ |
| 2208 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 2209 | i = i * 10LL; |
| 2210 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2211 | i = i + j; |
| 2212 | } |
| 2213 | } else { |
| 2214 | /* negative number */ |
| 2215 | s++; |
| 2216 | while (--len > 0) { |
| 2217 | j = (*s++) - '0'; |
| 2218 | if (j > 9) return 1; /* invalid char */ |
| 2219 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 2220 | i = i * 10LL; |
| 2221 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2222 | i = i - j; |
| 2223 | } |
| 2224 | } |
| 2225 | *ret = i; |
| 2226 | return 0; |
| 2227 | } |
| 2228 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 2229 | /* This function is used with pat_parse_dotted_ver(). It converts a string |
| 2230 | * composed by two number separated by a dot. Each part must contain in 16 bits |
| 2231 | * because internally they will be represented as a 32-bit quantity stored in |
| 2232 | * a 64-bit integer. It returns zero when the number has successfully been |
| 2233 | * converted, non-zero otherwise. When an error is returned, the <ret> value |
| 2234 | * is left untouched. |
| 2235 | * |
| 2236 | * "1.3" -> 0x0000000000010003 |
| 2237 | * "65535.65535" -> 0x00000000ffffffff |
| 2238 | */ |
| 2239 | int strl2llrc_dotted(const char *text, int len, long long *ret) |
| 2240 | { |
| 2241 | const char *end = &text[len]; |
| 2242 | const char *p; |
| 2243 | long long major, minor; |
| 2244 | |
| 2245 | /* Look for dot. */ |
| 2246 | for (p = text; p < end; p++) |
| 2247 | if (*p == '.') |
| 2248 | break; |
| 2249 | |
| 2250 | /* Convert major. */ |
| 2251 | if (strl2llrc(text, p - text, &major) != 0) |
| 2252 | return 1; |
| 2253 | |
| 2254 | /* Check major. */ |
| 2255 | if (major >= 65536) |
| 2256 | return 1; |
| 2257 | |
| 2258 | /* Convert minor. */ |
| 2259 | minor = 0; |
| 2260 | if (p < end) |
| 2261 | if (strl2llrc(p + 1, end - (p + 1), &minor) != 0) |
| 2262 | return 1; |
| 2263 | |
| 2264 | /* Check minor. */ |
| 2265 | if (minor >= 65536) |
| 2266 | return 1; |
| 2267 | |
| 2268 | /* Compose value. */ |
| 2269 | *ret = (major << 16) | (minor & 0xffff); |
| 2270 | return 0; |
| 2271 | } |
| 2272 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2273 | /* This function parses a time value optionally followed by a unit suffix among |
| 2274 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 2275 | * expected by the caller. The computation does its best to avoid overflows. |
| 2276 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 2277 | * by the function. In case of error, a pointer to the error is returned and |
| 2278 | * <ret> is left untouched. Values are automatically rounded up when needed. |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2279 | * Values resulting in values larger than or equal to 2^31 after conversion are |
| 2280 | * reported as an overflow as value PARSE_TIME_OVER. Non-null values resulting |
| 2281 | * in an underflow are reported as an underflow as value PARSE_TIME_UNDER. |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2282 | */ |
| 2283 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 2284 | { |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2285 | unsigned long long imult, idiv; |
| 2286 | unsigned long long omult, odiv; |
| 2287 | unsigned long long value, result; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2288 | const char *str = text; |
| 2289 | |
| 2290 | if (!isdigit((unsigned char)*text)) |
| 2291 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2292 | |
| 2293 | omult = odiv = 1; |
| 2294 | |
| 2295 | switch (unit_flags & TIME_UNIT_MASK) { |
| 2296 | case TIME_UNIT_US: omult = 1000000; break; |
| 2297 | case TIME_UNIT_MS: omult = 1000; break; |
| 2298 | case TIME_UNIT_S: break; |
| 2299 | case TIME_UNIT_MIN: odiv = 60; break; |
| 2300 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 2301 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 2302 | default: break; |
| 2303 | } |
| 2304 | |
| 2305 | value = 0; |
| 2306 | |
| 2307 | while (1) { |
| 2308 | unsigned int j; |
| 2309 | |
| 2310 | j = *text - '0'; |
| 2311 | if (j > 9) |
| 2312 | break; |
| 2313 | text++; |
| 2314 | value *= 10; |
| 2315 | value += j; |
| 2316 | } |
| 2317 | |
| 2318 | imult = idiv = 1; |
| 2319 | switch (*text) { |
| 2320 | case '\0': /* no unit = default unit */ |
| 2321 | imult = omult = idiv = odiv = 1; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2322 | goto end; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2323 | case 's': /* second = unscaled unit */ |
| 2324 | break; |
| 2325 | case 'u': /* microsecond : "us" */ |
| 2326 | if (text[1] == 's') { |
| 2327 | idiv = 1000000; |
| 2328 | text++; |
| 2329 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2330 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2331 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 2332 | if (text[1] == 's') { |
| 2333 | idiv = 1000; |
| 2334 | text++; |
| 2335 | } else |
| 2336 | imult = 60; |
| 2337 | break; |
| 2338 | case 'h': /* hour : "h" */ |
| 2339 | imult = 3600; |
| 2340 | break; |
| 2341 | case 'd': /* day : "d" */ |
| 2342 | imult = 86400; |
| 2343 | break; |
| 2344 | default: |
| 2345 | return text; |
| 2346 | break; |
| 2347 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2348 | if (*(++text) != '\0') { |
| 2349 | ha_warning("unexpected character '%c' after the timer value '%s', only " |
| 2350 | "(us=microseconds,ms=milliseconds,s=seconds,m=minutes,h=hours,d=days) are supported." |
| 2351 | " This will be reported as an error in next versions.\n", *text, str); |
| 2352 | } |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2353 | |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2354 | end: |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2355 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 2356 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 2357 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 2358 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 2359 | |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2360 | result = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 2361 | if (result >= 0x80000000) |
| 2362 | return PARSE_TIME_OVER; |
| 2363 | if (!result && value) |
| 2364 | return PARSE_TIME_UNDER; |
| 2365 | *ret = result; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2366 | return NULL; |
| 2367 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2368 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2369 | /* this function converts the string starting at <text> to an unsigned int |
| 2370 | * stored in <ret>. If an error is detected, the pointer to the unexpected |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 2371 | * character is returned. If the conversion is successful, NULL is returned. |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2372 | */ |
| 2373 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 2374 | unsigned value = 0; |
| 2375 | |
Christopher Faulet | 82635a0 | 2020-12-11 09:30:45 +0100 | [diff] [blame] | 2376 | if (!isdigit((unsigned char)*text)) |
| 2377 | return text; |
| 2378 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2379 | while (1) { |
| 2380 | unsigned int j; |
| 2381 | |
| 2382 | j = *text - '0'; |
| 2383 | if (j > 9) |
| 2384 | break; |
| 2385 | if (value > ~0U / 10) |
| 2386 | return text; |
| 2387 | value *= 10; |
| 2388 | if (value > (value + j)) |
| 2389 | return text; |
| 2390 | value += j; |
| 2391 | text++; |
| 2392 | } |
| 2393 | |
| 2394 | switch (*text) { |
| 2395 | case '\0': |
| 2396 | break; |
| 2397 | case 'K': |
| 2398 | case 'k': |
| 2399 | if (value > ~0U >> 10) |
| 2400 | return text; |
| 2401 | value = value << 10; |
| 2402 | break; |
| 2403 | case 'M': |
| 2404 | case 'm': |
| 2405 | if (value > ~0U >> 20) |
| 2406 | return text; |
| 2407 | value = value << 20; |
| 2408 | break; |
| 2409 | case 'G': |
| 2410 | case 'g': |
| 2411 | if (value > ~0U >> 30) |
| 2412 | return text; |
| 2413 | value = value << 30; |
| 2414 | break; |
| 2415 | default: |
| 2416 | return text; |
| 2417 | } |
| 2418 | |
Godbach | 58048a2 | 2015-01-28 17:36:16 +0800 | [diff] [blame] | 2419 | if (*text != '\0' && *++text != '\0') |
| 2420 | return text; |
| 2421 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2422 | *ret = value; |
| 2423 | return NULL; |
| 2424 | } |
| 2425 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2426 | /* |
| 2427 | * Parse binary string written in hexadecimal (source) and store the decoded |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2428 | * result into binstr and set binstrlen to the length of binstr. Memory for |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2429 | * binstr is allocated by the function. In case of error, returns 0 with an |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2430 | * error message in err. In success case, it returns the consumed length. |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2431 | */ |
| 2432 | int parse_binary(const char *source, char **binstr, int *binstrlen, char **err) |
| 2433 | { |
| 2434 | int len; |
| 2435 | const char *p = source; |
| 2436 | int i,j; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2437 | int alloc; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2438 | |
| 2439 | len = strlen(source); |
| 2440 | if (len % 2) { |
| 2441 | memprintf(err, "an even number of hex digit is expected"); |
| 2442 | return 0; |
| 2443 | } |
| 2444 | |
| 2445 | len = len >> 1; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2446 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2447 | if (!*binstr) { |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 2448 | *binstr = calloc(len, sizeof(**binstr)); |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2449 | if (!*binstr) { |
| 2450 | memprintf(err, "out of memory while loading string pattern"); |
| 2451 | return 0; |
| 2452 | } |
| 2453 | alloc = 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2454 | } |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2455 | else { |
| 2456 | if (*binstrlen < len) { |
Joseph Herlant | 76dbe78 | 2018-11-15 12:01:22 -0800 | [diff] [blame] | 2457 | memprintf(err, "no space available in the buffer. expect %d, provides %d", |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2458 | len, *binstrlen); |
| 2459 | return 0; |
| 2460 | } |
| 2461 | alloc = 0; |
| 2462 | } |
| 2463 | *binstrlen = len; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2464 | |
| 2465 | i = j = 0; |
| 2466 | while (j < len) { |
| 2467 | if (!ishex(p[i++])) |
| 2468 | goto bad_input; |
| 2469 | if (!ishex(p[i++])) |
| 2470 | goto bad_input; |
| 2471 | (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]); |
| 2472 | } |
Thierry FOURNIER | ee330af | 2014-01-21 11:36:14 +0100 | [diff] [blame] | 2473 | return len << 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2474 | |
| 2475 | bad_input: |
| 2476 | memprintf(err, "an hex digit is expected (found '%c')", p[i-1]); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2477 | if (alloc) |
| 2478 | ha_free(binstr); |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2479 | return 0; |
| 2480 | } |
| 2481 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2482 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 2483 | char *my_strndup(const char *src, int n) |
| 2484 | { |
| 2485 | int len = 0; |
| 2486 | char *ret; |
| 2487 | |
| 2488 | while (len < n && src[len]) |
| 2489 | len++; |
| 2490 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2491 | ret = malloc(len + 1); |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2492 | if (!ret) |
| 2493 | return ret; |
| 2494 | memcpy(ret, src, len); |
| 2495 | ret[len] = '\0'; |
| 2496 | return ret; |
| 2497 | } |
| 2498 | |
Baptiste Assmann | bb77c8e | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2499 | /* |
| 2500 | * search needle in haystack |
| 2501 | * returns the pointer if found, returns NULL otherwise |
| 2502 | */ |
| 2503 | const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) |
| 2504 | { |
| 2505 | const void *c = NULL; |
| 2506 | unsigned char f; |
| 2507 | |
| 2508 | if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) |
| 2509 | return NULL; |
| 2510 | |
| 2511 | f = *(char *)needle; |
| 2512 | c = haystack; |
| 2513 | while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { |
| 2514 | if ((haystacklen - (c - haystack)) < needlelen) |
| 2515 | return NULL; |
| 2516 | |
| 2517 | if (memcmp(c, needle, needlelen) == 0) |
| 2518 | return c; |
| 2519 | ++c; |
| 2520 | } |
| 2521 | return NULL; |
| 2522 | } |
| 2523 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2524 | /* get length of the initial segment consisting entirely of bytes in <accept> */ |
Christopher Faulet | 5eb96cb | 2020-04-15 10:23:01 +0200 | [diff] [blame] | 2525 | size_t my_memspn(const void *str, size_t len, const void *accept, size_t acceptlen) |
| 2526 | { |
| 2527 | size_t ret = 0; |
| 2528 | |
| 2529 | while (ret < len && memchr(accept, *((int *)str), acceptlen)) { |
| 2530 | str++; |
| 2531 | ret++; |
| 2532 | } |
| 2533 | return ret; |
| 2534 | } |
| 2535 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2536 | /* get length of the initial segment consisting entirely of bytes not in <rejcet> */ |
Christopher Faulet | 5eb96cb | 2020-04-15 10:23:01 +0200 | [diff] [blame] | 2537 | size_t my_memcspn(const void *str, size_t len, const void *reject, size_t rejectlen) |
| 2538 | { |
| 2539 | size_t ret = 0; |
| 2540 | |
| 2541 | while (ret < len) { |
| 2542 | if(memchr(reject, *((int *)str), rejectlen)) |
| 2543 | return ret; |
| 2544 | str++; |
| 2545 | ret++; |
| 2546 | } |
| 2547 | return ret; |
| 2548 | } |
| 2549 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2550 | /* This function returns the first unused key greater than or equal to <key> in |
| 2551 | * ID tree <root>. Zero is returned if no place is found. |
| 2552 | */ |
| 2553 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 2554 | { |
| 2555 | struct eb32_node *used; |
| 2556 | |
| 2557 | do { |
| 2558 | used = eb32_lookup_ge(root, key); |
| 2559 | if (!used || used->key > key) |
| 2560 | return key; /* key is available */ |
| 2561 | key++; |
| 2562 | } while (key); |
| 2563 | return key; |
| 2564 | } |
| 2565 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2566 | /* dump the full tree to <file> in DOT format for debugging purposes. Will |
| 2567 | * optionally highlight node <subj> if found, depending on operation <op> : |
| 2568 | * 0 : nothing |
| 2569 | * >0 : insertion, node/leaf are surrounded in red |
| 2570 | * <0 : removal, node/leaf are dashed with no background |
| 2571 | * Will optionally add "desc" as a label on the graph if set and non-null. |
| 2572 | */ |
| 2573 | void eb32sc_to_file(FILE *file, struct eb_root *root, const struct eb32sc_node *subj, int op, const char *desc) |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2574 | { |
| 2575 | struct eb32sc_node *node; |
| 2576 | unsigned long scope = -1; |
| 2577 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2578 | fprintf(file, "digraph ebtree {\n"); |
| 2579 | |
| 2580 | if (desc && *desc) { |
| 2581 | fprintf(file, |
| 2582 | " fontname=\"fixed\";\n" |
| 2583 | " fontsize=8;\n" |
| 2584 | " label=\"%s\";\n", desc); |
| 2585 | } |
| 2586 | |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2587 | fprintf(file, |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2588 | " node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n" |
| 2589 | " edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n" |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2590 | " \"%lx_n\" [label=\"root\\n%lx\"]\n", (long)eb_root_to_node(root), (long)root |
| 2591 | ); |
| 2592 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2593 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2594 | (long)eb_root_to_node(root), |
| 2595 | (long)eb_root_to_node(eb_clrtag(root->b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2596 | eb_gettag(root->b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2597 | |
| 2598 | node = eb32sc_first(root, scope); |
| 2599 | while (node) { |
| 2600 | if (node->node.node_p) { |
| 2601 | /* node part is used */ |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2602 | fprintf(file, " \"%lx_n\" [label=\"%lx\\nkey=%u\\nscope=%lx\\nbit=%d\" fillcolor=\"lightskyblue1\" %s];\n", |
| 2603 | (long)node, (long)node, node->key, node->node_s, node->node.bit, |
| 2604 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2605 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2606 | fprintf(file, " \"%lx_n\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2607 | (long)node, |
| 2608 | (long)eb_root_to_node(eb_clrtag(node->node.node_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2609 | eb_gettag(node->node.node_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2610 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2611 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2612 | (long)node, |
| 2613 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2614 | eb_gettag(node->node.branches.b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2615 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2616 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"R\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2617 | (long)node, |
| 2618 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[1])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2619 | eb_gettag(node->node.branches.b[1]) == EB_LEAF ? 'l' : 'n'); |
| 2620 | } |
| 2621 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2622 | fprintf(file, " \"%lx_l\" [label=\"%lx\\nkey=%u\\nscope=%lx\\npfx=%u\" fillcolor=\"yellow\" %s];\n", |
| 2623 | (long)node, (long)node, node->key, node->leaf_s, node->node.pfx, |
| 2624 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2625 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2626 | fprintf(file, " \"%lx_l\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2627 | (long)node, |
| 2628 | (long)eb_root_to_node(eb_clrtag(node->node.leaf_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2629 | eb_gettag(node->node.leaf_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2630 | node = eb32sc_next(node, scope); |
| 2631 | } |
| 2632 | fprintf(file, "}\n"); |
| 2633 | } |
| 2634 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 2635 | /* This function compares a sample word possibly followed by blanks to another |
| 2636 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 2637 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 2638 | * values. Note that it validates a word followed only by blanks but does not |
| 2639 | * validate a word followed by blanks then other chars. |
| 2640 | */ |
| 2641 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 2642 | { |
| 2643 | if (slen < wlen) |
| 2644 | return 0; |
| 2645 | |
| 2646 | while (wlen) { |
| 2647 | char c = *sample ^ *word; |
| 2648 | if (c && c != ('A' ^ 'a')) |
| 2649 | return 0; |
| 2650 | sample++; |
| 2651 | word++; |
| 2652 | slen--; |
| 2653 | wlen--; |
| 2654 | } |
| 2655 | |
| 2656 | while (slen) { |
| 2657 | if (*sample != ' ' && *sample != '\t') |
| 2658 | return 0; |
| 2659 | sample++; |
| 2660 | slen--; |
| 2661 | } |
| 2662 | return 1; |
| 2663 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2664 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2665 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 2666 | * is particularly fast because it avoids expensive operations such as |
| 2667 | * multiplies, which are optimized away at the end. It requires a properly |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2668 | * formatted address though (3 points). |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2669 | */ |
| 2670 | unsigned int inetaddr_host(const char *text) |
| 2671 | { |
| 2672 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2673 | register unsigned int dig100, dig10, dig1; |
| 2674 | int s; |
| 2675 | const char *p, *d; |
| 2676 | |
| 2677 | dig1 = dig10 = dig100 = ascii_zero; |
| 2678 | s = 24; |
| 2679 | |
| 2680 | p = text; |
| 2681 | while (1) { |
| 2682 | if (((unsigned)(*p - '0')) <= 9) { |
| 2683 | p++; |
| 2684 | continue; |
| 2685 | } |
| 2686 | |
| 2687 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2688 | if (p == text) |
| 2689 | goto end; |
| 2690 | |
| 2691 | d = p - 1; |
| 2692 | dig1 |= (unsigned int)(*d << s); |
| 2693 | if (d == text) |
| 2694 | goto end; |
| 2695 | |
| 2696 | d--; |
| 2697 | dig10 |= (unsigned int)(*d << s); |
| 2698 | if (d == text) |
| 2699 | goto end; |
| 2700 | |
| 2701 | d--; |
| 2702 | dig100 |= (unsigned int)(*d << s); |
| 2703 | end: |
| 2704 | if (!s || *p != '.') |
| 2705 | break; |
| 2706 | |
| 2707 | s -= 8; |
| 2708 | text = ++p; |
| 2709 | } |
| 2710 | |
| 2711 | dig100 -= ascii_zero; |
| 2712 | dig10 -= ascii_zero; |
| 2713 | dig1 -= ascii_zero; |
| 2714 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2715 | } |
| 2716 | |
| 2717 | /* |
| 2718 | * Idem except the first unparsed character has to be passed in <stop>. |
| 2719 | */ |
| 2720 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 2721 | { |
| 2722 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2723 | register unsigned int dig100, dig10, dig1; |
| 2724 | int s; |
| 2725 | const char *p, *d; |
| 2726 | |
| 2727 | dig1 = dig10 = dig100 = ascii_zero; |
| 2728 | s = 24; |
| 2729 | |
| 2730 | p = text; |
| 2731 | while (1) { |
| 2732 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 2733 | p++; |
| 2734 | continue; |
| 2735 | } |
| 2736 | |
| 2737 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2738 | if (p == text) |
| 2739 | goto end; |
| 2740 | |
| 2741 | d = p - 1; |
| 2742 | dig1 |= (unsigned int)(*d << s); |
| 2743 | if (d == text) |
| 2744 | goto end; |
| 2745 | |
| 2746 | d--; |
| 2747 | dig10 |= (unsigned int)(*d << s); |
| 2748 | if (d == text) |
| 2749 | goto end; |
| 2750 | |
| 2751 | d--; |
| 2752 | dig100 |= (unsigned int)(*d << s); |
| 2753 | end: |
| 2754 | if (!s || p == stop || *p != '.') |
| 2755 | break; |
| 2756 | |
| 2757 | s -= 8; |
| 2758 | text = ++p; |
| 2759 | } |
| 2760 | |
| 2761 | dig100 -= ascii_zero; |
| 2762 | dig10 -= ascii_zero; |
| 2763 | dig1 -= ascii_zero; |
| 2764 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2765 | } |
| 2766 | |
| 2767 | /* |
| 2768 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 2769 | * must not be NULL. |
| 2770 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2771 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2772 | { |
| 2773 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2774 | register unsigned int dig100, dig10, dig1; |
| 2775 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2776 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2777 | |
| 2778 | dig1 = dig10 = dig100 = ascii_zero; |
| 2779 | s = 24; |
| 2780 | |
| 2781 | p = text; |
| 2782 | while (1) { |
| 2783 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 2784 | p++; |
| 2785 | continue; |
| 2786 | } |
| 2787 | |
| 2788 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2789 | if (p == text) |
| 2790 | goto end; |
| 2791 | |
| 2792 | d = p - 1; |
| 2793 | dig1 |= (unsigned int)(*d << s); |
| 2794 | if (d == text) |
| 2795 | goto end; |
| 2796 | |
| 2797 | d--; |
| 2798 | dig10 |= (unsigned int)(*d << s); |
| 2799 | if (d == text) |
| 2800 | goto end; |
| 2801 | |
| 2802 | d--; |
| 2803 | dig100 |= (unsigned int)(*d << s); |
| 2804 | end: |
| 2805 | if (!s || p == stop || *p != '.') |
| 2806 | break; |
| 2807 | |
| 2808 | s -= 8; |
| 2809 | text = ++p; |
| 2810 | } |
| 2811 | |
| 2812 | *ret = p; |
| 2813 | dig100 -= ascii_zero; |
| 2814 | dig10 -= ascii_zero; |
| 2815 | dig1 -= ascii_zero; |
| 2816 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2817 | } |
| 2818 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 2819 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 2820 | * or the number of chars read in case of success. Maybe this could be replaced |
| 2821 | * by one of the functions above. Also, apparently this function does not support |
| 2822 | * hosts above 255 and requires exactly 4 octets. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 2823 | * The destination is only modified on success. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 2824 | */ |
| 2825 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 2826 | { |
| 2827 | const char *addr; |
| 2828 | int saw_digit, octets, ch; |
| 2829 | u_char tmp[4], *tp; |
| 2830 | const char *cp = buf; |
| 2831 | |
| 2832 | saw_digit = 0; |
| 2833 | octets = 0; |
| 2834 | *(tp = tmp) = 0; |
| 2835 | |
| 2836 | for (addr = buf; addr - buf < len; addr++) { |
| 2837 | unsigned char digit = (ch = *addr) - '0'; |
| 2838 | |
| 2839 | if (digit > 9 && ch != '.') |
| 2840 | break; |
| 2841 | |
| 2842 | if (digit <= 9) { |
| 2843 | u_int new = *tp * 10 + digit; |
| 2844 | |
| 2845 | if (new > 255) |
| 2846 | return 0; |
| 2847 | |
| 2848 | *tp = new; |
| 2849 | |
| 2850 | if (!saw_digit) { |
| 2851 | if (++octets > 4) |
| 2852 | return 0; |
| 2853 | saw_digit = 1; |
| 2854 | } |
| 2855 | } else if (ch == '.' && saw_digit) { |
| 2856 | if (octets == 4) |
| 2857 | return 0; |
| 2858 | |
| 2859 | *++tp = 0; |
| 2860 | saw_digit = 0; |
| 2861 | } else |
| 2862 | return 0; |
| 2863 | } |
| 2864 | |
| 2865 | if (octets < 4) |
| 2866 | return 0; |
| 2867 | |
| 2868 | memcpy(&dst->s_addr, tmp, 4); |
| 2869 | return addr - cp; |
| 2870 | } |
| 2871 | |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2872 | /* This function converts the string in <buf> of the len <len> to |
| 2873 | * struct in6_addr <dst> which must be allocated by the caller. |
| 2874 | * This function returns 1 in success case, otherwise zero. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 2875 | * The destination is only modified on success. |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2876 | */ |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2877 | int buf2ip6(const char *buf, size_t len, struct in6_addr *dst) |
| 2878 | { |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 2879 | char null_term_ip6[INET6_ADDRSTRLEN + 1]; |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 2880 | struct in6_addr out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2881 | |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 2882 | if (len > INET6_ADDRSTRLEN) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2883 | return 0; |
| 2884 | |
| 2885 | memcpy(null_term_ip6, buf, len); |
| 2886 | null_term_ip6[len] = '\0'; |
| 2887 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 2888 | if (!inet_pton(AF_INET6, null_term_ip6, &out)) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2889 | return 0; |
| 2890 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 2891 | *dst = out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 2892 | return 1; |
| 2893 | } |
| 2894 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 2895 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 2896 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 2897 | * if ptr is NULL or empty. The string is locally allocated. |
| 2898 | */ |
| 2899 | const char *quote_arg(const char *ptr) |
| 2900 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 2901 | static THREAD_LOCAL char val[32]; |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 2902 | int i; |
| 2903 | |
| 2904 | if (!ptr || !*ptr) |
| 2905 | return "end of line"; |
| 2906 | val[0] = '\''; |
Willy Tarreau | de2dd6b | 2013-01-24 02:14:42 +0100 | [diff] [blame] | 2907 | for (i = 1; i < sizeof(val) - 2 && *ptr; i++) |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 2908 | val[i] = *ptr++; |
| 2909 | val[i++] = '\''; |
| 2910 | val[i] = '\0'; |
| 2911 | return val; |
| 2912 | } |
| 2913 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 2914 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 2915 | int get_std_op(const char *str) |
| 2916 | { |
| 2917 | int ret = -1; |
| 2918 | |
| 2919 | if (*str == 'e' && str[1] == 'q') |
| 2920 | ret = STD_OP_EQ; |
| 2921 | else if (*str == 'n' && str[1] == 'e') |
| 2922 | ret = STD_OP_NE; |
| 2923 | else if (*str == 'l') { |
| 2924 | if (str[1] == 'e') ret = STD_OP_LE; |
| 2925 | else if (str[1] == 't') ret = STD_OP_LT; |
| 2926 | } |
| 2927 | else if (*str == 'g') { |
| 2928 | if (str[1] == 'e') ret = STD_OP_GE; |
| 2929 | else if (str[1] == 't') ret = STD_OP_GT; |
| 2930 | } |
| 2931 | |
| 2932 | if (ret == -1 || str[2] != '\0') |
| 2933 | return -1; |
| 2934 | return ret; |
| 2935 | } |
| 2936 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 2937 | /* hash a 32-bit integer to another 32-bit integer */ |
| 2938 | unsigned int full_hash(unsigned int a) |
| 2939 | { |
| 2940 | return __full_hash(a); |
| 2941 | } |
| 2942 | |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 2943 | /* Return the bit position in mask <m> of the nth bit set of rank <r>, between |
| 2944 | * 0 and LONGBITS-1 included, starting from the left. For example ranks 0,1,2,3 |
| 2945 | * for mask 0x55 will be 6, 4, 2 and 0 respectively. This algorithm is based on |
| 2946 | * a popcount variant and is described here : |
| 2947 | * https://graphics.stanford.edu/~seander/bithacks.html |
| 2948 | */ |
| 2949 | unsigned int mask_find_rank_bit(unsigned int r, unsigned long m) |
| 2950 | { |
| 2951 | unsigned long a, b, c, d; |
| 2952 | unsigned int s; |
| 2953 | unsigned int t; |
| 2954 | |
| 2955 | a = m - ((m >> 1) & ~0UL/3); |
| 2956 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
| 2957 | c = (b + (b >> 4)) & ~0UL/0x11; |
| 2958 | d = (c + (c >> 8)) & ~0UL/0x101; |
| 2959 | |
| 2960 | r++; // make r be 1..64 |
| 2961 | |
| 2962 | t = 0; |
| 2963 | s = LONGBITS; |
| 2964 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 2965 | unsigned long d2 = (d >> 16) >> 16; |
| 2966 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 2967 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 2968 | } |
| 2969 | |
| 2970 | t = (d >> (s - 16)) & 0xff; |
| 2971 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 2972 | t = (c >> (s - 8)) & 0xf; |
| 2973 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 2974 | t = (b >> (s - 4)) & 0x7; |
| 2975 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 2976 | t = (a >> (s - 2)) & 0x3; |
| 2977 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 2978 | t = (m >> (s - 1)) & 0x1; |
| 2979 | s -= ((t - r) & 256) >> 8; |
| 2980 | |
| 2981 | return s - 1; |
| 2982 | } |
| 2983 | |
| 2984 | /* Same as mask_find_rank_bit() above but makes use of pre-computed bitmaps |
| 2985 | * based on <m>, in <a..d>. These ones must be updated whenever <m> changes |
| 2986 | * using mask_prep_rank_map() below. |
| 2987 | */ |
| 2988 | unsigned int mask_find_rank_bit_fast(unsigned int r, unsigned long m, |
| 2989 | unsigned long a, unsigned long b, |
| 2990 | unsigned long c, unsigned long d) |
| 2991 | { |
| 2992 | unsigned int s; |
| 2993 | unsigned int t; |
| 2994 | |
| 2995 | r++; // make r be 1..64 |
| 2996 | |
| 2997 | t = 0; |
| 2998 | s = LONGBITS; |
| 2999 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 3000 | unsigned long d2 = (d >> 16) >> 16; |
| 3001 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3002 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 3003 | } |
| 3004 | |
| 3005 | t = (d >> (s - 16)) & 0xff; |
| 3006 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 3007 | t = (c >> (s - 8)) & 0xf; |
| 3008 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 3009 | t = (b >> (s - 4)) & 0x7; |
| 3010 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 3011 | t = (a >> (s - 2)) & 0x3; |
| 3012 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 3013 | t = (m >> (s - 1)) & 0x1; |
| 3014 | s -= ((t - r) & 256) >> 8; |
| 3015 | |
| 3016 | return s - 1; |
| 3017 | } |
| 3018 | |
| 3019 | /* Prepare the bitmaps used by the fast implementation of the find_rank_bit() |
| 3020 | * above. |
| 3021 | */ |
| 3022 | void mask_prep_rank_map(unsigned long m, |
| 3023 | unsigned long *a, unsigned long *b, |
| 3024 | unsigned long *c, unsigned long *d) |
| 3025 | { |
| 3026 | *a = m - ((m >> 1) & ~0UL/3); |
| 3027 | *b = (*a & ~0UL/5) + ((*a >> 2) & ~0UL/5); |
| 3028 | *c = (*b + (*b >> 4)) & ~0UL/0x11; |
| 3029 | *d = (*c + (*c >> 8)) & ~0UL/0x101; |
| 3030 | } |
| 3031 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3032 | /* Return non-zero if IPv4 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3033 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3034 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3035 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3036 | int in_net_ipv4(const void *addr, const struct in_addr *mask, const struct in_addr *net) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3037 | { |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3038 | struct in_addr addr_copy; |
| 3039 | |
| 3040 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
| 3041 | return((addr_copy.s_addr & mask->s_addr) == (net->s_addr & mask->s_addr)); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | /* Return non-zero if IPv6 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3045 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3046 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3047 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3048 | int in_net_ipv6(const void *addr, const struct in6_addr *mask, const struct in6_addr *net) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3049 | { |
| 3050 | int i; |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3051 | struct in6_addr addr_copy; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3052 | |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3053 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3054 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3055 | if (((((int *)&addr_copy)[i] & ((int *)mask)[i])) != |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3056 | (((int *)net)[i] & ((int *)mask)[i])) |
| 3057 | return 0; |
| 3058 | return 1; |
| 3059 | } |
| 3060 | |
| 3061 | /* RFC 4291 prefix */ |
| 3062 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 3063 | 0x00, 0x00, 0x00, 0x00, |
| 3064 | 0x00, 0x00, 0xFF, 0xFF }; |
| 3065 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3066 | /* Map IPv4 address on IPv6 address, as specified in RFC 3513. |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3067 | * Input and output may overlap. |
| 3068 | */ |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3069 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 3070 | { |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3071 | struct in_addr tmp_addr; |
| 3072 | |
| 3073 | tmp_addr.s_addr = sin_addr->s_addr; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3074 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3075 | memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3078 | /* Map IPv6 address on IPv4 address, as specified in RFC 3513. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3079 | * Return true if conversion is possible and false otherwise. |
| 3080 | */ |
| 3081 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 3082 | { |
| 3083 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 3084 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 3085 | sizeof(struct in_addr)); |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
| 3089 | return 0; |
| 3090 | } |
| 3091 | |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3092 | /* compare two struct sockaddr_storage and return: |
| 3093 | * 0 (true) if the addr is the same in both |
| 3094 | * 1 (false) if the addr is not the same in both |
| 3095 | * -1 (unable) if one of the addr is not AF_INET* |
| 3096 | */ |
| 3097 | int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2) |
| 3098 | { |
| 3099 | if ((ss1->ss_family != AF_INET) && (ss1->ss_family != AF_INET6)) |
| 3100 | return -1; |
| 3101 | |
| 3102 | if ((ss2->ss_family != AF_INET) && (ss2->ss_family != AF_INET6)) |
| 3103 | return -1; |
| 3104 | |
| 3105 | if (ss1->ss_family != ss2->ss_family) |
| 3106 | return 1; |
| 3107 | |
| 3108 | switch (ss1->ss_family) { |
| 3109 | case AF_INET: |
| 3110 | return memcmp(&((struct sockaddr_in *)ss1)->sin_addr, |
| 3111 | &((struct sockaddr_in *)ss2)->sin_addr, |
| 3112 | sizeof(struct in_addr)) != 0; |
| 3113 | case AF_INET6: |
| 3114 | return memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, |
| 3115 | &((struct sockaddr_in6 *)ss2)->sin6_addr, |
| 3116 | sizeof(struct in6_addr)) != 0; |
| 3117 | } |
| 3118 | |
| 3119 | return 1; |
| 3120 | } |
| 3121 | |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 3122 | /* compare a struct sockaddr_storage to a struct net_addr and return : |
| 3123 | * 0 (true) if <addr> is matching <net> |
| 3124 | * 1 (false) if <addr> is not matching <net> |
| 3125 | * -1 (unable) if <addr> or <net> is not AF_INET* |
| 3126 | */ |
| 3127 | int ipcmp2net(const struct sockaddr_storage *addr, const struct net_addr *net) |
| 3128 | { |
| 3129 | if ((addr->ss_family != AF_INET) && (addr->ss_family != AF_INET6)) |
| 3130 | return -1; |
| 3131 | |
| 3132 | if ((net->family != AF_INET) && (net->family != AF_INET6)) |
| 3133 | return -1; |
| 3134 | |
| 3135 | if (addr->ss_family != net->family) |
| 3136 | return 1; |
| 3137 | |
| 3138 | if (addr->ss_family == AF_INET && |
| 3139 | (((struct sockaddr_in *)addr)->sin_addr.s_addr & net->addr.v4.mask.s_addr) == net->addr.v4.ip.s_addr) |
| 3140 | return 0; |
| 3141 | else { |
| 3142 | const struct in6_addr *addr6 = &(((const struct sockaddr_in6*)addr)->sin6_addr); |
| 3143 | const struct in6_addr *nip6 = &net->addr.v6.ip; |
| 3144 | const struct in6_addr *nmask6 = &net->addr.v6.mask; |
| 3145 | |
| 3146 | if ((read_u32(&addr6->s6_addr[0]) & read_u32(&nmask6->s6_addr[0])) == read_u32(&nip6->s6_addr[0]) && |
| 3147 | (read_u32(&addr6->s6_addr[4]) & read_u32(&nmask6->s6_addr[4])) == read_u32(&nip6->s6_addr[4]) && |
| 3148 | (read_u32(&addr6->s6_addr[8]) & read_u32(&nmask6->s6_addr[8])) == read_u32(&nip6->s6_addr[8]) && |
| 3149 | (read_u32(&addr6->s6_addr[12]) & read_u32(&nmask6->s6_addr[12])) == read_u32(&nip6->s6_addr[12])) |
| 3150 | return 0; |
| 3151 | } |
| 3152 | |
| 3153 | return 1; |
| 3154 | } |
| 3155 | |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3156 | /* copy IP address from <source> into <dest> |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3157 | * The caller must allocate and clear <dest> before calling. |
| 3158 | * The source must be in either AF_INET or AF_INET6 family, or the destination |
| 3159 | * address will be undefined. If the destination address used to hold a port, |
| 3160 | * it is preserved, so that this function can be used to switch to another |
| 3161 | * address family with no risk. Returns a pointer to the destination. |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3162 | */ |
| 3163 | struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest) |
| 3164 | { |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3165 | int prev_port; |
| 3166 | |
| 3167 | prev_port = get_net_port(dest); |
| 3168 | memset(dest, 0, sizeof(*dest)); |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3169 | dest->ss_family = source->ss_family; |
| 3170 | |
| 3171 | /* copy new addr and apply it */ |
| 3172 | switch (source->ss_family) { |
| 3173 | case AF_INET: |
| 3174 | ((struct sockaddr_in *)dest)->sin_addr.s_addr = ((struct sockaddr_in *)source)->sin_addr.s_addr; |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3175 | ((struct sockaddr_in *)dest)->sin_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3176 | break; |
| 3177 | case AF_INET6: |
| 3178 | memcpy(((struct sockaddr_in6 *)dest)->sin6_addr.s6_addr, ((struct sockaddr_in6 *)source)->sin6_addr.s6_addr, sizeof(struct in6_addr)); |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3179 | ((struct sockaddr_in6 *)dest)->sin6_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3180 | break; |
| 3181 | } |
| 3182 | |
| 3183 | return dest; |
| 3184 | } |
| 3185 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3186 | char *human_time(int t, short hz_div) { |
| 3187 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 3188 | char *p = rv; |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3189 | char *end = rv + sizeof(rv); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3190 | int cnt=2; // print two numbers |
| 3191 | |
| 3192 | if (unlikely(t < 0 || hz_div <= 0)) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3193 | snprintf(p, end - p, "?"); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3194 | return rv; |
| 3195 | } |
| 3196 | |
| 3197 | if (unlikely(hz_div > 1)) |
| 3198 | t /= hz_div; |
| 3199 | |
| 3200 | if (t >= DAY) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3201 | p += snprintf(p, end - p, "%dd", t / DAY); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3202 | cnt--; |
| 3203 | } |
| 3204 | |
| 3205 | if (cnt && t % DAY / HOUR) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3206 | p += snprintf(p, end - p, "%dh", t % DAY / HOUR); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3207 | cnt--; |
| 3208 | } |
| 3209 | |
| 3210 | if (cnt && t % HOUR / MINUTE) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3211 | p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3212 | cnt--; |
| 3213 | } |
| 3214 | |
| 3215 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3216 | p += snprintf(p, end - p, "%ds", t % MINUTE / SEC); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3217 | |
| 3218 | return rv; |
| 3219 | } |
| 3220 | |
| 3221 | const char *monthname[12] = { |
| 3222 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 3223 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 3224 | }; |
| 3225 | |
| 3226 | /* date2str_log: write a date in the format : |
| 3227 | * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", |
| 3228 | * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 3229 | * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); |
| 3230 | * |
| 3231 | * without using sprintf. return a pointer to the last char written (\0) or |
| 3232 | * NULL if there isn't enough space. |
| 3233 | */ |
Willy Tarreau | f16cb41 | 2018-09-04 19:08:48 +0200 | [diff] [blame] | 3234 | char *date2str_log(char *dst, const struct tm *tm, const struct timeval *date, size_t size) |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3235 | { |
| 3236 | |
| 3237 | if (size < 25) /* the size is fixed: 24 chars + \0 */ |
| 3238 | return NULL; |
| 3239 | |
| 3240 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3241 | if (!dst) |
| 3242 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3243 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3244 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3245 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3246 | dst += 3; |
| 3247 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3248 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3249 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3250 | if (!dst) |
| 3251 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3252 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3253 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3254 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3255 | if (!dst) |
| 3256 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3257 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3258 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3259 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3260 | if (!dst) |
| 3261 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3262 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3263 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3264 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3265 | if (!dst) |
| 3266 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3267 | *dst++ = '.'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3268 | |
Willy Tarreau | 7d9421d | 2020-02-29 09:08:02 +0100 | [diff] [blame] | 3269 | dst = utoa_pad((unsigned int)(date->tv_usec/1000)%1000, dst, 4); // milliseconds |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3270 | if (!dst) |
| 3271 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3272 | *dst = '\0'; |
| 3273 | |
| 3274 | return dst; |
| 3275 | } |
| 3276 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3277 | /* Base year used to compute leap years */ |
| 3278 | #define TM_YEAR_BASE 1900 |
| 3279 | |
| 3280 | /* Return the difference in seconds between two times (leap seconds are ignored). |
| 3281 | * Retrieved from glibc 2.18 source code. |
| 3282 | */ |
| 3283 | static int my_tm_diff(const struct tm *a, const struct tm *b) |
| 3284 | { |
| 3285 | /* Compute intervening leap days correctly even if year is negative. |
| 3286 | * Take care to avoid int overflow in leap day calculations, |
| 3287 | * but it's OK to assume that A and B are close to each other. |
| 3288 | */ |
| 3289 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); |
| 3290 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); |
| 3291 | int a100 = a4 / 25 - (a4 % 25 < 0); |
| 3292 | int b100 = b4 / 25 - (b4 % 25 < 0); |
| 3293 | int a400 = a100 >> 2; |
| 3294 | int b400 = b100 >> 2; |
| 3295 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
| 3296 | int years = a->tm_year - b->tm_year; |
| 3297 | int days = (365 * years + intervening_leap_days |
| 3298 | + (a->tm_yday - b->tm_yday)); |
| 3299 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) |
| 3300 | + (a->tm_min - b->tm_min)) |
| 3301 | + (a->tm_sec - b->tm_sec)); |
| 3302 | } |
| 3303 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3304 | /* Return the GMT offset for a specific local time. |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3305 | * Both t and tm must represent the same time. |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3306 | * The string returned has the same format as returned by strftime(... "%z", tm). |
| 3307 | * Offsets are kept in an internal cache for better performances. |
| 3308 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3309 | const char *get_gmt_offset(time_t t, struct tm *tm) |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3310 | { |
| 3311 | /* Cache offsets from GMT (depending on whether DST is active or not) */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 3312 | static THREAD_LOCAL char gmt_offsets[2][5+1] = { "", "" }; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3313 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3314 | char *gmt_offset; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3315 | struct tm tm_gmt; |
| 3316 | int diff; |
| 3317 | int isdst = tm->tm_isdst; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3318 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3319 | /* Pretend DST not active if its status is unknown */ |
| 3320 | if (isdst < 0) |
| 3321 | isdst = 0; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3322 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3323 | /* Fetch the offset and initialize it if needed */ |
| 3324 | gmt_offset = gmt_offsets[isdst & 0x01]; |
| 3325 | if (unlikely(!*gmt_offset)) { |
| 3326 | get_gmtime(t, &tm_gmt); |
| 3327 | diff = my_tm_diff(tm, &tm_gmt); |
| 3328 | if (diff < 0) { |
| 3329 | diff = -diff; |
| 3330 | *gmt_offset = '-'; |
| 3331 | } else { |
| 3332 | *gmt_offset = '+'; |
| 3333 | } |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3334 | diff %= 86400U; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3335 | diff /= 60; /* Convert to minutes */ |
| 3336 | snprintf(gmt_offset+1, 4+1, "%02d%02d", diff/60, diff%60); |
| 3337 | } |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3338 | |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3339 | return gmt_offset; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3340 | } |
| 3341 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3342 | /* gmt2str_log: write a date in the format : |
| 3343 | * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf |
| 3344 | * return a pointer to the last char written (\0) or |
| 3345 | * NULL if there isn't enough space. |
| 3346 | */ |
| 3347 | char *gmt2str_log(char *dst, struct tm *tm, size_t size) |
| 3348 | { |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3349 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3350 | return NULL; |
| 3351 | |
| 3352 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3353 | if (!dst) |
| 3354 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3355 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3356 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3357 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3358 | dst += 3; |
| 3359 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3360 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3361 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3362 | if (!dst) |
| 3363 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3364 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3365 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3366 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3367 | if (!dst) |
| 3368 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3369 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3370 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3371 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3372 | if (!dst) |
| 3373 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3374 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3375 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3376 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3377 | if (!dst) |
| 3378 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3379 | *dst++ = ' '; |
| 3380 | *dst++ = '+'; |
| 3381 | *dst++ = '0'; |
| 3382 | *dst++ = '0'; |
| 3383 | *dst++ = '0'; |
| 3384 | *dst++ = '0'; |
| 3385 | *dst = '\0'; |
| 3386 | |
| 3387 | return dst; |
| 3388 | } |
| 3389 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3390 | /* localdate2str_log: write a date in the format : |
| 3391 | * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3392 | * Both t and tm must represent the same time. |
| 3393 | * return a pointer to the last char written (\0) or |
| 3394 | * NULL if there isn't enough space. |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3395 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3396 | char *localdate2str_log(char *dst, time_t t, struct tm *tm, size_t size) |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3397 | { |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3398 | const char *gmt_offset; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3399 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
| 3400 | return NULL; |
| 3401 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3402 | gmt_offset = get_gmt_offset(t, tm); |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3403 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3404 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3405 | if (!dst) |
| 3406 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3407 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3408 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3409 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3410 | dst += 3; |
| 3411 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3412 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3413 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3414 | if (!dst) |
| 3415 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3416 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3417 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3418 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3419 | if (!dst) |
| 3420 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3421 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3422 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3423 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3424 | if (!dst) |
| 3425 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3426 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3427 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3428 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3429 | if (!dst) |
| 3430 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3431 | *dst++ = ' '; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3432 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3433 | memcpy(dst, gmt_offset, 5); // Offset from local time to GMT |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3434 | dst += 5; |
| 3435 | *dst = '\0'; |
| 3436 | |
| 3437 | return dst; |
| 3438 | } |
| 3439 | |
Willy Tarreau | cb1949b | 2017-07-19 19:05:29 +0200 | [diff] [blame] | 3440 | /* Returns the number of seconds since 01/01/1970 0:0:0 GMT for GMT date <tm>. |
| 3441 | * It is meant as a portable replacement for timegm() for use with valid inputs. |
| 3442 | * Returns undefined results for invalid dates (eg: months out of range 0..11). |
| 3443 | */ |
| 3444 | time_t my_timegm(const struct tm *tm) |
| 3445 | { |
| 3446 | /* Each month has 28, 29, 30 or 31 days, or 28+N. The date in the year |
| 3447 | * is thus (current month - 1)*28 + cumulated_N[month] to count the |
| 3448 | * sum of the extra N days for elapsed months. The sum of all these N |
| 3449 | * days doesn't exceed 30 for a complete year (366-12*28) so it fits |
| 3450 | * in a 5-bit word. This means that with 60 bits we can represent a |
| 3451 | * matrix of all these values at once, which is fast and efficient to |
| 3452 | * access. The extra February day for leap years is not counted here. |
| 3453 | * |
| 3454 | * Jan : none = 0 (0) |
| 3455 | * Feb : Jan = 3 (3) |
| 3456 | * Mar : Jan..Feb = 3 (3 + 0) |
| 3457 | * Apr : Jan..Mar = 6 (3 + 0 + 3) |
| 3458 | * May : Jan..Apr = 8 (3 + 0 + 3 + 2) |
| 3459 | * Jun : Jan..May = 11 (3 + 0 + 3 + 2 + 3) |
| 3460 | * Jul : Jan..Jun = 13 (3 + 0 + 3 + 2 + 3 + 2) |
| 3461 | * Aug : Jan..Jul = 16 (3 + 0 + 3 + 2 + 3 + 2 + 3) |
| 3462 | * Sep : Jan..Aug = 19 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3) |
| 3463 | * Oct : Jan..Sep = 21 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2) |
| 3464 | * Nov : Jan..Oct = 24 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3) |
| 3465 | * Dec : Jan..Nov = 26 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3 + 2) |
| 3466 | */ |
| 3467 | uint64_t extra = |
| 3468 | ( 0ULL << 0*5) + ( 3ULL << 1*5) + ( 3ULL << 2*5) + /* Jan, Feb, Mar, */ |
| 3469 | ( 6ULL << 3*5) + ( 8ULL << 4*5) + (11ULL << 5*5) + /* Apr, May, Jun, */ |
| 3470 | (13ULL << 6*5) + (16ULL << 7*5) + (19ULL << 8*5) + /* Jul, Aug, Sep, */ |
| 3471 | (21ULL << 9*5) + (24ULL << 10*5) + (26ULL << 11*5); /* Oct, Nov, Dec, */ |
| 3472 | |
| 3473 | unsigned int y = tm->tm_year + 1900; |
| 3474 | unsigned int m = tm->tm_mon; |
| 3475 | unsigned long days = 0; |
| 3476 | |
| 3477 | /* days since 1/1/1970 for full years */ |
| 3478 | days += days_since_zero(y) - days_since_zero(1970); |
| 3479 | |
| 3480 | /* days for full months in the current year */ |
| 3481 | days += 28 * m + ((extra >> (m * 5)) & 0x1f); |
| 3482 | |
| 3483 | /* count + 1 after March for leap years. A leap year is a year multiple |
| 3484 | * of 4, unless it's multiple of 100 without being multiple of 400. 2000 |
| 3485 | * is leap, 1900 isn't, 1904 is. |
| 3486 | */ |
| 3487 | if ((m > 1) && !(y & 3) && ((y % 100) || !(y % 400))) |
| 3488 | days++; |
| 3489 | |
| 3490 | days += tm->tm_mday - 1; |
| 3491 | return days * 86400ULL + tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; |
| 3492 | } |
| 3493 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3494 | /* This function check a char. It returns true and updates |
| 3495 | * <date> and <len> pointer to the new position if the |
| 3496 | * character is found. |
| 3497 | */ |
| 3498 | static inline int parse_expect_char(const char **date, int *len, char c) |
| 3499 | { |
| 3500 | if (*len < 1 || **date != c) |
| 3501 | return 0; |
| 3502 | (*len)--; |
| 3503 | (*date)++; |
| 3504 | return 1; |
| 3505 | } |
| 3506 | |
| 3507 | /* This function expects a string <str> of len <l>. It return true and updates. |
| 3508 | * <date> and <len> if the string matches, otherwise, it returns false. |
| 3509 | */ |
| 3510 | static inline int parse_strcmp(const char **date, int *len, char *str, int l) |
| 3511 | { |
| 3512 | if (*len < l || strncmp(*date, str, l) != 0) |
| 3513 | return 0; |
| 3514 | (*len) -= l; |
| 3515 | (*date) += l; |
| 3516 | return 1; |
| 3517 | } |
| 3518 | |
| 3519 | /* This macro converts 3 chars name in integer. */ |
| 3520 | #define STR2I3(__a, __b, __c) ((__a) * 65536 + (__b) * 256 + (__c)) |
| 3521 | |
| 3522 | /* day-name = %x4D.6F.6E ; "Mon", case-sensitive |
| 3523 | * / %x54.75.65 ; "Tue", case-sensitive |
| 3524 | * / %x57.65.64 ; "Wed", case-sensitive |
| 3525 | * / %x54.68.75 ; "Thu", case-sensitive |
| 3526 | * / %x46.72.69 ; "Fri", case-sensitive |
| 3527 | * / %x53.61.74 ; "Sat", case-sensitive |
| 3528 | * / %x53.75.6E ; "Sun", case-sensitive |
| 3529 | * |
| 3530 | * This array must be alphabetically sorted |
| 3531 | */ |
| 3532 | static inline int parse_http_dayname(const char **date, int *len, struct tm *tm) |
| 3533 | { |
| 3534 | if (*len < 3) |
| 3535 | return 0; |
| 3536 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3537 | case STR2I3('M','o','n'): tm->tm_wday = 1; break; |
| 3538 | case STR2I3('T','u','e'): tm->tm_wday = 2; break; |
| 3539 | case STR2I3('W','e','d'): tm->tm_wday = 3; break; |
| 3540 | case STR2I3('T','h','u'): tm->tm_wday = 4; break; |
| 3541 | case STR2I3('F','r','i'): tm->tm_wday = 5; break; |
| 3542 | case STR2I3('S','a','t'): tm->tm_wday = 6; break; |
| 3543 | case STR2I3('S','u','n'): tm->tm_wday = 7; break; |
| 3544 | default: return 0; |
| 3545 | } |
| 3546 | *len -= 3; |
| 3547 | *date += 3; |
| 3548 | return 1; |
| 3549 | } |
| 3550 | |
| 3551 | /* month = %x4A.61.6E ; "Jan", case-sensitive |
| 3552 | * / %x46.65.62 ; "Feb", case-sensitive |
| 3553 | * / %x4D.61.72 ; "Mar", case-sensitive |
| 3554 | * / %x41.70.72 ; "Apr", case-sensitive |
| 3555 | * / %x4D.61.79 ; "May", case-sensitive |
| 3556 | * / %x4A.75.6E ; "Jun", case-sensitive |
| 3557 | * / %x4A.75.6C ; "Jul", case-sensitive |
| 3558 | * / %x41.75.67 ; "Aug", case-sensitive |
| 3559 | * / %x53.65.70 ; "Sep", case-sensitive |
| 3560 | * / %x4F.63.74 ; "Oct", case-sensitive |
| 3561 | * / %x4E.6F.76 ; "Nov", case-sensitive |
| 3562 | * / %x44.65.63 ; "Dec", case-sensitive |
| 3563 | * |
| 3564 | * This array must be alphabetically sorted |
| 3565 | */ |
| 3566 | static inline int parse_http_monthname(const char **date, int *len, struct tm *tm) |
| 3567 | { |
| 3568 | if (*len < 3) |
| 3569 | return 0; |
| 3570 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3571 | case STR2I3('J','a','n'): tm->tm_mon = 0; break; |
| 3572 | case STR2I3('F','e','b'): tm->tm_mon = 1; break; |
| 3573 | case STR2I3('M','a','r'): tm->tm_mon = 2; break; |
| 3574 | case STR2I3('A','p','r'): tm->tm_mon = 3; break; |
| 3575 | case STR2I3('M','a','y'): tm->tm_mon = 4; break; |
| 3576 | case STR2I3('J','u','n'): tm->tm_mon = 5; break; |
| 3577 | case STR2I3('J','u','l'): tm->tm_mon = 6; break; |
| 3578 | case STR2I3('A','u','g'): tm->tm_mon = 7; break; |
| 3579 | case STR2I3('S','e','p'): tm->tm_mon = 8; break; |
| 3580 | case STR2I3('O','c','t'): tm->tm_mon = 9; break; |
| 3581 | case STR2I3('N','o','v'): tm->tm_mon = 10; break; |
| 3582 | case STR2I3('D','e','c'): tm->tm_mon = 11; break; |
| 3583 | default: return 0; |
| 3584 | } |
| 3585 | *len -= 3; |
| 3586 | *date += 3; |
| 3587 | return 1; |
| 3588 | } |
| 3589 | |
| 3590 | /* day-name-l = %x4D.6F.6E.64.61.79 ; "Monday", case-sensitive |
| 3591 | * / %x54.75.65.73.64.61.79 ; "Tuesday", case-sensitive |
| 3592 | * / %x57.65.64.6E.65.73.64.61.79 ; "Wednesday", case-sensitive |
| 3593 | * / %x54.68.75.72.73.64.61.79 ; "Thursday", case-sensitive |
| 3594 | * / %x46.72.69.64.61.79 ; "Friday", case-sensitive |
| 3595 | * / %x53.61.74.75.72.64.61.79 ; "Saturday", case-sensitive |
| 3596 | * / %x53.75.6E.64.61.79 ; "Sunday", case-sensitive |
| 3597 | * |
| 3598 | * This array must be alphabetically sorted |
| 3599 | */ |
| 3600 | static inline int parse_http_ldayname(const char **date, int *len, struct tm *tm) |
| 3601 | { |
| 3602 | if (*len < 6) /* Minimum length. */ |
| 3603 | return 0; |
| 3604 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3605 | case STR2I3('M','o','n'): |
| 3606 | RET0_UNLESS(parse_strcmp(date, len, "Monday", 6)); |
| 3607 | tm->tm_wday = 1; |
| 3608 | return 1; |
| 3609 | case STR2I3('T','u','e'): |
| 3610 | RET0_UNLESS(parse_strcmp(date, len, "Tuesday", 7)); |
| 3611 | tm->tm_wday = 2; |
| 3612 | return 1; |
| 3613 | case STR2I3('W','e','d'): |
| 3614 | RET0_UNLESS(parse_strcmp(date, len, "Wednesday", 9)); |
| 3615 | tm->tm_wday = 3; |
| 3616 | return 1; |
| 3617 | case STR2I3('T','h','u'): |
| 3618 | RET0_UNLESS(parse_strcmp(date, len, "Thursday", 8)); |
| 3619 | tm->tm_wday = 4; |
| 3620 | return 1; |
| 3621 | case STR2I3('F','r','i'): |
| 3622 | RET0_UNLESS(parse_strcmp(date, len, "Friday", 6)); |
| 3623 | tm->tm_wday = 5; |
| 3624 | return 1; |
| 3625 | case STR2I3('S','a','t'): |
| 3626 | RET0_UNLESS(parse_strcmp(date, len, "Saturday", 8)); |
| 3627 | tm->tm_wday = 6; |
| 3628 | return 1; |
| 3629 | case STR2I3('S','u','n'): |
| 3630 | RET0_UNLESS(parse_strcmp(date, len, "Sunday", 6)); |
| 3631 | tm->tm_wday = 7; |
| 3632 | return 1; |
| 3633 | } |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | /* This function parses exactly 1 digit and returns the numeric value in "digit". */ |
| 3638 | static inline int parse_digit(const char **date, int *len, int *digit) |
| 3639 | { |
| 3640 | if (*len < 1 || **date < '0' || **date > '9') |
| 3641 | return 0; |
| 3642 | *digit = (**date - '0'); |
| 3643 | (*date)++; |
| 3644 | (*len)--; |
| 3645 | return 1; |
| 3646 | } |
| 3647 | |
| 3648 | /* This function parses exactly 2 digits and returns the numeric value in "digit". */ |
| 3649 | static inline int parse_2digit(const char **date, int *len, int *digit) |
| 3650 | { |
| 3651 | int value; |
| 3652 | |
| 3653 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3654 | (*digit) = value * 10; |
| 3655 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3656 | (*digit) += value; |
| 3657 | |
| 3658 | return 1; |
| 3659 | } |
| 3660 | |
| 3661 | /* This function parses exactly 4 digits and returns the numeric value in "digit". */ |
| 3662 | static inline int parse_4digit(const char **date, int *len, int *digit) |
| 3663 | { |
| 3664 | int value; |
| 3665 | |
| 3666 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3667 | (*digit) = value * 1000; |
| 3668 | |
| 3669 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3670 | (*digit) += value * 100; |
| 3671 | |
| 3672 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3673 | (*digit) += value * 10; |
| 3674 | |
| 3675 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3676 | (*digit) += value; |
| 3677 | |
| 3678 | return 1; |
| 3679 | } |
| 3680 | |
| 3681 | /* time-of-day = hour ":" minute ":" second |
| 3682 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3683 | * |
| 3684 | * hour = 2DIGIT |
| 3685 | * minute = 2DIGIT |
| 3686 | * second = 2DIGIT |
| 3687 | */ |
| 3688 | static inline int parse_http_time(const char **date, int *len, struct tm *tm) |
| 3689 | { |
| 3690 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_hour)); /* hour 2DIGIT */ |
| 3691 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3692 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_min)); /* min 2DIGIT */ |
| 3693 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3694 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_sec)); /* sec 2DIGIT */ |
| 3695 | return 1; |
| 3696 | } |
| 3697 | |
| 3698 | /* From RFC7231 |
| 3699 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3700 | * |
| 3701 | * IMF-fixdate = day-name "," SP date1 SP time-of-day SP GMT |
| 3702 | * ; fixed length/zone/capitalization subset of the format |
| 3703 | * ; see Section 3.3 of [RFC5322] |
| 3704 | * |
| 3705 | * |
| 3706 | * date1 = day SP month SP year |
| 3707 | * ; e.g., 02 Jun 1982 |
| 3708 | * |
| 3709 | * day = 2DIGIT |
| 3710 | * year = 4DIGIT |
| 3711 | * |
| 3712 | * GMT = %x47.4D.54 ; "GMT", case-sensitive |
| 3713 | * |
| 3714 | * time-of-day = hour ":" minute ":" second |
| 3715 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3716 | * |
| 3717 | * hour = 2DIGIT |
| 3718 | * minute = 2DIGIT |
| 3719 | * second = 2DIGIT |
| 3720 | * |
| 3721 | * DIGIT = decimal 0-9 |
| 3722 | */ |
| 3723 | int parse_imf_date(const char *date, int len, struct tm *tm) |
| 3724 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3725 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 3726 | memset(tm, 0, sizeof(*tm)); |
| 3727 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3728 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 3729 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 3730 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3731 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 3732 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3733 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 3734 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3735 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 3736 | tm->tm_year -= 1900; |
| 3737 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3738 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 3739 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3740 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 3741 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3742 | return 1; |
| 3743 | } |
| 3744 | |
| 3745 | /* From RFC7231 |
| 3746 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3747 | * |
| 3748 | * rfc850-date = day-name-l "," SP date2 SP time-of-day SP GMT |
| 3749 | * date2 = day "-" month "-" 2DIGIT |
| 3750 | * ; e.g., 02-Jun-82 |
| 3751 | * |
| 3752 | * day = 2DIGIT |
| 3753 | */ |
| 3754 | int parse_rfc850_date(const char *date, int len, struct tm *tm) |
| 3755 | { |
| 3756 | int year; |
| 3757 | |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3758 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 3759 | memset(tm, 0, sizeof(*tm)); |
| 3760 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3761 | RET0_UNLESS(parse_http_ldayname(&date, &len, tm)); /* Read the day name */ |
| 3762 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 3763 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3764 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 3765 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 3766 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 3767 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 3768 | |
| 3769 | /* year = 2DIGIT |
| 3770 | * |
| 3771 | * Recipients of a timestamp value in rfc850-(*date) format, which uses a |
| 3772 | * two-digit year, MUST interpret a timestamp that appears to be more |
| 3773 | * than 50 years in the future as representing the most recent year in |
| 3774 | * the past that had the same last two digits. |
| 3775 | */ |
| 3776 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_year)); |
| 3777 | |
| 3778 | /* expect SP */ |
| 3779 | if (!parse_expect_char(&date, &len, ' ')) { |
| 3780 | /* Maybe we have the date with 4 digits. */ |
| 3781 | RET0_UNLESS(parse_2digit(&date, &len, &year)); |
| 3782 | tm->tm_year = (tm->tm_year * 100 + year) - 1900; |
| 3783 | /* expect SP */ |
| 3784 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); |
| 3785 | } else { |
| 3786 | /* I fix 60 as pivot: >60: +1900, <60: +2000. Note that the |
| 3787 | * tm_year is the number of year since 1900, so for +1900, we |
| 3788 | * do nothing, and for +2000, we add 100. |
| 3789 | */ |
| 3790 | if (tm->tm_year <= 60) |
| 3791 | tm->tm_year += 100; |
| 3792 | } |
| 3793 | |
| 3794 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 3795 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3796 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 3797 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3798 | |
| 3799 | return 1; |
| 3800 | } |
| 3801 | |
| 3802 | /* From RFC7231 |
| 3803 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3804 | * |
| 3805 | * asctime-date = day-name SP date3 SP time-of-day SP year |
| 3806 | * date3 = month SP ( 2DIGIT / ( SP 1DIGIT )) |
| 3807 | * ; e.g., Jun 2 |
| 3808 | * |
| 3809 | * HTTP-date is case sensitive. A sender MUST NOT generate additional |
| 3810 | * whitespace in an HTTP-date beyond that specifically included as SP in |
| 3811 | * the grammar. |
| 3812 | */ |
| 3813 | int parse_asctime_date(const char *date, int len, struct tm *tm) |
| 3814 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3815 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 3816 | memset(tm, 0, sizeof(*tm)); |
| 3817 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3818 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 3819 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3820 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* expect month */ |
| 3821 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3822 | |
| 3823 | /* expect SP and 1DIGIT or 2DIGIT */ |
| 3824 | if (parse_expect_char(&date, &len, ' ')) |
| 3825 | RET0_UNLESS(parse_digit(&date, &len, &tm->tm_mday)); |
| 3826 | else |
| 3827 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); |
| 3828 | |
| 3829 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3830 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 3831 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3832 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 3833 | tm->tm_year -= 1900; |
| 3834 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | /* From RFC7231 |
| 3839 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3840 | * |
| 3841 | * HTTP-date = IMF-fixdate / obs-date |
| 3842 | * obs-date = rfc850-date / asctime-date |
| 3843 | * |
| 3844 | * parses an HTTP date in the RFC format and is accepted |
| 3845 | * alternatives. <date> is the strinf containing the date, |
| 3846 | * len is the len of the string. <tm> is filled with the |
| 3847 | * parsed time. We must considers this time as GMT. |
| 3848 | */ |
| 3849 | int parse_http_date(const char *date, int len, struct tm *tm) |
| 3850 | { |
| 3851 | if (parse_imf_date(date, len, tm)) |
| 3852 | return 1; |
| 3853 | |
| 3854 | if (parse_rfc850_date(date, len, tm)) |
| 3855 | return 1; |
| 3856 | |
| 3857 | if (parse_asctime_date(date, len, tm)) |
| 3858 | return 1; |
| 3859 | |
| 3860 | return 0; |
| 3861 | } |
| 3862 | |
Willy Tarreau | 4deeb10 | 2021-01-29 10:47:52 +0100 | [diff] [blame] | 3863 | /* print the time <ns> in a short form (exactly 7 chars) at the end of buffer |
| 3864 | * <out>. "-" is printed if the value is zero, "inf" if larger than 1000 years. |
| 3865 | * It returns the new buffer length, or 0 if it doesn't fit. The value will be |
| 3866 | * surrounded by <pfx> and <sfx> respectively if not NULL. |
| 3867 | */ |
| 3868 | int print_time_short(struct buffer *out, const char *pfx, uint64_t ns, const char *sfx) |
| 3869 | { |
| 3870 | double val = ns; // 52 bits of mantissa keep ns accuracy over 52 days |
| 3871 | const char *unit; |
| 3872 | |
| 3873 | if (!pfx) |
| 3874 | pfx = ""; |
| 3875 | if (!sfx) |
| 3876 | sfx = ""; |
| 3877 | |
| 3878 | do { |
| 3879 | unit = " - "; if (val <= 0.0) break; |
| 3880 | unit = "ns"; if (val < 1000.0) break; |
| 3881 | unit = "us"; val /= 1000.0; if (val < 1000.0) break; |
| 3882 | unit = "ms"; val /= 1000.0; if (val < 1000.0) break; |
| 3883 | unit = "s "; val /= 1000.0; if (val < 60.0) break; |
| 3884 | unit = "m "; val /= 60.0; if (val < 60.0) break; |
| 3885 | unit = "h "; val /= 60.0; if (val < 24.0) break; |
| 3886 | unit = "d "; val /= 24.0; if (val < 365.0) break; |
| 3887 | unit = "yr"; val /= 365.0; if (val < 1000.0) break; |
| 3888 | unit = " inf "; val = 0.0; break; |
| 3889 | } while (0); |
| 3890 | |
| 3891 | if (val <= 0.0) |
| 3892 | return chunk_appendf(out, "%s%7s%s", pfx, unit, sfx); |
| 3893 | else if (val < 10.0) |
| 3894 | return chunk_appendf(out, "%s%1.3f%s%s", pfx, val, unit, sfx); |
| 3895 | else if (val < 100.0) |
| 3896 | return chunk_appendf(out, "%s%2.2f%s%s", pfx, val, unit, sfx); |
| 3897 | else |
| 3898 | return chunk_appendf(out, "%s%3.1f%s%s", pfx, val, unit, sfx); |
| 3899 | } |
| 3900 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3901 | /* Dynamically allocates a string of the proper length to hold the formatted |
| 3902 | * output. NULL is returned on error. The caller is responsible for freeing the |
| 3903 | * memory area using free(). The resulting string is returned in <out> if the |
| 3904 | * pointer is not NULL. A previous version of <out> might be used to build the |
| 3905 | * new string, and it will be freed before returning if it is not NULL, which |
| 3906 | * makes it possible to build complex strings from iterative calls without |
| 3907 | * having to care about freeing intermediate values, as in the example below : |
| 3908 | * |
| 3909 | * memprintf(&err, "invalid argument: '%s'", arg); |
| 3910 | * ... |
| 3911 | * memprintf(&err, "parser said : <%s>\n", *err); |
| 3912 | * ... |
| 3913 | * free(*err); |
| 3914 | * |
| 3915 | * This means that <err> must be initialized to NULL before first invocation. |
| 3916 | * The return value also holds the allocated string, which eases error checking |
| 3917 | * and immediate consumption. If the output pointer is not used, NULL must be |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3918 | * passed instead and it will be ignored. The returned message will then also |
| 3919 | * be NULL so that the caller does not have to bother with freeing anything. |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3920 | * |
| 3921 | * It is also convenient to use it without any free except the last one : |
| 3922 | * err = NULL; |
| 3923 | * if (!fct1(err)) report(*err); |
| 3924 | * if (!fct2(err)) report(*err); |
| 3925 | * if (!fct3(err)) report(*err); |
| 3926 | * free(*err); |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 3927 | * |
| 3928 | * memprintf relies on memvprintf. This last version can be called from any |
| 3929 | * function with variadic arguments. |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3930 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 3931 | char *memvprintf(char **out, const char *format, va_list orig_args) |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3932 | { |
| 3933 | va_list args; |
| 3934 | char *ret = NULL; |
| 3935 | int allocated = 0; |
| 3936 | int needed = 0; |
| 3937 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3938 | if (!out) |
| 3939 | return NULL; |
| 3940 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3941 | do { |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 3942 | char buf1; |
| 3943 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3944 | /* vsnprintf() will return the required length even when the |
| 3945 | * target buffer is NULL. We do this in a loop just in case |
| 3946 | * intermediate evaluations get wrong. |
| 3947 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 3948 | va_copy(args, orig_args); |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 3949 | needed = vsnprintf(ret ? ret : &buf1, allocated, format, args); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3950 | va_end(args); |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 3951 | if (needed < allocated) { |
| 3952 | /* Note: on Solaris 8, the first iteration always |
| 3953 | * returns -1 if allocated is zero, so we force a |
| 3954 | * retry. |
| 3955 | */ |
| 3956 | if (!allocated) |
| 3957 | needed = 0; |
| 3958 | else |
| 3959 | break; |
| 3960 | } |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3961 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 3962 | allocated = needed + 1; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 3963 | ret = my_realloc2(ret, allocated); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3964 | } while (ret); |
| 3965 | |
| 3966 | if (needed < 0) { |
| 3967 | /* an error was encountered */ |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3968 | ha_free(&ret); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 3969 | } |
| 3970 | |
| 3971 | if (out) { |
| 3972 | free(*out); |
| 3973 | *out = ret; |
| 3974 | } |
| 3975 | |
| 3976 | return ret; |
| 3977 | } |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3978 | |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 3979 | char *memprintf(char **out, const char *format, ...) |
| 3980 | { |
| 3981 | va_list args; |
| 3982 | char *ret = NULL; |
| 3983 | |
| 3984 | va_start(args, format); |
| 3985 | ret = memvprintf(out, format, args); |
| 3986 | va_end(args); |
| 3987 | |
| 3988 | return ret; |
| 3989 | } |
| 3990 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 3991 | /* Used to add <level> spaces before each line of <out>, unless there is only one line. |
| 3992 | * The input argument is automatically freed and reassigned. The result will have to be |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 3993 | * freed by the caller. It also supports being passed a NULL which results in the same |
| 3994 | * output. |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 3995 | * Example of use : |
| 3996 | * parse(cmd, &err); (callee: memprintf(&err, ...)) |
| 3997 | * fprintf(stderr, "Parser said: %s\n", indent_error(&err)); |
| 3998 | * free(err); |
| 3999 | */ |
| 4000 | char *indent_msg(char **out, int level) |
| 4001 | { |
| 4002 | char *ret, *in, *p; |
| 4003 | int needed = 0; |
| 4004 | int lf = 0; |
| 4005 | int lastlf = 0; |
| 4006 | int len; |
| 4007 | |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 4008 | if (!out || !*out) |
| 4009 | return NULL; |
| 4010 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4011 | in = *out - 1; |
| 4012 | while ((in = strchr(in + 1, '\n')) != NULL) { |
| 4013 | lastlf = in - *out; |
| 4014 | lf++; |
| 4015 | } |
| 4016 | |
| 4017 | if (!lf) /* single line, no LF, return it as-is */ |
| 4018 | return *out; |
| 4019 | |
| 4020 | len = strlen(*out); |
| 4021 | |
| 4022 | if (lf == 1 && lastlf == len - 1) { |
| 4023 | /* single line, LF at end, strip it and return as-is */ |
| 4024 | (*out)[lastlf] = 0; |
| 4025 | return *out; |
| 4026 | } |
| 4027 | |
| 4028 | /* OK now we have at least one LF, we need to process the whole string |
| 4029 | * as a multi-line string. What we'll do : |
| 4030 | * - prefix with an LF if there is none |
| 4031 | * - add <level> spaces before each line |
| 4032 | * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) = |
| 4033 | * 1 + level + len + lf * level = 1 + level * (lf + 1) + len. |
| 4034 | */ |
| 4035 | |
| 4036 | needed = 1 + level * (lf + 1) + len + 1; |
| 4037 | p = ret = malloc(needed); |
| 4038 | in = *out; |
| 4039 | |
| 4040 | /* skip initial LFs */ |
| 4041 | while (*in == '\n') |
| 4042 | in++; |
| 4043 | |
| 4044 | /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */ |
| 4045 | while (*in) { |
| 4046 | *p++ = '\n'; |
| 4047 | memset(p, ' ', level); |
| 4048 | p += level; |
| 4049 | do { |
| 4050 | *p++ = *in++; |
| 4051 | } while (*in && *in != '\n'); |
| 4052 | if (*in) |
| 4053 | in++; |
| 4054 | } |
| 4055 | *p = 0; |
| 4056 | |
| 4057 | free(*out); |
| 4058 | *out = ret; |
| 4059 | |
| 4060 | return ret; |
| 4061 | } |
| 4062 | |
Willy Tarreau | a2c9911 | 2019-08-21 13:17:37 +0200 | [diff] [blame] | 4063 | /* makes a copy of message <in> into <out>, with each line prefixed with <pfx> |
| 4064 | * and end of lines replaced with <eol> if not 0. The first line to indent has |
| 4065 | * to be indicated in <first> (starts at zero), so that it is possible to skip |
| 4066 | * indenting the first line if it has to be appended after an existing message. |
| 4067 | * Empty strings are never indented, and NULL strings are considered empty both |
| 4068 | * for <in> and <pfx>. It returns non-zero if an EOL was appended as the last |
| 4069 | * character, non-zero otherwise. |
| 4070 | */ |
| 4071 | int append_prefixed_str(struct buffer *out, const char *in, const char *pfx, char eol, int first) |
| 4072 | { |
| 4073 | int bol, lf; |
| 4074 | int pfxlen = pfx ? strlen(pfx) : 0; |
| 4075 | |
| 4076 | if (!in) |
| 4077 | return 0; |
| 4078 | |
| 4079 | bol = 1; |
| 4080 | lf = 0; |
| 4081 | while (*in) { |
| 4082 | if (bol && pfxlen) { |
| 4083 | if (first > 0) |
| 4084 | first--; |
| 4085 | else |
| 4086 | b_putblk(out, pfx, pfxlen); |
| 4087 | bol = 0; |
| 4088 | } |
| 4089 | |
| 4090 | lf = (*in == '\n'); |
| 4091 | bol |= lf; |
| 4092 | b_putchr(out, (lf && eol) ? eol : *in); |
| 4093 | in++; |
| 4094 | } |
| 4095 | return lf; |
| 4096 | } |
| 4097 | |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4098 | /* removes environment variable <name> from the environment as found in |
| 4099 | * environ. This is only provided as an alternative for systems without |
| 4100 | * unsetenv() (old Solaris and AIX versions). THIS IS NOT THREAD SAFE. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4101 | * The principle is to scan environ for each occurrence of variable name |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4102 | * <name> and to replace the matching pointers with the last pointer of |
| 4103 | * the array (since variables are not ordered). |
| 4104 | * It always returns 0 (success). |
| 4105 | */ |
| 4106 | int my_unsetenv(const char *name) |
| 4107 | { |
| 4108 | extern char **environ; |
| 4109 | char **p = environ; |
| 4110 | int vars; |
| 4111 | int next; |
| 4112 | int len; |
| 4113 | |
| 4114 | len = strlen(name); |
| 4115 | for (vars = 0; p[vars]; vars++) |
| 4116 | ; |
| 4117 | next = 0; |
| 4118 | while (next < vars) { |
| 4119 | if (strncmp(p[next], name, len) != 0 || p[next][len] != '=') { |
| 4120 | next++; |
| 4121 | continue; |
| 4122 | } |
| 4123 | if (next < vars - 1) |
| 4124 | p[next] = p[vars - 1]; |
| 4125 | p[--vars] = NULL; |
| 4126 | } |
| 4127 | return 0; |
| 4128 | } |
| 4129 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4130 | /* Convert occurrences of environment variables in the input string to their |
| 4131 | * corresponding value. A variable is identified as a series of alphanumeric |
| 4132 | * characters or underscores following a '$' sign. The <in> string must be |
| 4133 | * free()able. NULL returns NULL. The resulting string might be reallocated if |
| 4134 | * some expansion is made. Variable names may also be enclosed into braces if |
| 4135 | * needed (eg: to concatenate alphanum characters). |
| 4136 | */ |
| 4137 | char *env_expand(char *in) |
| 4138 | { |
| 4139 | char *txt_beg; |
| 4140 | char *out; |
| 4141 | char *txt_end; |
| 4142 | char *var_beg; |
| 4143 | char *var_end; |
| 4144 | char *value; |
| 4145 | char *next; |
| 4146 | int out_len; |
| 4147 | int val_len; |
| 4148 | |
| 4149 | if (!in) |
| 4150 | return in; |
| 4151 | |
| 4152 | value = out = NULL; |
| 4153 | out_len = 0; |
| 4154 | |
| 4155 | txt_beg = in; |
| 4156 | do { |
| 4157 | /* look for next '$' sign in <in> */ |
| 4158 | for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++); |
| 4159 | |
| 4160 | if (!*txt_end && !out) /* end and no expansion performed */ |
| 4161 | return in; |
| 4162 | |
| 4163 | val_len = 0; |
| 4164 | next = txt_end; |
| 4165 | if (*txt_end == '$') { |
| 4166 | char save; |
| 4167 | |
| 4168 | var_beg = txt_end + 1; |
| 4169 | if (*var_beg == '{') |
| 4170 | var_beg++; |
| 4171 | |
| 4172 | var_end = var_beg; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4173 | while (isalnum((unsigned char)*var_end) || *var_end == '_') { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4174 | var_end++; |
| 4175 | } |
| 4176 | |
| 4177 | next = var_end; |
| 4178 | if (*var_end == '}' && (var_beg > txt_end + 1)) |
| 4179 | next++; |
| 4180 | |
| 4181 | /* get value of the variable name at this location */ |
| 4182 | save = *var_end; |
| 4183 | *var_end = '\0'; |
| 4184 | value = getenv(var_beg); |
| 4185 | *var_end = save; |
| 4186 | val_len = value ? strlen(value) : 0; |
| 4187 | } |
| 4188 | |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 4189 | out = my_realloc2(out, out_len + (txt_end - txt_beg) + val_len + 1); |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4190 | if (txt_end > txt_beg) { |
| 4191 | memcpy(out + out_len, txt_beg, txt_end - txt_beg); |
| 4192 | out_len += txt_end - txt_beg; |
| 4193 | } |
| 4194 | if (val_len) { |
| 4195 | memcpy(out + out_len, value, val_len); |
| 4196 | out_len += val_len; |
| 4197 | } |
| 4198 | out[out_len] = 0; |
| 4199 | txt_beg = next; |
| 4200 | } while (*txt_beg); |
| 4201 | |
| 4202 | /* here we know that <out> was allocated and that we don't need <in> anymore */ |
| 4203 | free(in); |
| 4204 | return out; |
| 4205 | } |
| 4206 | |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4207 | |
| 4208 | /* same as strstr() but case-insensitive and with limit length */ |
| 4209 | const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2) |
| 4210 | { |
| 4211 | char *pptr, *sptr, *start; |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 4212 | unsigned int slen, plen; |
| 4213 | unsigned int tmp1, tmp2; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4214 | |
| 4215 | if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found |
| 4216 | return NULL; |
| 4217 | |
| 4218 | if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match |
| 4219 | return str1; |
| 4220 | |
| 4221 | if (len_str1 < len_str2) // pattern is longer than string => search is not found |
| 4222 | return NULL; |
| 4223 | |
| 4224 | for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) { |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 4225 | while (toupper((unsigned char)*start) != toupper((unsigned char)*str2)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4226 | start++; |
| 4227 | slen--; |
| 4228 | tmp1++; |
| 4229 | |
| 4230 | if (tmp1 >= len_str1) |
| 4231 | return NULL; |
| 4232 | |
| 4233 | /* if pattern longer than string */ |
| 4234 | if (slen < plen) |
| 4235 | return NULL; |
| 4236 | } |
| 4237 | |
| 4238 | sptr = start; |
| 4239 | pptr = (char *)str2; |
| 4240 | |
| 4241 | tmp2 = 0; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 4242 | while (toupper((unsigned char)*sptr) == toupper((unsigned char)*pptr)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4243 | sptr++; |
| 4244 | pptr++; |
| 4245 | tmp2++; |
| 4246 | |
| 4247 | if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */ |
| 4248 | return start; |
| 4249 | if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */ |
| 4250 | return NULL; |
| 4251 | } |
| 4252 | } |
| 4253 | return NULL; |
| 4254 | } |
| 4255 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4256 | /* This function read the next valid utf8 char. |
| 4257 | * <s> is the byte srray to be decode, <len> is its length. |
| 4258 | * The function returns decoded char encoded like this: |
| 4259 | * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb |
| 4260 | * are the length read. The decoded character is stored in <c>. |
| 4261 | */ |
| 4262 | unsigned char utf8_next(const char *s, int len, unsigned int *c) |
| 4263 | { |
| 4264 | const unsigned char *p = (unsigned char *)s; |
| 4265 | int dec; |
| 4266 | unsigned char code = UTF8_CODE_OK; |
| 4267 | |
| 4268 | if (len < 1) |
| 4269 | return UTF8_CODE_OK; |
| 4270 | |
| 4271 | /* Check the type of UTF8 sequence |
| 4272 | * |
| 4273 | * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char |
| 4274 | * 10.. .... 0x80 <= x <= 0xbf : invalid sequence |
| 4275 | * 110. .... 0xc0 <= x <= 0xdf : 2 bytes |
| 4276 | * 1110 .... 0xe0 <= x <= 0xef : 3 bytes |
| 4277 | * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes |
| 4278 | * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes |
| 4279 | * 1111 110. 0xfc <= x <= 0xfd : 6 bytes |
| 4280 | * 1111 111. 0xfe <= x <= 0xff : invalid sequence |
| 4281 | */ |
| 4282 | switch (*p) { |
| 4283 | case 0x00 ... 0x7f: |
| 4284 | *c = *p; |
| 4285 | return UTF8_CODE_OK | 1; |
| 4286 | |
| 4287 | case 0x80 ... 0xbf: |
| 4288 | *c = *p; |
| 4289 | return UTF8_CODE_BADSEQ | 1; |
| 4290 | |
| 4291 | case 0xc0 ... 0xdf: |
| 4292 | if (len < 2) { |
| 4293 | *c = *p; |
| 4294 | return UTF8_CODE_BADSEQ | 1; |
| 4295 | } |
| 4296 | *c = *p & 0x1f; |
| 4297 | dec = 1; |
| 4298 | break; |
| 4299 | |
| 4300 | case 0xe0 ... 0xef: |
| 4301 | if (len < 3) { |
| 4302 | *c = *p; |
| 4303 | return UTF8_CODE_BADSEQ | 1; |
| 4304 | } |
| 4305 | *c = *p & 0x0f; |
| 4306 | dec = 2; |
| 4307 | break; |
| 4308 | |
| 4309 | case 0xf0 ... 0xf7: |
| 4310 | if (len < 4) { |
| 4311 | *c = *p; |
| 4312 | return UTF8_CODE_BADSEQ | 1; |
| 4313 | } |
| 4314 | *c = *p & 0x07; |
| 4315 | dec = 3; |
| 4316 | break; |
| 4317 | |
| 4318 | case 0xf8 ... 0xfb: |
| 4319 | if (len < 5) { |
| 4320 | *c = *p; |
| 4321 | return UTF8_CODE_BADSEQ | 1; |
| 4322 | } |
| 4323 | *c = *p & 0x03; |
| 4324 | dec = 4; |
| 4325 | break; |
| 4326 | |
| 4327 | case 0xfc ... 0xfd: |
| 4328 | if (len < 6) { |
| 4329 | *c = *p; |
| 4330 | return UTF8_CODE_BADSEQ | 1; |
| 4331 | } |
| 4332 | *c = *p & 0x01; |
| 4333 | dec = 5; |
| 4334 | break; |
| 4335 | |
| 4336 | case 0xfe ... 0xff: |
| 4337 | default: |
| 4338 | *c = *p; |
| 4339 | return UTF8_CODE_BADSEQ | 1; |
| 4340 | } |
| 4341 | |
| 4342 | p++; |
| 4343 | |
| 4344 | while (dec > 0) { |
| 4345 | |
| 4346 | /* need 0x10 for the 2 first bits */ |
| 4347 | if ( ( *p & 0xc0 ) != 0x80 ) |
| 4348 | return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff); |
| 4349 | |
| 4350 | /* add data at char */ |
| 4351 | *c = ( *c << 6 ) | ( *p & 0x3f ); |
| 4352 | |
| 4353 | dec--; |
| 4354 | p++; |
| 4355 | } |
| 4356 | |
| 4357 | /* Check ovelong encoding. |
| 4358 | * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff |
| 4359 | * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff |
| 4360 | * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff |
| 4361 | */ |
Thierry FOURNIER | 9e7ec08 | 2015-03-12 19:32:38 +0100 | [diff] [blame] | 4362 | if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) || |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4363 | (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) || |
| 4364 | (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) || |
| 4365 | (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4)) |
| 4366 | code |= UTF8_CODE_OVERLONG; |
| 4367 | |
| 4368 | /* Check invalid UTF8 range. */ |
| 4369 | if ((*c >= 0xd800 && *c <= 0xdfff) || |
| 4370 | (*c >= 0xfffe && *c <= 0xffff)) |
| 4371 | code |= UTF8_CODE_INVRANGE; |
| 4372 | |
| 4373 | return code | ((p-(unsigned char *)s)&0x0f); |
| 4374 | } |
| 4375 | |
Maxime de Roucy | dc88785 | 2016-05-13 23:52:54 +0200 | [diff] [blame] | 4376 | /* append a copy of string <str> (in a wordlist) at the end of the list <li> |
| 4377 | * On failure : return 0 and <err> filled with an error message. |
| 4378 | * The caller is responsible for freeing the <err> and <str> copy |
| 4379 | * memory area using free() |
| 4380 | */ |
| 4381 | int list_append_word(struct list *li, const char *str, char **err) |
| 4382 | { |
| 4383 | struct wordlist *wl; |
| 4384 | |
| 4385 | wl = calloc(1, sizeof(*wl)); |
| 4386 | if (!wl) { |
| 4387 | memprintf(err, "out of memory"); |
| 4388 | goto fail_wl; |
| 4389 | } |
| 4390 | |
| 4391 | wl->s = strdup(str); |
| 4392 | if (!wl->s) { |
| 4393 | memprintf(err, "out of memory"); |
| 4394 | goto fail_wl_s; |
| 4395 | } |
| 4396 | |
| 4397 | LIST_ADDQ(li, &wl->list); |
| 4398 | |
| 4399 | return 1; |
| 4400 | |
| 4401 | fail_wl_s: |
| 4402 | free(wl->s); |
| 4403 | fail_wl: |
| 4404 | free(wl); |
| 4405 | return 0; |
| 4406 | } |
| 4407 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4408 | /* indicates if a memory location may safely be read or not. The trick consists |
| 4409 | * in performing a harmless syscall using this location as an input and letting |
| 4410 | * the operating system report whether it's OK or not. For this we have the |
| 4411 | * stat() syscall, which will return EFAULT when the memory location supposed |
| 4412 | * to contain the file name is not readable. If it is readable it will then |
| 4413 | * either return 0 if the area contains an existing file name, or -1 with |
| 4414 | * another code. This must not be abused, and some audit systems might detect |
| 4415 | * this as abnormal activity. It's used only for unsafe dumps. |
| 4416 | */ |
| 4417 | int may_access(const void *ptr) |
| 4418 | { |
| 4419 | struct stat buf; |
| 4420 | |
| 4421 | if (stat(ptr, &buf) == 0) |
| 4422 | return 1; |
| 4423 | if (errno == EFAULT) |
| 4424 | return 0; |
| 4425 | return 1; |
| 4426 | } |
| 4427 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4428 | /* print a string of text buffer to <out>. The format is : |
| 4429 | * Non-printable chars \t, \n, \r and \e are * encoded in C format. |
| 4430 | * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped. |
| 4431 | * Print stopped if null char or <bsize> is reached, or if no more place in the chunk. |
| 4432 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4433 | int dump_text(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4434 | { |
| 4435 | unsigned char c; |
| 4436 | int ptr = 0; |
| 4437 | |
| 4438 | while (buf[ptr] && ptr < bsize) { |
| 4439 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4440 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\' && c != ' ' && c != '=') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4441 | if (out->data > out->size - 1) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4442 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4443 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4444 | } |
| 4445 | else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\' || c == ' ' || c == '=') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4446 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4447 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4448 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4449 | switch (c) { |
| 4450 | case ' ': c = ' '; break; |
| 4451 | case '\t': c = 't'; break; |
| 4452 | case '\n': c = 'n'; break; |
| 4453 | case '\r': c = 'r'; break; |
| 4454 | case '\e': c = 'e'; break; |
| 4455 | case '\\': c = '\\'; break; |
| 4456 | case '=': c = '='; break; |
| 4457 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4458 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4459 | } |
| 4460 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4461 | if (out->data > out->size - 4) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4462 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4463 | out->area[out->data++] = '\\'; |
| 4464 | out->area[out->data++] = 'x'; |
| 4465 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4466 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4467 | } |
| 4468 | ptr++; |
| 4469 | } |
| 4470 | |
| 4471 | return ptr; |
| 4472 | } |
| 4473 | |
| 4474 | /* print a buffer in hexa. |
| 4475 | * Print stopped if <bsize> is reached, or if no more place in the chunk. |
| 4476 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4477 | int dump_binary(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4478 | { |
| 4479 | unsigned char c; |
| 4480 | int ptr = 0; |
| 4481 | |
| 4482 | while (ptr < bsize) { |
| 4483 | c = buf[ptr]; |
| 4484 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4485 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4486 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4487 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4488 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4489 | |
| 4490 | ptr++; |
| 4491 | } |
| 4492 | return ptr; |
| 4493 | } |
| 4494 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4495 | /* Appends into buffer <out> a hex dump of memory area <buf> for <len> bytes, |
| 4496 | * prepending each line with prefix <pfx>. The output is *not* initialized. |
| 4497 | * The output will not wrap pas the buffer's end so it is more optimal if the |
| 4498 | * caller makes sure the buffer is aligned first. A trailing zero will always |
| 4499 | * be appended (and not counted) if there is room for it. The caller must make |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4500 | * sure that the area is dumpable first. If <unsafe> is non-null, the memory |
| 4501 | * locations are checked first for being readable. |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4502 | */ |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4503 | void dump_hex(struct buffer *out, const char *pfx, const void *buf, int len, int unsafe) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4504 | { |
| 4505 | const unsigned char *d = buf; |
| 4506 | int i, j, start; |
| 4507 | |
| 4508 | d = (const unsigned char *)(((unsigned long)buf) & -16); |
| 4509 | start = ((unsigned long)buf) & 15; |
| 4510 | |
| 4511 | for (i = 0; i < start + len; i += 16) { |
| 4512 | chunk_appendf(out, (sizeof(void *) == 4) ? "%s%8p: " : "%s%16p: ", pfx, d + i); |
| 4513 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4514 | // 0: unchecked, 1: checked safe, 2: danger |
| 4515 | unsafe = !!unsafe; |
| 4516 | if (unsafe && !may_access(d + i)) |
| 4517 | unsafe = 2; |
| 4518 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4519 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4520 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4521 | chunk_strcat(out, "'' "); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4522 | else if (unsafe > 1) |
| 4523 | chunk_strcat(out, "** "); |
| 4524 | else |
| 4525 | chunk_appendf(out, "%02x ", d[i + j]); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4526 | |
| 4527 | if (j == 7) |
| 4528 | chunk_strcat(out, "- "); |
| 4529 | } |
| 4530 | chunk_strcat(out, " "); |
| 4531 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4532 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4533 | chunk_strcat(out, "'"); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4534 | else if (unsafe > 1) |
| 4535 | chunk_strcat(out, "*"); |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4536 | else if (isprint((unsigned char)d[i + j])) |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4537 | chunk_appendf(out, "%c", d[i + j]); |
| 4538 | else |
| 4539 | chunk_strcat(out, "."); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4540 | } |
| 4541 | chunk_strcat(out, "\n"); |
| 4542 | } |
| 4543 | } |
| 4544 | |
Willy Tarreau | 762fb3e | 2020-03-03 15:57:10 +0100 | [diff] [blame] | 4545 | /* dumps <pfx> followed by <n> bytes from <addr> in hex form into buffer <buf> |
| 4546 | * enclosed in brackets after the address itself, formatted on 14 chars |
| 4547 | * including the "0x" prefix. This is meant to be used as a prefix for code |
| 4548 | * areas. For example: |
| 4549 | * "0x7f10b6557690 [48 c7 c0 0f 00 00 00 0f]" |
| 4550 | * It relies on may_access() to know if the bytes are dumpable, otherwise "--" |
| 4551 | * is emitted. A NULL <pfx> will be considered empty. |
| 4552 | */ |
| 4553 | void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr, int n) |
| 4554 | { |
| 4555 | int ok = 0; |
| 4556 | int i; |
| 4557 | |
| 4558 | chunk_appendf(buf, "%s%#14lx [", pfx ? pfx : "", (long)addr); |
| 4559 | |
| 4560 | for (i = 0; i < n; i++) { |
| 4561 | if (i == 0 || (((long)(addr + i) ^ (long)(addr)) & 4096)) |
| 4562 | ok = may_access(addr + i); |
| 4563 | if (ok) |
| 4564 | chunk_appendf(buf, "%02x%s", ((uint8_t*)addr)[i], (i<n-1) ? " " : "]"); |
| 4565 | else |
| 4566 | chunk_appendf(buf, "--%s", (i<n-1) ? " " : "]"); |
| 4567 | } |
| 4568 | } |
| 4569 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4570 | /* print a line of text buffer (limited to 70 bytes) to <out>. The format is : |
| 4571 | * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n> |
| 4572 | * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are |
| 4573 | * encoded in C format. Other non-printable chars are encoded "\xHH". Original |
| 4574 | * lines are respected within the limit of 70 output chars. Lines that are |
| 4575 | * continuation of a previous truncated line begin with "+" instead of " " |
| 4576 | * after the offset. The new pointer is returned. |
| 4577 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4578 | int dump_text_line(struct buffer *out, const char *buf, int bsize, int len, |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4579 | int *line, int ptr) |
| 4580 | { |
| 4581 | int end; |
| 4582 | unsigned char c; |
| 4583 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4584 | end = out->data + 80; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4585 | if (end > out->size) |
| 4586 | return ptr; |
| 4587 | |
| 4588 | chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+'); |
| 4589 | |
| 4590 | while (ptr < len && ptr < bsize) { |
| 4591 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4592 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4593 | if (out->data > end - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4594 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4595 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4596 | } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4597 | if (out->data > end - 3) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4598 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4599 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4600 | switch (c) { |
| 4601 | case '\t': c = 't'; break; |
| 4602 | case '\n': c = 'n'; break; |
| 4603 | case '\r': c = 'r'; break; |
| 4604 | case '\e': c = 'e'; break; |
| 4605 | case '\\': c = '\\'; break; |
| 4606 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4607 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4608 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4609 | if (out->data > end - 5) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4610 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4611 | out->area[out->data++] = '\\'; |
| 4612 | out->area[out->data++] = 'x'; |
| 4613 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4614 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4615 | } |
| 4616 | if (buf[ptr++] == '\n') { |
| 4617 | /* we had a line break, let's return now */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4618 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4619 | *line = ptr; |
| 4620 | return ptr; |
| 4621 | } |
| 4622 | } |
| 4623 | /* we have an incomplete line, we return it as-is */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4624 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4625 | return ptr; |
| 4626 | } |
| 4627 | |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4628 | /* displays a <len> long memory block at <buf>, assuming first byte of <buf> |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4629 | * has address <baseaddr>. String <pfx> may be placed as a prefix in front of |
| 4630 | * each line. It may be NULL if unused. The output is emitted to file <out>. |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4631 | */ |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4632 | void debug_hexdump(FILE *out, const char *pfx, const char *buf, |
| 4633 | unsigned int baseaddr, int len) |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4634 | { |
Willy Tarreau | 7345979 | 2017-04-11 07:58:08 +0200 | [diff] [blame] | 4635 | unsigned int i; |
| 4636 | int b, j; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4637 | |
| 4638 | for (i = 0; i < (len + (baseaddr & 15)); i += 16) { |
| 4639 | b = i - (baseaddr & 15); |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4640 | fprintf(out, "%s%08x: ", pfx ? pfx : "", i + (baseaddr & ~15)); |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4641 | for (j = 0; j < 8; j++) { |
| 4642 | if (b + j >= 0 && b + j < len) |
| 4643 | fprintf(out, "%02x ", (unsigned char)buf[b + j]); |
| 4644 | else |
| 4645 | fprintf(out, " "); |
| 4646 | } |
| 4647 | |
| 4648 | if (b + j >= 0 && b + j < len) |
| 4649 | fputc('-', out); |
| 4650 | else |
| 4651 | fputc(' ', out); |
| 4652 | |
| 4653 | for (j = 8; j < 16; j++) { |
| 4654 | if (b + j >= 0 && b + j < len) |
| 4655 | fprintf(out, " %02x", (unsigned char)buf[b + j]); |
| 4656 | else |
| 4657 | fprintf(out, " "); |
| 4658 | } |
| 4659 | |
| 4660 | fprintf(out, " "); |
| 4661 | for (j = 0; j < 16; j++) { |
| 4662 | if (b + j >= 0 && b + j < len) { |
| 4663 | if (isprint((unsigned char)buf[b + j])) |
| 4664 | fputc((unsigned char)buf[b + j], out); |
| 4665 | else |
| 4666 | fputc('.', out); |
| 4667 | } |
| 4668 | else |
| 4669 | fputc(' ', out); |
| 4670 | } |
| 4671 | fputc('\n', out); |
| 4672 | } |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4673 | } |
| 4674 | |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4675 | /* Tries to report the executable path name on platforms supporting this. If |
| 4676 | * not found or not possible, returns NULL. |
| 4677 | */ |
| 4678 | const char *get_exec_path() |
| 4679 | { |
| 4680 | const char *ret = NULL; |
| 4681 | |
| 4682 | #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
| 4683 | long execfn = getauxval(AT_EXECFN); |
| 4684 | |
| 4685 | if (execfn && execfn != ENOENT) |
| 4686 | ret = (const char *)execfn; |
| 4687 | #endif |
| 4688 | return ret; |
| 4689 | } |
| 4690 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4691 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4692 | /* calls dladdr() or dladdr1() on <addr> and <dli>. If dladdr1 is available, |
| 4693 | * also returns the symbol size in <size>, otherwise returns 0 there. |
| 4694 | */ |
| 4695 | static int dladdr_and_size(const void *addr, Dl_info *dli, size_t *size) |
| 4696 | { |
| 4697 | int ret; |
Willy Tarreau | 62af9c8 | 2020-03-10 07:51:48 +0100 | [diff] [blame] | 4698 | #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) // most detailed one |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4699 | const ElfW(Sym) *sym; |
| 4700 | |
| 4701 | ret = dladdr1(addr, dli, (void **)&sym, RTLD_DL_SYMENT); |
| 4702 | if (ret) |
| 4703 | *size = sym ? sym->st_size : 0; |
| 4704 | #else |
| 4705 | ret = dladdr(addr, dli); |
| 4706 | *size = 0; |
| 4707 | #endif |
| 4708 | return ret; |
| 4709 | } |
| 4710 | #endif |
| 4711 | |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4712 | /* Tries to append to buffer <buf> some indications about the symbol at address |
| 4713 | * <addr> using the following form: |
| 4714 | * lib:+0xoffset (unresolvable address from lib's base) |
| 4715 | * main+0xoffset (unresolvable address from main (+/-)) |
| 4716 | * lib:main+0xoffset (unresolvable lib address from main (+/-)) |
| 4717 | * name (resolved exact exec address) |
| 4718 | * lib:name (resolved exact lib address) |
| 4719 | * name+0xoffset/0xsize (resolved address within exec symbol) |
| 4720 | * lib:name+0xoffset/0xsize (resolved address within lib symbol) |
| 4721 | * |
| 4722 | * The file name (lib or executable) is limited to what lies between the last |
| 4723 | * '/' and the first following '.'. An optional prefix <pfx> is prepended before |
| 4724 | * the output if not null. The file is not dumped when it's the same as the one |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4725 | * that contains the "main" symbol, or when __ELF__ && USE_DL are not set. |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4726 | * |
| 4727 | * The symbol's base address is returned, or NULL when unresolved, in order to |
| 4728 | * allow the caller to match it against known ones. |
| 4729 | */ |
Willy Tarreau | 45fd103 | 2021-01-20 14:37:59 +0100 | [diff] [blame] | 4730 | const void *resolve_sym_name(struct buffer *buf, const char *pfx, const void *addr) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4731 | { |
| 4732 | const struct { |
| 4733 | const void *func; |
| 4734 | const char *name; |
| 4735 | } fcts[] = { |
| 4736 | { .func = process_stream, .name = "process_stream" }, |
| 4737 | { .func = task_run_applet, .name = "task_run_applet" }, |
| 4738 | { .func = si_cs_io_cb, .name = "si_cs_io_cb" }, |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 4739 | { .func = sock_conn_iocb, .name = "sock_conn_iocb" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4740 | { .func = dgram_fd_handler, .name = "dgram_fd_handler" }, |
| 4741 | { .func = listener_accept, .name = "listener_accept" }, |
Willy Tarreau | d597ec2 | 2021-01-29 14:29:06 +0100 | [diff] [blame] | 4742 | { .func = manage_global_listener_queue, .name = "manage_global_listener_queue" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4743 | { .func = poller_pipe_io_handler, .name = "poller_pipe_io_handler" }, |
| 4744 | { .func = mworker_accept_wrapper, .name = "mworker_accept_wrapper" }, |
Willy Tarreau | 02922e1 | 2021-01-29 12:27:57 +0100 | [diff] [blame] | 4745 | { .func = session_expire_embryonic, .name = "session_expire_embryonic" }, |
Willy Tarreau | fb5401f | 2021-01-29 12:25:23 +0100 | [diff] [blame] | 4746 | #ifdef USE_THREAD |
| 4747 | { .func = accept_queue_process, .name = "accept_queue_process" }, |
| 4748 | #endif |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4749 | #ifdef USE_LUA |
| 4750 | { .func = hlua_process_task, .name = "hlua_process_task" }, |
| 4751 | #endif |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 4752 | #ifdef SSL_MODE_ASYNC |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4753 | { .func = ssl_async_fd_free, .name = "ssl_async_fd_free" }, |
| 4754 | { .func = ssl_async_fd_handler, .name = "ssl_async_fd_handler" }, |
| 4755 | #endif |
| 4756 | }; |
| 4757 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4758 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4759 | Dl_info dli, dli_main; |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4760 | size_t size; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4761 | const char *fname, *p; |
| 4762 | #endif |
| 4763 | int i; |
| 4764 | |
| 4765 | if (pfx) |
| 4766 | chunk_appendf(buf, "%s", pfx); |
| 4767 | |
| 4768 | for (i = 0; i < sizeof(fcts) / sizeof(fcts[0]); i++) { |
| 4769 | if (addr == fcts[i].func) { |
| 4770 | chunk_appendf(buf, "%s", fcts[i].name); |
| 4771 | return addr; |
| 4772 | } |
| 4773 | } |
| 4774 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4775 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4776 | /* Now let's try to be smarter */ |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4777 | if (!dladdr_and_size(addr, &dli, &size)) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4778 | goto unknown; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4779 | |
| 4780 | /* 1. prefix the library name if it's not the same object as the one |
| 4781 | * that contains the main function. The name is picked between last '/' |
| 4782 | * and first following '.'. |
| 4783 | */ |
| 4784 | if (!dladdr(main, &dli_main)) |
| 4785 | dli_main.dli_fbase = NULL; |
| 4786 | |
| 4787 | if (dli_main.dli_fbase != dli.dli_fbase) { |
| 4788 | fname = dli.dli_fname; |
| 4789 | p = strrchr(fname, '/'); |
| 4790 | if (p++) |
| 4791 | fname = p; |
| 4792 | p = strchr(fname, '.'); |
| 4793 | if (!p) |
| 4794 | p = fname + strlen(fname); |
| 4795 | |
| 4796 | chunk_appendf(buf, "%.*s:", (int)(long)(p - fname), fname); |
| 4797 | } |
| 4798 | |
| 4799 | /* 2. symbol name */ |
| 4800 | if (dli.dli_sname) { |
| 4801 | /* known, dump it and return symbol's address (exact or relative) */ |
| 4802 | chunk_appendf(buf, "%s", dli.dli_sname); |
| 4803 | if (addr != dli.dli_saddr) { |
| 4804 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_saddr)); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4805 | if (size) |
| 4806 | chunk_appendf(buf, "/%#lx", (long)size); |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4807 | } |
| 4808 | return dli.dli_saddr; |
| 4809 | } |
| 4810 | else if (dli_main.dli_fbase != dli.dli_fbase) { |
| 4811 | /* unresolved symbol from a known library, report relative offset */ |
| 4812 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_fbase)); |
| 4813 | return NULL; |
| 4814 | } |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4815 | #endif /* __ELF__ && !__linux__ || USE_DL */ |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4816 | unknown: |
| 4817 | /* unresolved symbol from the main file, report relative offset to main */ |
| 4818 | if ((void*)addr < (void*)main) |
| 4819 | chunk_appendf(buf, "main-%#lx", (long)((void*)main - addr)); |
| 4820 | else |
| 4821 | chunk_appendf(buf, "main+%#lx", (long)(addr - (void*)main)); |
| 4822 | return NULL; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4823 | } |
| 4824 | |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 4825 | /* |
| 4826 | * Allocate an array of unsigned int with <nums> as address from <str> string |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 4827 | * made of integer separated by dot characters. |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 4828 | * |
| 4829 | * First, initializes the value with <sz> as address to 0 and initializes the |
| 4830 | * array with <nums> as address to NULL. Then allocates the array with <nums> as |
| 4831 | * address updating <sz> pointed value to the size of this array. |
| 4832 | * |
| 4833 | * Returns 1 if succeeded, 0 if not. |
| 4834 | */ |
| 4835 | int parse_dotted_uints(const char *str, unsigned int **nums, size_t *sz) |
| 4836 | { |
| 4837 | unsigned int *n; |
| 4838 | const char *s, *end; |
| 4839 | |
| 4840 | s = str; |
| 4841 | *sz = 0; |
| 4842 | end = str + strlen(str); |
| 4843 | *nums = n = NULL; |
| 4844 | |
| 4845 | while (1) { |
| 4846 | unsigned int r; |
| 4847 | |
| 4848 | if (s >= end) |
| 4849 | break; |
| 4850 | |
| 4851 | r = read_uint(&s, end); |
| 4852 | /* Expected characters after having read an uint: '\0' or '.', |
| 4853 | * if '.', must not be terminal. |
| 4854 | */ |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 4855 | if (*s != '\0'&& (*s++ != '.' || s == end)) { |
| 4856 | free(n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 4857 | return 0; |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 4858 | } |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 4859 | |
Frédéric Lécaille | 12a7184 | 2019-02-26 18:19:48 +0100 | [diff] [blame] | 4860 | n = my_realloc2(n, (*sz + 1) * sizeof *n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 4861 | if (!n) |
| 4862 | return 0; |
| 4863 | |
| 4864 | n[(*sz)++] = r; |
| 4865 | } |
| 4866 | *nums = n; |
| 4867 | |
| 4868 | return 1; |
| 4869 | } |
| 4870 | |
Willy Tarreau | 4d589e7 | 2019-08-23 19:02:26 +0200 | [diff] [blame] | 4871 | |
| 4872 | /* returns the number of bytes needed to encode <v> as a varint. An inline |
| 4873 | * version exists for use with constants (__varint_bytes()). |
| 4874 | */ |
| 4875 | int varint_bytes(uint64_t v) |
| 4876 | { |
| 4877 | int len = 1; |
| 4878 | |
| 4879 | if (v >= 240) { |
| 4880 | v = (v - 240) >> 4; |
| 4881 | while (1) { |
| 4882 | len++; |
| 4883 | if (v < 128) |
| 4884 | break; |
| 4885 | v = (v - 128) >> 7; |
| 4886 | } |
| 4887 | } |
| 4888 | return len; |
| 4889 | } |
| 4890 | |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 4891 | |
| 4892 | /* Random number generator state, see below */ |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 4893 | static uint64_t ha_random_state[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 4894 | |
| 4895 | /* This is a thread-safe implementation of xoroshiro128** described below: |
| 4896 | * http://prng.di.unimi.it/ |
| 4897 | * It features a 2^128 long sequence, returns 64 high-quality bits on each call, |
| 4898 | * supports fast jumps and passes all common quality tests. It is thread-safe, |
| 4899 | * uses a double-cas on 64-bit architectures supporting it, and falls back to a |
| 4900 | * local lock on other ones. |
| 4901 | */ |
| 4902 | uint64_t ha_random64() |
| 4903 | { |
| 4904 | uint64_t result; |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 4905 | uint64_t old[2] ALIGNED(2*sizeof(uint64_t)); |
| 4906 | uint64_t new[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 4907 | |
| 4908 | #if defined(USE_THREAD) && (!defined(HA_CAS_IS_8B) || !defined(HA_HAVE_CAS_DW)) |
| 4909 | static HA_SPINLOCK_T rand_lock; |
| 4910 | |
| 4911 | HA_SPIN_LOCK(OTHER_LOCK, &rand_lock); |
| 4912 | #endif |
| 4913 | |
| 4914 | old[0] = ha_random_state[0]; |
| 4915 | old[1] = ha_random_state[1]; |
| 4916 | |
| 4917 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 4918 | do { |
| 4919 | #endif |
| 4920 | result = rotl64(old[0] * 5, 7) * 9; |
| 4921 | new[1] = old[0] ^ old[1]; |
| 4922 | new[0] = rotl64(old[0], 24) ^ new[1] ^ (new[1] << 16); // a, b |
| 4923 | new[1] = rotl64(new[1], 37); // c |
| 4924 | |
| 4925 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 4926 | } while (unlikely(!_HA_ATOMIC_DWCAS(ha_random_state, old, new))); |
| 4927 | #else |
| 4928 | ha_random_state[0] = new[0]; |
| 4929 | ha_random_state[1] = new[1]; |
| 4930 | #if defined(USE_THREAD) |
| 4931 | HA_SPIN_UNLOCK(OTHER_LOCK, &rand_lock); |
| 4932 | #endif |
| 4933 | #endif |
| 4934 | return result; |
| 4935 | } |
| 4936 | |
| 4937 | /* seeds the random state using up to <len> bytes from <seed>, starting with |
| 4938 | * the first non-zero byte. |
| 4939 | */ |
| 4940 | void ha_random_seed(const unsigned char *seed, size_t len) |
| 4941 | { |
| 4942 | size_t pos; |
| 4943 | |
| 4944 | /* the seed must not be all zeroes, so we pre-fill it with alternating |
| 4945 | * bits and overwrite part of them with the block starting at the first |
| 4946 | * non-zero byte from the seed. |
| 4947 | */ |
| 4948 | memset(ha_random_state, 0x55, sizeof(ha_random_state)); |
| 4949 | |
| 4950 | for (pos = 0; pos < len; pos++) |
| 4951 | if (seed[pos] != 0) |
| 4952 | break; |
| 4953 | |
| 4954 | if (pos == len) |
| 4955 | return; |
| 4956 | |
| 4957 | seed += pos; |
| 4958 | len -= pos; |
| 4959 | |
| 4960 | if (len > sizeof(ha_random_state)) |
| 4961 | len = sizeof(ha_random_state); |
| 4962 | |
| 4963 | memcpy(ha_random_state, seed, len); |
| 4964 | } |
| 4965 | |
| 4966 | /* This causes a jump to (dist * 2^96) places in the pseudo-random sequence, |
| 4967 | * and is equivalent to calling ha_random64() as many times. It is used to |
| 4968 | * provide non-overlapping sequences of 2^96 numbers (~7*10^28) to up to 2^32 |
| 4969 | * different generators (i.e. different processes after a fork). The <dist> |
| 4970 | * argument is the distance to jump to and is used in a loop so it rather not |
| 4971 | * be too large if the processing time is a concern. |
| 4972 | * |
| 4973 | * BEWARE: this function is NOT thread-safe and must not be called during |
| 4974 | * concurrent accesses to ha_random64(). |
| 4975 | */ |
| 4976 | void ha_random_jump96(uint32_t dist) |
| 4977 | { |
| 4978 | while (dist--) { |
| 4979 | uint64_t s0 = 0; |
| 4980 | uint64_t s1 = 0; |
| 4981 | int b; |
| 4982 | |
| 4983 | for (b = 0; b < 64; b++) { |
| 4984 | if ((0xd2a98b26625eee7bULL >> b) & 1) { |
| 4985 | s0 ^= ha_random_state[0]; |
| 4986 | s1 ^= ha_random_state[1]; |
| 4987 | } |
| 4988 | ha_random64(); |
| 4989 | } |
| 4990 | |
| 4991 | for (b = 0; b < 64; b++) { |
| 4992 | if ((0xdddf9b1090aa7ac1ULL >> b) & 1) { |
| 4993 | s0 ^= ha_random_state[0]; |
| 4994 | s1 ^= ha_random_state[1]; |
| 4995 | } |
| 4996 | ha_random64(); |
| 4997 | } |
| 4998 | ha_random_state[0] = s0; |
| 4999 | ha_random_state[1] = s1; |
| 5000 | } |
| 5001 | } |
| 5002 | |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 5003 | /* Generates an RFC4122 UUID into chunk <output> which must be at least 37 |
| 5004 | * bytes large. |
| 5005 | */ |
| 5006 | void ha_generate_uuid(struct buffer *output) |
| 5007 | { |
| 5008 | uint32_t rnd[4]; |
| 5009 | uint64_t last; |
| 5010 | |
| 5011 | last = ha_random64(); |
| 5012 | rnd[0] = last; |
| 5013 | rnd[1] = last >> 32; |
| 5014 | |
| 5015 | last = ha_random64(); |
| 5016 | rnd[2] = last; |
| 5017 | rnd[3] = last >> 32; |
| 5018 | |
| 5019 | chunk_printf(output, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx", |
| 5020 | rnd[0], |
| 5021 | rnd[1] & 0xFFFF, |
| 5022 | ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version |
| 5023 | (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10), |
| 5024 | (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull); |
| 5025 | } |
| 5026 | |
| 5027 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5028 | /* only used by parse_line() below. It supports writing in place provided that |
| 5029 | * <in> is updated to the next location before calling it. In that case, the |
| 5030 | * char at <in> may be overwritten. |
| 5031 | */ |
| 5032 | #define EMIT_CHAR(x) \ |
| 5033 | do { \ |
| 5034 | char __c = (char)(x); \ |
| 5035 | if ((opts & PARSE_OPT_INPLACE) && out+outpos > in) \ |
| 5036 | err |= PARSE_ERR_OVERLAP; \ |
| 5037 | if (outpos >= outmax) \ |
| 5038 | err |= PARSE_ERR_TOOLARGE; \ |
| 5039 | if (!err) \ |
| 5040 | out[outpos] = __c; \ |
| 5041 | outpos++; \ |
| 5042 | } while (0) |
| 5043 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 5044 | /* Parse <in>, copy it into <out> split into isolated words whose pointers |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5045 | * are put in <args>. If more than <outlen> bytes have to be emitted, the |
| 5046 | * extraneous ones are not emitted but <outlen> is updated so that the caller |
| 5047 | * knows how much to realloc. Similarly, <args> are not updated beyond <nbargs> |
| 5048 | * but the returned <nbargs> indicates how many were found. All trailing args |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5049 | * up to <nbargs> point to the trailing zero, and as long as <nbargs> is > 0, |
| 5050 | * it is guaranteed that at least one arg will point to the zero. It is safe |
| 5051 | * to call it with a NULL <args> if <nbargs> is 0. |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5052 | * |
| 5053 | * <out> may overlap with <in> provided that it never goes further, in which |
| 5054 | * case the parser will accept to perform in-place parsing and unquoting/ |
| 5055 | * unescaping but only if environment variables do not lead to expansion that |
| 5056 | * causes overlapping, otherwise the input string being destroyed, the error |
| 5057 | * will not be recoverable. Note that even during out-of-place <in> will |
| 5058 | * experience temporary modifications in-place for variable resolution and must |
| 5059 | * be writable, and will also receive zeroes to delimit words when using |
| 5060 | * in-place copy. Parsing options <opts> taken from PARSE_OPT_*. Return value |
| 5061 | * is zero on success otherwise a bitwise-or of PARSE_ERR_*. Upon error, the |
| 5062 | * starting point of the first invalid character sequence or unmatched |
| 5063 | * quote/brace is reported in <errptr> if not NULL. When using in-place parsing |
| 5064 | * error reporting might be difficult since zeroes will have been inserted into |
| 5065 | * the string. One solution for the caller may consist in replacing all args |
| 5066 | * delimiters with spaces in this case. |
| 5067 | */ |
| 5068 | uint32_t parse_line(char *in, char *out, size_t *outlen, char **args, int *nbargs, uint32_t opts, char **errptr) |
| 5069 | { |
| 5070 | char *quote = NULL; |
| 5071 | char *brace = NULL; |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5072 | char *word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5073 | unsigned char hex1, hex2; |
| 5074 | size_t outmax = *outlen; |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5075 | int argsmax = *nbargs - 1; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5076 | size_t outpos = 0; |
| 5077 | int squote = 0; |
| 5078 | int dquote = 0; |
| 5079 | int arg = 0; |
| 5080 | uint32_t err = 0; |
| 5081 | |
| 5082 | *nbargs = 0; |
| 5083 | *outlen = 0; |
| 5084 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5085 | /* argsmax may be -1 here, protecting args[] from any write */ |
| 5086 | if (arg < argsmax) |
| 5087 | args[arg] = out; |
| 5088 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5089 | while (1) { |
| 5090 | if (*in >= '-' && *in != '\\') { |
| 5091 | /* speedup: directly send all regular chars starting |
| 5092 | * with '-', '.', '/', alnum etc... |
| 5093 | */ |
| 5094 | EMIT_CHAR(*in++); |
| 5095 | continue; |
| 5096 | } |
| 5097 | else if (*in == '\0' || *in == '\n' || *in == '\r') { |
| 5098 | /* end of line */ |
| 5099 | break; |
| 5100 | } |
| 5101 | else if (*in == '#' && (opts & PARSE_OPT_SHARP) && !squote && !dquote) { |
| 5102 | /* comment */ |
| 5103 | break; |
| 5104 | } |
| 5105 | else if (*in == '"' && !squote && (opts & PARSE_OPT_DQUOTE)) { /* double quote outside single quotes */ |
| 5106 | if (dquote) { |
| 5107 | dquote = 0; |
| 5108 | quote = NULL; |
| 5109 | } |
| 5110 | else { |
| 5111 | dquote = 1; |
| 5112 | quote = in; |
| 5113 | } |
| 5114 | in++; |
| 5115 | continue; |
| 5116 | } |
| 5117 | else if (*in == '\'' && !dquote && (opts & PARSE_OPT_SQUOTE)) { /* single quote outside double quotes */ |
| 5118 | if (squote) { |
| 5119 | squote = 0; |
| 5120 | quote = NULL; |
| 5121 | } |
| 5122 | else { |
| 5123 | squote = 1; |
| 5124 | quote = in; |
| 5125 | } |
| 5126 | in++; |
| 5127 | continue; |
| 5128 | } |
| 5129 | else if (*in == '\\' && !squote && (opts & PARSE_OPT_BKSLASH)) { |
| 5130 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 5131 | * C equivalent value but only when they have a special meaning and within |
| 5132 | * double quotes for some of them. Other combinations left unchanged (eg: \1). |
| 5133 | */ |
| 5134 | char tosend = *in; |
| 5135 | |
| 5136 | switch (in[1]) { |
| 5137 | case ' ': |
| 5138 | case '\\': |
| 5139 | tosend = in[1]; |
| 5140 | in++; |
| 5141 | break; |
| 5142 | |
| 5143 | case 't': |
| 5144 | tosend = '\t'; |
| 5145 | in++; |
| 5146 | break; |
| 5147 | |
| 5148 | case 'n': |
| 5149 | tosend = '\n'; |
| 5150 | in++; |
| 5151 | break; |
| 5152 | |
| 5153 | case 'r': |
| 5154 | tosend = '\r'; |
| 5155 | in++; |
| 5156 | break; |
| 5157 | |
| 5158 | case '#': |
| 5159 | /* escaping of "#" only if comments are supported */ |
| 5160 | if (opts & PARSE_OPT_SHARP) |
| 5161 | in++; |
| 5162 | tosend = *in; |
| 5163 | break; |
| 5164 | |
| 5165 | case '\'': |
| 5166 | /* escaping of "'" only outside single quotes and only if single quotes are supported */ |
| 5167 | if (opts & PARSE_OPT_SQUOTE && !squote) |
| 5168 | in++; |
| 5169 | tosend = *in; |
| 5170 | break; |
| 5171 | |
| 5172 | case '"': |
| 5173 | /* escaping of '"' only outside single quotes and only if double quotes are supported */ |
| 5174 | if (opts & PARSE_OPT_DQUOTE && !squote) |
| 5175 | in++; |
| 5176 | tosend = *in; |
| 5177 | break; |
| 5178 | |
| 5179 | case '$': |
| 5180 | /* escaping of '$' only inside double quotes and only if env supported */ |
| 5181 | if (opts & PARSE_OPT_ENV && dquote) |
| 5182 | in++; |
| 5183 | tosend = *in; |
| 5184 | break; |
| 5185 | |
| 5186 | case 'x': |
| 5187 | if (!ishex(in[2]) || !ishex(in[3])) { |
| 5188 | /* invalid or incomplete hex sequence */ |
| 5189 | err |= PARSE_ERR_HEX; |
| 5190 | if (errptr) |
| 5191 | *errptr = in; |
| 5192 | goto leave; |
| 5193 | } |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 5194 | hex1 = toupper((unsigned char)in[2]) - '0'; |
| 5195 | hex2 = toupper((unsigned char)in[3]) - '0'; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5196 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 5197 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 5198 | tosend = (hex1 << 4) + hex2; |
| 5199 | in += 3; |
| 5200 | break; |
| 5201 | |
| 5202 | default: |
| 5203 | /* other combinations are not escape sequences */ |
| 5204 | break; |
| 5205 | } |
| 5206 | |
| 5207 | in++; |
| 5208 | EMIT_CHAR(tosend); |
| 5209 | } |
| 5210 | else if (isspace((unsigned char)*in) && !squote && !dquote) { |
| 5211 | /* a non-escaped space is an argument separator */ |
| 5212 | while (isspace((unsigned char)*in)) |
| 5213 | in++; |
| 5214 | EMIT_CHAR(0); |
| 5215 | arg++; |
| 5216 | if (arg < argsmax) |
| 5217 | args[arg] = out + outpos; |
| 5218 | else |
| 5219 | err |= PARSE_ERR_TOOMANY; |
| 5220 | } |
| 5221 | else if (*in == '$' && (opts & PARSE_OPT_ENV) && (dquote || !(opts & PARSE_OPT_DQUOTE))) { |
| 5222 | /* environment variables are evaluated anywhere, or only |
| 5223 | * inside double quotes if they are supported. |
| 5224 | */ |
| 5225 | char *var_name; |
| 5226 | char save_char; |
| 5227 | char *value; |
| 5228 | |
| 5229 | in++; |
| 5230 | |
| 5231 | if (*in == '{') |
| 5232 | brace = in++; |
| 5233 | |
| 5234 | if (!isalpha((unsigned char)*in) && *in != '_') { |
| 5235 | /* unacceptable character in variable name */ |
| 5236 | err |= PARSE_ERR_VARNAME; |
| 5237 | if (errptr) |
| 5238 | *errptr = in; |
| 5239 | goto leave; |
| 5240 | } |
| 5241 | |
| 5242 | var_name = in; |
| 5243 | while (isalnum((unsigned char)*in) || *in == '_') |
| 5244 | in++; |
| 5245 | |
| 5246 | save_char = *in; |
| 5247 | *in = '\0'; |
| 5248 | value = getenv(var_name); |
| 5249 | *in = save_char; |
| 5250 | |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5251 | /* support for '[*]' sequence to force word expansion, |
| 5252 | * only available inside braces */ |
| 5253 | if (*in == '[' && brace && (opts & PARSE_OPT_WORD_EXPAND)) { |
| 5254 | word_expand = in++; |
| 5255 | |
| 5256 | if (*in++ != '*' || *in++ != ']') { |
| 5257 | err |= PARSE_ERR_WRONG_EXPAND; |
| 5258 | if (errptr) |
| 5259 | *errptr = word_expand; |
| 5260 | goto leave; |
| 5261 | } |
| 5262 | } |
| 5263 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5264 | if (brace) { |
| 5265 | if (*in != '}') { |
| 5266 | /* unmatched brace */ |
| 5267 | err |= PARSE_ERR_BRACE; |
| 5268 | if (errptr) |
| 5269 | *errptr = brace; |
| 5270 | goto leave; |
| 5271 | } |
| 5272 | in++; |
| 5273 | brace = NULL; |
| 5274 | } |
| 5275 | |
| 5276 | if (value) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5277 | while (*value) { |
| 5278 | /* expand as individual parameters on a space character */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5279 | if (word_expand && isspace((unsigned char)*value)) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5280 | EMIT_CHAR(0); |
| 5281 | ++arg; |
| 5282 | if (arg < argsmax) |
| 5283 | args[arg] = out + outpos; |
| 5284 | else |
| 5285 | err |= PARSE_ERR_TOOMANY; |
| 5286 | |
| 5287 | /* skip consecutive spaces */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5288 | while (isspace((unsigned char)*++value)) |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5289 | ; |
| 5290 | } else { |
| 5291 | EMIT_CHAR(*value++); |
| 5292 | } |
| 5293 | } |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5294 | } |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5295 | word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5296 | } |
| 5297 | else { |
| 5298 | /* any other regular char */ |
| 5299 | EMIT_CHAR(*in++); |
| 5300 | } |
| 5301 | } |
| 5302 | |
| 5303 | /* end of output string */ |
| 5304 | EMIT_CHAR(0); |
| 5305 | arg++; |
| 5306 | |
| 5307 | if (quote) { |
| 5308 | /* unmatched quote */ |
| 5309 | err |= PARSE_ERR_QUOTE; |
| 5310 | if (errptr) |
| 5311 | *errptr = quote; |
| 5312 | goto leave; |
| 5313 | } |
| 5314 | leave: |
| 5315 | *nbargs = arg; |
| 5316 | *outlen = outpos; |
| 5317 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5318 | /* empty all trailing args by making them point to the trailing zero, |
| 5319 | * at least the last one in any case. |
| 5320 | */ |
| 5321 | if (arg > argsmax) |
| 5322 | arg = argsmax; |
| 5323 | |
| 5324 | while (arg >= 0 && arg <= argsmax) |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5325 | args[arg++] = out + outpos - 1; |
| 5326 | |
| 5327 | return err; |
| 5328 | } |
| 5329 | #undef EMIT_CHAR |
| 5330 | |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 5331 | /* This is used to sanitize an input line that's about to be used for error reporting. |
| 5332 | * It will adjust <line> to print approximately <width> chars around <pos>, trying to |
| 5333 | * preserve the beginning, with leading or trailing "..." when the line is truncated. |
| 5334 | * If non-printable chars are present in the output. It returns the new offset <pos> |
| 5335 | * in the modified line. Non-printable characters are replaced with '?'. <width> must |
| 5336 | * be at least 6 to support two "..." otherwise the result is undefined. The line |
| 5337 | * itself must have at least 7 chars allocated for the same reason. |
| 5338 | */ |
| 5339 | size_t sanitize_for_printing(char *line, size_t pos, size_t width) |
| 5340 | { |
| 5341 | size_t shift = 0; |
| 5342 | char *out = line; |
| 5343 | char *in = line; |
| 5344 | char *end = line + width; |
| 5345 | |
| 5346 | if (pos >= width) { |
| 5347 | /* if we have to shift, we'll be out of context, so let's |
| 5348 | * try to put <pos> at the center of width. |
| 5349 | */ |
| 5350 | shift = pos - width / 2; |
| 5351 | in += shift + 3; |
| 5352 | end = out + width - 3; |
| 5353 | out[0] = out[1] = out[2] = '.'; |
| 5354 | out += 3; |
| 5355 | } |
| 5356 | |
| 5357 | while (out < end && *in) { |
| 5358 | if (isspace((unsigned char)*in)) |
| 5359 | *out++ = ' '; |
| 5360 | else if (isprint((unsigned char)*in)) |
| 5361 | *out++ = *in; |
| 5362 | else |
| 5363 | *out++ = '?'; |
| 5364 | in++; |
| 5365 | } |
| 5366 | |
| 5367 | if (end < line + width) { |
| 5368 | out[0] = out[1] = out[2] = '.'; |
| 5369 | out += 3; |
| 5370 | } |
| 5371 | |
| 5372 | *out++ = 0; |
| 5373 | return pos - shift; |
| 5374 | } |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 5375 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5376 | /* Update array <fp> with the fingerprint of word <word> by counting the |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5377 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 5378 | * 32*from+to. Positions for 'from' and 'to' are: |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5379 | * 1..26=letter, 27=digit, 28=other/begin/end. |
| 5380 | * Row "from=0" is used to mark the character's presence. Others unused. |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5381 | */ |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5382 | void update_word_fingerprint(uint8_t *fp, const char *word) |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5383 | { |
| 5384 | const char *p; |
| 5385 | int from, to; |
| 5386 | int c; |
| 5387 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5388 | from = 28; // begin |
| 5389 | for (p = word; *p; p++) { |
| 5390 | c = tolower(*p); |
| 5391 | switch(c) { |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5392 | case 'a'...'z': to = c - 'a' + 1; break; |
| 5393 | case 'A'...'Z': to = tolower(c) - 'a' + 1; break; |
| 5394 | case '0'...'9': to = 27; break; |
| 5395 | default: to = 28; break; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5396 | } |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5397 | fp[to] = 1; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5398 | fp[32 * from + to]++; |
| 5399 | from = to; |
| 5400 | } |
| 5401 | to = 28; // end |
| 5402 | fp[32 * from + to]++; |
| 5403 | } |
| 5404 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5405 | /* Initialize array <fp> with the fingerprint of word <word> by counting the |
| 5406 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 5407 | * 32*from+to. Positions for 'from' and 'to' are: |
| 5408 | * 0..25=letter, 26=digit, 27=other, 28=begin, 29=end, others unused. |
| 5409 | */ |
| 5410 | void make_word_fingerprint(uint8_t *fp, const char *word) |
| 5411 | { |
| 5412 | memset(fp, 0, 1024); |
| 5413 | update_word_fingerprint(fp, word); |
| 5414 | } |
| 5415 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5416 | /* Return the distance between two word fingerprints created by function |
| 5417 | * make_word_fingerprint(). It's a positive integer calculated as the sum of |
Willy Tarreau | 714c4c1 | 2021-03-15 09:44:53 +0100 | [diff] [blame] | 5418 | * the differences between each location. |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5419 | */ |
| 5420 | int word_fingerprint_distance(const uint8_t *fp1, const uint8_t *fp2) |
| 5421 | { |
| 5422 | int i, k, dist = 0; |
| 5423 | |
| 5424 | for (i = 0; i < 1024; i++) { |
| 5425 | k = (int)fp1[i] - (int)fp2[i]; |
Willy Tarreau | 714c4c1 | 2021-03-15 09:44:53 +0100 | [diff] [blame] | 5426 | dist += abs(k); |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5427 | } |
| 5428 | return dist; |
| 5429 | } |
| 5430 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 5431 | static int init_tools_per_thread() |
| 5432 | { |
| 5433 | /* Let's make each thread start from a different position */ |
| 5434 | statistical_prng_state += tid * MAX_THREADS; |
| 5435 | if (!statistical_prng_state) |
| 5436 | statistical_prng_state++; |
| 5437 | return 1; |
| 5438 | } |
| 5439 | REGISTER_PER_THREAD_INIT(init_tools_per_thread); |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 5440 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5441 | /* |
| 5442 | * Local variables: |
| 5443 | * c-indent-level: 8 |
| 5444 | * c-basic-offset: 8 |
| 5445 | * End: |
| 5446 | */ |