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