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