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