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